blob: b1e091ae2f377bcecd5fadc417776081d88aba53 [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>
Alex Williamson3d27e232011-12-20 21:59:09 -070020#include <linux/namei.h>
21#include <linux/fs.h>
Avi Kivitybfd99ff2009-08-26 14:57:50 +030022#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 }
Michael S. Tsirkinb93a3552012-01-18 20:07:09 +020052 if (index < 0)
Avi Kivitybfd99ff2009-08-26 14:57:50 +030053 printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
Avi Kivitybfd99ff2009-08-26 14:57:50 +030054
55 return index;
56}
57
Jan Kiszka07700a92012-02-28 14:19:54 +010058static irqreturn_t kvm_assigned_dev_intx(int irq, void *dev_id)
59{
60 struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
61 int ret;
62
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)) {
Jan Kiszkacf9eeac2012-03-14 11:02:11 +010080 spin_lock(&assigned_dev->intx_mask_lock);
Jan Kiszka07700a92012-02-28 14:19:54 +010081 if (!(assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX))
82 kvm_set_irq(assigned_dev->kvm,
83 assigned_dev->irq_source_id, vector, 1);
Jan Kiszkacf9eeac2012-03-14 11:02:11 +010084 spin_unlock(&assigned_dev->intx_mask_lock);
Jan Kiszka07700a92012-02-28 14:19:54 +010085 } 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)
Avi Kivitybfd99ff2009-08-26 14:57:50 +030091{
Jan Kiszka06452112010-11-16 22:30:03 +010092 struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
Avi Kivitybfd99ff2009-08-26 14:57:50 +030093
Jan Kiszka07700a92012-02-28 14:19:54 +010094 if (!(assigned_dev->flags & KVM_DEV_ASSIGN_PCI_2_3)) {
95 spin_lock_irq(&assigned_dev->intx_lock);
Jan Kiszka06452112010-11-16 22:30:03 +010096 disable_irq_nosync(irq);
97 assigned_dev->host_irq_disabled = true;
Jan Kiszka07700a92012-02-28 14:19:54 +010098 spin_unlock_irq(&assigned_dev->intx_lock);
Jan Kiszka06452112010-11-16 22:30:03 +010099 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300100
Jan Kiszka07700a92012-02-28 14:19:54 +0100101 kvm_assigned_dev_raise_guest_irq(assigned_dev,
102 assigned_dev->guest_irq);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300103
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300104 return IRQ_HANDLED;
105}
106
Jan Kiszka07700a92012-02-28 14:19:54 +0100107#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);
114
115 return IRQ_HANDLED;
116}
117#endif
118
Jan Kiszkacc079392011-09-12 18:57:56 +0200119#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;
Jan Kiszka07700a92012-02-28 14:19:54 +0100128 kvm_assigned_dev_raise_guest_irq(assigned_dev, vector);
Jan Kiszkacc079392011-09-12 18:57:56 +0200129 }
130
131 return IRQ_HANDLED;
132}
133#endif
134
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300135/* Ack the irq line for an assigned device */
136static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
137{
Jan Kiszkac61fa9d2011-09-11 10:16:20 +0200138 struct kvm_assigned_dev_kernel *dev =
139 container_of(kian, struct kvm_assigned_dev_kernel,
140 ack_notifier);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300141
142 kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
143
Jan Kiszkacf9eeac2012-03-14 11:02:11 +0100144 spin_lock(&dev->intx_mask_lock);
Jan Kiszka07700a92012-02-28 14:19:54 +0100145
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);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300166 }
Jan Kiszka07700a92012-02-28 14:19:54 +0100167
Jan Kiszkacf9eeac2012-03-14 11:02:11 +0100168 spin_unlock(&dev->intx_mask_lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300169}
170
171static void deassign_guest_irq(struct kvm *kvm,
172 struct kvm_assigned_dev_kernel *assigned_dev)
173{
Jan Kiszkac61fa9d2011-09-11 10:16:20 +0200174 if (assigned_dev->ack_notifier.gsi != -1)
175 kvm_unregister_irq_ack_notifier(kvm,
176 &assigned_dev->ack_notifier);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300177
Jan Kiszka0c106b52010-11-16 22:30:02 +0100178 kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
179 assigned_dev->guest_irq, 0);
180
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300181 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 /*
Jan Kiszka06452112010-11-16 22:30:03 +0100192 * We disable irq here to prevent further events.
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300193 *
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
Jan Kiszka06452112010-11-16 22:30:03 +0100199 * on a currently running IRQ handler.
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300200 */
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++)
Jan Kiszka06452112010-11-16 22:30:03 +0100204 disable_irq(assigned_dev->host_msix_entries[i].vector);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300205
206 for (i = 0; i < assigned_dev->entries_nr; i++)
207 free_irq(assigned_dev->host_msix_entries[i].vector,
Jan Kiszka9f9f6b72011-09-11 10:16:16 +0200208 assigned_dev);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300209
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 */
Jan Kiszka07700a92012-02-28 14:19:54 +0100216 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);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300225
Jan Kiszka9f9f6b72011-09-11 10:16:16 +0200226 free_irq(assigned_dev->host_irq, assigned_dev);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300227
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
Alex Williamsonf8fcfd72011-05-10 10:02:39 -0600270 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);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300277
Greg Rose67778292011-07-22 05:46:07 +0000278 assigned_dev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
279
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300280 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{
Jan Kiszka07700a92012-02-28 14:19:54 +0100305 irq_handler_t irq_handler;
306 unsigned long flags;
307
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300308 dev->host_irq = dev->dev->irq;
Jan Kiszka07700a92012-02-28 14:19:54 +0100309
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.
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300315 */
Jan Kiszka07700a92012-02-28 14:19:54 +0100316 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))
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300326 return -EIO;
Jan Kiszka07700a92012-02-28 14:19:54 +0100327
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 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300333 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;
Jan Kiszka07700a92012-02-28 14:19:54 +0100349 if (request_threaded_irq(dev->host_irq, NULL,
350 kvm_assigned_dev_thread_msi, 0,
351 dev->irq_name, dev)) {
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300352 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++) {
Jan Kiszka06452112010-11-16 22:30:03 +0100376 r = request_threaded_irq(dev->host_msix_entries[i].vector,
Jan Kiszkacc079392011-09-12 18:57:56 +0200377 NULL, kvm_assigned_dev_thread_msix,
Jan Kiszka9f9f6b72011-09-11 10:16:16 +0200378 0, dev->irq_name, dev);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300379 if (r)
jing zhangd57e2c02010-03-13 15:00:45 +0800380 goto err;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300381 }
382
383 return 0;
jing zhangd57e2c02010-03-13 15:00:45 +0800384err:
385 for (i -= 1; i >= 0; i--)
Jan Kiszka9f9f6b72011-09-11 10:16:16 +0200386 free_irq(dev->host_msix_entries[i].vector, dev);
jing zhangd57e2c02010-03-13 15:00:45 +0800387 pci_disable_msix(dev->dev);
388 return r;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300389}
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;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300409 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;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300420 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
Jan Kiszka1e001d42010-11-16 22:30:04 +0100433 snprintf(dev->irq_name, sizeof(dev->irq_name), "kvm:%s",
434 pci_name(dev->dev));
435
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300436 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 }
Jan Kiszka07700a92012-02-28 14:19:54 +0100453 dev->host_irq_disabled = false;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300454
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;
Jan Kiszkac61fa9d2011-09-11 10:16:20 +0200498 if (dev->ack_notifier.gsi != -1)
499 kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300500 } 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
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300514 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;
Jan Kiszka07700a92012-02-28 14:19:54 +0100555 unsigned long irq_type;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300556
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
Jan Kiszka07700a92012-02-28 14:19:54 +0100564 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);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300567out:
568 mutex_unlock(&kvm->lock);
569 return r;
570}
571
Alex Williamson3d27e232011-12-20 21:59:09 -0700572/*
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
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300632static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
633 struct kvm_assigned_pci_dev *assigned_dev)
634{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200635 int r = 0, idx;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300636 struct kvm_assigned_dev_kernel *match;
637 struct pci_dev *dev;
638
Alex Williamson42387372011-12-20 21:59:03 -0700639 if (!(assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU))
640 return -EINVAL;
641
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300642 mutex_lock(&kvm->lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200643 idx = srcu_read_lock(&kvm->srcu);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300644
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 }
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +0800660 dev = pci_get_domain_bus_and_slot(assigned_dev->segnr,
661 assigned_dev->busnr,
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300662 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 }
Alex Williamson3d27e232011-12-20 21:59:09 -0700668
669 /* Don't allow bridges to be assigned */
Jan Kiszkaf961f722012-06-15 07:56:20 -0600670 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
Alex Williamson3d27e232011-12-20 21:59:09 -0700671 r = -EPERM;
672 goto out_put;
673 }
674
675 r = probe_sysfs_permissions(dev);
676 if (r)
677 goto out_put;
678
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300679 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);
Jan Kiszkaed786612010-11-16 22:30:05 +0100692 pci_save_state(dev);
Alex Williamsonf8fcfd72011-05-10 10:02:39 -0600693 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));
Jan Kiszka07700a92012-02-28 14:19:54 +0100697
698 if (!pci_intx_mask_supported(dev))
699 assigned_dev->flags &= ~KVM_DEV_ASSIGN_PCI_2_3;
700
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300701 match->assigned_dev_id = assigned_dev->assigned_dev_id;
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +0800702 match->host_segnr = assigned_dev->segnr;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300703 match->host_busnr = assigned_dev->busnr;
704 match->host_devfn = assigned_dev->devfn;
705 match->flags = assigned_dev->flags;
706 match->dev = dev;
Jan Kiszka06452112010-11-16 22:30:03 +0100707 spin_lock_init(&match->intx_lock);
Jan Kiszkacf9eeac2012-03-14 11:02:11 +0100708 spin_lock_init(&match->intx_mask_lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300709 match->irq_source_id = -1;
710 match->kvm = kvm;
711 match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300712
713 list_add(&match->list, &kvm->arch.assigned_dev_head);
714
Alex Williamson42387372011-12-20 21:59:03 -0700715 if (!kvm->arch.iommu_domain) {
716 r = kvm_iommu_map_guest(kvm);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300717 if (r)
718 goto out_list_del;
719 }
Alex Williamson42387372011-12-20 21:59:03 -0700720 r = kvm_assign_device(kvm, match);
721 if (r)
722 goto out_list_del;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300723
724out:
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200725 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangfae3a352009-12-15 10:28:07 +0800726 mutex_unlock(&kvm->lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300727 return r;
728out_list_del:
Alex Williamsonf8fcfd72011-05-10 10:02:39 -0600729 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));
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300732 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);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200740 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangfae3a352009-12-15 10:28:07 +0800741 mutex_unlock(&kvm->lock);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300742 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
Alex Williamson42387372011-12-20 21:59:03 -0700762 kvm_deassign_device(kvm, match);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300763
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 ||
Jan Kiszka9f3191a2011-06-11 12:23:55 +0200791 adev->entries_nr > KVM_MAX_MSIX_PER_DEV) {
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300792 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 }
Jan Kiszka06452112010-11-16 22:30:03 +0100803 adev->guest_msix_entries =
804 kzalloc(sizeof(struct msix_entry) * entry_nr->entry_nr,
805 GFP_KERNEL);
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300806 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
Jan Kiszka07700a92012-02-28 14:19:54 +0100854static 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
Jan Kiszkacf9eeac2012-03-14 11:02:11 +0100869 spin_lock(&match->intx_mask_lock);
Jan Kiszka07700a92012-02-28 14:19:54 +0100870
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
Jan Kiszkacf9eeac2012-03-14 11:02:11 +0100896 spin_unlock(&match->intx_mask_lock);
Jan Kiszka07700a92012-02-28 14:19:54 +0100897
898out:
899 mutex_unlock(&kvm->lock);
900 return r;
901}
902
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300903long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
904 unsigned long arg)
905{
906 void __user *argp = (void __user *)arg;
Jan Kiszka51de2712010-11-16 22:30:06 +0100907 int r;
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300908
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 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300925 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 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300947 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 }
Avi Kivitybfd99ff2009-08-26 14:57:50 +0300958#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
Jan Kiszka07700a92012-02-28 14:19:54 +01001010 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 }
Jan Kiszka51de2712010-11-16 22:30:06 +01001019 default:
1020 r = -ENOTTY;
1021 break;
Avi Kivitybfd99ff2009-08-26 14:57:50 +03001022 }
1023out:
1024 return r;
1025}