Linux Audio

Check our new training course

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