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