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