blob: eaf3a50f976947560ee9a9e4827cbe71d2879d11 [file] [log] [blame]
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001/*
2 * Kernel-based Virtual Machine - device assignment support
3 *
Avi Kivity221d0592010-05-23 18:37:00 +03004 * Copyright (C) 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivitybfd99ff2009-08-26 14:57:50 +03005 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Avi Kivitybfd99ff2009-08-26 14:57:50 +030020#include "irq.h"
21
22static 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
36static 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
Jan Kiszka06452112010-11-16 22:30:03 +010058static irqreturn_t kvm_assigned_dev_thread(int irq, void *dev_id)
Avi Kivitybfd99ff2009-08-26 14:57:50 +030059{
Jan Kiszka06452112010-11-16 22:30:03 +010060 struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
61 u32 vector;
62 int index;
Avi Kivitybfd99ff2009-08-26 14:57:50 +030063
Jan Kiszka06452112010-11-16 22:30:03 +010064 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 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +030070
Avi Kivitybfd99ff2009-08-26 14:57:50 +030071 if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) {
Jan Kiszka06452112010-11-16 22:30:03 +010072 index = find_index_from_host_irq(assigned_dev, irq);
73 if (index >= 0) {
74 vector = assigned_dev->
75 guest_msix_entries[index].vector;
Avi Kivitybfd99ff2009-08-26 14:57:50 +030076 kvm_set_irq(assigned_dev->kvm,
Jan Kiszka06452112010-11-16 22:30:03 +010077 assigned_dev->irq_source_id, vector, 1);
Avi Kivitybfd99ff2009-08-26 14:57:50 +030078 }
79 } else
80 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
81 assigned_dev->guest_irq, 1);
82
Avi Kivitybfd99ff2009-08-26 14:57:50 +030083 return IRQ_HANDLED;
84}
85
86/* Ack the irq line for an assigned device */
87static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
88{
89 struct kvm_assigned_dev_kernel *dev;
Avi Kivitybfd99ff2009-08-26 14:57:50 +030090
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 */
Jan Kiszka06452112010-11-16 22:30:03 +0100102 spin_lock(&dev->intx_lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300103 if (dev->host_irq_disabled) {
104 enable_irq(dev->host_irq);
105 dev->host_irq_disabled = false;
106 }
Jan Kiszka06452112010-11-16 22:30:03 +0100107 spin_unlock(&dev->intx_lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300108}
109
110static 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
Jan Kiszka0c106b52010-11-16 22:30:02 +0100116 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
117 assigned_dev->guest_irq, 0);
118
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300119 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() */
126static void deassign_host_irq(struct kvm *kvm,
127 struct kvm_assigned_dev_kernel *assigned_dev)
128{
129 /*
Jan Kiszka06452112010-11-16 22:30:03 +0100130 * We disable irq here to prevent further events.
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300131 *
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
Jan Kiszka06452112010-11-16 22:30:03 +0100137 * on a currently running IRQ handler.
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300138 */
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++)
Jan Kiszka06452112010-11-16 22:30:03 +0100142 disable_irq(assigned_dev->host_msix_entries[i].vector);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300143
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 */
Jan Kiszka06452112010-11-16 22:30:03 +0100154 disable_irq(assigned_dev->host_irq);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300155
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
165static 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
188static 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
194static 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
Alex Williamsonf8fcfd72011-05-10 10:02:39 -0600200 pci_reset_function(assigned_dev->dev);
201 if (pci_load_and_free_saved_state(assigned_dev->dev,
202 &assigned_dev->pci_saved_state))
203 printk(KERN_INFO "%s: Couldn't reload %s saved state\n",
204 __func__, dev_name(&assigned_dev->dev->dev));
205 else
206 pci_restore_state(assigned_dev->dev);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300207
Greg Rose67778292011-07-22 05:46:07 +0000208 assigned_dev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
209
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300210 pci_release_regions(assigned_dev->dev);
211 pci_disable_device(assigned_dev->dev);
212 pci_dev_put(assigned_dev->dev);
213
214 list_del(&assigned_dev->list);
215 kfree(assigned_dev);
216}
217
218void kvm_free_all_assigned_devices(struct kvm *kvm)
219{
220 struct list_head *ptr, *ptr2;
221 struct kvm_assigned_dev_kernel *assigned_dev;
222
223 list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
224 assigned_dev = list_entry(ptr,
225 struct kvm_assigned_dev_kernel,
226 list);
227
228 kvm_free_assigned_device(kvm, assigned_dev);
229 }
230}
231
232static int assigned_device_enable_host_intx(struct kvm *kvm,
233 struct kvm_assigned_dev_kernel *dev)
234{
235 dev->host_irq = dev->dev->irq;
236 /* Even though this is PCI, we don't want to use shared
237 * interrupts. Sharing host devices with guest-assigned devices
238 * on the same interrupt line is not a happy situation: there
239 * are going to be long delays in accepting, acking, etc.
240 */
Jan Kiszka06452112010-11-16 22:30:03 +0100241 if (request_threaded_irq(dev->host_irq, NULL, kvm_assigned_dev_thread,
Jan Kiszka1e001d42010-11-16 22:30:04 +0100242 IRQF_ONESHOT, dev->irq_name, (void *)dev))
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300243 return -EIO;
244 return 0;
245}
246
247#ifdef __KVM_HAVE_MSI
248static int assigned_device_enable_host_msi(struct kvm *kvm,
249 struct kvm_assigned_dev_kernel *dev)
250{
251 int r;
252
253 if (!dev->dev->msi_enabled) {
254 r = pci_enable_msi(dev->dev);
255 if (r)
256 return r;
257 }
258
259 dev->host_irq = dev->dev->irq;
Jan Kiszka06452112010-11-16 22:30:03 +0100260 if (request_threaded_irq(dev->host_irq, NULL, kvm_assigned_dev_thread,
Jan Kiszka1e001d42010-11-16 22:30:04 +0100261 0, dev->irq_name, (void *)dev)) {
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300262 pci_disable_msi(dev->dev);
263 return -EIO;
264 }
265
266 return 0;
267}
268#endif
269
270#ifdef __KVM_HAVE_MSIX
271static int assigned_device_enable_host_msix(struct kvm *kvm,
272 struct kvm_assigned_dev_kernel *dev)
273{
274 int i, r = -EINVAL;
275
276 /* host_msix_entries and guest_msix_entries should have been
277 * initialized */
278 if (dev->entries_nr == 0)
279 return r;
280
281 r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr);
282 if (r)
283 return r;
284
285 for (i = 0; i < dev->entries_nr; i++) {
Jan Kiszka06452112010-11-16 22:30:03 +0100286 r = request_threaded_irq(dev->host_msix_entries[i].vector,
287 NULL, kvm_assigned_dev_thread,
Jan Kiszka1e001d42010-11-16 22:30:04 +0100288 0, dev->irq_name, (void *)dev);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300289 if (r)
jing zhangd57e2c02010-03-13 15:00:45 +0800290 goto err;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300291 }
292
293 return 0;
jing zhangd57e2c02010-03-13 15:00:45 +0800294err:
295 for (i -= 1; i >= 0; i--)
296 free_irq(dev->host_msix_entries[i].vector, (void *)dev);
297 pci_disable_msix(dev->dev);
298 return r;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300299}
300
301#endif
302
303static int assigned_device_enable_guest_intx(struct kvm *kvm,
304 struct kvm_assigned_dev_kernel *dev,
305 struct kvm_assigned_irq *irq)
306{
307 dev->guest_irq = irq->guest_irq;
308 dev->ack_notifier.gsi = irq->guest_irq;
309 return 0;
310}
311
312#ifdef __KVM_HAVE_MSI
313static int assigned_device_enable_guest_msi(struct kvm *kvm,
314 struct kvm_assigned_dev_kernel *dev,
315 struct kvm_assigned_irq *irq)
316{
317 dev->guest_irq = irq->guest_irq;
318 dev->ack_notifier.gsi = -1;
319 dev->host_irq_disabled = false;
320 return 0;
321}
322#endif
323
324#ifdef __KVM_HAVE_MSIX
325static int assigned_device_enable_guest_msix(struct kvm *kvm,
326 struct kvm_assigned_dev_kernel *dev,
327 struct kvm_assigned_irq *irq)
328{
329 dev->guest_irq = irq->guest_irq;
330 dev->ack_notifier.gsi = -1;
331 dev->host_irq_disabled = false;
332 return 0;
333}
334#endif
335
336static int assign_host_irq(struct kvm *kvm,
337 struct kvm_assigned_dev_kernel *dev,
338 __u32 host_irq_type)
339{
340 int r = -EEXIST;
341
342 if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK)
343 return r;
344
Jan Kiszka1e001d42010-11-16 22:30:04 +0100345 snprintf(dev->irq_name, sizeof(dev->irq_name), "kvm:%s",
346 pci_name(dev->dev));
347
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300348 switch (host_irq_type) {
349 case KVM_DEV_IRQ_HOST_INTX:
350 r = assigned_device_enable_host_intx(kvm, dev);
351 break;
352#ifdef __KVM_HAVE_MSI
353 case KVM_DEV_IRQ_HOST_MSI:
354 r = assigned_device_enable_host_msi(kvm, dev);
355 break;
356#endif
357#ifdef __KVM_HAVE_MSIX
358 case KVM_DEV_IRQ_HOST_MSIX:
359 r = assigned_device_enable_host_msix(kvm, dev);
360 break;
361#endif
362 default:
363 r = -EINVAL;
364 }
365
366 if (!r)
367 dev->irq_requested_type |= host_irq_type;
368
369 return r;
370}
371
372static int assign_guest_irq(struct kvm *kvm,
373 struct kvm_assigned_dev_kernel *dev,
374 struct kvm_assigned_irq *irq,
375 unsigned long guest_irq_type)
376{
377 int id;
378 int r = -EEXIST;
379
380 if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK)
381 return r;
382
383 id = kvm_request_irq_source_id(kvm);
384 if (id < 0)
385 return id;
386
387 dev->irq_source_id = id;
388
389 switch (guest_irq_type) {
390 case KVM_DEV_IRQ_GUEST_INTX:
391 r = assigned_device_enable_guest_intx(kvm, dev, irq);
392 break;
393#ifdef __KVM_HAVE_MSI
394 case KVM_DEV_IRQ_GUEST_MSI:
395 r = assigned_device_enable_guest_msi(kvm, dev, irq);
396 break;
397#endif
398#ifdef __KVM_HAVE_MSIX
399 case KVM_DEV_IRQ_GUEST_MSIX:
400 r = assigned_device_enable_guest_msix(kvm, dev, irq);
401 break;
402#endif
403 default:
404 r = -EINVAL;
405 }
406
407 if (!r) {
408 dev->irq_requested_type |= guest_irq_type;
409 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
410 } else
411 kvm_free_irq_source_id(kvm, dev->irq_source_id);
412
413 return r;
414}
415
416/* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
417static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
418 struct kvm_assigned_irq *assigned_irq)
419{
420 int r = -EINVAL;
421 struct kvm_assigned_dev_kernel *match;
422 unsigned long host_irq_type, guest_irq_type;
423
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300424 if (!irqchip_in_kernel(kvm))
425 return r;
426
427 mutex_lock(&kvm->lock);
428 r = -ENODEV;
429 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
430 assigned_irq->assigned_dev_id);
431 if (!match)
432 goto out;
433
434 host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK);
435 guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK);
436
437 r = -EINVAL;
438 /* can only assign one type at a time */
439 if (hweight_long(host_irq_type) > 1)
440 goto out;
441 if (hweight_long(guest_irq_type) > 1)
442 goto out;
443 if (host_irq_type == 0 && guest_irq_type == 0)
444 goto out;
445
446 r = 0;
447 if (host_irq_type)
448 r = assign_host_irq(kvm, match, host_irq_type);
449 if (r)
450 goto out;
451
452 if (guest_irq_type)
453 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
454out:
455 mutex_unlock(&kvm->lock);
456 return r;
457}
458
459static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
460 struct kvm_assigned_irq
461 *assigned_irq)
462{
463 int r = -ENODEV;
464 struct kvm_assigned_dev_kernel *match;
465
466 mutex_lock(&kvm->lock);
467
468 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
469 assigned_irq->assigned_dev_id);
470 if (!match)
471 goto out;
472
473 r = kvm_deassign_irq(kvm, match, assigned_irq->flags);
474out:
475 mutex_unlock(&kvm->lock);
476 return r;
477}
478
479static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
480 struct kvm_assigned_pci_dev *assigned_dev)
481{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200482 int r = 0, idx;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300483 struct kvm_assigned_dev_kernel *match;
484 struct pci_dev *dev;
485
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300486 mutex_lock(&kvm->lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200487 idx = srcu_read_lock(&kvm->srcu);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300488
489 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
490 assigned_dev->assigned_dev_id);
491 if (match) {
492 /* device already assigned */
493 r = -EEXIST;
494 goto out;
495 }
496
497 match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
498 if (match == NULL) {
499 printk(KERN_INFO "%s: Couldn't allocate memory\n",
500 __func__);
501 r = -ENOMEM;
502 goto out;
503 }
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +0800504 dev = pci_get_domain_bus_and_slot(assigned_dev->segnr,
505 assigned_dev->busnr,
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300506 assigned_dev->devfn);
507 if (!dev) {
508 printk(KERN_INFO "%s: host device not found\n", __func__);
509 r = -EINVAL;
510 goto out_free;
511 }
512 if (pci_enable_device(dev)) {
513 printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
514 r = -EBUSY;
515 goto out_put;
516 }
517 r = pci_request_regions(dev, "kvm_assigned_device");
518 if (r) {
519 printk(KERN_INFO "%s: Could not get access to device regions\n",
520 __func__);
521 goto out_disable;
522 }
523
524 pci_reset_function(dev);
Jan Kiszkaed786612010-11-16 22:30:05 +0100525 pci_save_state(dev);
Alex Williamsonf8fcfd72011-05-10 10:02:39 -0600526 match->pci_saved_state = pci_store_saved_state(dev);
527 if (!match->pci_saved_state)
528 printk(KERN_DEBUG "%s: Couldn't store %s saved state\n",
529 __func__, dev_name(&dev->dev));
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300530 match->assigned_dev_id = assigned_dev->assigned_dev_id;
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +0800531 match->host_segnr = assigned_dev->segnr;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300532 match->host_busnr = assigned_dev->busnr;
533 match->host_devfn = assigned_dev->devfn;
534 match->flags = assigned_dev->flags;
535 match->dev = dev;
Jan Kiszka06452112010-11-16 22:30:03 +0100536 spin_lock_init(&match->intx_lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300537 match->irq_source_id = -1;
538 match->kvm = kvm;
539 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300540
541 list_add(&match->list, &kvm->arch.assigned_dev_head);
542
543 if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
544 if (!kvm->arch.iommu_domain) {
545 r = kvm_iommu_map_guest(kvm);
546 if (r)
547 goto out_list_del;
548 }
549 r = kvm_assign_device(kvm, match);
550 if (r)
551 goto out_list_del;
552 }
553
554out:
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200555 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangfae3a352009-12-15 10:28:07 +0800556 mutex_unlock(&kvm->lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300557 return r;
558out_list_del:
Alex Williamsonf8fcfd72011-05-10 10:02:39 -0600559 if (pci_load_and_free_saved_state(dev, &match->pci_saved_state))
560 printk(KERN_INFO "%s: Couldn't reload %s saved state\n",
561 __func__, dev_name(&dev->dev));
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300562 list_del(&match->list);
563 pci_release_regions(dev);
564out_disable:
565 pci_disable_device(dev);
566out_put:
567 pci_dev_put(dev);
568out_free:
569 kfree(match);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200570 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangfae3a352009-12-15 10:28:07 +0800571 mutex_unlock(&kvm->lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300572 return r;
573}
574
575static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
576 struct kvm_assigned_pci_dev *assigned_dev)
577{
578 int r = 0;
579 struct kvm_assigned_dev_kernel *match;
580
581 mutex_lock(&kvm->lock);
582
583 match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
584 assigned_dev->assigned_dev_id);
585 if (!match) {
586 printk(KERN_INFO "%s: device hasn't been assigned before, "
587 "so cannot be deassigned\n", __func__);
588 r = -EINVAL;
589 goto out;
590 }
591
592 if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
593 kvm_deassign_device(kvm, match);
594
595 kvm_free_assigned_device(kvm, match);
596
597out:
598 mutex_unlock(&kvm->lock);
599 return r;
600}
601
602
603#ifdef __KVM_HAVE_MSIX
604static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm,
605 struct kvm_assigned_msix_nr *entry_nr)
606{
607 int r = 0;
608 struct kvm_assigned_dev_kernel *adev;
609
610 mutex_lock(&kvm->lock);
611
612 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
613 entry_nr->assigned_dev_id);
614 if (!adev) {
615 r = -EINVAL;
616 goto msix_nr_out;
617 }
618
619 if (adev->entries_nr == 0) {
620 adev->entries_nr = entry_nr->entry_nr;
621 if (adev->entries_nr == 0 ||
Jan Kiszka9f3191a2011-06-11 12:23:55 +0200622 adev->entries_nr > KVM_MAX_MSIX_PER_DEV) {
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300623 r = -EINVAL;
624 goto msix_nr_out;
625 }
626
627 adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) *
628 entry_nr->entry_nr,
629 GFP_KERNEL);
630 if (!adev->host_msix_entries) {
631 r = -ENOMEM;
632 goto msix_nr_out;
633 }
Jan Kiszka06452112010-11-16 22:30:03 +0100634 adev->guest_msix_entries =
635 kzalloc(sizeof(struct msix_entry) * entry_nr->entry_nr,
636 GFP_KERNEL);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300637 if (!adev->guest_msix_entries) {
638 kfree(adev->host_msix_entries);
639 r = -ENOMEM;
640 goto msix_nr_out;
641 }
642 } else /* Not allowed set MSI-X number twice */
643 r = -EINVAL;
644msix_nr_out:
645 mutex_unlock(&kvm->lock);
646 return r;
647}
648
649static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm,
650 struct kvm_assigned_msix_entry *entry)
651{
652 int r = 0, i;
653 struct kvm_assigned_dev_kernel *adev;
654
655 mutex_lock(&kvm->lock);
656
657 adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
658 entry->assigned_dev_id);
659
660 if (!adev) {
661 r = -EINVAL;
662 goto msix_entry_out;
663 }
664
665 for (i = 0; i < adev->entries_nr; i++)
666 if (adev->guest_msix_entries[i].vector == 0 ||
667 adev->guest_msix_entries[i].entry == entry->entry) {
668 adev->guest_msix_entries[i].entry = entry->entry;
669 adev->guest_msix_entries[i].vector = entry->gsi;
670 adev->host_msix_entries[i].entry = entry->entry;
671 break;
672 }
673 if (i == adev->entries_nr) {
674 r = -ENOSPC;
675 goto msix_entry_out;
676 }
677
678msix_entry_out:
679 mutex_unlock(&kvm->lock);
680
681 return r;
682}
683#endif
684
685long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
686 unsigned long arg)
687{
688 void __user *argp = (void __user *)arg;
Jan Kiszka51de2712010-11-16 22:30:06 +0100689 int r;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300690
691 switch (ioctl) {
692 case KVM_ASSIGN_PCI_DEVICE: {
693 struct kvm_assigned_pci_dev assigned_dev;
694
695 r = -EFAULT;
696 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
697 goto out;
698 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
699 if (r)
700 goto out;
701 break;
702 }
703 case KVM_ASSIGN_IRQ: {
704 r = -EOPNOTSUPP;
705 break;
706 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300707 case KVM_ASSIGN_DEV_IRQ: {
708 struct kvm_assigned_irq assigned_irq;
709
710 r = -EFAULT;
711 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
712 goto out;
713 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
714 if (r)
715 goto out;
716 break;
717 }
718 case KVM_DEASSIGN_DEV_IRQ: {
719 struct kvm_assigned_irq assigned_irq;
720
721 r = -EFAULT;
722 if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
723 goto out;
724 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
725 if (r)
726 goto out;
727 break;
728 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300729 case KVM_DEASSIGN_PCI_DEVICE: {
730 struct kvm_assigned_pci_dev assigned_dev;
731
732 r = -EFAULT;
733 if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
734 goto out;
735 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
736 if (r)
737 goto out;
738 break;
739 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300740#ifdef KVM_CAP_IRQ_ROUTING
741 case KVM_SET_GSI_ROUTING: {
742 struct kvm_irq_routing routing;
743 struct kvm_irq_routing __user *urouting;
744 struct kvm_irq_routing_entry *entries;
745
746 r = -EFAULT;
747 if (copy_from_user(&routing, argp, sizeof(routing)))
748 goto out;
749 r = -EINVAL;
750 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
751 goto out;
752 if (routing.flags)
753 goto out;
754 r = -ENOMEM;
755 entries = vmalloc(routing.nr * sizeof(*entries));
756 if (!entries)
757 goto out;
758 r = -EFAULT;
759 urouting = argp;
760 if (copy_from_user(entries, urouting->entries,
761 routing.nr * sizeof(*entries)))
762 goto out_free_irq_routing;
763 r = kvm_set_irq_routing(kvm, entries, routing.nr,
764 routing.flags);
765 out_free_irq_routing:
766 vfree(entries);
767 break;
768 }
769#endif /* KVM_CAP_IRQ_ROUTING */
770#ifdef __KVM_HAVE_MSIX
771 case KVM_ASSIGN_SET_MSIX_NR: {
772 struct kvm_assigned_msix_nr entry_nr;
773 r = -EFAULT;
774 if (copy_from_user(&entry_nr, argp, sizeof entry_nr))
775 goto out;
776 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
777 if (r)
778 goto out;
779 break;
780 }
781 case KVM_ASSIGN_SET_MSIX_ENTRY: {
782 struct kvm_assigned_msix_entry entry;
783 r = -EFAULT;
784 if (copy_from_user(&entry, argp, sizeof entry))
785 goto out;
786 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
787 if (r)
788 goto out;
789 break;
790 }
791#endif
Jan Kiszka51de2712010-11-16 22:30:06 +0100792 default:
793 r = -ENOTTY;
794 break;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300795 }
796out:
797 return r;
798}
799