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