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 | |
| 24 | #ifdef CONFIG_X86 |
| 25 | #include <asm/msidef.h> |
| 26 | #endif |
| 27 | |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 28 | #include "irq.h" |
| 29 | |
| 30 | #include "ioapic.h" |
| 31 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 32 | static void kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e, |
| 33 | struct kvm *kvm, int level) |
| 34 | { |
| 35 | #ifdef CONFIG_X86 |
| 36 | kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level); |
| 37 | #endif |
| 38 | } |
| 39 | |
| 40 | static void kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, |
| 41 | struct kvm *kvm, int level) |
| 42 | { |
| 43 | kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level); |
| 44 | } |
| 45 | |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame^] | 46 | static void kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, |
| 47 | struct kvm *kvm, int level) |
| 48 | { |
| 49 | int vcpu_id; |
| 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 | 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 | kvm_apic_set_irq(vcpu, vector, trig_mode); |
| 86 | } |
| 87 | break; |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 93 | /* This should be called with the kvm->lock mutex held */ |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 94 | void 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] | 95 | { |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 96 | struct kvm_kernel_irq_routing_entry *e; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame^] | 97 | unsigned long *irq_state, sig_level; |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 98 | |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame^] | 99 | if (irq < KVM_IOAPIC_NUM_PINS) { |
| 100 | irq_state = (unsigned long *)&kvm->arch.irq_states[irq]; |
| 101 | |
| 102 | /* Logical OR for level trig interrupt */ |
| 103 | if (level) |
| 104 | set_bit(irq_source_id, irq_state); |
| 105 | else |
| 106 | clear_bit(irq_source_id, irq_state); |
| 107 | sig_level = !!(*irq_state); |
| 108 | } else /* Deal with MSI/MSI-X */ |
| 109 | sig_level = 1; |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 110 | |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 111 | /* Not possible to detect if the guest uses the PIC or the |
| 112 | * IOAPIC. So set the bit in both. The guest will ignore |
| 113 | * writes to the unused one. |
| 114 | */ |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 115 | list_for_each_entry(e, &kvm->irq_routing, link) |
| 116 | if (e->gsi == irq) |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame^] | 117 | e->set(e, kvm, sig_level); |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 120 | void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin) |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 121 | { |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 122 | struct kvm_kernel_irq_routing_entry *e; |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 123 | struct kvm_irq_ack_notifier *kian; |
| 124 | struct hlist_node *n; |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 125 | unsigned gsi = pin; |
| 126 | |
| 127 | list_for_each_entry(e, &kvm->irq_routing, link) |
| 128 | if (e->irqchip.irqchip == irqchip && |
| 129 | e->irqchip.pin == pin) { |
| 130 | gsi = e->gsi; |
| 131 | break; |
| 132 | } |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 133 | |
| 134 | hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link) |
| 135 | if (kian->gsi == gsi) |
| 136 | kian->irq_acked(kian); |
| 137 | } |
| 138 | |
| 139 | void kvm_register_irq_ack_notifier(struct kvm *kvm, |
| 140 | struct kvm_irq_ack_notifier *kian) |
| 141 | { |
| 142 | hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list); |
| 143 | } |
| 144 | |
Sheng Yang | e19e30e | 2008-10-20 16:07:10 +0800 | [diff] [blame] | 145 | void kvm_unregister_irq_ack_notifier(struct kvm_irq_ack_notifier *kian) |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 146 | { |
Mark McLoughlin | fdd897e | 2008-12-01 13:57:46 +0000 | [diff] [blame] | 147 | hlist_del_init(&kian->link); |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 148 | } |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 149 | |
| 150 | /* The caller must hold kvm->lock mutex */ |
| 151 | int kvm_request_irq_source_id(struct kvm *kvm) |
| 152 | { |
| 153 | unsigned long *bitmap = &kvm->arch.irq_sources_bitmap; |
| 154 | int irq_source_id = find_first_zero_bit(bitmap, |
| 155 | sizeof(kvm->arch.irq_sources_bitmap)); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 156 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 157 | if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) { |
| 158 | printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n"); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 159 | return -EFAULT; |
| 160 | } |
| 161 | |
| 162 | ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID); |
| 163 | set_bit(irq_source_id, bitmap); |
| 164 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 165 | return irq_source_id; |
| 166 | } |
| 167 | |
| 168 | void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id) |
| 169 | { |
| 170 | int i; |
| 171 | |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 172 | ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID); |
| 173 | |
| 174 | if (irq_source_id < 0 || |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 175 | irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) { |
| 176 | printk(KERN_ERR "kvm: IRQ source ID out of range!\n"); |
| 177 | return; |
| 178 | } |
| 179 | for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++) |
| 180 | clear_bit(irq_source_id, &kvm->arch.irq_states[i]); |
| 181 | clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap); |
| 182 | } |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 183 | |
| 184 | void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, |
| 185 | struct kvm_irq_mask_notifier *kimn) |
| 186 | { |
| 187 | kimn->irq = irq; |
| 188 | hlist_add_head(&kimn->link, &kvm->mask_notifier_list); |
| 189 | } |
| 190 | |
| 191 | void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, |
| 192 | struct kvm_irq_mask_notifier *kimn) |
| 193 | { |
| 194 | hlist_del(&kimn->link); |
| 195 | } |
| 196 | |
| 197 | void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask) |
| 198 | { |
| 199 | struct kvm_irq_mask_notifier *kimn; |
| 200 | struct hlist_node *n; |
| 201 | |
| 202 | hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link) |
| 203 | if (kimn->irq == irq) |
| 204 | kimn->func(kimn, mask); |
| 205 | } |
| 206 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 207 | static void __kvm_free_irq_routing(struct list_head *irq_routing) |
| 208 | { |
| 209 | struct kvm_kernel_irq_routing_entry *e, *n; |
| 210 | |
| 211 | list_for_each_entry_safe(e, n, irq_routing, link) |
| 212 | kfree(e); |
| 213 | } |
| 214 | |
| 215 | void kvm_free_irq_routing(struct kvm *kvm) |
| 216 | { |
| 217 | __kvm_free_irq_routing(&kvm->irq_routing); |
| 218 | } |
| 219 | |
| 220 | int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e, |
| 221 | const struct kvm_irq_routing_entry *ue) |
| 222 | { |
| 223 | int r = -EINVAL; |
| 224 | int delta; |
| 225 | |
| 226 | e->gsi = ue->gsi; |
| 227 | switch (ue->type) { |
| 228 | case KVM_IRQ_ROUTING_IRQCHIP: |
| 229 | delta = 0; |
| 230 | switch (ue->u.irqchip.irqchip) { |
| 231 | case KVM_IRQCHIP_PIC_MASTER: |
| 232 | e->set = kvm_set_pic_irq; |
| 233 | break; |
| 234 | case KVM_IRQCHIP_PIC_SLAVE: |
| 235 | e->set = kvm_set_pic_irq; |
| 236 | delta = 8; |
| 237 | break; |
| 238 | case KVM_IRQCHIP_IOAPIC: |
| 239 | e->set = kvm_set_ioapic_irq; |
| 240 | break; |
| 241 | default: |
| 242 | goto out; |
| 243 | } |
| 244 | e->irqchip.irqchip = ue->u.irqchip.irqchip; |
| 245 | e->irqchip.pin = ue->u.irqchip.pin + delta; |
| 246 | break; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame^] | 247 | case KVM_IRQ_ROUTING_MSI: |
| 248 | e->set = kvm_set_msi; |
| 249 | e->msi.address_lo = ue->u.msi.address_lo; |
| 250 | e->msi.address_hi = ue->u.msi.address_hi; |
| 251 | e->msi.data = ue->u.msi.data; |
| 252 | break; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 253 | default: |
| 254 | goto out; |
| 255 | } |
| 256 | r = 0; |
| 257 | out: |
| 258 | return r; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | int kvm_set_irq_routing(struct kvm *kvm, |
| 263 | const struct kvm_irq_routing_entry *ue, |
| 264 | unsigned nr, |
| 265 | unsigned flags) |
| 266 | { |
| 267 | struct list_head irq_list = LIST_HEAD_INIT(irq_list); |
| 268 | struct list_head tmp = LIST_HEAD_INIT(tmp); |
| 269 | struct kvm_kernel_irq_routing_entry *e = NULL; |
| 270 | unsigned i; |
| 271 | int r; |
| 272 | |
| 273 | for (i = 0; i < nr; ++i) { |
| 274 | r = -EINVAL; |
| 275 | if (ue->gsi >= KVM_MAX_IRQ_ROUTES) |
| 276 | goto out; |
| 277 | if (ue->flags) |
| 278 | goto out; |
| 279 | r = -ENOMEM; |
| 280 | e = kzalloc(sizeof(*e), GFP_KERNEL); |
| 281 | if (!e) |
| 282 | goto out; |
| 283 | r = setup_routing_entry(e, ue); |
| 284 | if (r) |
| 285 | goto out; |
| 286 | ++ue; |
| 287 | list_add(&e->link, &irq_list); |
| 288 | e = NULL; |
| 289 | } |
| 290 | |
| 291 | mutex_lock(&kvm->lock); |
| 292 | list_splice(&kvm->irq_routing, &tmp); |
| 293 | INIT_LIST_HEAD(&kvm->irq_routing); |
| 294 | list_splice(&irq_list, &kvm->irq_routing); |
| 295 | INIT_LIST_HEAD(&irq_list); |
| 296 | list_splice(&tmp, &irq_list); |
| 297 | mutex_unlock(&kvm->lock); |
| 298 | |
| 299 | r = 0; |
| 300 | |
| 301 | out: |
| 302 | kfree(e); |
| 303 | __kvm_free_irq_routing(&irq_list); |
| 304 | return r; |
| 305 | } |
| 306 | |
| 307 | #define IOAPIC_ROUTING_ENTRY(irq) \ |
| 308 | { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \ |
| 309 | .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) } |
| 310 | #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq) |
| 311 | |
| 312 | #ifdef CONFIG_X86 |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 313 | # define PIC_ROUTING_ENTRY(irq) \ |
| 314 | { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \ |
| 315 | .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 } |
| 316 | # define ROUTING_ENTRY2(irq) \ |
| 317 | IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq) |
| 318 | #else |
| 319 | # define ROUTING_ENTRY2(irq) \ |
| 320 | IOAPIC_ROUTING_ENTRY(irq) |
| 321 | #endif |
| 322 | |
| 323 | static const struct kvm_irq_routing_entry default_routing[] = { |
| 324 | ROUTING_ENTRY2(0), ROUTING_ENTRY2(1), |
| 325 | ROUTING_ENTRY2(2), ROUTING_ENTRY2(3), |
| 326 | ROUTING_ENTRY2(4), ROUTING_ENTRY2(5), |
| 327 | ROUTING_ENTRY2(6), ROUTING_ENTRY2(7), |
| 328 | ROUTING_ENTRY2(8), ROUTING_ENTRY2(9), |
| 329 | ROUTING_ENTRY2(10), ROUTING_ENTRY2(11), |
| 330 | ROUTING_ENTRY2(12), ROUTING_ENTRY2(13), |
| 331 | ROUTING_ENTRY2(14), ROUTING_ENTRY2(15), |
| 332 | ROUTING_ENTRY1(16), ROUTING_ENTRY1(17), |
| 333 | ROUTING_ENTRY1(18), ROUTING_ENTRY1(19), |
| 334 | ROUTING_ENTRY1(20), ROUTING_ENTRY1(21), |
| 335 | ROUTING_ENTRY1(22), ROUTING_ENTRY1(23), |
| 336 | #ifdef CONFIG_IA64 |
| 337 | ROUTING_ENTRY1(24), ROUTING_ENTRY1(25), |
| 338 | ROUTING_ENTRY1(26), ROUTING_ENTRY1(27), |
| 339 | ROUTING_ENTRY1(28), ROUTING_ENTRY1(29), |
| 340 | ROUTING_ENTRY1(30), ROUTING_ENTRY1(31), |
| 341 | ROUTING_ENTRY1(32), ROUTING_ENTRY1(33), |
| 342 | ROUTING_ENTRY1(34), ROUTING_ENTRY1(35), |
| 343 | ROUTING_ENTRY1(36), ROUTING_ENTRY1(37), |
| 344 | ROUTING_ENTRY1(38), ROUTING_ENTRY1(39), |
| 345 | ROUTING_ENTRY1(40), ROUTING_ENTRY1(41), |
| 346 | ROUTING_ENTRY1(42), ROUTING_ENTRY1(43), |
| 347 | ROUTING_ENTRY1(44), ROUTING_ENTRY1(45), |
| 348 | ROUTING_ENTRY1(46), ROUTING_ENTRY1(47), |
| 349 | #endif |
| 350 | }; |
| 351 | |
| 352 | int kvm_setup_default_irq_routing(struct kvm *kvm) |
| 353 | { |
| 354 | return kvm_set_irq_routing(kvm, default_routing, |
| 355 | ARRAY_SIZE(default_routing), 0); |
| 356 | } |