4 * This module allows virtio devices to be used over a virtual PCI device.
5 * This can be used with QEMU based VMMs like KVM or Xen.
7 * Copyright IBM Corp. 2007
10 * Anthony Liguori <aliguori@us.ibm.com>
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
17 #include <linux/module.h>
18 #include <linux/list.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/virtio.h>
23 #include <linux/virtio_config.h>
24 #include <linux/virtio_ring.h>
25 #include <linux/virtio_pci.h>
26 #include <linux/highmem.h>
27 #include <linux/spinlock.h>
29 MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
30 MODULE_DESCRIPTION("virtio-pci");
31 MODULE_LICENSE("GPL");
34 /* Our device structure */
35 struct virtio_pci_device
37 struct virtio_device vdev;
38 struct pci_dev *pci_dev;
40 /* the IO mapping for the PCI config space */
43 /* a list of queues so we can dispatch IRQs */
45 struct list_head virtqueues;
50 struct msix_entry *msix_entries;
51 cpumask_var_t *msix_affinity_masks;
52 /* Name strings for interrupts. This size should be enough,
53 * and I'm too lazy to allocate each name separately. */
54 char (*msix_names)[256];
55 /* Number of available vectors */
56 unsigned msix_vectors;
57 /* Vectors allocated, excluding per-vq vectors if any */
58 unsigned msix_used_vectors;
60 /* Whether we have vector per vq */
64 /* Constants for MSI-X */
65 /* Use first vector for configuration changes, second and the rest for
66 * virtqueues Thus, we need at least 2 vectors for MSI. */
68 VP_MSIX_CONFIG_VECTOR = 0,
69 VP_MSIX_VQ_VECTOR = 1,
72 struct virtio_pci_vq_info
74 /* the actual virtqueue */
77 /* the number of entries in the queue */
80 /* the virtual address of the ring queue */
83 /* the list node for the virtqueues list */
84 struct list_head node;
86 /* MSI-X vector (or none) */
90 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
91 static const struct pci_device_id virtio_pci_id_table[] = {
92 { PCI_DEVICE(0x1af4, PCI_ANY_ID) },
96 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
98 /* Convert a generic virtio device to our structure */
99 static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
101 return container_of(vdev, struct virtio_pci_device, vdev);
104 /* virtio config->get_features() implementation */
105 static u64 vp_get_features(struct virtio_device *vdev)
107 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
109 /* When someone needs more than 32 feature bits, we'll need to
110 * steal a bit to indicate that the rest are somewhere else. */
111 return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
114 /* virtio config->finalize_features() implementation */
115 static void vp_finalize_features(struct virtio_device *vdev)
117 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
119 /* Give virtio_ring a chance to accept features. */
120 vring_transport_features(vdev);
122 /* We only support 32 feature bits. */
123 iowrite32(vdev->features, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
126 /* virtio config->get() implementation */
127 static void vp_get(struct virtio_device *vdev, unsigned offset,
128 void *buf, unsigned len)
130 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
131 void __iomem *ioaddr = vp_dev->ioaddr +
132 VIRTIO_PCI_CONFIG(vp_dev) + offset;
136 for (i = 0; i < len; i++)
137 ptr[i] = ioread8(ioaddr + i);
140 /* the config->set() implementation. it's symmetric to the config->get()
142 static void vp_set(struct virtio_device *vdev, unsigned offset,
143 const void *buf, unsigned len)
145 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
146 void __iomem *ioaddr = vp_dev->ioaddr +
147 VIRTIO_PCI_CONFIG(vp_dev) + offset;
151 for (i = 0; i < len; i++)
152 iowrite8(ptr[i], ioaddr + i);
155 /* config->{get,set}_status() implementations */
156 static u8 vp_get_status(struct virtio_device *vdev)
158 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
159 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
162 static void vp_set_status(struct virtio_device *vdev, u8 status)
164 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
165 /* We should never be setting status to 0. */
167 iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
170 /* wait for pending irq handlers */
171 static void vp_synchronize_vectors(struct virtio_device *vdev)
173 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
176 if (vp_dev->intx_enabled)
177 synchronize_irq(vp_dev->pci_dev->irq);
179 for (i = 0; i < vp_dev->msix_vectors; ++i)
180 synchronize_irq(vp_dev->msix_entries[i].vector);
183 static void vp_reset(struct virtio_device *vdev)
185 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
186 /* 0 status means a reset. */
187 iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
188 /* Flush out the status write, and flush in device writes,
189 * including MSi-X interrupts, if any. */
190 ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
191 /* Flush pending VQ/configuration callbacks. */
192 vp_synchronize_vectors(vdev);
195 /* the notify function used when creating a virt queue */
196 static bool vp_notify(struct virtqueue *vq)
198 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
200 /* we write the queue's selector into the notification register to
201 * signal the other end */
202 iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
206 /* Handle a configuration change: Tell driver if it wants to know. */
207 static irqreturn_t vp_config_changed(int irq, void *opaque)
209 struct virtio_pci_device *vp_dev = opaque;
211 virtio_config_changed(&vp_dev->vdev);
215 /* Notify all virtqueues on an interrupt. */
216 static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
218 struct virtio_pci_device *vp_dev = opaque;
219 struct virtio_pci_vq_info *info;
220 irqreturn_t ret = IRQ_NONE;
223 spin_lock_irqsave(&vp_dev->lock, flags);
224 list_for_each_entry(info, &vp_dev->virtqueues, node) {
225 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
228 spin_unlock_irqrestore(&vp_dev->lock, flags);
233 /* A small wrapper to also acknowledge the interrupt when it's handled.
234 * I really need an EIO hook for the vring so I can ack the interrupt once we
235 * know that we'll be handling the IRQ but before we invoke the callback since
236 * the callback may notify the host which results in the host attempting to
237 * raise an interrupt that we would then mask once we acknowledged the
239 static irqreturn_t vp_interrupt(int irq, void *opaque)
241 struct virtio_pci_device *vp_dev = opaque;
244 /* reading the ISR has the effect of also clearing it so it's very
245 * important to save off the value. */
246 isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
248 /* It's definitely not us if the ISR was not high */
252 /* Configuration change? Tell driver if it wants to know. */
253 if (isr & VIRTIO_PCI_ISR_CONFIG)
254 vp_config_changed(irq, opaque);
256 return vp_vring_interrupt(irq, opaque);
259 static void vp_free_vectors(struct virtio_device *vdev)
261 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
264 if (vp_dev->intx_enabled) {
265 free_irq(vp_dev->pci_dev->irq, vp_dev);
266 vp_dev->intx_enabled = 0;
269 for (i = 0; i < vp_dev->msix_used_vectors; ++i)
270 free_irq(vp_dev->msix_entries[i].vector, vp_dev);
272 for (i = 0; i < vp_dev->msix_vectors; i++)
273 if (vp_dev->msix_affinity_masks[i])
274 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
276 if (vp_dev->msix_enabled) {
277 /* Disable the vector used for configuration */
278 iowrite16(VIRTIO_MSI_NO_VECTOR,
279 vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
280 /* Flush the write out to device */
281 ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
283 pci_disable_msix(vp_dev->pci_dev);
284 vp_dev->msix_enabled = 0;
287 vp_dev->msix_vectors = 0;
288 vp_dev->msix_used_vectors = 0;
289 kfree(vp_dev->msix_names);
290 vp_dev->msix_names = NULL;
291 kfree(vp_dev->msix_entries);
292 vp_dev->msix_entries = NULL;
293 kfree(vp_dev->msix_affinity_masks);
294 vp_dev->msix_affinity_masks = NULL;
297 static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
300 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
301 const char *name = dev_name(&vp_dev->vdev.dev);
305 vp_dev->msix_vectors = nvectors;
307 vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
309 if (!vp_dev->msix_entries)
311 vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
313 if (!vp_dev->msix_names)
315 vp_dev->msix_affinity_masks
316 = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks,
318 if (!vp_dev->msix_affinity_masks)
320 for (i = 0; i < nvectors; ++i)
321 if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
325 for (i = 0; i < nvectors; ++i)
326 vp_dev->msix_entries[i].entry = i;
328 err = pci_enable_msix_exact(vp_dev->pci_dev,
329 vp_dev->msix_entries, nvectors);
332 vp_dev->msix_enabled = 1;
334 /* Set the vector used for configuration */
335 v = vp_dev->msix_used_vectors;
336 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
338 err = request_irq(vp_dev->msix_entries[v].vector,
339 vp_config_changed, 0, vp_dev->msix_names[v],
343 ++vp_dev->msix_used_vectors;
345 iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
346 /* Verify we had enough resources to assign the vector */
347 v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
348 if (v == VIRTIO_MSI_NO_VECTOR) {
353 if (!per_vq_vectors) {
354 /* Shared vector for all VQs */
355 v = vp_dev->msix_used_vectors;
356 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
357 "%s-virtqueues", name);
358 err = request_irq(vp_dev->msix_entries[v].vector,
359 vp_vring_interrupt, 0, vp_dev->msix_names[v],
363 ++vp_dev->msix_used_vectors;
367 vp_free_vectors(vdev);
371 static int vp_request_intx(struct virtio_device *vdev)
374 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
376 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
377 IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
379 vp_dev->intx_enabled = 1;
383 static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
384 void (*callback)(struct virtqueue *vq),
388 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
389 struct virtio_pci_vq_info *info;
390 struct virtqueue *vq;
391 unsigned long flags, size;
395 /* Select the queue we're interested in */
396 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
398 /* Check if queue is either not available or already active. */
399 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
400 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
401 return ERR_PTR(-ENOENT);
403 /* allocate and fill out our structure the represents an active
405 info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
407 return ERR_PTR(-ENOMEM);
410 info->msix_vector = msix_vec;
412 size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
413 info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
414 if (info->queue == NULL) {
419 /* activate the queue */
420 iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
421 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
423 /* create the vring */
424 vq = vring_new_virtqueue(index, info->num, VIRTIO_PCI_VRING_ALIGN, vdev,
425 true, info->queue, vp_notify, callback, name);
428 goto out_activate_queue;
434 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
435 iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
436 msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
437 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
444 spin_lock_irqsave(&vp_dev->lock, flags);
445 list_add(&info->node, &vp_dev->virtqueues);
446 spin_unlock_irqrestore(&vp_dev->lock, flags);
448 INIT_LIST_HEAD(&info->node);
454 vring_del_virtqueue(vq);
456 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
457 free_pages_exact(info->queue, size);
463 static void vp_del_vq(struct virtqueue *vq)
465 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
466 struct virtio_pci_vq_info *info = vq->priv;
467 unsigned long flags, size;
469 spin_lock_irqsave(&vp_dev->lock, flags);
470 list_del(&info->node);
471 spin_unlock_irqrestore(&vp_dev->lock, flags);
473 iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
475 if (vp_dev->msix_enabled) {
476 iowrite16(VIRTIO_MSI_NO_VECTOR,
477 vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
478 /* Flush the write out to device */
479 ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
482 vring_del_virtqueue(vq);
484 /* Select and deactivate the queue */
485 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
487 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
488 free_pages_exact(info->queue, size);
492 /* the config->del_vqs() implementation */
493 static void vp_del_vqs(struct virtio_device *vdev)
495 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
496 struct virtqueue *vq, *n;
497 struct virtio_pci_vq_info *info;
499 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
501 if (vp_dev->per_vq_vectors &&
502 info->msix_vector != VIRTIO_MSI_NO_VECTOR)
503 free_irq(vp_dev->msix_entries[info->msix_vector].vector,
507 vp_dev->per_vq_vectors = false;
509 vp_free_vectors(vdev);
512 static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
513 struct virtqueue *vqs[],
514 vq_callback_t *callbacks[],
519 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
521 int i, err, nvectors, allocated_vectors;
524 /* Old style: one normal interrupt for change and all vqs. */
525 err = vp_request_intx(vdev);
529 if (per_vq_vectors) {
530 /* Best option: one for change interrupt, one per vq. */
532 for (i = 0; i < nvqs; ++i)
536 /* Second best: one for change, shared for all vqs. */
540 err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
545 vp_dev->per_vq_vectors = per_vq_vectors;
546 allocated_vectors = vp_dev->msix_used_vectors;
547 for (i = 0; i < nvqs; ++i) {
551 } else if (!callbacks[i] || !vp_dev->msix_enabled)
552 msix_vec = VIRTIO_MSI_NO_VECTOR;
553 else if (vp_dev->per_vq_vectors)
554 msix_vec = allocated_vectors++;
556 msix_vec = VP_MSIX_VQ_VECTOR;
557 vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
558 if (IS_ERR(vqs[i])) {
559 err = PTR_ERR(vqs[i]);
563 if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
566 /* allocate per-vq irq if available and necessary */
567 snprintf(vp_dev->msix_names[msix_vec],
568 sizeof *vp_dev->msix_names,
570 dev_name(&vp_dev->vdev.dev), names[i]);
571 err = request_irq(vp_dev->msix_entries[msix_vec].vector,
573 vp_dev->msix_names[msix_vec],
589 /* the config->find_vqs() implementation */
590 static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
591 struct virtqueue *vqs[],
592 vq_callback_t *callbacks[],
597 /* Try MSI-X with one vector per queue. */
598 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
601 /* Fallback: MSI-X with one vector for config, one shared for queues. */
602 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
606 /* Finally fall back to regular interrupts. */
607 return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
611 static const char *vp_bus_name(struct virtio_device *vdev)
613 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
615 return pci_name(vp_dev->pci_dev);
618 /* Setup the affinity for a virtqueue:
619 * - force the affinity for per vq vector
620 * - OR over all affinities for shared MSI
621 * - ignore the affinity request if we're using INTX
623 static int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
625 struct virtio_device *vdev = vq->vdev;
626 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
627 struct virtio_pci_vq_info *info = vq->priv;
628 struct cpumask *mask;
634 if (vp_dev->msix_enabled) {
635 mask = vp_dev->msix_affinity_masks[info->msix_vector];
636 irq = vp_dev->msix_entries[info->msix_vector].vector;
638 irq_set_affinity_hint(irq, NULL);
640 cpumask_set_cpu(cpu, mask);
641 irq_set_affinity_hint(irq, mask);
647 static const struct virtio_config_ops virtio_pci_config_ops = {
650 .get_status = vp_get_status,
651 .set_status = vp_set_status,
653 .find_vqs = vp_find_vqs,
654 .del_vqs = vp_del_vqs,
655 .get_features = vp_get_features,
656 .finalize_features = vp_finalize_features,
657 .bus_name = vp_bus_name,
658 .set_vq_affinity = vp_set_vq_affinity,
661 static void virtio_pci_release_dev(struct device *_d)
664 * No need for a release method as we allocate/free
665 * all devices together with the pci devices.
666 * Provide an empty one to avoid getting a warning from core.
670 /* the PCI probing function */
671 static int virtio_pci_probe(struct pci_dev *pci_dev,
672 const struct pci_device_id *id)
674 struct virtio_pci_device *vp_dev;
677 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
678 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
681 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
682 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
683 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
687 /* allocate our structure and fill it out */
688 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
692 vp_dev->vdev.dev.parent = &pci_dev->dev;
693 vp_dev->vdev.dev.release = virtio_pci_release_dev;
694 vp_dev->vdev.config = &virtio_pci_config_ops;
695 vp_dev->pci_dev = pci_dev;
696 INIT_LIST_HEAD(&vp_dev->virtqueues);
697 spin_lock_init(&vp_dev->lock);
699 /* Disable MSI/MSIX to bring device to a known good state. */
700 pci_msi_off(pci_dev);
702 /* enable the device */
703 err = pci_enable_device(pci_dev);
707 err = pci_request_regions(pci_dev, "virtio-pci");
709 goto out_enable_device;
711 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
712 if (vp_dev->ioaddr == NULL) {
714 goto out_req_regions;
717 pci_set_drvdata(pci_dev, vp_dev);
718 pci_set_master(pci_dev);
720 /* we use the subsystem vendor/device id as the virtio vendor/device
721 * id. this allows us to use the same PCI vendor/device id for all
722 * virtio devices and to identify the particular virtio driver by
723 * the subsystem ids */
724 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
725 vp_dev->vdev.id.device = pci_dev->subsystem_device;
727 /* finally register the virtio device */
728 err = register_virtio_device(&vp_dev->vdev);
730 goto out_set_drvdata;
735 pci_iounmap(pci_dev, vp_dev->ioaddr);
737 pci_release_regions(pci_dev);
739 pci_disable_device(pci_dev);
745 static void virtio_pci_remove(struct pci_dev *pci_dev)
747 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
749 unregister_virtio_device(&vp_dev->vdev);
751 vp_del_vqs(&vp_dev->vdev);
752 pci_iounmap(pci_dev, vp_dev->ioaddr);
753 pci_release_regions(pci_dev);
754 pci_disable_device(pci_dev);
758 #ifdef CONFIG_PM_SLEEP
759 static int virtio_pci_freeze(struct device *dev)
761 struct pci_dev *pci_dev = to_pci_dev(dev);
762 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
765 ret = virtio_device_freeze(&vp_dev->vdev);
768 pci_disable_device(pci_dev);
772 static int virtio_pci_restore(struct device *dev)
774 struct pci_dev *pci_dev = to_pci_dev(dev);
775 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
778 ret = pci_enable_device(pci_dev);
782 pci_set_master(pci_dev);
783 return virtio_device_restore(&vp_dev->vdev);
786 static const struct dev_pm_ops virtio_pci_pm_ops = {
787 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
791 static struct pci_driver virtio_pci_driver = {
792 .name = "virtio-pci",
793 .id_table = virtio_pci_id_table,
794 .probe = virtio_pci_probe,
795 .remove = virtio_pci_remove,
796 #ifdef CONFIG_PM_SLEEP
797 .driver.pm = &virtio_pci_pm_ops,
801 module_pci_driver(virtio_pci_driver);