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