2 * Virtio PCI driver - common functionality for all device versions
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
8 * Copyright Red Hat, Inc. 2014
11 * Anthony Liguori <aliguori@us.ibm.com>
12 * Rusty Russell <rusty@rustcorp.com.au>
13 * Michael S. Tsirkin <mst@redhat.com>
15 * This work is licensed under the terms of the GNU GPL, version 2 or later.
16 * See the COPYING file in the top-level directory.
20 #include "virtio_pci_common.h"
22 static bool force_legacy = false;
24 #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
25 module_param(force_legacy, bool, 0444);
26 MODULE_PARM_DESC(force_legacy,
27 "Force legacy mode for transitional virtio 1 devices");
30 /* wait for pending irq handlers */
31 void vp_synchronize_vectors(struct virtio_device *vdev)
33 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
36 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, 0));
37 for (i = 1; i < vp_dev->msix_vectors; i++)
38 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
41 /* the notify function used when creating a virt queue */
42 bool vp_notify(struct virtqueue *vq)
44 /* we write the queue's selector into the notification register to
45 * signal the other end */
46 iowrite16(vq->index, (void __iomem *)vq->priv);
50 /* Handle a configuration change: Tell driver if it wants to know. */
51 static irqreturn_t vp_config_changed(int irq, void *opaque)
53 struct virtio_pci_device *vp_dev = opaque;
55 virtio_config_changed(&vp_dev->vdev);
59 /* Notify all virtqueues on an interrupt. */
60 static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
62 struct virtio_pci_device *vp_dev = opaque;
63 irqreturn_t ret = IRQ_NONE;
66 list_for_each_entry(vq, &vp_dev->vdev.vqs, list) {
67 if (vq->callback && vring_interrupt(irq, vq) == IRQ_HANDLED)
74 /* A small wrapper to also acknowledge the interrupt when it's handled.
75 * I really need an EIO hook for the vring so I can ack the interrupt once we
76 * know that we'll be handling the IRQ but before we invoke the callback since
77 * the callback may notify the host which results in the host attempting to
78 * raise an interrupt that we would then mask once we acknowledged the
80 static irqreturn_t vp_interrupt(int irq, void *opaque)
82 struct virtio_pci_device *vp_dev = opaque;
85 /* reading the ISR has the effect of also clearing it so it's very
86 * important to save off the value. */
87 isr = ioread8(vp_dev->isr);
89 /* It's definitely not us if the ISR was not high */
93 /* Configuration change? Tell driver if it wants to know. */
94 if (isr & VIRTIO_PCI_ISR_CONFIG)
95 vp_config_changed(irq, opaque);
97 return vp_vring_interrupt(irq, opaque);
100 static void vp_remove_vqs(struct virtio_device *vdev)
102 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
103 struct virtqueue *vq, *n;
105 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
106 if (vp_dev->msix_vector_map) {
107 int v = vp_dev->msix_vector_map[vq->index];
109 if (v != VIRTIO_MSI_NO_VECTOR)
110 free_irq(pci_irq_vector(vp_dev->pci_dev, v),
117 /* the config->del_vqs() implementation */
118 void vp_del_vqs(struct virtio_device *vdev)
120 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
123 if (WARN_ON_ONCE(list_empty_careful(&vdev->vqs)))
128 if (vp_dev->pci_dev->msix_enabled) {
129 for (i = 0; i < vp_dev->msix_vectors; i++)
130 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
132 /* Disable the vector used for configuration */
133 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
135 kfree(vp_dev->msix_affinity_masks);
136 kfree(vp_dev->msix_names);
137 kfree(vp_dev->msix_vector_map);
140 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
141 pci_free_irq_vectors(vp_dev->pci_dev);
144 static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
145 struct virtqueue *vqs[], vq_callback_t *callbacks[],
146 const char * const names[])
148 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
149 const char *name = dev_name(&vp_dev->vdev.dev);
150 int i, err = -ENOMEM, allocated_vectors, nvectors;
155 for (i = 0; i < nvqs; i++)
159 /* Try one vector per queue first. */
160 err = pci_alloc_irq_vectors(vp_dev->pci_dev, nvectors, nvectors,
163 /* Fallback to one vector for config, one shared for queues. */
165 err = pci_alloc_irq_vectors(vp_dev->pci_dev, 2, 2,
173 vp_dev->msix_vectors = nvectors;
174 vp_dev->msix_names = kmalloc_array(nvectors,
175 sizeof(*vp_dev->msix_names), GFP_KERNEL);
176 if (!vp_dev->msix_names)
177 goto out_free_irq_vectors;
179 vp_dev->msix_affinity_masks = kcalloc(nvectors,
180 sizeof(*vp_dev->msix_affinity_masks), GFP_KERNEL);
181 if (!vp_dev->msix_affinity_masks)
182 goto out_free_msix_names;
184 for (i = 0; i < nvectors; ++i) {
185 if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
187 goto out_free_msix_affinity_masks;
190 /* Set the vector used for configuration */
191 snprintf(vp_dev->msix_names[0], sizeof(*vp_dev->msix_names),
193 err = request_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_config_changed,
194 0, vp_dev->msix_names[0], vp_dev);
196 goto out_free_msix_affinity_masks;
198 /* Verify we had enough resources to assign the vector */
199 if (vp_dev->config_vector(vp_dev, 0) == VIRTIO_MSI_NO_VECTOR) {
201 goto out_free_config_irq;
204 vp_dev->msix_vector_map = kmalloc_array(nvqs,
205 sizeof(*vp_dev->msix_vector_map), GFP_KERNEL);
206 if (!vp_dev->msix_vector_map)
207 goto out_disable_config_irq;
209 allocated_vectors = 1; /* vector 0 is the config interrupt */
210 for (i = 0; i < nvqs; ++i) {
217 msix_vec = allocated_vectors;
219 msix_vec = VIRTIO_MSI_NO_VECTOR;
221 vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
223 if (IS_ERR(vqs[i])) {
224 err = PTR_ERR(vqs[i]);
228 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
229 vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
233 snprintf(vp_dev->msix_names[i + 1],
234 sizeof(*vp_dev->msix_names), "%s-%s",
235 dev_name(&vp_dev->vdev.dev), names[i]);
236 err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
237 vring_interrupt, IRQF_SHARED,
238 vp_dev->msix_names[i + 1], vqs[i]);
240 /* don't free this irq on error */
241 vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
244 vp_dev->msix_vector_map[i] = msix_vec;
247 * Use a different vector for each queue if they are available,
248 * else share the same vector for all VQs.
258 kfree(vp_dev->msix_vector_map);
259 out_disable_config_irq:
260 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
262 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
263 out_free_msix_affinity_masks:
264 for (i = 0; i < nvectors; i++) {
265 if (vp_dev->msix_affinity_masks[i])
266 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
268 kfree(vp_dev->msix_affinity_masks);
270 kfree(vp_dev->msix_names);
271 out_free_irq_vectors:
272 pci_free_irq_vectors(vp_dev->pci_dev);
276 static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
277 struct virtqueue *vqs[], vq_callback_t *callbacks[],
278 const char * const names[])
280 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
283 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
284 dev_name(&vdev->dev), vp_dev);
288 for (i = 0; i < nvqs; ++i) {
293 vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
294 VIRTIO_MSI_NO_VECTOR);
295 if (IS_ERR(vqs[i])) {
296 err = PTR_ERR(vqs[i]);
305 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
309 /* the config->find_vqs() implementation */
310 int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
311 struct virtqueue *vqs[],
312 vq_callback_t *callbacks[],
313 const char * const names[])
317 err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names);
320 return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names);
323 const char *vp_bus_name(struct virtio_device *vdev)
325 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
327 return pci_name(vp_dev->pci_dev);
330 /* Setup the affinity for a virtqueue:
331 * - force the affinity for per vq vector
332 * - OR over all affinities for shared MSI
333 * - ignore the affinity request if we're using INTX
335 int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
337 struct virtio_device *vdev = vq->vdev;
338 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
343 if (vp_dev->pci_dev->msix_enabled) {
344 int vec = vp_dev->msix_vector_map[vq->index];
345 struct cpumask *mask = vp_dev->msix_affinity_masks[vec];
346 unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec);
349 irq_set_affinity_hint(irq, NULL);
352 cpumask_set_cpu(cpu, mask);
353 irq_set_affinity_hint(irq, mask);
359 #ifdef CONFIG_PM_SLEEP
360 static int virtio_pci_freeze(struct device *dev)
362 struct pci_dev *pci_dev = to_pci_dev(dev);
363 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
366 ret = virtio_device_freeze(&vp_dev->vdev);
369 pci_disable_device(pci_dev);
373 static int virtio_pci_restore(struct device *dev)
375 struct pci_dev *pci_dev = to_pci_dev(dev);
376 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
379 ret = pci_enable_device(pci_dev);
383 pci_set_master(pci_dev);
384 return virtio_device_restore(&vp_dev->vdev);
387 static const struct dev_pm_ops virtio_pci_pm_ops = {
388 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
393 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
394 static const struct pci_device_id virtio_pci_id_table[] = {
395 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
399 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
401 static void virtio_pci_release_dev(struct device *_d)
403 struct virtio_device *vdev = dev_to_virtio(_d);
404 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
406 /* As struct device is a kobject, it's not safe to
407 * free the memory (including the reference counter itself)
408 * until it's release callback. */
412 static int virtio_pci_probe(struct pci_dev *pci_dev,
413 const struct pci_device_id *id)
415 struct virtio_pci_device *vp_dev;
418 /* allocate our structure and fill it out */
419 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
423 pci_set_drvdata(pci_dev, vp_dev);
424 vp_dev->vdev.dev.parent = &pci_dev->dev;
425 vp_dev->vdev.dev.release = virtio_pci_release_dev;
426 vp_dev->pci_dev = pci_dev;
428 /* enable the device */
429 rc = pci_enable_device(pci_dev);
431 goto err_enable_device;
434 rc = virtio_pci_legacy_probe(vp_dev);
435 /* Also try modern mode if we can't map BAR0 (no IO space). */
436 if (rc == -ENODEV || rc == -ENOMEM)
437 rc = virtio_pci_modern_probe(vp_dev);
441 rc = virtio_pci_modern_probe(vp_dev);
443 rc = virtio_pci_legacy_probe(vp_dev);
448 pci_set_master(pci_dev);
450 rc = register_virtio_device(&vp_dev->vdev);
458 virtio_pci_legacy_remove(vp_dev);
460 virtio_pci_modern_remove(vp_dev);
462 pci_disable_device(pci_dev);
468 static void virtio_pci_remove(struct pci_dev *pci_dev)
470 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
471 struct device *dev = get_device(&vp_dev->vdev.dev);
473 unregister_virtio_device(&vp_dev->vdev);
476 virtio_pci_legacy_remove(vp_dev);
478 virtio_pci_modern_remove(vp_dev);
480 pci_disable_device(pci_dev);
484 static struct pci_driver virtio_pci_driver = {
485 .name = "virtio-pci",
486 .id_table = virtio_pci_id_table,
487 .probe = virtio_pci_probe,
488 .remove = virtio_pci_remove,
489 #ifdef CONFIG_PM_SLEEP
490 .driver.pm = &virtio_pci_pm_ops,
494 module_pci_driver(virtio_pci_driver);
496 MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
497 MODULE_DESCRIPTION("virtio-pci");
498 MODULE_LICENSE("GPL");