Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine - device assignment support |
| 3 | * |
| 4 | * Copyright (C) 2006-9 Red Hat, Inc |
| 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> |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 20 | #include "irq.h" |
| 21 | |
| 22 | static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head, |
| 23 | int assigned_dev_id) |
| 24 | { |
| 25 | struct list_head *ptr; |
| 26 | struct kvm_assigned_dev_kernel *match; |
| 27 | |
| 28 | list_for_each(ptr, head) { |
| 29 | match = list_entry(ptr, struct kvm_assigned_dev_kernel, list); |
| 30 | if (match->assigned_dev_id == assigned_dev_id) |
| 31 | return match; |
| 32 | } |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | static int find_index_from_host_irq(struct kvm_assigned_dev_kernel |
| 37 | *assigned_dev, int irq) |
| 38 | { |
| 39 | int i, index; |
| 40 | struct msix_entry *host_msix_entries; |
| 41 | |
| 42 | host_msix_entries = assigned_dev->host_msix_entries; |
| 43 | |
| 44 | index = -1; |
| 45 | for (i = 0; i < assigned_dev->entries_nr; i++) |
| 46 | if (irq == host_msix_entries[i].vector) { |
| 47 | index = i; |
| 48 | break; |
| 49 | } |
| 50 | if (index < 0) { |
| 51 | printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n"); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | return index; |
| 56 | } |
| 57 | |
| 58 | static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work) |
| 59 | { |
| 60 | struct kvm_assigned_dev_kernel *assigned_dev; |
| 61 | struct kvm *kvm; |
| 62 | int i; |
| 63 | |
| 64 | assigned_dev = container_of(work, struct kvm_assigned_dev_kernel, |
| 65 | interrupt_work); |
| 66 | kvm = assigned_dev->kvm; |
| 67 | |
| 68 | spin_lock_irq(&assigned_dev->assigned_dev_lock); |
| 69 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { |
| 70 | struct kvm_guest_msix_entry *guest_entries = |
| 71 | assigned_dev->guest_msix_entries; |
| 72 | for (i = 0; i < assigned_dev->entries_nr; i++) { |
| 73 | if (!(guest_entries[i].flags & |
| 74 | KVM_ASSIGNED_MSIX_PENDING)) |
| 75 | continue; |
| 76 | guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING; |
| 77 | kvm_set_irq(assigned_dev->kvm, |
| 78 | assigned_dev->irq_source_id, |
| 79 | guest_entries[i].vector, 1); |
| 80 | } |
| 81 | } else |
| 82 | kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id, |
| 83 | assigned_dev->guest_irq, 1); |
| 84 | |
| 85 | spin_unlock_irq(&assigned_dev->assigned_dev_lock); |
| 86 | } |
| 87 | |
| 88 | static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id) |
| 89 | { |
| 90 | unsigned long flags; |
| 91 | struct kvm_assigned_dev_kernel *assigned_dev = |
| 92 | (struct kvm_assigned_dev_kernel *) dev_id; |
| 93 | |
| 94 | spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags); |
| 95 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { |
| 96 | int index = find_index_from_host_irq(assigned_dev, irq); |
| 97 | if (index < 0) |
| 98 | goto out; |
| 99 | assigned_dev->guest_msix_entries[index].flags |= |
| 100 | KVM_ASSIGNED_MSIX_PENDING; |
| 101 | } |
| 102 | |
| 103 | schedule_work(&assigned_dev->interrupt_work); |
| 104 | |
| 105 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) { |
| 106 | disable_irq_nosync(irq); |
| 107 | assigned_dev->host_irq_disabled = true; |
| 108 | } |
| 109 | |
| 110 | out: |
| 111 | spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags); |
| 112 | return IRQ_HANDLED; |
| 113 | } |
| 114 | |
| 115 | /* Ack the irq line for an assigned device */ |
| 116 | static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian) |
| 117 | { |
| 118 | struct kvm_assigned_dev_kernel *dev; |
| 119 | unsigned long flags; |
| 120 | |
| 121 | if (kian->gsi == -1) |
| 122 | return; |
| 123 | |
| 124 | dev = container_of(kian, struct kvm_assigned_dev_kernel, |
| 125 | ack_notifier); |
| 126 | |
| 127 | kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0); |
| 128 | |
| 129 | /* The guest irq may be shared so this ack may be |
| 130 | * from another device. |
| 131 | */ |
| 132 | spin_lock_irqsave(&dev->assigned_dev_lock, flags); |
| 133 | if (dev->host_irq_disabled) { |
| 134 | enable_irq(dev->host_irq); |
| 135 | dev->host_irq_disabled = false; |
| 136 | } |
| 137 | spin_unlock_irqrestore(&dev->assigned_dev_lock, flags); |
| 138 | } |
| 139 | |
| 140 | static void deassign_guest_irq(struct kvm *kvm, |
| 141 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 142 | { |
| 143 | kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier); |
| 144 | assigned_dev->ack_notifier.gsi = -1; |
| 145 | |
| 146 | if (assigned_dev->irq_source_id != -1) |
| 147 | kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id); |
| 148 | assigned_dev->irq_source_id = -1; |
| 149 | assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK); |
| 150 | } |
| 151 | |
| 152 | /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */ |
| 153 | static void deassign_host_irq(struct kvm *kvm, |
| 154 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 155 | { |
| 156 | /* |
| 157 | * In kvm_free_device_irq, cancel_work_sync return true if: |
| 158 | * 1. work is scheduled, and then cancelled. |
| 159 | * 2. work callback is executed. |
| 160 | * |
| 161 | * The first one ensured that the irq is disabled and no more events |
| 162 | * would happen. But for the second one, the irq may be enabled (e.g. |
| 163 | * for MSI). So we disable irq here to prevent further events. |
| 164 | * |
| 165 | * Notice this maybe result in nested disable if the interrupt type is |
| 166 | * INTx, but it's OK for we are going to free it. |
| 167 | * |
| 168 | * If this function is a part of VM destroy, please ensure that till |
| 169 | * now, the kvm state is still legal for probably we also have to wait |
| 170 | * interrupt_work done. |
| 171 | */ |
| 172 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { |
| 173 | int i; |
| 174 | for (i = 0; i < assigned_dev->entries_nr; i++) |
| 175 | disable_irq_nosync(assigned_dev-> |
| 176 | host_msix_entries[i].vector); |
| 177 | |
| 178 | cancel_work_sync(&assigned_dev->interrupt_work); |
| 179 | |
| 180 | for (i = 0; i < assigned_dev->entries_nr; i++) |
| 181 | free_irq(assigned_dev->host_msix_entries[i].vector, |
| 182 | (void *)assigned_dev); |
| 183 | |
| 184 | assigned_dev->entries_nr = 0; |
| 185 | kfree(assigned_dev->host_msix_entries); |
| 186 | kfree(assigned_dev->guest_msix_entries); |
| 187 | pci_disable_msix(assigned_dev->dev); |
| 188 | } else { |
| 189 | /* Deal with MSI and INTx */ |
| 190 | disable_irq_nosync(assigned_dev->host_irq); |
| 191 | cancel_work_sync(&assigned_dev->interrupt_work); |
| 192 | |
| 193 | free_irq(assigned_dev->host_irq, (void *)assigned_dev); |
| 194 | |
| 195 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI) |
| 196 | pci_disable_msi(assigned_dev->dev); |
| 197 | } |
| 198 | |
| 199 | assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK); |
| 200 | } |
| 201 | |
| 202 | static int kvm_deassign_irq(struct kvm *kvm, |
| 203 | struct kvm_assigned_dev_kernel *assigned_dev, |
| 204 | unsigned long irq_requested_type) |
| 205 | { |
| 206 | unsigned long guest_irq_type, host_irq_type; |
| 207 | |
| 208 | if (!irqchip_in_kernel(kvm)) |
| 209 | return -EINVAL; |
| 210 | /* no irq assignment to deassign */ |
| 211 | if (!assigned_dev->irq_requested_type) |
| 212 | return -ENXIO; |
| 213 | |
| 214 | host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK; |
| 215 | guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK; |
| 216 | |
| 217 | if (host_irq_type) |
| 218 | deassign_host_irq(kvm, assigned_dev); |
| 219 | if (guest_irq_type) |
| 220 | deassign_guest_irq(kvm, assigned_dev); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static void kvm_free_assigned_irq(struct kvm *kvm, |
| 226 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 227 | { |
| 228 | kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type); |
| 229 | } |
| 230 | |
| 231 | static void kvm_free_assigned_device(struct kvm *kvm, |
| 232 | struct kvm_assigned_dev_kernel |
| 233 | *assigned_dev) |
| 234 | { |
| 235 | kvm_free_assigned_irq(kvm, assigned_dev); |
| 236 | |
| 237 | pci_reset_function(assigned_dev->dev); |
| 238 | |
| 239 | pci_release_regions(assigned_dev->dev); |
| 240 | pci_disable_device(assigned_dev->dev); |
| 241 | pci_dev_put(assigned_dev->dev); |
| 242 | |
| 243 | list_del(&assigned_dev->list); |
| 244 | kfree(assigned_dev); |
| 245 | } |
| 246 | |
| 247 | void kvm_free_all_assigned_devices(struct kvm *kvm) |
| 248 | { |
| 249 | struct list_head *ptr, *ptr2; |
| 250 | struct kvm_assigned_dev_kernel *assigned_dev; |
| 251 | |
| 252 | list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) { |
| 253 | assigned_dev = list_entry(ptr, |
| 254 | struct kvm_assigned_dev_kernel, |
| 255 | list); |
| 256 | |
| 257 | kvm_free_assigned_device(kvm, assigned_dev); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static int assigned_device_enable_host_intx(struct kvm *kvm, |
| 262 | struct kvm_assigned_dev_kernel *dev) |
| 263 | { |
| 264 | dev->host_irq = dev->dev->irq; |
| 265 | /* Even though this is PCI, we don't want to use shared |
| 266 | * interrupts. Sharing host devices with guest-assigned devices |
| 267 | * on the same interrupt line is not a happy situation: there |
| 268 | * are going to be long delays in accepting, acking, etc. |
| 269 | */ |
| 270 | if (request_irq(dev->host_irq, kvm_assigned_dev_intr, |
| 271 | 0, "kvm_assigned_intx_device", (void *)dev)) |
| 272 | return -EIO; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | #ifdef __KVM_HAVE_MSI |
| 277 | static int assigned_device_enable_host_msi(struct kvm *kvm, |
| 278 | struct kvm_assigned_dev_kernel *dev) |
| 279 | { |
| 280 | int r; |
| 281 | |
| 282 | if (!dev->dev->msi_enabled) { |
| 283 | r = pci_enable_msi(dev->dev); |
| 284 | if (r) |
| 285 | return r; |
| 286 | } |
| 287 | |
| 288 | dev->host_irq = dev->dev->irq; |
| 289 | if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0, |
| 290 | "kvm_assigned_msi_device", (void *)dev)) { |
| 291 | pci_disable_msi(dev->dev); |
| 292 | return -EIO; |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | #endif |
| 298 | |
| 299 | #ifdef __KVM_HAVE_MSIX |
| 300 | static int assigned_device_enable_host_msix(struct kvm *kvm, |
| 301 | struct kvm_assigned_dev_kernel *dev) |
| 302 | { |
| 303 | int i, r = -EINVAL; |
| 304 | |
| 305 | /* host_msix_entries and guest_msix_entries should have been |
| 306 | * initialized */ |
| 307 | if (dev->entries_nr == 0) |
| 308 | return r; |
| 309 | |
| 310 | r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr); |
| 311 | if (r) |
| 312 | return r; |
| 313 | |
| 314 | for (i = 0; i < dev->entries_nr; i++) { |
| 315 | r = request_irq(dev->host_msix_entries[i].vector, |
| 316 | kvm_assigned_dev_intr, 0, |
| 317 | "kvm_assigned_msix_device", |
| 318 | (void *)dev); |
| 319 | /* FIXME: free requested_irq's on failure */ |
| 320 | if (r) |
| 321 | return r; |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | #endif |
| 328 | |
| 329 | static int assigned_device_enable_guest_intx(struct kvm *kvm, |
| 330 | struct kvm_assigned_dev_kernel *dev, |
| 331 | struct kvm_assigned_irq *irq) |
| 332 | { |
| 333 | dev->guest_irq = irq->guest_irq; |
| 334 | dev->ack_notifier.gsi = irq->guest_irq; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | #ifdef __KVM_HAVE_MSI |
| 339 | static int assigned_device_enable_guest_msi(struct kvm *kvm, |
| 340 | struct kvm_assigned_dev_kernel *dev, |
| 341 | struct kvm_assigned_irq *irq) |
| 342 | { |
| 343 | dev->guest_irq = irq->guest_irq; |
| 344 | dev->ack_notifier.gsi = -1; |
| 345 | dev->host_irq_disabled = false; |
| 346 | return 0; |
| 347 | } |
| 348 | #endif |
| 349 | |
| 350 | #ifdef __KVM_HAVE_MSIX |
| 351 | static int assigned_device_enable_guest_msix(struct kvm *kvm, |
| 352 | struct kvm_assigned_dev_kernel *dev, |
| 353 | struct kvm_assigned_irq *irq) |
| 354 | { |
| 355 | dev->guest_irq = irq->guest_irq; |
| 356 | dev->ack_notifier.gsi = -1; |
| 357 | dev->host_irq_disabled = false; |
| 358 | return 0; |
| 359 | } |
| 360 | #endif |
| 361 | |
| 362 | static int assign_host_irq(struct kvm *kvm, |
| 363 | struct kvm_assigned_dev_kernel *dev, |
| 364 | __u32 host_irq_type) |
| 365 | { |
| 366 | int r = -EEXIST; |
| 367 | |
| 368 | if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK) |
| 369 | return r; |
| 370 | |
| 371 | switch (host_irq_type) { |
| 372 | case KVM_DEV_IRQ_HOST_INTX: |
| 373 | r = assigned_device_enable_host_intx(kvm, dev); |
| 374 | break; |
| 375 | #ifdef __KVM_HAVE_MSI |
| 376 | case KVM_DEV_IRQ_HOST_MSI: |
| 377 | r = assigned_device_enable_host_msi(kvm, dev); |
| 378 | break; |
| 379 | #endif |
| 380 | #ifdef __KVM_HAVE_MSIX |
| 381 | case KVM_DEV_IRQ_HOST_MSIX: |
| 382 | r = assigned_device_enable_host_msix(kvm, dev); |
| 383 | break; |
| 384 | #endif |
| 385 | default: |
| 386 | r = -EINVAL; |
| 387 | } |
| 388 | |
| 389 | if (!r) |
| 390 | dev->irq_requested_type |= host_irq_type; |
| 391 | |
| 392 | return r; |
| 393 | } |
| 394 | |
| 395 | static int assign_guest_irq(struct kvm *kvm, |
| 396 | struct kvm_assigned_dev_kernel *dev, |
| 397 | struct kvm_assigned_irq *irq, |
| 398 | unsigned long guest_irq_type) |
| 399 | { |
| 400 | int id; |
| 401 | int r = -EEXIST; |
| 402 | |
| 403 | if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK) |
| 404 | return r; |
| 405 | |
| 406 | id = kvm_request_irq_source_id(kvm); |
| 407 | if (id < 0) |
| 408 | return id; |
| 409 | |
| 410 | dev->irq_source_id = id; |
| 411 | |
| 412 | switch (guest_irq_type) { |
| 413 | case KVM_DEV_IRQ_GUEST_INTX: |
| 414 | r = assigned_device_enable_guest_intx(kvm, dev, irq); |
| 415 | break; |
| 416 | #ifdef __KVM_HAVE_MSI |
| 417 | case KVM_DEV_IRQ_GUEST_MSI: |
| 418 | r = assigned_device_enable_guest_msi(kvm, dev, irq); |
| 419 | break; |
| 420 | #endif |
| 421 | #ifdef __KVM_HAVE_MSIX |
| 422 | case KVM_DEV_IRQ_GUEST_MSIX: |
| 423 | r = assigned_device_enable_guest_msix(kvm, dev, irq); |
| 424 | break; |
| 425 | #endif |
| 426 | default: |
| 427 | r = -EINVAL; |
| 428 | } |
| 429 | |
| 430 | if (!r) { |
| 431 | dev->irq_requested_type |= guest_irq_type; |
| 432 | kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier); |
| 433 | } else |
| 434 | kvm_free_irq_source_id(kvm, dev->irq_source_id); |
| 435 | |
| 436 | return r; |
| 437 | } |
| 438 | |
| 439 | /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */ |
| 440 | static int kvm_vm_ioctl_assign_irq(struct kvm *kvm, |
| 441 | struct kvm_assigned_irq *assigned_irq) |
| 442 | { |
| 443 | int r = -EINVAL; |
| 444 | struct kvm_assigned_dev_kernel *match; |
| 445 | unsigned long host_irq_type, guest_irq_type; |
| 446 | |
| 447 | if (!capable(CAP_SYS_RAWIO)) |
| 448 | return -EPERM; |
| 449 | |
| 450 | if (!irqchip_in_kernel(kvm)) |
| 451 | return r; |
| 452 | |
| 453 | mutex_lock(&kvm->lock); |
| 454 | r = -ENODEV; |
| 455 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 456 | assigned_irq->assigned_dev_id); |
| 457 | if (!match) |
| 458 | goto out; |
| 459 | |
| 460 | host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK); |
| 461 | guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK); |
| 462 | |
| 463 | r = -EINVAL; |
| 464 | /* can only assign one type at a time */ |
| 465 | if (hweight_long(host_irq_type) > 1) |
| 466 | goto out; |
| 467 | if (hweight_long(guest_irq_type) > 1) |
| 468 | goto out; |
| 469 | if (host_irq_type == 0 && guest_irq_type == 0) |
| 470 | goto out; |
| 471 | |
| 472 | r = 0; |
| 473 | if (host_irq_type) |
| 474 | r = assign_host_irq(kvm, match, host_irq_type); |
| 475 | if (r) |
| 476 | goto out; |
| 477 | |
| 478 | if (guest_irq_type) |
| 479 | r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type); |
| 480 | out: |
| 481 | mutex_unlock(&kvm->lock); |
| 482 | return r; |
| 483 | } |
| 484 | |
| 485 | static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm, |
| 486 | struct kvm_assigned_irq |
| 487 | *assigned_irq) |
| 488 | { |
| 489 | int r = -ENODEV; |
| 490 | struct kvm_assigned_dev_kernel *match; |
| 491 | |
| 492 | mutex_lock(&kvm->lock); |
| 493 | |
| 494 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 495 | assigned_irq->assigned_dev_id); |
| 496 | if (!match) |
| 497 | goto out; |
| 498 | |
| 499 | r = kvm_deassign_irq(kvm, match, assigned_irq->flags); |
| 500 | out: |
| 501 | mutex_unlock(&kvm->lock); |
| 502 | return r; |
| 503 | } |
| 504 | |
| 505 | static int kvm_vm_ioctl_assign_device(struct kvm *kvm, |
| 506 | struct kvm_assigned_pci_dev *assigned_dev) |
| 507 | { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 508 | int r = 0, idx; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 509 | struct kvm_assigned_dev_kernel *match; |
| 510 | struct pci_dev *dev; |
| 511 | |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 512 | mutex_lock(&kvm->lock); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 513 | idx = srcu_read_lock(&kvm->srcu); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 514 | |
| 515 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 516 | assigned_dev->assigned_dev_id); |
| 517 | if (match) { |
| 518 | /* device already assigned */ |
| 519 | r = -EEXIST; |
| 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL); |
| 524 | if (match == NULL) { |
| 525 | printk(KERN_INFO "%s: Couldn't allocate memory\n", |
| 526 | __func__); |
| 527 | r = -ENOMEM; |
| 528 | goto out; |
| 529 | } |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 530 | dev = pci_get_domain_bus_and_slot(assigned_dev->segnr, |
| 531 | assigned_dev->busnr, |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 532 | assigned_dev->devfn); |
| 533 | if (!dev) { |
| 534 | printk(KERN_INFO "%s: host device not found\n", __func__); |
| 535 | r = -EINVAL; |
| 536 | goto out_free; |
| 537 | } |
| 538 | if (pci_enable_device(dev)) { |
| 539 | printk(KERN_INFO "%s: Could not enable PCI device\n", __func__); |
| 540 | r = -EBUSY; |
| 541 | goto out_put; |
| 542 | } |
| 543 | r = pci_request_regions(dev, "kvm_assigned_device"); |
| 544 | if (r) { |
| 545 | printk(KERN_INFO "%s: Could not get access to device regions\n", |
| 546 | __func__); |
| 547 | goto out_disable; |
| 548 | } |
| 549 | |
| 550 | pci_reset_function(dev); |
| 551 | |
| 552 | match->assigned_dev_id = assigned_dev->assigned_dev_id; |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 553 | match->host_segnr = assigned_dev->segnr; |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 554 | match->host_busnr = assigned_dev->busnr; |
| 555 | match->host_devfn = assigned_dev->devfn; |
| 556 | match->flags = assigned_dev->flags; |
| 557 | match->dev = dev; |
| 558 | spin_lock_init(&match->assigned_dev_lock); |
| 559 | match->irq_source_id = -1; |
| 560 | match->kvm = kvm; |
| 561 | match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq; |
| 562 | INIT_WORK(&match->interrupt_work, |
| 563 | kvm_assigned_dev_interrupt_work_handler); |
| 564 | |
| 565 | list_add(&match->list, &kvm->arch.assigned_dev_head); |
| 566 | |
| 567 | if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) { |
| 568 | if (!kvm->arch.iommu_domain) { |
| 569 | r = kvm_iommu_map_guest(kvm); |
| 570 | if (r) |
| 571 | goto out_list_del; |
| 572 | } |
| 573 | r = kvm_assign_device(kvm, match); |
| 574 | if (r) |
| 575 | goto out_list_del; |
| 576 | } |
| 577 | |
| 578 | out: |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 579 | srcu_read_unlock(&kvm->srcu, idx); |
Sheng Yang | fae3a35 | 2009-12-15 10:28:07 +0800 | [diff] [blame] | 580 | mutex_unlock(&kvm->lock); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 581 | return r; |
| 582 | out_list_del: |
| 583 | list_del(&match->list); |
| 584 | pci_release_regions(dev); |
| 585 | out_disable: |
| 586 | pci_disable_device(dev); |
| 587 | out_put: |
| 588 | pci_dev_put(dev); |
| 589 | out_free: |
| 590 | kfree(match); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 591 | srcu_read_unlock(&kvm->srcu, idx); |
Sheng Yang | fae3a35 | 2009-12-15 10:28:07 +0800 | [diff] [blame] | 592 | mutex_unlock(&kvm->lock); |
Avi Kivity | bfd99ff | 2009-08-26 14:57:50 +0300 | [diff] [blame] | 593 | return r; |
| 594 | } |
| 595 | |
| 596 | static int kvm_vm_ioctl_deassign_device(struct kvm *kvm, |
| 597 | struct kvm_assigned_pci_dev *assigned_dev) |
| 598 | { |
| 599 | int r = 0; |
| 600 | struct kvm_assigned_dev_kernel *match; |
| 601 | |
| 602 | mutex_lock(&kvm->lock); |
| 603 | |
| 604 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 605 | assigned_dev->assigned_dev_id); |
| 606 | if (!match) { |
| 607 | printk(KERN_INFO "%s: device hasn't been assigned before, " |
| 608 | "so cannot be deassigned\n", __func__); |
| 609 | r = -EINVAL; |
| 610 | goto out; |
| 611 | } |
| 612 | |
| 613 | if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) |
| 614 | kvm_deassign_device(kvm, match); |
| 615 | |
| 616 | kvm_free_assigned_device(kvm, match); |
| 617 | |
| 618 | out: |
| 619 | mutex_unlock(&kvm->lock); |
| 620 | return r; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | #ifdef __KVM_HAVE_MSIX |
| 625 | static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm, |
| 626 | struct kvm_assigned_msix_nr *entry_nr) |
| 627 | { |
| 628 | int r = 0; |
| 629 | struct kvm_assigned_dev_kernel *adev; |
| 630 | |
| 631 | mutex_lock(&kvm->lock); |
| 632 | |
| 633 | adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 634 | entry_nr->assigned_dev_id); |
| 635 | if (!adev) { |
| 636 | r = -EINVAL; |
| 637 | goto msix_nr_out; |
| 638 | } |
| 639 | |
| 640 | if (adev->entries_nr == 0) { |
| 641 | adev->entries_nr = entry_nr->entry_nr; |
| 642 | if (adev->entries_nr == 0 || |
| 643 | adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) { |
| 644 | r = -EINVAL; |
| 645 | goto msix_nr_out; |
| 646 | } |
| 647 | |
| 648 | adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) * |
| 649 | entry_nr->entry_nr, |
| 650 | GFP_KERNEL); |
| 651 | if (!adev->host_msix_entries) { |
| 652 | r = -ENOMEM; |
| 653 | goto msix_nr_out; |
| 654 | } |
| 655 | adev->guest_msix_entries = kzalloc( |
| 656 | sizeof(struct kvm_guest_msix_entry) * |
| 657 | entry_nr->entry_nr, GFP_KERNEL); |
| 658 | if (!adev->guest_msix_entries) { |
| 659 | kfree(adev->host_msix_entries); |
| 660 | r = -ENOMEM; |
| 661 | goto msix_nr_out; |
| 662 | } |
| 663 | } else /* Not allowed set MSI-X number twice */ |
| 664 | r = -EINVAL; |
| 665 | msix_nr_out: |
| 666 | mutex_unlock(&kvm->lock); |
| 667 | return r; |
| 668 | } |
| 669 | |
| 670 | static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm, |
| 671 | struct kvm_assigned_msix_entry *entry) |
| 672 | { |
| 673 | int r = 0, i; |
| 674 | struct kvm_assigned_dev_kernel *adev; |
| 675 | |
| 676 | mutex_lock(&kvm->lock); |
| 677 | |
| 678 | adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, |
| 679 | entry->assigned_dev_id); |
| 680 | |
| 681 | if (!adev) { |
| 682 | r = -EINVAL; |
| 683 | goto msix_entry_out; |
| 684 | } |
| 685 | |
| 686 | for (i = 0; i < adev->entries_nr; i++) |
| 687 | if (adev->guest_msix_entries[i].vector == 0 || |
| 688 | adev->guest_msix_entries[i].entry == entry->entry) { |
| 689 | adev->guest_msix_entries[i].entry = entry->entry; |
| 690 | adev->guest_msix_entries[i].vector = entry->gsi; |
| 691 | adev->host_msix_entries[i].entry = entry->entry; |
| 692 | break; |
| 693 | } |
| 694 | if (i == adev->entries_nr) { |
| 695 | r = -ENOSPC; |
| 696 | goto msix_entry_out; |
| 697 | } |
| 698 | |
| 699 | msix_entry_out: |
| 700 | mutex_unlock(&kvm->lock); |
| 701 | |
| 702 | return r; |
| 703 | } |
| 704 | #endif |
| 705 | |
| 706 | long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl, |
| 707 | unsigned long arg) |
| 708 | { |
| 709 | void __user *argp = (void __user *)arg; |
| 710 | int r = -ENOTTY; |
| 711 | |
| 712 | switch (ioctl) { |
| 713 | case KVM_ASSIGN_PCI_DEVICE: { |
| 714 | struct kvm_assigned_pci_dev assigned_dev; |
| 715 | |
| 716 | r = -EFAULT; |
| 717 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) |
| 718 | goto out; |
| 719 | r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev); |
| 720 | if (r) |
| 721 | goto out; |
| 722 | break; |
| 723 | } |
| 724 | case KVM_ASSIGN_IRQ: { |
| 725 | r = -EOPNOTSUPP; |
| 726 | break; |
| 727 | } |
| 728 | #ifdef KVM_CAP_ASSIGN_DEV_IRQ |
| 729 | case KVM_ASSIGN_DEV_IRQ: { |
| 730 | struct kvm_assigned_irq assigned_irq; |
| 731 | |
| 732 | r = -EFAULT; |
| 733 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) |
| 734 | goto out; |
| 735 | r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq); |
| 736 | if (r) |
| 737 | goto out; |
| 738 | break; |
| 739 | } |
| 740 | case KVM_DEASSIGN_DEV_IRQ: { |
| 741 | struct kvm_assigned_irq assigned_irq; |
| 742 | |
| 743 | r = -EFAULT; |
| 744 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) |
| 745 | goto out; |
| 746 | r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq); |
| 747 | if (r) |
| 748 | goto out; |
| 749 | break; |
| 750 | } |
| 751 | #endif |
| 752 | #ifdef KVM_CAP_DEVICE_DEASSIGNMENT |
| 753 | case KVM_DEASSIGN_PCI_DEVICE: { |
| 754 | struct kvm_assigned_pci_dev assigned_dev; |
| 755 | |
| 756 | r = -EFAULT; |
| 757 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) |
| 758 | goto out; |
| 759 | r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev); |
| 760 | if (r) |
| 761 | goto out; |
| 762 | break; |
| 763 | } |
| 764 | #endif |
| 765 | #ifdef KVM_CAP_IRQ_ROUTING |
| 766 | case KVM_SET_GSI_ROUTING: { |
| 767 | struct kvm_irq_routing routing; |
| 768 | struct kvm_irq_routing __user *urouting; |
| 769 | struct kvm_irq_routing_entry *entries; |
| 770 | |
| 771 | r = -EFAULT; |
| 772 | if (copy_from_user(&routing, argp, sizeof(routing))) |
| 773 | goto out; |
| 774 | r = -EINVAL; |
| 775 | if (routing.nr >= KVM_MAX_IRQ_ROUTES) |
| 776 | goto out; |
| 777 | if (routing.flags) |
| 778 | goto out; |
| 779 | r = -ENOMEM; |
| 780 | entries = vmalloc(routing.nr * sizeof(*entries)); |
| 781 | if (!entries) |
| 782 | goto out; |
| 783 | r = -EFAULT; |
| 784 | urouting = argp; |
| 785 | if (copy_from_user(entries, urouting->entries, |
| 786 | routing.nr * sizeof(*entries))) |
| 787 | goto out_free_irq_routing; |
| 788 | r = kvm_set_irq_routing(kvm, entries, routing.nr, |
| 789 | routing.flags); |
| 790 | out_free_irq_routing: |
| 791 | vfree(entries); |
| 792 | break; |
| 793 | } |
| 794 | #endif /* KVM_CAP_IRQ_ROUTING */ |
| 795 | #ifdef __KVM_HAVE_MSIX |
| 796 | case KVM_ASSIGN_SET_MSIX_NR: { |
| 797 | struct kvm_assigned_msix_nr entry_nr; |
| 798 | r = -EFAULT; |
| 799 | if (copy_from_user(&entry_nr, argp, sizeof entry_nr)) |
| 800 | goto out; |
| 801 | r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr); |
| 802 | if (r) |
| 803 | goto out; |
| 804 | break; |
| 805 | } |
| 806 | case KVM_ASSIGN_SET_MSIX_ENTRY: { |
| 807 | struct kvm_assigned_msix_entry entry; |
| 808 | r = -EFAULT; |
| 809 | if (copy_from_user(&entry, argp, sizeof entry)) |
| 810 | goto out; |
| 811 | r = kvm_vm_ioctl_set_msix_entry(kvm, &entry); |
| 812 | if (r) |
| 813 | goto out; |
| 814 | break; |
| 815 | } |
| 816 | #endif |
| 817 | } |
| 818 | out: |
| 819 | return r; |
| 820 | } |
| 821 | |