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 | * |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 20 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <linux/kvm_host.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 25 | #include <trace/events/kvm.h> |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 26 | |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 27 | #include <asm/msidef.h> |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 28 | #ifdef CONFIG_IA64 |
| 29 | #include <asm/iosapic.h> |
| 30 | #endif |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 31 | |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 32 | #include "irq.h" |
| 33 | |
| 34 | #include "ioapic.h" |
| 35 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 36 | 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] | 37 | struct kvm *kvm, int irq_source_id, int level) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 38 | { |
| 39 | #ifdef CONFIG_X86 |
Gleb Natapov | 1a6e4a8 | 2009-08-24 11:54:19 +0300 | [diff] [blame] | 40 | struct kvm_pic *pic = pic_irqchip(kvm); |
Michael S. Tsirkin | 1a577b72 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 41 | return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level); |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 42 | #else |
| 43 | return -1; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 44 | #endif |
| 45 | } |
| 46 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 47 | 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] | 48 | struct kvm *kvm, int irq_source_id, int level) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 49 | { |
Gleb Natapov | 1a6e4a8 | 2009-08-24 11:54:19 +0300 | [diff] [blame] | 50 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; |
Michael S. Tsirkin | 1a577b72 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 51 | return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 54 | 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] | 55 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 56 | #ifdef CONFIG_IA64 |
| 57 | return irq->delivery_mode == |
| 58 | (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT); |
| 59 | #else |
| 60 | return irq->delivery_mode == APIC_DM_LOWEST; |
| 61 | #endif |
| 62 | } |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 63 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 64 | int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, |
| 65 | struct kvm_lapic_irq *irq) |
| 66 | { |
| 67 | int i, r = -1; |
| 68 | struct kvm_vcpu *vcpu, *lowest = NULL; |
| 69 | |
| 70 | if (irq->dest_mode == 0 && irq->dest_id == 0xff && |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 71 | kvm_is_dm_lowest_prio(irq)) { |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 72 | printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n"); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 73 | irq->delivery_mode = APIC_DM_FIXED; |
| 74 | } |
| 75 | |
| 76 | if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r)) |
| 77 | return r; |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 78 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 79 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 80 | if (!kvm_apic_present(vcpu)) |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 81 | continue; |
Sheng Yang | 74a3a8f | 2009-03-04 13:33:02 +0800 | [diff] [blame] | 82 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 83 | if (!kvm_apic_match_dest(vcpu, src, irq->shorthand, |
| 84 | irq->dest_id, irq->dest_mode)) |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 85 | continue; |
| 86 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 87 | if (!kvm_is_dm_lowest_prio(irq)) { |
| 88 | if (r < 0) |
| 89 | r = 0; |
| 90 | r += kvm_apic_set_irq(vcpu, irq); |
Chris Lalancette | aefd18f | 2010-06-16 17:11:13 -0400 | [diff] [blame] | 91 | } else if (kvm_lapic_enabled(vcpu)) { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 92 | if (!lowest) |
| 93 | lowest = vcpu; |
| 94 | else if (kvm_apic_compare_prio(vcpu, lowest) < 0) |
| 95 | lowest = vcpu; |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 96 | } |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 99 | if (lowest) |
| 100 | r = kvm_apic_set_irq(lowest, irq); |
| 101 | |
| 102 | return r; |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 103 | } |
| 104 | |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 105 | static inline void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e, |
| 106 | struct kvm_lapic_irq *irq) |
| 107 | { |
| 108 | trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data); |
| 109 | |
| 110 | irq->dest_id = (e->msi.address_lo & |
| 111 | MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT; |
| 112 | irq->vector = (e->msi.data & |
| 113 | MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT; |
| 114 | irq->dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo; |
| 115 | irq->trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data; |
| 116 | irq->delivery_mode = e->msi.data & 0x700; |
| 117 | irq->level = 1; |
| 118 | irq->shorthand = 0; |
| 119 | /* TODO Deal with RH bit of MSI message address */ |
| 120 | } |
| 121 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 122 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, |
| 123 | struct kvm *kvm, int irq_source_id, int level) |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 124 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 125 | struct kvm_lapic_irq irq; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 126 | |
Gleb Natapov | 1a6e4a8 | 2009-08-24 11:54:19 +0300 | [diff] [blame] | 127 | if (!level) |
| 128 | return -1; |
| 129 | |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 130 | kvm_set_msi_irq(e, &irq); |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 131 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 132 | return kvm_irq_delivery_to_apic(kvm, NULL, &irq); |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 133 | } |
| 134 | |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 135 | |
| 136 | static int kvm_set_msi_inatomic(struct kvm_kernel_irq_routing_entry *e, |
| 137 | struct kvm *kvm) |
| 138 | { |
| 139 | struct kvm_lapic_irq irq; |
| 140 | int r; |
| 141 | |
| 142 | kvm_set_msi_irq(e, &irq); |
| 143 | |
| 144 | if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r)) |
| 145 | return r; |
| 146 | else |
| 147 | return -EWOULDBLOCK; |
| 148 | } |
| 149 | |
Jan Kiszka | 07975ad | 2012-03-29 21:14:12 +0200 | [diff] [blame] | 150 | int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi) |
| 151 | { |
| 152 | struct kvm_kernel_irq_routing_entry route; |
| 153 | |
| 154 | if (!irqchip_in_kernel(kvm) || msi->flags != 0) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | route.msi.address_lo = msi->address_lo; |
| 158 | route.msi.address_hi = msi->address_hi; |
| 159 | route.msi.data = msi->data; |
| 160 | |
| 161 | return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1); |
| 162 | } |
| 163 | |
Gleb Natapov | 680b364 | 2009-08-24 11:54:26 +0300 | [diff] [blame] | 164 | /* |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 165 | * Return value: |
| 166 | * < 0 Interrupt was ignored (masked or not delivered for other reasons) |
| 167 | * = 0 Interrupt was coalesced (previous irq is still pending) |
| 168 | * > 0 Number of CPUs interrupt was delivered to |
| 169 | */ |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 170 | 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] | 171 | { |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 172 | struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS]; |
| 173 | int ret = -1, i = 0; |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 174 | struct kvm_irq_routing_table *irq_rt; |
| 175 | struct hlist_node *n; |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 176 | |
Avi Kivity | ae8c1c4 | 2009-07-01 12:09:41 +0300 | [diff] [blame] | 177 | trace_kvm_set_irq(irq, level, irq_source_id); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 178 | |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 179 | /* Not possible to detect if the guest uses the PIC or the |
| 180 | * IOAPIC. So set the bit in both. The guest will ignore |
| 181 | * writes to the unused one. |
| 182 | */ |
Gleb Natapov | e42bba9 | 2009-08-24 11:54:22 +0300 | [diff] [blame] | 183 | rcu_read_lock(); |
| 184 | irq_rt = rcu_dereference(kvm->irq_routing); |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 185 | if (irq < irq_rt->nr_rt_entries) |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 186 | hlist_for_each_entry(e, n, &irq_rt->map[irq], link) |
| 187 | irq_set[i++] = *e; |
Gleb Natapov | e42bba9 | 2009-08-24 11:54:22 +0300 | [diff] [blame] | 188 | rcu_read_unlock(); |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 189 | |
| 190 | while(i--) { |
| 191 | int r; |
| 192 | r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level); |
| 193 | if (r < 0) |
| 194 | continue; |
| 195 | |
| 196 | ret = r + ((ret < 0) ? 0 : ret); |
| 197 | } |
| 198 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 199 | return ret; |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 200 | } |
| 201 | |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 202 | /* |
| 203 | * Deliver an IRQ in an atomic context if we can, or return a failure, |
| 204 | * user can retry in a process context. |
| 205 | * Return value: |
| 206 | * -EWOULDBLOCK - Can't deliver in atomic context: retry in a process context. |
| 207 | * Other values - No need to retry. |
| 208 | */ |
| 209 | int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level) |
| 210 | { |
| 211 | struct kvm_kernel_irq_routing_entry *e; |
| 212 | int ret = -EINVAL; |
| 213 | struct kvm_irq_routing_table *irq_rt; |
| 214 | struct hlist_node *n; |
| 215 | |
| 216 | trace_kvm_set_irq(irq, level, irq_source_id); |
| 217 | |
| 218 | /* |
| 219 | * Injection into either PIC or IOAPIC might need to scan all CPUs, |
| 220 | * which would need to be retried from thread context; when same GSI |
| 221 | * is connected to both PIC and IOAPIC, we'd have to report a |
| 222 | * partial failure here. |
| 223 | * Since there's no easy way to do this, we only support injecting MSI |
| 224 | * which is limited to 1:1 GSI mapping. |
| 225 | */ |
| 226 | rcu_read_lock(); |
| 227 | irq_rt = rcu_dereference(kvm->irq_routing); |
| 228 | if (irq < irq_rt->nr_rt_entries) |
| 229 | hlist_for_each_entry(e, n, &irq_rt->map[irq], link) { |
| 230 | if (likely(e->type == KVM_IRQ_ROUTING_MSI)) |
| 231 | ret = kvm_set_msi_inatomic(e, kvm); |
| 232 | else |
| 233 | ret = -EWOULDBLOCK; |
| 234 | break; |
| 235 | } |
| 236 | rcu_read_unlock(); |
| 237 | return ret; |
| 238 | } |
| 239 | |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 240 | void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin) |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 241 | { |
| 242 | struct kvm_irq_ack_notifier *kian; |
| 243 | struct hlist_node *n; |
Gleb Natapov | 3e71f88 | 2009-08-24 11:54:21 +0300 | [diff] [blame] | 244 | int gsi; |
Marcelo Tosatti | 44882ee | 2009-01-27 15:12:38 -0200 | [diff] [blame] | 245 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 246 | trace_kvm_ack_irq(irqchip, pin); |
| 247 | |
Gleb Natapov | e42bba9 | 2009-08-24 11:54:22 +0300 | [diff] [blame] | 248 | rcu_read_lock(); |
| 249 | gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin]; |
Gleb Natapov | 3e71f88 | 2009-08-24 11:54:21 +0300 | [diff] [blame] | 250 | if (gsi != -1) |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 251 | hlist_for_each_entry_rcu(kian, n, &kvm->irq_ack_notifier_list, |
| 252 | link) |
Gleb Natapov | 3e71f88 | 2009-08-24 11:54:21 +0300 | [diff] [blame] | 253 | if (kian->gsi == gsi) |
| 254 | kian->irq_acked(kian); |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 255 | rcu_read_unlock(); |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void kvm_register_irq_ack_notifier(struct kvm *kvm, |
| 259 | struct kvm_irq_ack_notifier *kian) |
| 260 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 261 | mutex_lock(&kvm->irq_lock); |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 262 | hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 263 | mutex_unlock(&kvm->irq_lock); |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 266 | void kvm_unregister_irq_ack_notifier(struct kvm *kvm, |
| 267 | struct kvm_irq_ack_notifier *kian) |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 268 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 269 | mutex_lock(&kvm->irq_lock); |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 270 | hlist_del_init_rcu(&kian->link); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 271 | mutex_unlock(&kvm->irq_lock); |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 272 | synchronize_rcu(); |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 273 | } |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 274 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 275 | int kvm_request_irq_source_id(struct kvm *kvm) |
| 276 | { |
| 277 | unsigned long *bitmap = &kvm->arch.irq_sources_bitmap; |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 278 | int irq_source_id; |
| 279 | |
| 280 | mutex_lock(&kvm->irq_lock); |
Marcelo Tosatti | cd5a268 | 2009-10-17 22:47:23 -0300 | [diff] [blame] | 281 | irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 282 | |
Marcelo Tosatti | cd5a268 | 2009-10-17 22:47:23 -0300 | [diff] [blame] | 283 | if (irq_source_id >= BITS_PER_LONG) { |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 284 | printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n"); |
Jiri Slaby | 0c6ddce | 2009-09-25 09:33:38 +0200 | [diff] [blame] | 285 | irq_source_id = -EFAULT; |
| 286 | goto unlock; |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 290 | #ifdef CONFIG_X86 |
| 291 | ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); |
| 292 | #endif |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 293 | set_bit(irq_source_id, bitmap); |
Jiri Slaby | 0c6ddce | 2009-09-25 09:33:38 +0200 | [diff] [blame] | 294 | unlock: |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 295 | mutex_unlock(&kvm->irq_lock); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 296 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 297 | return irq_source_id; |
| 298 | } |
| 299 | |
| 300 | void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id) |
| 301 | { |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 302 | ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 303 | #ifdef CONFIG_X86 |
| 304 | ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); |
| 305 | #endif |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 306 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 307 | mutex_lock(&kvm->irq_lock); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 308 | if (irq_source_id < 0 || |
Marcelo Tosatti | cd5a268 | 2009-10-17 22:47:23 -0300 | [diff] [blame] | 309 | irq_source_id >= BITS_PER_LONG) { |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 310 | printk(KERN_ERR "kvm: IRQ source ID out of range!\n"); |
Jiri Slaby | 0c6ddce | 2009-09-25 09:33:38 +0200 | [diff] [blame] | 311 | goto unlock; |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 312 | } |
Marcelo Tosatti | e50212b | 2009-10-29 13:44:17 -0200 | [diff] [blame] | 313 | clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap); |
| 314 | if (!irqchip_in_kernel(kvm)) |
| 315 | goto unlock; |
| 316 | |
Michael S. Tsirkin | 1a577b72 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 317 | kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id); |
Gleb Natapov | 1a6e4a8 | 2009-08-24 11:54:19 +0300 | [diff] [blame] | 318 | #ifdef CONFIG_X86 |
Michael S. Tsirkin | 1a577b72 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 319 | kvm_pic_clear_all(pic_irqchip(kvm), irq_source_id); |
Gleb Natapov | 1a6e4a8 | 2009-08-24 11:54:19 +0300 | [diff] [blame] | 320 | #endif |
Jiri Slaby | 0c6ddce | 2009-09-25 09:33:38 +0200 | [diff] [blame] | 321 | unlock: |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 322 | mutex_unlock(&kvm->irq_lock); |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 323 | } |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 324 | |
| 325 | void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, |
| 326 | struct kvm_irq_mask_notifier *kimn) |
| 327 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 328 | mutex_lock(&kvm->irq_lock); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 329 | kimn->irq = irq; |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 330 | hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 331 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, |
| 335 | struct kvm_irq_mask_notifier *kimn) |
| 336 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 337 | mutex_lock(&kvm->irq_lock); |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 338 | hlist_del_rcu(&kimn->link); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 339 | mutex_unlock(&kvm->irq_lock); |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 340 | synchronize_rcu(); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Gleb Natapov | 4a99435 | 2010-07-11 15:32:23 +0300 | [diff] [blame] | 343 | void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin, |
| 344 | bool mask) |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 345 | { |
| 346 | struct kvm_irq_mask_notifier *kimn; |
| 347 | struct hlist_node *n; |
Gleb Natapov | 4a99435 | 2010-07-11 15:32:23 +0300 | [diff] [blame] | 348 | int gsi; |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 349 | |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 350 | rcu_read_lock(); |
Gleb Natapov | 4a99435 | 2010-07-11 15:32:23 +0300 | [diff] [blame] | 351 | gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin]; |
| 352 | if (gsi != -1) |
| 353 | hlist_for_each_entry_rcu(kimn, n, &kvm->mask_notifier_list, link) |
| 354 | if (kimn->irq == gsi) |
| 355 | kimn->func(kimn, mask); |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 356 | rcu_read_unlock(); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 359 | void kvm_free_irq_routing(struct kvm *kvm) |
| 360 | { |
Gleb Natapov | e42bba9 | 2009-08-24 11:54:22 +0300 | [diff] [blame] | 361 | /* Called only during vm destruction. Nobody can use the pointer |
| 362 | at this stage */ |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 363 | kfree(kvm->irq_routing); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 366 | static int setup_routing_entry(struct kvm_irq_routing_table *rt, |
| 367 | struct kvm_kernel_irq_routing_entry *e, |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 368 | const struct kvm_irq_routing_entry *ue) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 369 | { |
| 370 | int r = -EINVAL; |
| 371 | int delta; |
Marcelo Tosatti | d72118c | 2010-01-12 16:42:09 -0200 | [diff] [blame] | 372 | unsigned max_pin; |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 373 | struct kvm_kernel_irq_routing_entry *ei; |
| 374 | struct hlist_node *n; |
| 375 | |
| 376 | /* |
| 377 | * Do not allow GSI to be mapped to the same irqchip more than once. |
| 378 | * Allow only one to one mapping between GSI and MSI. |
| 379 | */ |
| 380 | hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link) |
| 381 | if (ei->type == KVM_IRQ_ROUTING_MSI || |
Avi Kivity | f2ebd42 | 2012-04-22 17:02:11 +0300 | [diff] [blame] | 382 | ue->type == KVM_IRQ_ROUTING_MSI || |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 383 | ue->u.irqchip.irqchip == ei->irqchip.irqchip) |
| 384 | return r; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 385 | |
| 386 | e->gsi = ue->gsi; |
Michael S. Tsirkin | 5116d8f | 2009-07-26 17:10:01 +0300 | [diff] [blame] | 387 | e->type = ue->type; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 388 | switch (ue->type) { |
| 389 | case KVM_IRQ_ROUTING_IRQCHIP: |
| 390 | delta = 0; |
| 391 | switch (ue->u.irqchip.irqchip) { |
| 392 | case KVM_IRQCHIP_PIC_MASTER: |
| 393 | e->set = kvm_set_pic_irq; |
Michael S. Tsirkin | 93b6547 | 2012-07-19 13:55:53 +0300 | [diff] [blame] | 394 | max_pin = PIC_NUM_PINS; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 395 | break; |
| 396 | case KVM_IRQCHIP_PIC_SLAVE: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 397 | e->set = kvm_set_pic_irq; |
Michael S. Tsirkin | 93b6547 | 2012-07-19 13:55:53 +0300 | [diff] [blame] | 398 | max_pin = PIC_NUM_PINS; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 399 | delta = 8; |
| 400 | break; |
| 401 | case KVM_IRQCHIP_IOAPIC: |
Marcelo Tosatti | d72118c | 2010-01-12 16:42:09 -0200 | [diff] [blame] | 402 | max_pin = KVM_IOAPIC_NUM_PINS; |
Chris Wright | efbc100 | 2009-05-01 14:15:43 -0700 | [diff] [blame] | 403 | e->set = kvm_set_ioapic_irq; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 404 | break; |
| 405 | default: |
| 406 | goto out; |
| 407 | } |
| 408 | e->irqchip.irqchip = ue->u.irqchip.irqchip; |
| 409 | e->irqchip.pin = ue->u.irqchip.pin + delta; |
Marcelo Tosatti | d72118c | 2010-01-12 16:42:09 -0200 | [diff] [blame] | 410 | if (e->irqchip.pin >= max_pin) |
Gleb Natapov | 3e71f88 | 2009-08-24 11:54:21 +0300 | [diff] [blame] | 411 | goto out; |
| 412 | rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 413 | break; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 414 | case KVM_IRQ_ROUTING_MSI: |
| 415 | e->set = kvm_set_msi; |
| 416 | e->msi.address_lo = ue->u.msi.address_lo; |
| 417 | e->msi.address_hi = ue->u.msi.address_hi; |
| 418 | e->msi.data = ue->u.msi.data; |
| 419 | break; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 420 | default: |
| 421 | goto out; |
| 422 | } |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 423 | |
| 424 | hlist_add_head(&e->link, &rt->map[e->gsi]); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 425 | r = 0; |
| 426 | out: |
| 427 | return r; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | int kvm_set_irq_routing(struct kvm *kvm, |
| 432 | const struct kvm_irq_routing_entry *ue, |
| 433 | unsigned nr, |
| 434 | unsigned flags) |
| 435 | { |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 436 | struct kvm_irq_routing_table *new, *old; |
Gleb Natapov | 3e71f88 | 2009-08-24 11:54:21 +0300 | [diff] [blame] | 437 | u32 i, j, nr_rt_entries = 0; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 438 | int r; |
| 439 | |
| 440 | for (i = 0; i < nr; ++i) { |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 441 | if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES) |
| 442 | return -EINVAL; |
| 443 | nr_rt_entries = max(nr_rt_entries, ue[i].gsi); |
| 444 | } |
| 445 | |
| 446 | nr_rt_entries += 1; |
| 447 | |
| 448 | new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head)) |
| 449 | + (nr * sizeof(struct kvm_kernel_irq_routing_entry)), |
| 450 | GFP_KERNEL); |
| 451 | |
| 452 | if (!new) |
| 453 | return -ENOMEM; |
| 454 | |
| 455 | new->rt_entries = (void *)&new->map[nr_rt_entries]; |
| 456 | |
| 457 | new->nr_rt_entries = nr_rt_entries; |
Gleb Natapov | 3e71f88 | 2009-08-24 11:54:21 +0300 | [diff] [blame] | 458 | for (i = 0; i < 3; i++) |
| 459 | for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++) |
| 460 | new->chip[i][j] = -1; |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 461 | |
| 462 | for (i = 0; i < nr; ++i) { |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 463 | r = -EINVAL; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 464 | if (ue->flags) |
| 465 | goto out; |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 466 | r = setup_routing_entry(new, &new->rt_entries[i], ue); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 467 | if (r) |
| 468 | goto out; |
| 469 | ++ue; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 472 | mutex_lock(&kvm->irq_lock); |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 473 | old = kvm->irq_routing; |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 474 | kvm_irq_routing_update(kvm, new); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 475 | mutex_unlock(&kvm->irq_lock); |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 476 | |
Gleb Natapov | e42bba9 | 2009-08-24 11:54:22 +0300 | [diff] [blame] | 477 | synchronize_rcu(); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 478 | |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 479 | new = old; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 480 | r = 0; |
| 481 | |
| 482 | out: |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 483 | kfree(new); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 484 | return r; |
| 485 | } |
| 486 | |
| 487 | #define IOAPIC_ROUTING_ENTRY(irq) \ |
| 488 | { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \ |
| 489 | .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) } |
| 490 | #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq) |
| 491 | |
| 492 | #ifdef CONFIG_X86 |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 493 | # define PIC_ROUTING_ENTRY(irq) \ |
| 494 | { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \ |
| 495 | .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 } |
| 496 | # define ROUTING_ENTRY2(irq) \ |
| 497 | IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq) |
| 498 | #else |
| 499 | # define ROUTING_ENTRY2(irq) \ |
| 500 | IOAPIC_ROUTING_ENTRY(irq) |
| 501 | #endif |
| 502 | |
| 503 | static const struct kvm_irq_routing_entry default_routing[] = { |
| 504 | ROUTING_ENTRY2(0), ROUTING_ENTRY2(1), |
| 505 | ROUTING_ENTRY2(2), ROUTING_ENTRY2(3), |
| 506 | ROUTING_ENTRY2(4), ROUTING_ENTRY2(5), |
| 507 | ROUTING_ENTRY2(6), ROUTING_ENTRY2(7), |
| 508 | ROUTING_ENTRY2(8), ROUTING_ENTRY2(9), |
| 509 | ROUTING_ENTRY2(10), ROUTING_ENTRY2(11), |
| 510 | ROUTING_ENTRY2(12), ROUTING_ENTRY2(13), |
| 511 | ROUTING_ENTRY2(14), ROUTING_ENTRY2(15), |
| 512 | ROUTING_ENTRY1(16), ROUTING_ENTRY1(17), |
| 513 | ROUTING_ENTRY1(18), ROUTING_ENTRY1(19), |
| 514 | ROUTING_ENTRY1(20), ROUTING_ENTRY1(21), |
| 515 | ROUTING_ENTRY1(22), ROUTING_ENTRY1(23), |
| 516 | #ifdef CONFIG_IA64 |
| 517 | ROUTING_ENTRY1(24), ROUTING_ENTRY1(25), |
| 518 | ROUTING_ENTRY1(26), ROUTING_ENTRY1(27), |
| 519 | ROUTING_ENTRY1(28), ROUTING_ENTRY1(29), |
| 520 | ROUTING_ENTRY1(30), ROUTING_ENTRY1(31), |
| 521 | ROUTING_ENTRY1(32), ROUTING_ENTRY1(33), |
| 522 | ROUTING_ENTRY1(34), ROUTING_ENTRY1(35), |
| 523 | ROUTING_ENTRY1(36), ROUTING_ENTRY1(37), |
| 524 | ROUTING_ENTRY1(38), ROUTING_ENTRY1(39), |
| 525 | ROUTING_ENTRY1(40), ROUTING_ENTRY1(41), |
| 526 | ROUTING_ENTRY1(42), ROUTING_ENTRY1(43), |
| 527 | ROUTING_ENTRY1(44), ROUTING_ENTRY1(45), |
| 528 | ROUTING_ENTRY1(46), ROUTING_ENTRY1(47), |
| 529 | #endif |
| 530 | }; |
| 531 | |
| 532 | int kvm_setup_default_irq_routing(struct kvm *kvm) |
| 533 | { |
| 534 | return kvm_set_irq_routing(kvm, default_routing, |
| 535 | ARRAY_SIZE(default_routing), 0); |
| 536 | } |