Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine - device assignment support |
| 3 | * |
Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 4 | * Copyright (C) 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 7 | * the COPYING file in the top-level directory. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kvm_host.h> |
| 12 | #include <linux/kvm.h> |
| 13 | #include <linux/uaccess.h> |
| 14 | #include <linux/vmalloc.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/pci.h> |
| 18 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Alex Williamson | 3d27e23 | 2011-12-20 21:59:09 -0700 | [diff] [blame] | 20 | #include <linux/namei.h> |
| 21 | #include <linux/fs.h> |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 22 | #include "irq.h" |
| 23 | |
| 24 | static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head, |
| 25 | int assigned_dev_id) |
| 26 | { |
| 27 | struct list_head *ptr; |
| 28 | struct kvm_assigned_dev_kernel *match; |
| 29 | |
| 30 | list_for_each(ptr, head) { |
| 31 | match = list_entry(ptr, struct kvm_assigned_dev_kernel, list); |
| 32 | if (match->assigned_dev_id == assigned_dev_id) |
| 33 | return match; |
| 34 | } |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | static int find_index_from_host_irq(struct kvm_assigned_dev_kernel |
| 39 | *assigned_dev, int irq) |
| 40 | { |
| 41 | int i, index; |
| 42 | struct msix_entry *host_msix_entries; |
| 43 | |
| 44 | host_msix_entries = assigned_dev->host_msix_entries; |
| 45 | |
| 46 | index = -1; |
| 47 | for (i = 0; i < assigned_dev->entries_nr; i++) |
| 48 | if (irq == host_msix_entries[i].vector) { |
| 49 | index = i; |
| 50 | break; |
| 51 | } |
Michael S. Tsirkin | b93a355 | 2012-01-18 20:07:09 +0200 | [diff] [blame] | 52 | if (index < 0) |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 53 | printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n"); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 54 | |
| 55 | return index; |
| 56 | } |
| 57 | |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 58 | static irqreturn_t kvm_assigned_dev_intx(int irq, void *dev_id) |
| 59 | { |
| 60 | struct kvm_assigned_dev_kernel *assigned_dev = dev_id; |
| 61 | int ret; |
| 62 | |
| 63 | spin_lock(&assigned_dev->intx_lock); |
| 64 | if (pci_check_and_mask_intx(assigned_dev->dev)) { |
| 65 | assigned_dev->host_irq_disabled = true; |
| 66 | ret = IRQ_WAKE_THREAD; |
| 67 | } else |
| 68 | ret = IRQ_NONE; |
| 69 | spin_unlock(&assigned_dev->intx_lock); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | kvm_assigned_dev_raise_guest_irq(struct kvm_assigned_dev_kernel *assigned_dev, |
| 76 | int vector) |
| 77 | { |
| 78 | if (unlikely(assigned_dev->irq_requested_type & |
| 79 | KVM_DEV_IRQ_GUEST_INTX)) { |
Jan Kiszka | cf9eeac | 2012-03-14 11:02:11 +0100 | [diff] [blame] | 80 | spin_lock(&assigned_dev->intx_mask_lock); |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 81 | if (!(assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX)) |
| 82 | kvm_set_irq(assigned_dev->kvm, |
| 83 | assigned_dev->irq_source_id, vector, 1); |
Jan Kiszka | cf9eeac | 2012-03-14 11:02:11 +0100 | [diff] [blame] | 84 | spin_unlock(&assigned_dev->intx_mask_lock); |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 85 | } else |
| 86 | kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id, |
| 87 | vector, 1); |
| 88 | } |
| 89 | |
| 90 | static irqreturn_t kvm_assigned_dev_thread_intx(int irq, void *dev_id) |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 91 | { |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 92 | struct kvm_assigned_dev_kernel *assigned_dev = dev_id; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 93 | |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 94 | if (!(assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) { |
| 95 | spin_lock_irq(&assigned_dev->intx_lock); |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 96 | disable_irq_nosync(irq); |
| 97 | assigned_dev->host_irq_disabled = true; |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 98 | spin_unlock_irq(&assigned_dev->intx_lock); |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 99 | } |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 100 | |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 101 | kvm_assigned_dev_raise_guest_irq(assigned_dev, |
| 102 | assigned_dev->guest_irq); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 103 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 104 | return IRQ_HANDLED; |
| 105 | } |
| 106 | |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 107 | #ifdef __KVM_HAVE_MSI |
| 108 | static irqreturn_t kvm_assigned_dev_thread_msi(int irq, void *dev_id) |
| 109 | { |
| 110 | struct kvm_assigned_dev_kernel *assigned_dev = dev_id; |
| 111 | |
| 112 | kvm_assigned_dev_raise_guest_irq(assigned_dev, |
| 113 | assigned_dev->guest_irq); |
| 114 | |
| 115 | return IRQ_HANDLED; |
| 116 | } |
| 117 | #endif |
| 118 | |
Jan Kiszka | cc07939 | 2011-09-12 18:57:56 +0200 | [diff] [blame] | 119 | #ifdef __KVM_HAVE_MSIX |
| 120 | static irqreturn_t kvm_assigned_dev_thread_msix(int irq, void *dev_id) |
| 121 | { |
| 122 | struct kvm_assigned_dev_kernel *assigned_dev = dev_id; |
| 123 | int index = find_index_from_host_irq(assigned_dev, irq); |
| 124 | u32 vector; |
| 125 | |
| 126 | if (index >= 0) { |
| 127 | vector = assigned_dev->guest_msix_entries[index].vector; |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 128 | kvm_assigned_dev_raise_guest_irq(assigned_dev, vector); |
Jan Kiszka | cc07939 | 2011-09-12 18:57:56 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return IRQ_HANDLED; |
| 132 | } |
| 133 | #endif |
| 134 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 135 | /* Ack the irq line for an assigned device */ |
| 136 | static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian) |
| 137 | { |
Jan Kiszka | c61fa9d | 2011-09-11 10:16:20 +0200 | [diff] [blame] | 138 | struct kvm_assigned_dev_kernel *dev = |
| 139 | container_of(kian, struct kvm_assigned_dev_kernel, |
| 140 | ack_notifier); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 141 | |
| 142 | kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0); |
| 143 | |
Jan Kiszka | cf9eeac | 2012-03-14 11:02:11 +0100 | [diff] [blame] | 144 | spin_lock(&dev->intx_mask_lock); |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 145 | |
| 146 | if (!(dev->flags & KVM_DEV_ASSIGN_MASK_INTX)) { |
| 147 | bool reassert = false; |
| 148 | |
| 149 | spin_lock_irq(&dev->intx_lock); |
| 150 | /* |
| 151 | * The guest IRQ may be shared so this ack can come from an |
| 152 | * IRQ for another guest device. |
| 153 | */ |
| 154 | if (dev->host_irq_disabled) { |
| 155 | if (!(dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) |
| 156 | enable_irq(dev->host_irq); |
| 157 | else if (!pci_check_and_unmask_intx(dev->dev)) |
| 158 | reassert = true; |
| 159 | dev->host_irq_disabled = reassert; |
| 160 | } |
| 161 | spin_unlock_irq(&dev->intx_lock); |
| 162 | |
| 163 | if (reassert) |
| 164 | kvm_set_irq(dev->kvm, dev->irq_source_id, |
| 165 | dev->guest_irq, 1); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 166 | } |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 167 | |
Jan Kiszka | cf9eeac | 2012-03-14 11:02:11 +0100 | [diff] [blame] | 168 | spin_unlock(&dev->intx_mask_lock); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void deassign_guest_irq(struct kvm *kvm, |
| 172 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 173 | { |
Jan Kiszka | c61fa9d | 2011-09-11 10:16:20 +0200 | [diff] [blame] | 174 | if (assigned_dev->ack_notifier.gsi != -1) |
| 175 | kvm_unregister_irq_ack_notifier(kvm, |
| 176 | &assigned_dev->ack_notifier); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 177 | |
Jan Kiszka | 0c106b5 | 2010-11-16 22:30:02 +0100 | [diff] [blame] | 178 | kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id, |
| 179 | assigned_dev->guest_irq, 0); |
| 180 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 181 | if (assigned_dev->irq_source_id != -1) |
| 182 | kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id); |
| 183 | assigned_dev->irq_source_id = -1; |
| 184 | assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK); |
| 185 | } |
| 186 | |
| 187 | /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */ |
| 188 | static void deassign_host_irq(struct kvm *kvm, |
| 189 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 190 | { |
| 191 | /* |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 192 | * We disable irq here to prevent further events. |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 193 | * |
| 194 | * Notice this maybe result in nested disable if the interrupt type is |
| 195 | * INTx, but it's OK for we are going to free it. |
| 196 | * |
| 197 | * If this function is a part of VM destroy, please ensure that till |
| 198 | * now, the kvm state is still legal for probably we also have to wait |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 199 | * on a currently running IRQ handler. |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 200 | */ |
| 201 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { |
| 202 | int i; |
| 203 | for (i = 0; i < assigned_dev->entries_nr; i++) |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 204 | disable_irq(assigned_dev->host_msix_entries[i].vector); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 205 | |
| 206 | for (i = 0; i < assigned_dev->entries_nr; i++) |
| 207 | free_irq(assigned_dev->host_msix_entries[i].vector, |
Jan Kiszka | 9f9f6b7 | 2011-09-11 10:16:16 +0200 | [diff] [blame] | 208 | assigned_dev); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 209 | |
| 210 | assigned_dev->entries_nr = 0; |
| 211 | kfree(assigned_dev->host_msix_entries); |
| 212 | kfree(assigned_dev->guest_msix_entries); |
| 213 | pci_disable_msix(assigned_dev->dev); |
| 214 | } else { |
| 215 | /* Deal with MSI and INTx */ |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 216 | if ((assigned_dev->irq_requested_type & |
| 217 | KVM_DEV_IRQ_HOST_INTX) && |
| 218 | (assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) { |
| 219 | spin_lock_irq(&assigned_dev->intx_lock); |
| 220 | pci_intx(assigned_dev->dev, false); |
| 221 | spin_unlock_irq(&assigned_dev->intx_lock); |
| 222 | synchronize_irq(assigned_dev->host_irq); |
| 223 | } else |
| 224 | disable_irq(assigned_dev->host_irq); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 225 | |
Jan Kiszka | 9f9f6b7 | 2011-09-11 10:16:16 +0200 | [diff] [blame] | 226 | free_irq(assigned_dev->host_irq, assigned_dev); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 227 | |
| 228 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI) |
| 229 | pci_disable_msi(assigned_dev->dev); |
| 230 | } |
| 231 | |
| 232 | assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK); |
| 233 | } |
| 234 | |
| 235 | static int kvm_deassign_irq(struct kvm *kvm, |
| 236 | struct kvm_assigned_dev_kernel *assigned_dev, |
| 237 | unsigned long irq_requested_type) |
| 238 | { |
| 239 | unsigned long guest_irq_type, host_irq_type; |
| 240 | |
| 241 | if (!irqchip_in_kernel(kvm)) |
| 242 | return -EINVAL; |
| 243 | /* no irq assignment to deassign */ |
| 244 | if (!assigned_dev->irq_requested_type) |
| 245 | return -ENXIO; |
| 246 | |
| 247 | host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK; |
| 248 | guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK; |
| 249 | |
| 250 | if (host_irq_type) |
| 251 | deassign_host_irq(kvm, assigned_dev); |
| 252 | if (guest_irq_type) |
| 253 | deassign_guest_irq(kvm, assigned_dev); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static void kvm_free_assigned_irq(struct kvm *kvm, |
| 259 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 260 | { |
| 261 | kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type); |
| 262 | } |
| 263 | |
| 264 | static void kvm_free_assigned_device(struct kvm *kvm, |
| 265 | struct kvm_assigned_dev_kernel |
| 266 | *assigned_dev) |
| 267 | { |
| 268 | kvm_free_assigned_irq(kvm, assigned_dev); |
| 269 | |
Alex Williamson | f8fcfd7 | 2011-05-10 10:02:39 -0600 | [diff] [blame] | 270 | pci_reset_function(assigned_dev->dev); |
| 271 | if (pci_load_and_free_saved_state(assigned_dev->dev, |
| 272 | &assigned_dev->pci_saved_state)) |
| 273 | printk(KERN_INFO "%s: Couldn't reload %s saved state\n", |
| 274 | __func__, dev_name(&assigned_dev->dev->dev)); |
| 275 | else |
| 276 | pci_restore_state(assigned_dev->dev); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 277 | |
Greg Rose | 6777829 | 2011-07-22 05:46:07 +0000 | [diff] [blame] | 278 | assigned_dev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED; |
| 279 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 280 | pci_release_regions(assigned_dev->dev); |
| 281 | pci_disable_device(assigned_dev->dev); |
| 282 | pci_dev_put(assigned_dev->dev); |
| 283 | |
| 284 | list_del(&assigned_dev->list); |
| 285 | kfree(assigned_dev); |
| 286 | } |
| 287 | |
| 288 | void kvm_free_all_assigned_devices(struct kvm *kvm) |
| 289 | { |
| 290 | struct list_head *ptr, *ptr2; |
| 291 | struct kvm_assigned_dev_kernel *assigned_dev; |
| 292 | |
| 293 | list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) { |
| 294 | assigned_dev = list_entry(ptr, |
| 295 | struct kvm_assigned_dev_kernel, |
| 296 | list); |
| 297 | |
| 298 | kvm_free_assigned_device(kvm, assigned_dev); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static int assigned_device_enable_host_intx(struct kvm *kvm, |
| 303 | struct kvm_assigned_dev_kernel *dev) |
| 304 | { |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 305 | irq_handler_t irq_handler; |
| 306 | unsigned long flags; |
| 307 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 308 | dev->host_irq = dev->dev->irq; |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * We can only share the IRQ line with other host devices if we are |
| 312 | * able to disable the IRQ source at device-level - independently of |
| 313 | * the guest driver. Otherwise host devices may suffer from unbounded |
| 314 | * IRQ latencies when the guest keeps the line asserted. |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 315 | */ |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 316 | if (dev->flags & KVM_DEV_ASSIGN_PCI_2_3) { |
| 317 | irq_handler = kvm_assigned_dev_intx; |
| 318 | flags = IRQF_SHARED; |
| 319 | } else { |
| 320 | irq_handler = NULL; |
| 321 | flags = IRQF_ONESHOT; |
| 322 | } |
| 323 | if (request_threaded_irq(dev->host_irq, irq_handler, |
| 324 | kvm_assigned_dev_thread_intx, flags, |
| 325 | dev->irq_name, dev)) |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 326 | return -EIO; |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 327 | |
| 328 | if (dev->flags & KVM_DEV_ASSIGN_PCI_2_3) { |
| 329 | spin_lock_irq(&dev->intx_lock); |
| 330 | pci_intx(dev->dev, true); |
| 331 | spin_unlock_irq(&dev->intx_lock); |
| 332 | } |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | #ifdef __KVM_HAVE_MSI |
| 337 | static int assigned_device_enable_host_msi(struct kvm *kvm, |
| 338 | struct kvm_assigned_dev_kernel *dev) |
| 339 | { |
| 340 | int r; |
| 341 | |
| 342 | if (!dev->dev->msi_enabled) { |
| 343 | r = pci_enable_msi(dev->dev); |
| 344 | if (r) |
| 345 | return r; |
| 346 | } |
| 347 | |
| 348 | dev->host_irq = dev->dev->irq; |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 349 | if (request_threaded_irq(dev->host_irq, NULL, |
| 350 | kvm_assigned_dev_thread_msi, 0, |
| 351 | dev->irq_name, dev)) { |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 352 | pci_disable_msi(dev->dev); |
| 353 | return -EIO; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | #endif |
| 359 | |
| 360 | #ifdef __KVM_HAVE_MSIX |
| 361 | static int assigned_device_enable_host_msix(struct kvm *kvm, |
| 362 | struct kvm_assigned_dev_kernel *dev) |
| 363 | { |
| 364 | int i, r = -EINVAL; |
| 365 | |
| 366 | /* host_msix_entries and guest_msix_entries should have been |
| 367 | * initialized */ |
| 368 | if (dev->entries_nr == 0) |
| 369 | return r; |
| 370 | |
| 371 | r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr); |
| 372 | if (r) |
| 373 | return r; |
| 374 | |
| 375 | for (i = 0; i < dev->entries_nr; i++) { |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 376 | r = request_threaded_irq(dev->host_msix_entries[i].vector, |
Jan Kiszka | cc07939 | 2011-09-12 18:57:56 +0200 | [diff] [blame] | 377 | NULL, kvm_assigned_dev_thread_msix, |
Jan Kiszka | 9f9f6b7 | 2011-09-11 10:16:16 +0200 | [diff] [blame] | 378 | 0, dev->irq_name, dev); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 379 | if (r) |
jing zhang | d57e2c0 | 2010-03-13 15:00:45 +0800 | [diff] [blame] | 380 | goto err; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return 0; |
jing zhang | d57e2c0 | 2010-03-13 15:00:45 +0800 | [diff] [blame] | 384 | err: |
| 385 | for (i -= 1; i >= 0; i--) |
Jan Kiszka | 9f9f6b7 | 2011-09-11 10:16:16 +0200 | [diff] [blame] | 386 | free_irq(dev->host_msix_entries[i].vector, dev); |
jing zhang | d57e2c0 | 2010-03-13 15:00:45 +0800 | [diff] [blame] | 387 | pci_disable_msix(dev->dev); |
| 388 | return r; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | #endif |
| 392 | |
| 393 | static int assigned_device_enable_guest_intx(struct kvm *kvm, |
| 394 | struct kvm_assigned_dev_kernel *dev, |
| 395 | struct kvm_assigned_irq *irq) |
| 396 | { |
| 397 | dev->guest_irq = irq->guest_irq; |
| 398 | dev->ack_notifier.gsi = irq->guest_irq; |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | #ifdef __KVM_HAVE_MSI |
| 403 | static int assigned_device_enable_guest_msi(struct kvm *kvm, |
| 404 | struct kvm_assigned_dev_kernel *dev, |
| 405 | struct kvm_assigned_irq *irq) |
| 406 | { |
| 407 | dev->guest_irq = irq->guest_irq; |
| 408 | dev->ack_notifier.gsi = -1; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | #endif |
| 412 | |
| 413 | #ifdef __KVM_HAVE_MSIX |
| 414 | static int assigned_device_enable_guest_msix(struct kvm *kvm, |
| 415 | struct kvm_assigned_dev_kernel *dev, |
| 416 | struct kvm_assigned_irq *irq) |
| 417 | { |
| 418 | dev->guest_irq = irq->guest_irq; |
| 419 | dev->ack_notifier.gsi = -1; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | #endif |
| 423 | |
| 424 | static int assign_host_irq(struct kvm *kvm, |
| 425 | struct kvm_assigned_dev_kernel *dev, |
| 426 | __u32 host_irq_type) |
| 427 | { |
| 428 | int r = -EEXIST; |
| 429 | |
| 430 | if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK) |
| 431 | return r; |
| 432 | |
Jan Kiszka | 1e001d4 | 2010-11-16 22:30:04 +0100 | [diff] [blame] | 433 | snprintf(dev->irq_name, sizeof(dev->irq_name), "kvm:%s", |
| 434 | pci_name(dev->dev)); |
| 435 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 436 | switch (host_irq_type) { |
| 437 | case KVM_DEV_IRQ_HOST_INTX: |
| 438 | r = assigned_device_enable_host_intx(kvm, dev); |
| 439 | break; |
| 440 | #ifdef __KVM_HAVE_MSI |
| 441 | case KVM_DEV_IRQ_HOST_MSI: |
| 442 | r = assigned_device_enable_host_msi(kvm, dev); |
| 443 | break; |
| 444 | #endif |
| 445 | #ifdef __KVM_HAVE_MSIX |
| 446 | case KVM_DEV_IRQ_HOST_MSIX: |
| 447 | r = assigned_device_enable_host_msix(kvm, dev); |
| 448 | break; |
| 449 | #endif |
| 450 | default: |
| 451 | r = -EINVAL; |
| 452 | } |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 453 | dev->host_irq_disabled = false; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 454 | |
| 455 | if (!r) |
| 456 | dev->irq_requested_type |= host_irq_type; |
| 457 | |
| 458 | return r; |
| 459 | } |
| 460 | |
| 461 | static int assign_guest_irq(struct kvm *kvm, |
| 462 | struct kvm_assigned_dev_kernel *dev, |
| 463 | struct kvm_assigned_irq *irq, |
| 464 | unsigned long guest_irq_type) |
| 465 | { |
| 466 | int id; |
| 467 | int r = -EEXIST; |
| 468 | |
| 469 | if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK) |
| 470 | return r; |
| 471 | |
| 472 | id = kvm_request_irq_source_id(kvm); |
| 473 | if (id < 0) |
| 474 | return id; |
| 475 | |
| 476 | dev->irq_source_id = id; |
| 477 | |
| 478 | switch (guest_irq_type) { |
| 479 | case KVM_DEV_IRQ_GUEST_INTX: |
| 480 | r = assigned_device_enable_guest_intx(kvm, dev, irq); |
| 481 | break; |
| 482 | #ifdef __KVM_HAVE_MSI |
| 483 | case KVM_DEV_IRQ_GUEST_MSI: |
| 484 | r = assigned_device_enable_guest_msi(kvm, dev, irq); |
| 485 | break; |
| 486 | #endif |
| 487 | #ifdef __KVM_HAVE_MSIX |
| 488 | case KVM_DEV_IRQ_GUEST_MSIX: |
| 489 | r = assigned_device_enable_guest_msix(kvm, dev, irq); |
| 490 | break; |
| 491 | #endif |
| 492 | default: |
| 493 | r = -EINVAL; |
| 494 | } |
| 495 | |
| 496 | if (!r) { |
| 497 | dev->irq_requested_type |= guest_irq_type; |
Jan Kiszka | c61fa9d | 2011-09-11 10:16:20 +0200 | [diff] [blame] | 498 | if (dev->ack_notifier.gsi != -1) |
| 499 | kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 500 | } else |
| 501 | kvm_free_irq_source_id(kvm, dev->irq_source_id); |
| 502 | |
| 503 | return r; |
| 504 | } |
| 505 | |
| 506 | /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */ |
| 507 | static int kvm_vm_ioctl_assign_irq(struct kvm *kvm, |
| 508 | struct kvm_assigned_irq *assigned_irq) |
| 509 | { |
| 510 | int r = -EINVAL; |
| 511 | struct kvm_assigned_dev_kernel *match; |
| 512 | unsigned long host_irq_type, guest_irq_type; |
| 513 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 514 | if (!irqchip_in_kernel(kvm)) |
| 515 | return r; |
| 516 | |
| 517 | mutex_lock(&kvm->lock); |
| 518 | r = -ENODEV; |
| 519 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 520 | assigned_irq->assigned_dev_id); |
| 521 | if (!match) |
| 522 | goto out; |
| 523 | |
| 524 | host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK); |
| 525 | guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK); |
| 526 | |
| 527 | r = -EINVAL; |
| 528 | /* can only assign one type at a time */ |
| 529 | if (hweight_long(host_irq_type) > 1) |
| 530 | goto out; |
| 531 | if (hweight_long(guest_irq_type) > 1) |
| 532 | goto out; |
| 533 | if (host_irq_type == 0 && guest_irq_type == 0) |
| 534 | goto out; |
| 535 | |
| 536 | r = 0; |
| 537 | if (host_irq_type) |
| 538 | r = assign_host_irq(kvm, match, host_irq_type); |
| 539 | if (r) |
| 540 | goto out; |
| 541 | |
| 542 | if (guest_irq_type) |
| 543 | r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type); |
| 544 | out: |
| 545 | mutex_unlock(&kvm->lock); |
| 546 | return r; |
| 547 | } |
| 548 | |
| 549 | static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm, |
| 550 | struct kvm_assigned_irq |
| 551 | *assigned_irq) |
| 552 | { |
| 553 | int r = -ENODEV; |
| 554 | struct kvm_assigned_dev_kernel *match; |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 555 | unsigned long irq_type; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 556 | |
| 557 | mutex_lock(&kvm->lock); |
| 558 | |
| 559 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 560 | assigned_irq->assigned_dev_id); |
| 561 | if (!match) |
| 562 | goto out; |
| 563 | |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 564 | irq_type = assigned_irq->flags & (KVM_DEV_IRQ_HOST_MASK | |
| 565 | KVM_DEV_IRQ_GUEST_MASK); |
| 566 | r = kvm_deassign_irq(kvm, match, irq_type); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 567 | out: |
| 568 | mutex_unlock(&kvm->lock); |
| 569 | return r; |
| 570 | } |
| 571 | |
Alex Williamson | 3d27e23 | 2011-12-20 21:59:09 -0700 | [diff] [blame] | 572 | /* |
| 573 | * We want to test whether the caller has been granted permissions to |
| 574 | * use this device. To be able to configure and control the device, |
| 575 | * the user needs access to PCI configuration space and BAR resources. |
| 576 | * These are accessed through PCI sysfs. PCI config space is often |
| 577 | * passed to the process calling this ioctl via file descriptor, so we |
| 578 | * can't rely on access to that file. We can check for permissions |
| 579 | * on each of the BAR resource files, which is a pretty clear |
| 580 | * indicator that the user has been granted access to the device. |
| 581 | */ |
| 582 | static int probe_sysfs_permissions(struct pci_dev *dev) |
| 583 | { |
| 584 | #ifdef CONFIG_SYSFS |
| 585 | int i; |
| 586 | bool bar_found = false; |
| 587 | |
| 588 | for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++) { |
| 589 | char *kpath, *syspath; |
| 590 | struct path path; |
| 591 | struct inode *inode; |
| 592 | int r; |
| 593 | |
| 594 | if (!pci_resource_len(dev, i)) |
| 595 | continue; |
| 596 | |
| 597 | kpath = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); |
| 598 | if (!kpath) |
| 599 | return -ENOMEM; |
| 600 | |
| 601 | /* Per sysfs-rules, sysfs is always at /sys */ |
| 602 | syspath = kasprintf(GFP_KERNEL, "/sys%s/resource%d", kpath, i); |
| 603 | kfree(kpath); |
| 604 | if (!syspath) |
| 605 | return -ENOMEM; |
| 606 | |
| 607 | r = kern_path(syspath, LOOKUP_FOLLOW, &path); |
| 608 | kfree(syspath); |
| 609 | if (r) |
| 610 | return r; |
| 611 | |
| 612 | inode = path.dentry->d_inode; |
| 613 | |
| 614 | r = inode_permission(inode, MAY_READ | MAY_WRITE | MAY_ACCESS); |
| 615 | path_put(&path); |
| 616 | if (r) |
| 617 | return r; |
| 618 | |
| 619 | bar_found = true; |
| 620 | } |
| 621 | |
| 622 | /* If no resources, probably something special */ |
| 623 | if (!bar_found) |
| 624 | return -EPERM; |
| 625 | |
| 626 | return 0; |
| 627 | #else |
| 628 | return -EINVAL; /* No way to control the device without sysfs */ |
| 629 | #endif |
| 630 | } |
| 631 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 632 | static int kvm_vm_ioctl_assign_device(struct kvm *kvm, |
| 633 | struct kvm_assigned_pci_dev *assigned_dev) |
| 634 | { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 635 | int r = 0, idx; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 636 | struct kvm_assigned_dev_kernel *match; |
| 637 | struct pci_dev *dev; |
| 638 | |
Alex Williamson | 4238737 | 2011-12-20 21:59:03 -0700 | [diff] [blame] | 639 | if (!(assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)) |
| 640 | return -EINVAL; |
| 641 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 642 | mutex_lock(&kvm->lock); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 643 | idx = srcu_read_lock(&kvm->srcu); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 644 | |
| 645 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 646 | assigned_dev->assigned_dev_id); |
| 647 | if (match) { |
| 648 | /* device already assigned */ |
| 649 | r = -EEXIST; |
| 650 | goto out; |
| 651 | } |
| 652 | |
| 653 | match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL); |
| 654 | if (match == NULL) { |
| 655 | printk(KERN_INFO "%s: Couldn't allocate memory\n", |
| 656 | __func__); |
| 657 | r = -ENOMEM; |
| 658 | goto out; |
| 659 | } |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 660 | dev = pci_get_domain_bus_and_slot(assigned_dev->segnr, |
| 661 | assigned_dev->busnr, |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 662 | assigned_dev->devfn); |
| 663 | if (!dev) { |
| 664 | printk(KERN_INFO "%s: host device not found\n", __func__); |
| 665 | r = -EINVAL; |
| 666 | goto out_free; |
| 667 | } |
Alex Williamson | 3d27e23 | 2011-12-20 21:59:09 -0700 | [diff] [blame] | 668 | |
| 669 | /* Don't allow bridges to be assigned */ |
Jan Kiszka | f961f72 | 2012-06-15 07:56:20 -0600 | [diff] [blame] | 670 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) { |
Alex Williamson | 3d27e23 | 2011-12-20 21:59:09 -0700 | [diff] [blame] | 671 | r = -EPERM; |
| 672 | goto out_put; |
| 673 | } |
| 674 | |
| 675 | r = probe_sysfs_permissions(dev); |
| 676 | if (r) |
| 677 | goto out_put; |
| 678 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 679 | if (pci_enable_device(dev)) { |
| 680 | printk(KERN_INFO "%s: Could not enable PCI device\n", __func__); |
| 681 | r = -EBUSY; |
| 682 | goto out_put; |
| 683 | } |
| 684 | r = pci_request_regions(dev, "kvm_assigned_device"); |
| 685 | if (r) { |
| 686 | printk(KERN_INFO "%s: Could not get access to device regions\n", |
| 687 | __func__); |
| 688 | goto out_disable; |
| 689 | } |
| 690 | |
| 691 | pci_reset_function(dev); |
Jan Kiszka | ed78661 | 2010-11-16 22:30:05 +0100 | [diff] [blame] | 692 | pci_save_state(dev); |
Alex Williamson | f8fcfd7 | 2011-05-10 10:02:39 -0600 | [diff] [blame] | 693 | match->pci_saved_state = pci_store_saved_state(dev); |
| 694 | if (!match->pci_saved_state) |
| 695 | printk(KERN_DEBUG "%s: Couldn't store %s saved state\n", |
| 696 | __func__, dev_name(&dev->dev)); |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 697 | |
| 698 | if (!pci_intx_mask_supported(dev)) |
| 699 | assigned_dev->flags &= ~KVM_DEV_ASSIGN_PCI_2_3; |
| 700 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 701 | match->assigned_dev_id = assigned_dev->assigned_dev_id; |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 702 | match->host_segnr = assigned_dev->segnr; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 703 | match->host_busnr = assigned_dev->busnr; |
| 704 | match->host_devfn = assigned_dev->devfn; |
| 705 | match->flags = assigned_dev->flags; |
| 706 | match->dev = dev; |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 707 | spin_lock_init(&match->intx_lock); |
Jan Kiszka | cf9eeac | 2012-03-14 11:02:11 +0100 | [diff] [blame] | 708 | spin_lock_init(&match->intx_mask_lock); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 709 | match->irq_source_id = -1; |
| 710 | match->kvm = kvm; |
| 711 | match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 712 | |
| 713 | list_add(&match->list, &kvm->arch.assigned_dev_head); |
| 714 | |
Alex Williamson | 4238737 | 2011-12-20 21:59:03 -0700 | [diff] [blame] | 715 | if (!kvm->arch.iommu_domain) { |
| 716 | r = kvm_iommu_map_guest(kvm); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 717 | if (r) |
| 718 | goto out_list_del; |
| 719 | } |
Alex Williamson | 4238737 | 2011-12-20 21:59:03 -0700 | [diff] [blame] | 720 | r = kvm_assign_device(kvm, match); |
| 721 | if (r) |
| 722 | goto out_list_del; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 723 | |
| 724 | out: |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 725 | srcu_read_unlock(&kvm->srcu, idx); |
Sheng Yang | fae3a35 | 2009-12-15 10:28:07 +0800 | [diff] [blame] | 726 | mutex_unlock(&kvm->lock); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 727 | return r; |
| 728 | out_list_del: |
Alex Williamson | f8fcfd7 | 2011-05-10 10:02:39 -0600 | [diff] [blame] | 729 | if (pci_load_and_free_saved_state(dev, &match->pci_saved_state)) |
| 730 | printk(KERN_INFO "%s: Couldn't reload %s saved state\n", |
| 731 | __func__, dev_name(&dev->dev)); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 732 | list_del(&match->list); |
| 733 | pci_release_regions(dev); |
| 734 | out_disable: |
| 735 | pci_disable_device(dev); |
| 736 | out_put: |
| 737 | pci_dev_put(dev); |
| 738 | out_free: |
| 739 | kfree(match); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 740 | srcu_read_unlock(&kvm->srcu, idx); |
Sheng Yang | fae3a35 | 2009-12-15 10:28:07 +0800 | [diff] [blame] | 741 | mutex_unlock(&kvm->lock); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 742 | return r; |
| 743 | } |
| 744 | |
| 745 | static int kvm_vm_ioctl_deassign_device(struct kvm *kvm, |
| 746 | struct kvm_assigned_pci_dev *assigned_dev) |
| 747 | { |
| 748 | int r = 0; |
| 749 | struct kvm_assigned_dev_kernel *match; |
| 750 | |
| 751 | mutex_lock(&kvm->lock); |
| 752 | |
| 753 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 754 | assigned_dev->assigned_dev_id); |
| 755 | if (!match) { |
| 756 | printk(KERN_INFO "%s: device hasn't been assigned before, " |
| 757 | "so cannot be deassigned\n", __func__); |
| 758 | r = -EINVAL; |
| 759 | goto out; |
| 760 | } |
| 761 | |
Alex Williamson | 4238737 | 2011-12-20 21:59:03 -0700 | [diff] [blame] | 762 | kvm_deassign_device(kvm, match); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 763 | |
| 764 | kvm_free_assigned_device(kvm, match); |
| 765 | |
| 766 | out: |
| 767 | mutex_unlock(&kvm->lock); |
| 768 | return r; |
| 769 | } |
| 770 | |
| 771 | |
| 772 | #ifdef __KVM_HAVE_MSIX |
| 773 | static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm, |
| 774 | struct kvm_assigned_msix_nr *entry_nr) |
| 775 | { |
| 776 | int r = 0; |
| 777 | struct kvm_assigned_dev_kernel *adev; |
| 778 | |
| 779 | mutex_lock(&kvm->lock); |
| 780 | |
| 781 | adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 782 | entry_nr->assigned_dev_id); |
| 783 | if (!adev) { |
| 784 | r = -EINVAL; |
| 785 | goto msix_nr_out; |
| 786 | } |
| 787 | |
| 788 | if (adev->entries_nr == 0) { |
| 789 | adev->entries_nr = entry_nr->entry_nr; |
| 790 | if (adev->entries_nr == 0 || |
Jan Kiszka | 9f3191a | 2011-06-11 12:23:55 +0200 | [diff] [blame] | 791 | adev->entries_nr > KVM_MAX_MSIX_PER_DEV) { |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 792 | r = -EINVAL; |
| 793 | goto msix_nr_out; |
| 794 | } |
| 795 | |
| 796 | adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) * |
| 797 | entry_nr->entry_nr, |
| 798 | GFP_KERNEL); |
| 799 | if (!adev->host_msix_entries) { |
| 800 | r = -ENOMEM; |
| 801 | goto msix_nr_out; |
| 802 | } |
Jan Kiszka | 0645211 | 2010-11-16 22:30:03 +0100 | [diff] [blame] | 803 | adev->guest_msix_entries = |
| 804 | kzalloc(sizeof(struct msix_entry) * entry_nr->entry_nr, |
| 805 | GFP_KERNEL); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 806 | if (!adev->guest_msix_entries) { |
| 807 | kfree(adev->host_msix_entries); |
| 808 | r = -ENOMEM; |
| 809 | goto msix_nr_out; |
| 810 | } |
| 811 | } else /* Not allowed set MSI-X number twice */ |
| 812 | r = -EINVAL; |
| 813 | msix_nr_out: |
| 814 | mutex_unlock(&kvm->lock); |
| 815 | return r; |
| 816 | } |
| 817 | |
| 818 | static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm, |
| 819 | struct kvm_assigned_msix_entry *entry) |
| 820 | { |
| 821 | int r = 0, i; |
| 822 | struct kvm_assigned_dev_kernel *adev; |
| 823 | |
| 824 | mutex_lock(&kvm->lock); |
| 825 | |
| 826 | adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 827 | entry->assigned_dev_id); |
| 828 | |
| 829 | if (!adev) { |
| 830 | r = -EINVAL; |
| 831 | goto msix_entry_out; |
| 832 | } |
| 833 | |
| 834 | for (i = 0; i < adev->entries_nr; i++) |
| 835 | if (adev->guest_msix_entries[i].vector == 0 || |
| 836 | adev->guest_msix_entries[i].entry == entry->entry) { |
| 837 | adev->guest_msix_entries[i].entry = entry->entry; |
| 838 | adev->guest_msix_entries[i].vector = entry->gsi; |
| 839 | adev->host_msix_entries[i].entry = entry->entry; |
| 840 | break; |
| 841 | } |
| 842 | if (i == adev->entries_nr) { |
| 843 | r = -ENOSPC; |
| 844 | goto msix_entry_out; |
| 845 | } |
| 846 | |
| 847 | msix_entry_out: |
| 848 | mutex_unlock(&kvm->lock); |
| 849 | |
| 850 | return r; |
| 851 | } |
| 852 | #endif |
| 853 | |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 854 | static int kvm_vm_ioctl_set_pci_irq_mask(struct kvm *kvm, |
| 855 | struct kvm_assigned_pci_dev *assigned_dev) |
| 856 | { |
| 857 | int r = 0; |
| 858 | struct kvm_assigned_dev_kernel *match; |
| 859 | |
| 860 | mutex_lock(&kvm->lock); |
| 861 | |
| 862 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 863 | assigned_dev->assigned_dev_id); |
| 864 | if (!match) { |
| 865 | r = -ENODEV; |
| 866 | goto out; |
| 867 | } |
| 868 | |
Jan Kiszka | cf9eeac | 2012-03-14 11:02:11 +0100 | [diff] [blame] | 869 | spin_lock(&match->intx_mask_lock); |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 870 | |
| 871 | match->flags &= ~KVM_DEV_ASSIGN_MASK_INTX; |
| 872 | match->flags |= assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX; |
| 873 | |
| 874 | if (match->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) { |
| 875 | if (assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX) { |
| 876 | kvm_set_irq(match->kvm, match->irq_source_id, |
| 877 | match->guest_irq, 0); |
| 878 | /* |
| 879 | * Masking at hardware-level is performed on demand, |
| 880 | * i.e. when an IRQ actually arrives at the host. |
| 881 | */ |
| 882 | } else if (!(assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) { |
| 883 | /* |
| 884 | * Unmask the IRQ line if required. Unmasking at |
| 885 | * device level will be performed by user space. |
| 886 | */ |
| 887 | spin_lock_irq(&match->intx_lock); |
| 888 | if (match->host_irq_disabled) { |
| 889 | enable_irq(match->host_irq); |
| 890 | match->host_irq_disabled = false; |
| 891 | } |
| 892 | spin_unlock_irq(&match->intx_lock); |
| 893 | } |
| 894 | } |
| 895 | |
Jan Kiszka | cf9eeac | 2012-03-14 11:02:11 +0100 | [diff] [blame] | 896 | spin_unlock(&match->intx_mask_lock); |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 897 | |
| 898 | out: |
| 899 | mutex_unlock(&kvm->lock); |
| 900 | return r; |
| 901 | } |
| 902 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 903 | long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl, |
| 904 | unsigned long arg) |
| 905 | { |
| 906 | void __user *argp = (void __user *)arg; |
Jan Kiszka | 51de271 | 2010-11-16 22:30:06 +0100 | [diff] [blame] | 907 | int r; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 908 | |
| 909 | switch (ioctl) { |
| 910 | case KVM_ASSIGN_PCI_DEVICE: { |
| 911 | struct kvm_assigned_pci_dev assigned_dev; |
| 912 | |
| 913 | r = -EFAULT; |
| 914 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) |
| 915 | goto out; |
| 916 | r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev); |
| 917 | if (r) |
| 918 | goto out; |
| 919 | break; |
| 920 | } |
| 921 | case KVM_ASSIGN_IRQ: { |
| 922 | r = -EOPNOTSUPP; |
| 923 | break; |
| 924 | } |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 925 | case KVM_ASSIGN_DEV_IRQ: { |
| 926 | struct kvm_assigned_irq assigned_irq; |
| 927 | |
| 928 | r = -EFAULT; |
| 929 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) |
| 930 | goto out; |
| 931 | r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq); |
| 932 | if (r) |
| 933 | goto out; |
| 934 | break; |
| 935 | } |
| 936 | case KVM_DEASSIGN_DEV_IRQ: { |
| 937 | struct kvm_assigned_irq assigned_irq; |
| 938 | |
| 939 | r = -EFAULT; |
| 940 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) |
| 941 | goto out; |
| 942 | r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq); |
| 943 | if (r) |
| 944 | goto out; |
| 945 | break; |
| 946 | } |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 947 | case KVM_DEASSIGN_PCI_DEVICE: { |
| 948 | struct kvm_assigned_pci_dev assigned_dev; |
| 949 | |
| 950 | r = -EFAULT; |
| 951 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) |
| 952 | goto out; |
| 953 | r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev); |
| 954 | if (r) |
| 955 | goto out; |
| 956 | break; |
| 957 | } |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 958 | #ifdef KVM_CAP_IRQ_ROUTING |
| 959 | case KVM_SET_GSI_ROUTING: { |
| 960 | struct kvm_irq_routing routing; |
| 961 | struct kvm_irq_routing __user *urouting; |
| 962 | struct kvm_irq_routing_entry *entries; |
| 963 | |
| 964 | r = -EFAULT; |
| 965 | if (copy_from_user(&routing, argp, sizeof(routing))) |
| 966 | goto out; |
| 967 | r = -EINVAL; |
| 968 | if (routing.nr >= KVM_MAX_IRQ_ROUTES) |
| 969 | goto out; |
| 970 | if (routing.flags) |
| 971 | goto out; |
| 972 | r = -ENOMEM; |
| 973 | entries = vmalloc(routing.nr * sizeof(*entries)); |
| 974 | if (!entries) |
| 975 | goto out; |
| 976 | r = -EFAULT; |
| 977 | urouting = argp; |
| 978 | if (copy_from_user(entries, urouting->entries, |
| 979 | routing.nr * sizeof(*entries))) |
| 980 | goto out_free_irq_routing; |
| 981 | r = kvm_set_irq_routing(kvm, entries, routing.nr, |
| 982 | routing.flags); |
| 983 | out_free_irq_routing: |
| 984 | vfree(entries); |
| 985 | break; |
| 986 | } |
| 987 | #endif /* KVM_CAP_IRQ_ROUTING */ |
| 988 | #ifdef __KVM_HAVE_MSIX |
| 989 | case KVM_ASSIGN_SET_MSIX_NR: { |
| 990 | struct kvm_assigned_msix_nr entry_nr; |
| 991 | r = -EFAULT; |
| 992 | if (copy_from_user(&entry_nr, argp, sizeof entry_nr)) |
| 993 | goto out; |
| 994 | r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr); |
| 995 | if (r) |
| 996 | goto out; |
| 997 | break; |
| 998 | } |
| 999 | case KVM_ASSIGN_SET_MSIX_ENTRY: { |
| 1000 | struct kvm_assigned_msix_entry entry; |
| 1001 | r = -EFAULT; |
| 1002 | if (copy_from_user(&entry, argp, sizeof entry)) |
| 1003 | goto out; |
| 1004 | r = kvm_vm_ioctl_set_msix_entry(kvm, &entry); |
| 1005 | if (r) |
| 1006 | goto out; |
| 1007 | break; |
| 1008 | } |
| 1009 | #endif |
Jan Kiszka | 07700a9 | 2012-02-28 14:19:54 +0100 | [diff] [blame] | 1010 | case KVM_ASSIGN_SET_INTX_MASK: { |
| 1011 | struct kvm_assigned_pci_dev assigned_dev; |
| 1012 | |
| 1013 | r = -EFAULT; |
| 1014 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) |
| 1015 | goto out; |
| 1016 | r = kvm_vm_ioctl_set_pci_irq_mask(kvm, &assigned_dev); |
| 1017 | break; |
| 1018 | } |
Jan Kiszka | 51de271 | 2010-11-16 22:30:06 +0100 | [diff] [blame] | 1019 | default: |
| 1020 | r = -ENOTTY; |
| 1021 | break; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 1022 | } |
| 1023 | out: |
| 1024 | return r; |
| 1025 | } |