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