Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 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> |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 23 | |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 24 | #include <asm/msidef.h> |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 25 | #ifdef CONFIG_IA64 |
| 26 | #include <asm/iosapic.h> |
| 27 | #endif |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 28 | |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 29 | #include "irq.h" |
| 30 | |
| 31 | #include "ioapic.h" |
| 32 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 33 | static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e, |
| 34 | struct kvm *kvm, int level) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 35 | { |
| 36 | #ifdef CONFIG_X86 |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 37 | return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level); |
| 38 | #else |
| 39 | return -1; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 40 | #endif |
| 41 | } |
| 42 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 43 | static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, |
| 44 | struct kvm *kvm, int level) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 45 | { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 46 | return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 49 | inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq) |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 50 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 51 | #ifdef CONFIG_IA64 |
| 52 | return irq->delivery_mode == |
| 53 | (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT); |
| 54 | #else |
| 55 | return irq->delivery_mode == APIC_DM_LOWEST; |
| 56 | #endif |
| 57 | } |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 58 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 59 | int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, |
| 60 | struct kvm_lapic_irq *irq) |
| 61 | { |
| 62 | int i, r = -1; |
| 63 | struct kvm_vcpu *vcpu, *lowest = NULL; |
| 64 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 65 | WARN_ON(!mutex_is_locked(&kvm->irq_lock)); |
| 66 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 67 | if (irq->dest_mode == 0 && irq->dest_id == 0xff && |
| 68 | kvm_is_dm_lowest_prio(irq)) |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 69 | printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n"); |
| 70 | |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 71 | for (i = 0; i < KVM_MAX_VCPUS; i++) { |
| 72 | vcpu = kvm->vcpus[i]; |
Sheng Yang | 74a3a8f | 2009-03-04 13:33:02 +0800 | [diff] [blame] | 73 | |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 74 | if (!vcpu || !kvm_apic_present(vcpu)) |
| 75 | continue; |
Sheng Yang | 74a3a8f | 2009-03-04 13:33:02 +0800 | [diff] [blame] | 76 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 77 | if (!kvm_apic_match_dest(vcpu, src, irq->shorthand, |
| 78 | irq->dest_id, irq->dest_mode)) |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 79 | continue; |
| 80 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 81 | if (!kvm_is_dm_lowest_prio(irq)) { |
| 82 | if (r < 0) |
| 83 | r = 0; |
| 84 | r += kvm_apic_set_irq(vcpu, irq); |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 85 | } else { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 86 | if (!lowest) |
| 87 | lowest = vcpu; |
| 88 | else if (kvm_apic_compare_prio(vcpu, lowest) < 0) |
| 89 | lowest = vcpu; |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 90 | } |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 93 | if (lowest) |
| 94 | r = kvm_apic_set_irq(lowest, irq); |
| 95 | |
| 96 | return r; |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 99 | static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, |
| 100 | struct kvm *kvm, int level) |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 101 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 102 | struct kvm_lapic_irq irq; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 103 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 104 | irq.dest_id = (e->msi.address_lo & |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 105 | MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT; |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 106 | irq.vector = (e->msi.data & |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 107 | MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT; |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 108 | irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo; |
| 109 | irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data; |
| 110 | irq.delivery_mode = e->msi.data & 0x700; |
| 111 | irq.level = 1; |
| 112 | irq.shorthand = 0; |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 113 | |
| 114 | /* TODO Deal with RH bit of MSI message address */ |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 115 | return kvm_irq_delivery_to_apic(kvm, NULL, &irq); |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 118 | /* This should be called with the kvm->irq_lock mutex held |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 119 | * Return value: |
| 120 | * < 0 Interrupt was ignored (masked or not delivered for other reasons) |
| 121 | * = 0 Interrupt was coalesced (previous irq is still pending) |
| 122 | * > 0 Number of CPUs interrupt was delivered to |
| 123 | */ |
| 124 | int kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level) |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 125 | { |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 126 | struct kvm_kernel_irq_routing_entry *e; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 127 | unsigned long *irq_state, sig_level; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 128 | int ret = -1; |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 129 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 130 | WARN_ON(!mutex_is_locked(&kvm->irq_lock)); |
| 131 | |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 132 | if (irq < KVM_IOAPIC_NUM_PINS) { |
| 133 | irq_state = (unsigned long *)&kvm->arch.irq_states[irq]; |
| 134 | |
| 135 | /* Logical OR for level trig interrupt */ |
| 136 | if (level) |
| 137 | set_bit(irq_source_id, irq_state); |
| 138 | else |
| 139 | clear_bit(irq_source_id, irq_state); |
| 140 | sig_level = !!(*irq_state); |
| 141 | } else /* Deal with MSI/MSI-X */ |
| 142 | sig_level = 1; |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 143 | |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 144 | /* Not possible to detect if the guest uses the PIC or the |
| 145 | * IOAPIC. So set the bit in both. The guest will ignore |
| 146 | * writes to the unused one. |
| 147 | */ |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 148 | list_for_each_entry(e, &kvm->irq_routing, link) |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 149 | if (e->gsi == irq) { |
| 150 | int r = e->set(e, kvm, sig_level); |
| 151 | if (r < 0) |
| 152 | continue; |
| 153 | |
| 154 | ret = r + ((ret < 0) ? 0 : ret); |
| 155 | } |
| 156 | return ret; |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 159 | void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin) |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 160 | { |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 161 | struct kvm_kernel_irq_routing_entry *e; |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 162 | struct kvm_irq_ack_notifier *kian; |
| 163 | struct hlist_node *n; |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 164 | unsigned gsi = pin; |
| 165 | |
| 166 | list_for_each_entry(e, &kvm->irq_routing, link) |
Michael S. Tsirkin | 5116d8f | 2009-07-26 17:10:01 +0300 | [diff] [blame] | 167 | if (e->type == KVM_IRQ_ROUTING_IRQCHIP && |
| 168 | e->irqchip.irqchip == irqchip && |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 169 | e->irqchip.pin == pin) { |
| 170 | gsi = e->gsi; |
| 171 | break; |
| 172 | } |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 173 | |
| 174 | hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link) |
| 175 | if (kian->gsi == gsi) |
| 176 | kian->irq_acked(kian); |
| 177 | } |
| 178 | |
| 179 | void kvm_register_irq_ack_notifier(struct kvm *kvm, |
| 180 | struct kvm_irq_ack_notifier *kian) |
| 181 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 182 | mutex_lock(&kvm->irq_lock); |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 183 | hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 184 | mutex_unlock(&kvm->irq_lock); |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 185 | } |
| 186 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 187 | void kvm_unregister_irq_ack_notifier(struct kvm *kvm, |
| 188 | struct kvm_irq_ack_notifier *kian) |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 189 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 190 | mutex_lock(&kvm->irq_lock); |
Mark McLoughlin | fdd897e | 2008-12-01 13:57:46 +0000 | [diff] [blame] | 191 | hlist_del_init(&kian->link); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 192 | mutex_unlock(&kvm->irq_lock); |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 193 | } |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 194 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 195 | int kvm_request_irq_source_id(struct kvm *kvm) |
| 196 | { |
| 197 | unsigned long *bitmap = &kvm->arch.irq_sources_bitmap; |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 198 | int irq_source_id; |
| 199 | |
| 200 | mutex_lock(&kvm->irq_lock); |
| 201 | irq_source_id = find_first_zero_bit(bitmap, |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 202 | sizeof(kvm->arch.irq_sources_bitmap)); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 203 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 204 | if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) { |
| 205 | printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n"); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 206 | return -EFAULT; |
| 207 | } |
| 208 | |
| 209 | ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID); |
| 210 | set_bit(irq_source_id, bitmap); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 211 | mutex_unlock(&kvm->irq_lock); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 212 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 213 | return irq_source_id; |
| 214 | } |
| 215 | |
| 216 | void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id) |
| 217 | { |
| 218 | int i; |
| 219 | |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 220 | ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID); |
| 221 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 222 | mutex_lock(&kvm->irq_lock); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 223 | if (irq_source_id < 0 || |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 224 | irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) { |
| 225 | printk(KERN_ERR "kvm: IRQ source ID out of range!\n"); |
| 226 | return; |
| 227 | } |
| 228 | for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++) |
| 229 | clear_bit(irq_source_id, &kvm->arch.irq_states[i]); |
| 230 | clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 231 | mutex_unlock(&kvm->irq_lock); |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 232 | } |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 233 | |
| 234 | void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, |
| 235 | struct kvm_irq_mask_notifier *kimn) |
| 236 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 237 | mutex_lock(&kvm->irq_lock); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 238 | kimn->irq = irq; |
| 239 | hlist_add_head(&kimn->link, &kvm->mask_notifier_list); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 240 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, |
| 244 | struct kvm_irq_mask_notifier *kimn) |
| 245 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 246 | mutex_lock(&kvm->irq_lock); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 247 | hlist_del(&kimn->link); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 248 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask) |
| 252 | { |
| 253 | struct kvm_irq_mask_notifier *kimn; |
| 254 | struct hlist_node *n; |
| 255 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 256 | WARN_ON(!mutex_is_locked(&kvm->irq_lock)); |
| 257 | |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 258 | hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link) |
| 259 | if (kimn->irq == irq) |
| 260 | kimn->func(kimn, mask); |
| 261 | } |
| 262 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 263 | static void __kvm_free_irq_routing(struct list_head *irq_routing) |
| 264 | { |
| 265 | struct kvm_kernel_irq_routing_entry *e, *n; |
| 266 | |
| 267 | list_for_each_entry_safe(e, n, irq_routing, link) |
| 268 | kfree(e); |
| 269 | } |
| 270 | |
| 271 | void kvm_free_irq_routing(struct kvm *kvm) |
| 272 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 273 | mutex_lock(&kvm->irq_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 274 | __kvm_free_irq_routing(&kvm->irq_routing); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 275 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 278 | static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e, |
| 279 | const struct kvm_irq_routing_entry *ue) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 280 | { |
| 281 | int r = -EINVAL; |
| 282 | int delta; |
| 283 | |
| 284 | e->gsi = ue->gsi; |
Michael S. Tsirkin | 5116d8f | 2009-07-26 17:10:01 +0300 | [diff] [blame] | 285 | e->type = ue->type; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 286 | switch (ue->type) { |
| 287 | case KVM_IRQ_ROUTING_IRQCHIP: |
| 288 | delta = 0; |
| 289 | switch (ue->u.irqchip.irqchip) { |
| 290 | case KVM_IRQCHIP_PIC_MASTER: |
| 291 | e->set = kvm_set_pic_irq; |
| 292 | break; |
| 293 | case KVM_IRQCHIP_PIC_SLAVE: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 294 | e->set = kvm_set_pic_irq; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 295 | delta = 8; |
| 296 | break; |
| 297 | case KVM_IRQCHIP_IOAPIC: |
Chris Wright | efbc100 | 2009-05-01 14:15:43 -0700 | [diff] [blame] | 298 | e->set = kvm_set_ioapic_irq; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 299 | break; |
| 300 | default: |
| 301 | goto out; |
| 302 | } |
| 303 | e->irqchip.irqchip = ue->u.irqchip.irqchip; |
| 304 | e->irqchip.pin = ue->u.irqchip.pin + delta; |
| 305 | break; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 306 | case KVM_IRQ_ROUTING_MSI: |
| 307 | e->set = kvm_set_msi; |
| 308 | e->msi.address_lo = ue->u.msi.address_lo; |
| 309 | e->msi.address_hi = ue->u.msi.address_hi; |
| 310 | e->msi.data = ue->u.msi.data; |
| 311 | break; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 312 | default: |
| 313 | goto out; |
| 314 | } |
| 315 | r = 0; |
| 316 | out: |
| 317 | return r; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | int kvm_set_irq_routing(struct kvm *kvm, |
| 322 | const struct kvm_irq_routing_entry *ue, |
| 323 | unsigned nr, |
| 324 | unsigned flags) |
| 325 | { |
| 326 | struct list_head irq_list = LIST_HEAD_INIT(irq_list); |
| 327 | struct list_head tmp = LIST_HEAD_INIT(tmp); |
| 328 | struct kvm_kernel_irq_routing_entry *e = NULL; |
| 329 | unsigned i; |
| 330 | int r; |
| 331 | |
| 332 | for (i = 0; i < nr; ++i) { |
| 333 | r = -EINVAL; |
| 334 | if (ue->gsi >= KVM_MAX_IRQ_ROUTES) |
| 335 | goto out; |
| 336 | if (ue->flags) |
| 337 | goto out; |
| 338 | r = -ENOMEM; |
| 339 | e = kzalloc(sizeof(*e), GFP_KERNEL); |
| 340 | if (!e) |
| 341 | goto out; |
| 342 | r = setup_routing_entry(e, ue); |
| 343 | if (r) |
| 344 | goto out; |
| 345 | ++ue; |
| 346 | list_add(&e->link, &irq_list); |
| 347 | e = NULL; |
| 348 | } |
| 349 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 350 | mutex_lock(&kvm->irq_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 351 | list_splice(&kvm->irq_routing, &tmp); |
| 352 | INIT_LIST_HEAD(&kvm->irq_routing); |
| 353 | list_splice(&irq_list, &kvm->irq_routing); |
| 354 | INIT_LIST_HEAD(&irq_list); |
| 355 | list_splice(&tmp, &irq_list); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 356 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 357 | |
| 358 | r = 0; |
| 359 | |
| 360 | out: |
| 361 | kfree(e); |
| 362 | __kvm_free_irq_routing(&irq_list); |
| 363 | return r; |
| 364 | } |
| 365 | |
| 366 | #define IOAPIC_ROUTING_ENTRY(irq) \ |
| 367 | { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \ |
| 368 | .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) } |
| 369 | #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq) |
| 370 | |
| 371 | #ifdef CONFIG_X86 |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 372 | # define PIC_ROUTING_ENTRY(irq) \ |
| 373 | { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \ |
| 374 | .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 } |
| 375 | # define ROUTING_ENTRY2(irq) \ |
| 376 | IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq) |
| 377 | #else |
| 378 | # define ROUTING_ENTRY2(irq) \ |
| 379 | IOAPIC_ROUTING_ENTRY(irq) |
| 380 | #endif |
| 381 | |
| 382 | static const struct kvm_irq_routing_entry default_routing[] = { |
| 383 | ROUTING_ENTRY2(0), ROUTING_ENTRY2(1), |
| 384 | ROUTING_ENTRY2(2), ROUTING_ENTRY2(3), |
| 385 | ROUTING_ENTRY2(4), ROUTING_ENTRY2(5), |
| 386 | ROUTING_ENTRY2(6), ROUTING_ENTRY2(7), |
| 387 | ROUTING_ENTRY2(8), ROUTING_ENTRY2(9), |
| 388 | ROUTING_ENTRY2(10), ROUTING_ENTRY2(11), |
| 389 | ROUTING_ENTRY2(12), ROUTING_ENTRY2(13), |
| 390 | ROUTING_ENTRY2(14), ROUTING_ENTRY2(15), |
| 391 | ROUTING_ENTRY1(16), ROUTING_ENTRY1(17), |
| 392 | ROUTING_ENTRY1(18), ROUTING_ENTRY1(19), |
| 393 | ROUTING_ENTRY1(20), ROUTING_ENTRY1(21), |
| 394 | ROUTING_ENTRY1(22), ROUTING_ENTRY1(23), |
| 395 | #ifdef CONFIG_IA64 |
| 396 | ROUTING_ENTRY1(24), ROUTING_ENTRY1(25), |
| 397 | ROUTING_ENTRY1(26), ROUTING_ENTRY1(27), |
| 398 | ROUTING_ENTRY1(28), ROUTING_ENTRY1(29), |
| 399 | ROUTING_ENTRY1(30), ROUTING_ENTRY1(31), |
| 400 | ROUTING_ENTRY1(32), ROUTING_ENTRY1(33), |
| 401 | ROUTING_ENTRY1(34), ROUTING_ENTRY1(35), |
| 402 | ROUTING_ENTRY1(36), ROUTING_ENTRY1(37), |
| 403 | ROUTING_ENTRY1(38), ROUTING_ENTRY1(39), |
| 404 | ROUTING_ENTRY1(40), ROUTING_ENTRY1(41), |
| 405 | ROUTING_ENTRY1(42), ROUTING_ENTRY1(43), |
| 406 | ROUTING_ENTRY1(44), ROUTING_ENTRY1(45), |
| 407 | ROUTING_ENTRY1(46), ROUTING_ENTRY1(47), |
| 408 | #endif |
| 409 | }; |
| 410 | |
| 411 | int kvm_setup_default_irq_routing(struct kvm *kvm) |
| 412 | { |
| 413 | return kvm_set_irq_routing(kvm, default_routing, |
| 414 | ARRAY_SIZE(default_routing), 0); |
| 415 | } |