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 /* Name strings for interrupts. This size should be enough,
52 * and I'm too lazy to allocate each name separately. */
53 char (*msix_names)[256];
54 /* Number of available vectors */
55 unsigned msix_vectors;
56 /* Vectors allocated, excluding per-vq vectors if any */
57 unsigned msix_used_vectors;
58 /* Whether we have vector per vq */
62 /* Constants for MSI-X */
63 /* Use first vector for configuration changes, second and the rest for
64 * virtqueues Thus, we need at least 2 vectors for MSI. */
66 VP_MSIX_CONFIG_VECTOR = 0,
67 VP_MSIX_VQ_VECTOR = 1,
70 struct virtio_pci_vq_info
72 /* the actual virtqueue */
75 /* the number of entries in the queue */
78 /* the index of the queue */
81 /* the virtual address of the ring queue */
84 /* the list node for the virtqueues list */
85 struct list_head node;
87 /* MSI-X vector (or none) */
91 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
92 static struct pci_device_id virtio_pci_id_table[] = {
93 { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
97 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
99 /* Convert a generic virtio device to our structure */
100 static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
102 return container_of(vdev, struct virtio_pci_device, vdev);
105 /* virtio config->get_features() implementation */
106 static u32 vp_get_features(struct virtio_device *vdev)
108 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
110 /* When someone needs more than 32 feature bits, we'll need to
111 * steal a bit to indicate that the rest are somewhere else. */
112 return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
115 /* virtio config->finalize_features() implementation */
116 static void vp_finalize_features(struct virtio_device *vdev)
118 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
120 /* Give virtio_ring a chance to accept features. */
121 vring_transport_features(vdev);
123 /* We only support 32 feature bits. */
124 BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
125 iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
128 /* virtio config->get() implementation */
129 static void vp_get(struct virtio_device *vdev, unsigned offset,
130 void *buf, unsigned len)
132 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
133 void __iomem *ioaddr = vp_dev->ioaddr +
134 VIRTIO_PCI_CONFIG(vp_dev) + offset;
138 for (i = 0; i < len; i++)
139 ptr[i] = ioread8(ioaddr + i);
142 /* the config->set() implementation. it's symmetric to the config->get()
144 static void vp_set(struct virtio_device *vdev, unsigned offset,
145 const void *buf, unsigned len)
147 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
148 void __iomem *ioaddr = vp_dev->ioaddr +
149 VIRTIO_PCI_CONFIG(vp_dev) + offset;
153 for (i = 0; i < len; i++)
154 iowrite8(ptr[i], ioaddr + i);
157 /* config->{get,set}_status() implementations */
158 static u8 vp_get_status(struct virtio_device *vdev)
160 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
161 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
164 static void vp_set_status(struct virtio_device *vdev, u8 status)
166 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
167 /* We should never be setting status to 0. */
169 iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
172 /* wait for pending irq handlers */
173 static void vp_synchronize_vectors(struct virtio_device *vdev)
175 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
178 if (vp_dev->intx_enabled)
179 synchronize_irq(vp_dev->pci_dev->irq);
181 for (i = 0; i < vp_dev->msix_vectors; ++i)
182 synchronize_irq(vp_dev->msix_entries[i].vector);
185 static void vp_reset(struct virtio_device *vdev)
187 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
188 /* 0 status means a reset. */
189 iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
190 /* Flush out the status write, and flush in device writes,
191 * including MSi-X interrupts, if any. */
192 ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
193 /* Flush pending VQ/configuration callbacks. */
194 vp_synchronize_vectors(vdev);
197 /* the notify function used when creating a virt queue */
198 static void vp_notify(struct virtqueue *vq)
200 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
201 struct virtio_pci_vq_info *info = vq->priv;
203 /* we write the queue's selector into the notification register to
204 * signal the other end */
205 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
208 /* Handle a configuration change: Tell driver if it wants to know. */
209 static irqreturn_t vp_config_changed(int irq, void *opaque)
211 struct virtio_pci_device *vp_dev = opaque;
212 struct virtio_driver *drv;
213 drv = container_of(vp_dev->vdev.dev.driver,
214 struct virtio_driver, driver);
216 if (drv && drv->config_changed)
217 drv->config_changed(&vp_dev->vdev);
221 /* Notify all virtqueues on an interrupt. */
222 static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
224 struct virtio_pci_device *vp_dev = opaque;
225 struct virtio_pci_vq_info *info;
226 irqreturn_t ret = IRQ_NONE;
229 spin_lock_irqsave(&vp_dev->lock, flags);
230 list_for_each_entry(info, &vp_dev->virtqueues, node) {
231 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
234 spin_unlock_irqrestore(&vp_dev->lock, flags);
239 /* A small wrapper to also acknowledge the interrupt when it's handled.
240 * I really need an EIO hook for the vring so I can ack the interrupt once we
241 * know that we'll be handling the IRQ but before we invoke the callback since
242 * the callback may notify the host which results in the host attempting to
243 * raise an interrupt that we would then mask once we acknowledged the
245 static irqreturn_t vp_interrupt(int irq, void *opaque)
247 struct virtio_pci_device *vp_dev = opaque;
250 /* reading the ISR has the effect of also clearing it so it's very
251 * important to save off the value. */
252 isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
254 /* It's definitely not us if the ISR was not high */
258 /* Configuration change? Tell driver if it wants to know. */
259 if (isr & VIRTIO_PCI_ISR_CONFIG)
260 vp_config_changed(irq, opaque);
262 return vp_vring_interrupt(irq, opaque);
265 static void vp_free_vectors(struct virtio_device *vdev)
267 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
270 if (vp_dev->intx_enabled) {
271 free_irq(vp_dev->pci_dev->irq, vp_dev);
272 vp_dev->intx_enabled = 0;
275 for (i = 0; i < vp_dev->msix_used_vectors; ++i)
276 free_irq(vp_dev->msix_entries[i].vector, vp_dev);
278 if (vp_dev->msix_enabled) {
279 /* Disable the vector used for configuration */
280 iowrite16(VIRTIO_MSI_NO_VECTOR,
281 vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
282 /* Flush the write out to device */
283 ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
285 pci_disable_msix(vp_dev->pci_dev);
286 vp_dev->msix_enabled = 0;
287 vp_dev->msix_vectors = 0;
290 vp_dev->msix_used_vectors = 0;
291 kfree(vp_dev->msix_names);
292 vp_dev->msix_names = NULL;
293 kfree(vp_dev->msix_entries);
294 vp_dev->msix_entries = 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_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
307 if (!vp_dev->msix_entries)
309 vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
311 if (!vp_dev->msix_names)
314 for (i = 0; i < nvectors; ++i)
315 vp_dev->msix_entries[i].entry = i;
317 /* pci_enable_msix returns positive if we can't get this many. */
318 err = pci_enable_msix(vp_dev->pci_dev, vp_dev->msix_entries, nvectors);
323 vp_dev->msix_vectors = nvectors;
324 vp_dev->msix_enabled = 1;
326 /* Set the vector used for configuration */
327 v = vp_dev->msix_used_vectors;
328 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
330 err = request_irq(vp_dev->msix_entries[v].vector,
331 vp_config_changed, 0, vp_dev->msix_names[v],
335 ++vp_dev->msix_used_vectors;
337 iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
338 /* Verify we had enough resources to assign the vector */
339 v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
340 if (v == VIRTIO_MSI_NO_VECTOR) {
345 if (!per_vq_vectors) {
346 /* Shared vector for all VQs */
347 v = vp_dev->msix_used_vectors;
348 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
349 "%s-virtqueues", name);
350 err = request_irq(vp_dev->msix_entries[v].vector,
351 vp_vring_interrupt, 0, vp_dev->msix_names[v],
355 ++vp_dev->msix_used_vectors;
359 vp_free_vectors(vdev);
363 static int vp_request_intx(struct virtio_device *vdev)
366 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
368 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
369 IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
371 vp_dev->intx_enabled = 1;
375 static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
376 void (*callback)(struct virtqueue *vq),
380 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
381 struct virtio_pci_vq_info *info;
382 struct virtqueue *vq;
383 unsigned long flags, size;
387 /* Select the queue we're interested in */
388 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
390 /* Check if queue is either not available or already active. */
391 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
392 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
393 return ERR_PTR(-ENOENT);
395 /* allocate and fill out our structure the represents an active
397 info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
399 return ERR_PTR(-ENOMEM);
401 info->queue_index = index;
403 info->msix_vector = msix_vec;
405 size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
406 info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
407 if (info->queue == NULL) {
412 /* activate the queue */
413 iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
414 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
416 /* create the vring */
417 vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN,
418 vdev, info->queue, vp_notify, callback, name);
421 goto out_activate_queue;
427 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
428 iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
429 msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
430 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
437 spin_lock_irqsave(&vp_dev->lock, flags);
438 list_add(&info->node, &vp_dev->virtqueues);
439 spin_unlock_irqrestore(&vp_dev->lock, flags);
441 INIT_LIST_HEAD(&info->node);
447 vring_del_virtqueue(vq);
449 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
450 free_pages_exact(info->queue, size);
456 static void vp_del_vq(struct virtqueue *vq)
458 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
459 struct virtio_pci_vq_info *info = vq->priv;
460 unsigned long flags, size;
462 spin_lock_irqsave(&vp_dev->lock, flags);
463 list_del(&info->node);
464 spin_unlock_irqrestore(&vp_dev->lock, flags);
466 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
468 if (vp_dev->msix_enabled) {
469 iowrite16(VIRTIO_MSI_NO_VECTOR,
470 vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
471 /* Flush the write out to device */
472 ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
475 vring_del_virtqueue(vq);
477 /* Select and deactivate the queue */
478 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
480 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
481 free_pages_exact(info->queue, size);
485 /* the config->del_vqs() implementation */
486 static void vp_del_vqs(struct virtio_device *vdev)
488 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
489 struct virtqueue *vq, *n;
490 struct virtio_pci_vq_info *info;
492 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
494 if (vp_dev->per_vq_vectors &&
495 info->msix_vector != VIRTIO_MSI_NO_VECTOR)
496 free_irq(vp_dev->msix_entries[info->msix_vector].vector,
500 vp_dev->per_vq_vectors = false;
502 vp_free_vectors(vdev);
505 static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
506 struct virtqueue *vqs[],
507 vq_callback_t *callbacks[],
512 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
514 int i, err, nvectors, allocated_vectors;
517 /* Old style: one normal interrupt for change and all vqs. */
518 err = vp_request_intx(vdev);
522 if (per_vq_vectors) {
523 /* Best option: one for change interrupt, one per vq. */
525 for (i = 0; i < nvqs; ++i)
529 /* Second best: one for change, shared for all vqs. */
533 err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
538 vp_dev->per_vq_vectors = per_vq_vectors;
539 allocated_vectors = vp_dev->msix_used_vectors;
540 for (i = 0; i < nvqs; ++i) {
541 if (!callbacks[i] || !vp_dev->msix_enabled)
542 msix_vec = VIRTIO_MSI_NO_VECTOR;
543 else if (vp_dev->per_vq_vectors)
544 msix_vec = allocated_vectors++;
546 msix_vec = VP_MSIX_VQ_VECTOR;
547 vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
548 if (IS_ERR(vqs[i])) {
549 err = PTR_ERR(vqs[i]);
553 if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
556 /* allocate per-vq irq if available and necessary */
557 snprintf(vp_dev->msix_names[msix_vec],
558 sizeof *vp_dev->msix_names,
560 dev_name(&vp_dev->vdev.dev), names[i]);
561 err = request_irq(vp_dev->msix_entries[msix_vec].vector,
563 vp_dev->msix_names[msix_vec],
579 /* the config->find_vqs() implementation */
580 static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
581 struct virtqueue *vqs[],
582 vq_callback_t *callbacks[],
587 /* Try MSI-X with one vector per queue. */
588 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
591 /* Fallback: MSI-X with one vector for config, one shared for queues. */
592 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
596 /* Finally fall back to regular interrupts. */
597 return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
601 static struct virtio_config_ops virtio_pci_config_ops = {
604 .get_status = vp_get_status,
605 .set_status = vp_set_status,
607 .find_vqs = vp_find_vqs,
608 .del_vqs = vp_del_vqs,
609 .get_features = vp_get_features,
610 .finalize_features = vp_finalize_features,
613 static void virtio_pci_release_dev(struct device *_d)
616 * No need for a release method as we allocate/free
617 * all devices together with the pci devices.
618 * Provide an empty one to avoid getting a warning from core.
622 /* the PCI probing function */
623 static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
624 const struct pci_device_id *id)
626 struct virtio_pci_device *vp_dev;
629 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
630 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
633 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
634 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
635 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
639 /* allocate our structure and fill it out */
640 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
644 vp_dev->vdev.dev.parent = &pci_dev->dev;
645 vp_dev->vdev.dev.release = virtio_pci_release_dev;
646 vp_dev->vdev.config = &virtio_pci_config_ops;
647 vp_dev->pci_dev = pci_dev;
648 INIT_LIST_HEAD(&vp_dev->virtqueues);
649 spin_lock_init(&vp_dev->lock);
651 /* Disable MSI/MSIX to bring device to a known good state. */
652 pci_msi_off(pci_dev);
654 /* enable the device */
655 err = pci_enable_device(pci_dev);
659 err = pci_request_regions(pci_dev, "virtio-pci");
661 goto out_enable_device;
663 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
664 if (vp_dev->ioaddr == NULL)
665 goto out_req_regions;
667 pci_set_drvdata(pci_dev, vp_dev);
668 pci_set_master(pci_dev);
670 /* we use the subsystem vendor/device id as the virtio vendor/device
671 * id. this allows us to use the same PCI vendor/device id for all
672 * virtio devices and to identify the particular virtio driver by
673 * the subsystem ids */
674 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
675 vp_dev->vdev.id.device = pci_dev->subsystem_device;
677 /* finally register the virtio device */
678 err = register_virtio_device(&vp_dev->vdev);
680 goto out_set_drvdata;
685 pci_set_drvdata(pci_dev, NULL);
686 pci_iounmap(pci_dev, vp_dev->ioaddr);
688 pci_release_regions(pci_dev);
690 pci_disable_device(pci_dev);
696 static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
698 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
700 unregister_virtio_device(&vp_dev->vdev);
702 vp_del_vqs(&vp_dev->vdev);
703 pci_set_drvdata(pci_dev, NULL);
704 pci_iounmap(pci_dev, vp_dev->ioaddr);
705 pci_release_regions(pci_dev);
706 pci_disable_device(pci_dev);
711 static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
713 pci_save_state(pci_dev);
714 pci_set_power_state(pci_dev, PCI_D3hot);
718 static int virtio_pci_resume(struct pci_dev *pci_dev)
720 pci_restore_state(pci_dev);
721 pci_set_power_state(pci_dev, PCI_D0);
726 static struct pci_driver virtio_pci_driver = {
727 .name = "virtio-pci",
728 .id_table = virtio_pci_id_table,
729 .probe = virtio_pci_probe,
730 .remove = __devexit_p(virtio_pci_remove),
732 .suspend = virtio_pci_suspend,
733 .resume = virtio_pci_resume,
737 static int __init virtio_pci_init(void)
739 return pci_register_driver(&virtio_pci_driver);
742 module_init(virtio_pci_init);
744 static void __exit virtio_pci_exit(void)
746 pci_unregister_driver(&virtio_pci_driver);
749 module_exit(virtio_pci_exit);