Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * irqchip.c: Common API for in kernel interrupt controllers |
| 3 | * Copyright (c) 2007, Intel Corporation. |
| 4 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 5 | * Copyright (c) 2013, Alexander Graf <agraf@suse.de> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms and conditions of the GNU General Public License, |
| 9 | * version 2, as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 18 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 19 | * |
| 20 | * This file is derived from virt/kvm/irq_comm.c. |
| 21 | * |
| 22 | * Authors: |
| 23 | * Yaozu (Eddie) Dong <Eddie.dong@intel.com> |
| 24 | * Alexander Graf <agraf@suse.de> |
| 25 | */ |
| 26 | |
| 27 | #include <linux/kvm_host.h> |
| 28 | #include <linux/slab.h> |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 29 | #include <linux/srcu.h> |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 30 | #include <linux/export.h> |
| 31 | #include <trace/events/kvm.h> |
| 32 | #include "irq.h" |
| 33 | |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 34 | struct kvm_irq_routing_table { |
| 35 | int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS]; |
| 36 | struct kvm_kernel_irq_routing_entry *rt_entries; |
| 37 | u32 nr_rt_entries; |
| 38 | /* |
| 39 | * Array indexed by gsi. Each entry contains list of irq chips |
| 40 | * the gsi is connected to. |
| 41 | */ |
| 42 | struct hlist_head map[0]; |
| 43 | }; |
| 44 | |
| 45 | int kvm_irq_map_gsi(struct kvm *kvm, |
| 46 | struct kvm_kernel_irq_routing_entry *entries, int gsi) |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 47 | { |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 48 | struct kvm_irq_routing_table *irq_rt; |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 49 | struct kvm_kernel_irq_routing_entry *e; |
| 50 | int n = 0; |
| 51 | |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 52 | irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, |
| 53 | lockdep_is_held(&kvm->irq_lock)); |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 54 | if (gsi < irq_rt->nr_rt_entries) { |
| 55 | hlist_for_each_entry(e, &irq_rt->map[gsi], link) { |
| 56 | entries[n] = *e; |
| 57 | ++n; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return n; |
| 62 | } |
| 63 | |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 64 | int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin) |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 65 | { |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 66 | struct kvm_irq_routing_table *irq_rt; |
| 67 | |
| 68 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 69 | return irq_rt->chip[irqchip][pin]; |
| 70 | } |
| 71 | |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 72 | bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin) |
| 73 | { |
| 74 | struct kvm_irq_ack_notifier *kian; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 75 | int gsi, idx; |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 76 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 77 | idx = srcu_read_lock(&kvm->irq_srcu); |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 78 | gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin); |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 79 | if (gsi != -1) |
| 80 | hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list, |
| 81 | link) |
| 82 | if (kian->gsi == gsi) { |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 83 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 87 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 88 | |
| 89 | return false; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(kvm_irq_has_notifier); |
| 92 | |
| 93 | void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin) |
| 94 | { |
| 95 | struct kvm_irq_ack_notifier *kian; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 96 | int gsi, idx; |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 97 | |
| 98 | trace_kvm_ack_irq(irqchip, pin); |
| 99 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 100 | idx = srcu_read_lock(&kvm->irq_srcu); |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 101 | gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin); |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 102 | if (gsi != -1) |
| 103 | hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list, |
| 104 | link) |
| 105 | if (kian->gsi == gsi) |
| 106 | kian->irq_acked(kian); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 107 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void kvm_register_irq_ack_notifier(struct kvm *kvm, |
| 111 | struct kvm_irq_ack_notifier *kian) |
| 112 | { |
| 113 | mutex_lock(&kvm->irq_lock); |
| 114 | hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list); |
| 115 | mutex_unlock(&kvm->irq_lock); |
| 116 | #ifdef __KVM_HAVE_IOAPIC |
| 117 | kvm_vcpu_request_scan_ioapic(kvm); |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | void kvm_unregister_irq_ack_notifier(struct kvm *kvm, |
| 122 | struct kvm_irq_ack_notifier *kian) |
| 123 | { |
| 124 | mutex_lock(&kvm->irq_lock); |
| 125 | hlist_del_init_rcu(&kian->link); |
| 126 | mutex_unlock(&kvm->irq_lock); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 127 | synchronize_srcu(&kvm->irq_srcu); |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 128 | #ifdef __KVM_HAVE_IOAPIC |
| 129 | kvm_vcpu_request_scan_ioapic(kvm); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi) |
| 134 | { |
| 135 | struct kvm_kernel_irq_routing_entry route; |
| 136 | |
| 137 | if (!irqchip_in_kernel(kvm) || msi->flags != 0) |
| 138 | return -EINVAL; |
| 139 | |
| 140 | route.msi.address_lo = msi->address_lo; |
| 141 | route.msi.address_hi = msi->address_hi; |
| 142 | route.msi.data = msi->data; |
| 143 | |
| 144 | return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, false); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Return value: |
| 149 | * < 0 Interrupt was ignored (masked or not delivered for other reasons) |
| 150 | * = 0 Interrupt was coalesced (previous irq is still pending) |
| 151 | * > 0 Number of CPUs interrupt was delivered to |
| 152 | */ |
| 153 | int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level, |
| 154 | bool line_status) |
| 155 | { |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 156 | struct kvm_kernel_irq_routing_entry irq_set[KVM_NR_IRQCHIPS]; |
| 157 | int ret = -1, i, idx; |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 158 | |
| 159 | trace_kvm_set_irq(irq, level, irq_source_id); |
| 160 | |
| 161 | /* Not possible to detect if the guest uses the PIC or the |
| 162 | * IOAPIC. So set the bit in both. The guest will ignore |
| 163 | * writes to the unused one. |
| 164 | */ |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 165 | idx = srcu_read_lock(&kvm->irq_srcu); |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 166 | i = kvm_irq_map_gsi(kvm, irq_set, irq); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 167 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Alexander Graf | 1c9f852 | 2013-04-15 23:04:10 +0200 | [diff] [blame] | 168 | |
| 169 | while(i--) { |
| 170 | int r; |
| 171 | r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level, |
| 172 | line_status); |
| 173 | if (r < 0) |
| 174 | continue; |
| 175 | |
| 176 | ret = r + ((ret < 0) ? 0 : ret); |
| 177 | } |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | void kvm_free_irq_routing(struct kvm *kvm) |
| 183 | { |
| 184 | /* Called only during vm destruction. Nobody can use the pointer |
| 185 | at this stage */ |
| 186 | kfree(kvm->irq_routing); |
| 187 | } |
Alexander Graf | e8cde09 | 2013-04-15 23:23:21 +0200 | [diff] [blame] | 188 | |
| 189 | static int setup_routing_entry(struct kvm_irq_routing_table *rt, |
| 190 | struct kvm_kernel_irq_routing_entry *e, |
| 191 | const struct kvm_irq_routing_entry *ue) |
| 192 | { |
| 193 | int r = -EINVAL; |
| 194 | struct kvm_kernel_irq_routing_entry *ei; |
| 195 | |
| 196 | /* |
| 197 | * Do not allow GSI to be mapped to the same irqchip more than once. |
| 198 | * Allow only one to one mapping between GSI and MSI. |
| 199 | */ |
| 200 | hlist_for_each_entry(ei, &rt->map[ue->gsi], link) |
| 201 | if (ei->type == KVM_IRQ_ROUTING_MSI || |
| 202 | ue->type == KVM_IRQ_ROUTING_MSI || |
| 203 | ue->u.irqchip.irqchip == ei->irqchip.irqchip) |
| 204 | return r; |
| 205 | |
| 206 | e->gsi = ue->gsi; |
| 207 | e->type = ue->type; |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 208 | r = kvm_set_routing_entry(e, ue); |
Alexander Graf | e8cde09 | 2013-04-15 23:23:21 +0200 | [diff] [blame] | 209 | if (r) |
| 210 | goto out; |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 211 | if (e->type == KVM_IRQ_ROUTING_IRQCHIP) |
| 212 | rt->chip[e->irqchip.irqchip][e->irqchip.pin] = e->gsi; |
Alexander Graf | e8cde09 | 2013-04-15 23:23:21 +0200 | [diff] [blame] | 213 | |
| 214 | hlist_add_head(&e->link, &rt->map[e->gsi]); |
| 215 | r = 0; |
| 216 | out: |
| 217 | return r; |
| 218 | } |
| 219 | |
| 220 | int kvm_set_irq_routing(struct kvm *kvm, |
| 221 | const struct kvm_irq_routing_entry *ue, |
| 222 | unsigned nr, |
| 223 | unsigned flags) |
| 224 | { |
| 225 | struct kvm_irq_routing_table *new, *old; |
| 226 | u32 i, j, nr_rt_entries = 0; |
| 227 | int r; |
| 228 | |
| 229 | for (i = 0; i < nr; ++i) { |
| 230 | if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES) |
| 231 | return -EINVAL; |
| 232 | nr_rt_entries = max(nr_rt_entries, ue[i].gsi); |
| 233 | } |
| 234 | |
| 235 | nr_rt_entries += 1; |
| 236 | |
| 237 | new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head)) |
| 238 | + (nr * sizeof(struct kvm_kernel_irq_routing_entry)), |
| 239 | GFP_KERNEL); |
| 240 | |
| 241 | if (!new) |
| 242 | return -ENOMEM; |
| 243 | |
| 244 | new->rt_entries = (void *)&new->map[nr_rt_entries]; |
| 245 | |
| 246 | new->nr_rt_entries = nr_rt_entries; |
| 247 | for (i = 0; i < KVM_NR_IRQCHIPS; i++) |
| 248 | for (j = 0; j < KVM_IRQCHIP_NUM_PINS; j++) |
| 249 | new->chip[i][j] = -1; |
| 250 | |
| 251 | for (i = 0; i < nr; ++i) { |
| 252 | r = -EINVAL; |
| 253 | if (ue->flags) |
| 254 | goto out; |
| 255 | r = setup_routing_entry(new, &new->rt_entries[i], ue); |
| 256 | if (r) |
| 257 | goto out; |
| 258 | ++ue; |
| 259 | } |
| 260 | |
| 261 | mutex_lock(&kvm->irq_lock); |
| 262 | old = kvm->irq_routing; |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame^] | 263 | rcu_assign_pointer(kvm->irq_routing, new); |
| 264 | kvm_irq_routing_update(kvm); |
Alexander Graf | e8cde09 | 2013-04-15 23:23:21 +0200 | [diff] [blame] | 265 | mutex_unlock(&kvm->irq_lock); |
| 266 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 267 | synchronize_srcu_expedited(&kvm->irq_srcu); |
Alexander Graf | e8cde09 | 2013-04-15 23:23:21 +0200 | [diff] [blame] | 268 | |
| 269 | new = old; |
| 270 | r = 0; |
| 271 | |
| 272 | out: |
| 273 | kfree(new); |
| 274 | return r; |
| 275 | } |