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