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