linux/virt/kvm/irq_comm.c
<<
>>
Prefs
   1/*
   2 * irq_comm.c: Common API for in kernel interrupt controller
   3 * Copyright (c) 2007, Intel Corporation.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms and conditions of the GNU General Public License,
   7 * version 2, as published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * You should have received a copy of the GNU General Public License along with
  15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16 * Place - Suite 330, Boston, MA 02111-1307 USA.
  17 * Authors:
  18 *   Yaozu (Eddie) Dong <Eddie.dong@intel.com>
  19 *
  20 */
  21
  22#include <linux/kvm_host.h>
  23
  24#include <asm/msidef.h>
  25
  26#include "irq.h"
  27
  28#include "ioapic.h"
  29
  30static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
  31                           struct kvm *kvm, int level)
  32{
  33#ifdef CONFIG_X86
  34        return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
  35#else
  36        return -1;
  37#endif
  38}
  39
  40static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
  41                              struct kvm *kvm, int level)
  42{
  43        return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
  44}
  45
  46static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
  47                       struct kvm *kvm, int level)
  48{
  49        int vcpu_id, r = -1;
  50        struct kvm_vcpu *vcpu;
  51        struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
  52        int dest_id = (e->msi.address_lo & MSI_ADDR_DEST_ID_MASK)
  53                        >> MSI_ADDR_DEST_ID_SHIFT;
  54        int vector = (e->msi.data & MSI_DATA_VECTOR_MASK)
  55                        >> MSI_DATA_VECTOR_SHIFT;
  56        int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
  57                                (unsigned long *)&e->msi.address_lo);
  58        int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
  59                                (unsigned long *)&e->msi.data);
  60        int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
  61                                (unsigned long *)&e->msi.data);
  62        u32 deliver_bitmask;
  63
  64        BUG_ON(!ioapic);
  65
  66        deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
  67                                dest_id, dest_mode);
  68        /* IOAPIC delivery mode value is the same as MSI here */
  69        switch (delivery_mode) {
  70        case IOAPIC_LOWEST_PRIORITY:
  71                vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
  72                                deliver_bitmask);
  73                if (vcpu != NULL)
  74                        r = kvm_apic_set_irq(vcpu, vector, trig_mode);
  75                else
  76                        printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
  77                break;
  78        case IOAPIC_FIXED:
  79                for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
  80                        if (!(deliver_bitmask & (1 << vcpu_id)))
  81                                continue;
  82                        deliver_bitmask &= ~(1 << vcpu_id);
  83                        vcpu = ioapic->kvm->vcpus[vcpu_id];
  84                        if (vcpu) {
  85                                if (r < 0)
  86                                        r = 0;
  87                                r += kvm_apic_set_irq(vcpu, vector, trig_mode);
  88                        }
  89                }
  90                break;
  91        default:
  92                break;
  93        }
  94        return r;
  95}
  96
  97/* This should be called with the kvm->lock mutex held
  98 * Return value:
  99 *  < 0   Interrupt was ignored (masked or not delivered for other reasons)
 100 *  = 0   Interrupt was coalesced (previous irq is still pending)
 101 *  > 0   Number of CPUs interrupt was delivered to
 102 */
 103int kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
 104{
 105        struct kvm_kernel_irq_routing_entry *e;
 106        unsigned long *irq_state, sig_level;
 107        int ret = -1;
 108
 109        if (irq < KVM_IOAPIC_NUM_PINS) {
 110                irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
 111
 112                /* Logical OR for level trig interrupt */
 113                if (level)
 114                        set_bit(irq_source_id, irq_state);
 115                else
 116                        clear_bit(irq_source_id, irq_state);
 117                sig_level = !!(*irq_state);
 118        } else /* Deal with MSI/MSI-X */
 119                sig_level = 1;
 120
 121        /* Not possible to detect if the guest uses the PIC or the
 122         * IOAPIC.  So set the bit in both. The guest will ignore
 123         * writes to the unused one.
 124         */
 125        list_for_each_entry(e, &kvm->irq_routing, link)
 126                if (e->gsi == irq) {
 127                        int r = e->set(e, kvm, sig_level);
 128                        if (r < 0)
 129                                continue;
 130
 131                        ret = r + ((ret < 0) ? 0 : ret);
 132                }
 133        return ret;
 134}
 135
 136void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
 137{
 138        struct kvm_kernel_irq_routing_entry *e;
 139        struct kvm_irq_ack_notifier *kian;
 140        struct hlist_node *n;
 141        unsigned gsi = pin;
 142
 143        list_for_each_entry(e, &kvm->irq_routing, link)
 144                if (e->type == KVM_IRQ_ROUTING_IRQCHIP &&
 145                    e->irqchip.irqchip == irqchip &&
 146                    e->irqchip.pin == pin) {
 147                        gsi = e->gsi;
 148                        break;
 149                }
 150
 151        hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
 152                if (kian->gsi == gsi)
 153                        kian->irq_acked(kian);
 154}
 155
 156void kvm_register_irq_ack_notifier(struct kvm *kvm,
 157                                   struct kvm_irq_ack_notifier *kian)
 158{
 159        hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
 160}
 161
 162void kvm_unregister_irq_ack_notifier(struct kvm_irq_ack_notifier *kian)
 163{
 164        hlist_del_init(&kian->link);
 165}
 166
 167/* The caller must hold kvm->lock mutex */
 168int kvm_request_irq_source_id(struct kvm *kvm)
 169{
 170        unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
 171        int irq_source_id = find_first_zero_bit(bitmap,
 172                                sizeof(kvm->arch.irq_sources_bitmap));
 173
 174        if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
 175                printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
 176                return -EFAULT;
 177        }
 178
 179        ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
 180        set_bit(irq_source_id, bitmap);
 181
 182        return irq_source_id;
 183}
 184
 185void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
 186{
 187        int i;
 188
 189        ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
 190
 191        if (irq_source_id < 0 ||
 192            irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
 193                printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
 194                return;
 195        }
 196        for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
 197                clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
 198        clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
 199}
 200
 201void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
 202                                    struct kvm_irq_mask_notifier *kimn)
 203{
 204        kimn->irq = irq;
 205        hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
 206}
 207
 208void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
 209                                      struct kvm_irq_mask_notifier *kimn)
 210{
 211        hlist_del(&kimn->link);
 212}
 213
 214void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
 215{
 216        struct kvm_irq_mask_notifier *kimn;
 217        struct hlist_node *n;
 218
 219        hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
 220                if (kimn->irq == irq)
 221                        kimn->func(kimn, mask);
 222}
 223
 224static void __kvm_free_irq_routing(struct list_head *irq_routing)
 225{
 226        struct kvm_kernel_irq_routing_entry *e, *n;
 227
 228        list_for_each_entry_safe(e, n, irq_routing, link)
 229                kfree(e);
 230}
 231
 232void kvm_free_irq_routing(struct kvm *kvm)
 233{
 234        __kvm_free_irq_routing(&kvm->irq_routing);
 235}
 236
 237static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
 238                               const struct kvm_irq_routing_entry *ue)
 239{
 240        int r = -EINVAL;
 241        int delta;
 242
 243        e->gsi = ue->gsi;
 244        e->type = ue->type;
 245        switch (ue->type) {
 246        case KVM_IRQ_ROUTING_IRQCHIP:
 247                delta = 0;
 248                switch (ue->u.irqchip.irqchip) {
 249                case KVM_IRQCHIP_PIC_MASTER:
 250                        e->set = kvm_set_pic_irq;
 251                        break;
 252                case KVM_IRQCHIP_PIC_SLAVE:
 253                        e->set = kvm_set_pic_irq;
 254                        delta = 8;
 255                        break;
 256                case KVM_IRQCHIP_IOAPIC:
 257                                e->set = kvm_set_ioapic_irq;
 258                        break;
 259                default:
 260                        goto out;
 261                }
 262                e->irqchip.irqchip = ue->u.irqchip.irqchip;
 263                e->irqchip.pin = ue->u.irqchip.pin + delta;
 264                break;
 265        case KVM_IRQ_ROUTING_MSI:
 266                e->set = kvm_set_msi;
 267                e->msi.address_lo = ue->u.msi.address_lo;
 268                e->msi.address_hi = ue->u.msi.address_hi;
 269                e->msi.data = ue->u.msi.data;
 270                break;
 271        default:
 272                goto out;
 273        }
 274        r = 0;
 275out:
 276        return r;
 277}
 278
 279
 280int kvm_set_irq_routing(struct kvm *kvm,
 281                        const struct kvm_irq_routing_entry *ue,
 282                        unsigned nr,
 283                        unsigned flags)
 284{
 285        struct list_head irq_list = LIST_HEAD_INIT(irq_list);
 286        struct list_head tmp = LIST_HEAD_INIT(tmp);
 287        struct kvm_kernel_irq_routing_entry *e = NULL;
 288        unsigned i;
 289        int r;
 290
 291        for (i = 0; i < nr; ++i) {
 292                r = -EINVAL;
 293                if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
 294                        goto out;
 295                if (ue->flags)
 296                        goto out;
 297                r = -ENOMEM;
 298                e = kzalloc(sizeof(*e), GFP_KERNEL);
 299                if (!e)
 300                        goto out;
 301                r = setup_routing_entry(e, ue);
 302                if (r)
 303                        goto out;
 304                ++ue;
 305                list_add(&e->link, &irq_list);
 306                e = NULL;
 307        }
 308
 309        mutex_lock(&kvm->lock);
 310        list_splice(&kvm->irq_routing, &tmp);
 311        INIT_LIST_HEAD(&kvm->irq_routing);
 312        list_splice(&irq_list, &kvm->irq_routing);
 313        INIT_LIST_HEAD(&irq_list);
 314        list_splice(&tmp, &irq_list);
 315        mutex_unlock(&kvm->lock);
 316
 317        r = 0;
 318
 319out:
 320        kfree(e);
 321        __kvm_free_irq_routing(&irq_list);
 322        return r;
 323}
 324
 325#define IOAPIC_ROUTING_ENTRY(irq) \
 326        { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
 327          .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
 328#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
 329
 330#ifdef CONFIG_X86
 331#  define PIC_ROUTING_ENTRY(irq) \
 332        { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
 333          .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
 334#  define ROUTING_ENTRY2(irq) \
 335        IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
 336#else
 337#  define ROUTING_ENTRY2(irq) \
 338        IOAPIC_ROUTING_ENTRY(irq)
 339#endif
 340
 341static const struct kvm_irq_routing_entry default_routing[] = {
 342        ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
 343        ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
 344        ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
 345        ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
 346        ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
 347        ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
 348        ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
 349        ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
 350        ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
 351        ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
 352        ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
 353        ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
 354#ifdef CONFIG_IA64
 355        ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
 356        ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
 357        ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
 358        ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
 359        ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
 360        ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
 361        ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
 362        ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
 363        ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
 364        ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
 365        ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
 366        ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
 367#endif
 368};
 369
 370int kvm_setup_default_irq_routing(struct kvm *kvm)
 371{
 372        return kvm_set_irq_routing(kvm, default_routing,
 373                                   ARRAY_SIZE(default_routing), 0);
 374}
 375