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 /* wait for pending irq handlers */
23 void vp_synchronize_vectors(struct virtio_device
*vdev
)
25 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
28 if (vp_dev
->intx_enabled
)
29 synchronize_irq(vp_dev
->pci_dev
->irq
);
31 for (i
= 0; i
< vp_dev
->msix_vectors
; ++i
)
32 synchronize_irq(vp_dev
->msix_entries
[i
].vector
);
35 /* the notify function used when creating a virt queue */
36 bool vp_notify(struct virtqueue
*vq
)
38 /* we write the queue's selector into the notification register to
39 * signal the other end */
40 iowrite16(vq
->index
, (void __iomem
*)vq
->priv
);
44 /* Handle a configuration change: Tell driver if it wants to know. */
45 static irqreturn_t
vp_config_changed(int irq
, void *opaque
)
47 struct virtio_pci_device
*vp_dev
= opaque
;
49 virtio_config_changed(&vp_dev
->vdev
);
53 /* Notify all virtqueues on an interrupt. */
54 static irqreturn_t
vp_vring_interrupt(int irq
, void *opaque
)
56 struct virtio_pci_device
*vp_dev
= opaque
;
57 struct virtio_pci_vq_info
*info
;
58 irqreturn_t ret
= IRQ_NONE
;
61 spin_lock_irqsave(&vp_dev
->lock
, flags
);
62 list_for_each_entry(info
, &vp_dev
->virtqueues
, node
) {
63 if (vring_interrupt(irq
, info
->vq
) == IRQ_HANDLED
)
66 spin_unlock_irqrestore(&vp_dev
->lock
, flags
);
71 /* A small wrapper to also acknowledge the interrupt when it's handled.
72 * I really need an EIO hook for the vring so I can ack the interrupt once we
73 * know that we'll be handling the IRQ but before we invoke the callback since
74 * the callback may notify the host which results in the host attempting to
75 * raise an interrupt that we would then mask once we acknowledged the
77 static irqreturn_t
vp_interrupt(int irq
, void *opaque
)
79 struct virtio_pci_device
*vp_dev
= opaque
;
82 /* reading the ISR has the effect of also clearing it so it's very
83 * important to save off the value. */
84 isr
= ioread8(vp_dev
->isr
);
86 /* It's definitely not us if the ISR was not high */
90 /* Configuration change? Tell driver if it wants to know. */
91 if (isr
& VIRTIO_PCI_ISR_CONFIG
)
92 vp_config_changed(irq
, opaque
);
94 return vp_vring_interrupt(irq
, opaque
);
97 static void vp_free_vectors(struct virtio_device
*vdev
)
99 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
102 if (vp_dev
->intx_enabled
) {
103 free_irq(vp_dev
->pci_dev
->irq
, vp_dev
);
104 vp_dev
->intx_enabled
= 0;
107 for (i
= 0; i
< vp_dev
->msix_used_vectors
; ++i
)
108 free_irq(vp_dev
->msix_entries
[i
].vector
, vp_dev
);
110 for (i
= 0; i
< vp_dev
->msix_vectors
; i
++)
111 if (vp_dev
->msix_affinity_masks
[i
])
112 free_cpumask_var(vp_dev
->msix_affinity_masks
[i
]);
114 if (vp_dev
->msix_enabled
) {
115 /* Disable the vector used for configuration */
116 vp_dev
->config_vector(vp_dev
, VIRTIO_MSI_NO_VECTOR
);
118 pci_disable_msix(vp_dev
->pci_dev
);
119 vp_dev
->msix_enabled
= 0;
122 vp_dev
->msix_vectors
= 0;
123 vp_dev
->msix_used_vectors
= 0;
124 kfree(vp_dev
->msix_names
);
125 vp_dev
->msix_names
= NULL
;
126 kfree(vp_dev
->msix_entries
);
127 vp_dev
->msix_entries
= NULL
;
128 kfree(vp_dev
->msix_affinity_masks
);
129 vp_dev
->msix_affinity_masks
= NULL
;
132 static int vp_request_msix_vectors(struct virtio_device
*vdev
, int nvectors
,
135 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
136 const char *name
= dev_name(&vp_dev
->vdev
.dev
);
140 vp_dev
->msix_vectors
= nvectors
;
142 vp_dev
->msix_entries
= kmalloc(nvectors
* sizeof *vp_dev
->msix_entries
,
144 if (!vp_dev
->msix_entries
)
146 vp_dev
->msix_names
= kmalloc(nvectors
* sizeof *vp_dev
->msix_names
,
148 if (!vp_dev
->msix_names
)
150 vp_dev
->msix_affinity_masks
151 = kzalloc(nvectors
* sizeof *vp_dev
->msix_affinity_masks
,
153 if (!vp_dev
->msix_affinity_masks
)
155 for (i
= 0; i
< nvectors
; ++i
)
156 if (!alloc_cpumask_var(&vp_dev
->msix_affinity_masks
[i
],
160 for (i
= 0; i
< nvectors
; ++i
)
161 vp_dev
->msix_entries
[i
].entry
= i
;
163 err
= pci_enable_msix_exact(vp_dev
->pci_dev
,
164 vp_dev
->msix_entries
, nvectors
);
167 vp_dev
->msix_enabled
= 1;
169 /* Set the vector used for configuration */
170 v
= vp_dev
->msix_used_vectors
;
171 snprintf(vp_dev
->msix_names
[v
], sizeof *vp_dev
->msix_names
,
173 err
= request_irq(vp_dev
->msix_entries
[v
].vector
,
174 vp_config_changed
, 0, vp_dev
->msix_names
[v
],
178 ++vp_dev
->msix_used_vectors
;
180 v
= vp_dev
->config_vector(vp_dev
, v
);
181 /* Verify we had enough resources to assign the vector */
182 if (v
== VIRTIO_MSI_NO_VECTOR
) {
187 if (!per_vq_vectors
) {
188 /* Shared vector for all VQs */
189 v
= vp_dev
->msix_used_vectors
;
190 snprintf(vp_dev
->msix_names
[v
], sizeof *vp_dev
->msix_names
,
191 "%s-virtqueues", name
);
192 err
= request_irq(vp_dev
->msix_entries
[v
].vector
,
193 vp_vring_interrupt
, 0, vp_dev
->msix_names
[v
],
197 ++vp_dev
->msix_used_vectors
;
201 vp_free_vectors(vdev
);
205 static int vp_request_intx(struct virtio_device
*vdev
)
208 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
210 err
= request_irq(vp_dev
->pci_dev
->irq
, vp_interrupt
,
211 IRQF_SHARED
, dev_name(&vdev
->dev
), vp_dev
);
213 vp_dev
->intx_enabled
= 1;
217 static struct virtqueue
*vp_setup_vq(struct virtio_device
*vdev
, unsigned index
,
218 void (*callback
)(struct virtqueue
*vq
),
222 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
223 struct virtio_pci_vq_info
*info
= kmalloc(sizeof *info
, GFP_KERNEL
);
224 struct virtqueue
*vq
;
227 /* fill out our structure that represents an active queue */
229 return ERR_PTR(-ENOMEM
);
231 vq
= vp_dev
->setup_vq(vp_dev
, info
, index
, callback
, name
, msix_vec
);
237 spin_lock_irqsave(&vp_dev
->lock
, flags
);
238 list_add(&info
->node
, &vp_dev
->virtqueues
);
239 spin_unlock_irqrestore(&vp_dev
->lock
, flags
);
241 INIT_LIST_HEAD(&info
->node
);
244 vp_dev
->vqs
[index
] = info
;
252 static void vp_del_vq(struct virtqueue
*vq
)
254 struct virtio_pci_device
*vp_dev
= to_vp_device(vq
->vdev
);
255 struct virtio_pci_vq_info
*info
= vp_dev
->vqs
[vq
->index
];
258 spin_lock_irqsave(&vp_dev
->lock
, flags
);
259 list_del(&info
->node
);
260 spin_unlock_irqrestore(&vp_dev
->lock
, flags
);
262 vp_dev
->del_vq(info
);
266 /* the config->del_vqs() implementation */
267 void vp_del_vqs(struct virtio_device
*vdev
)
269 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
270 struct virtqueue
*vq
, *n
;
271 struct virtio_pci_vq_info
*info
;
273 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
) {
274 info
= vp_dev
->vqs
[vq
->index
];
275 if (vp_dev
->per_vq_vectors
&&
276 info
->msix_vector
!= VIRTIO_MSI_NO_VECTOR
)
277 free_irq(vp_dev
->msix_entries
[info
->msix_vector
].vector
,
281 vp_dev
->per_vq_vectors
= false;
283 vp_free_vectors(vdev
);
287 static int vp_try_to_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
288 struct virtqueue
*vqs
[],
289 vq_callback_t
*callbacks
[],
294 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
296 int i
, err
, nvectors
, allocated_vectors
;
298 vp_dev
->vqs
= kmalloc(nvqs
* sizeof *vp_dev
->vqs
, GFP_KERNEL
);
303 /* Old style: one normal interrupt for change and all vqs. */
304 err
= vp_request_intx(vdev
);
308 if (per_vq_vectors
) {
309 /* Best option: one for change interrupt, one per vq. */
311 for (i
= 0; i
< nvqs
; ++i
)
315 /* Second best: one for change, shared for all vqs. */
319 err
= vp_request_msix_vectors(vdev
, nvectors
, per_vq_vectors
);
324 vp_dev
->per_vq_vectors
= per_vq_vectors
;
325 allocated_vectors
= vp_dev
->msix_used_vectors
;
326 for (i
= 0; i
< nvqs
; ++i
) {
330 } else if (!callbacks
[i
] || !vp_dev
->msix_enabled
)
331 msix_vec
= VIRTIO_MSI_NO_VECTOR
;
332 else if (vp_dev
->per_vq_vectors
)
333 msix_vec
= allocated_vectors
++;
335 msix_vec
= VP_MSIX_VQ_VECTOR
;
336 vqs
[i
] = vp_setup_vq(vdev
, i
, callbacks
[i
], names
[i
], msix_vec
);
337 if (IS_ERR(vqs
[i
])) {
338 err
= PTR_ERR(vqs
[i
]);
342 if (!vp_dev
->per_vq_vectors
|| msix_vec
== VIRTIO_MSI_NO_VECTOR
)
345 /* allocate per-vq irq if available and necessary */
346 snprintf(vp_dev
->msix_names
[msix_vec
],
347 sizeof *vp_dev
->msix_names
,
349 dev_name(&vp_dev
->vdev
.dev
), names
[i
]);
350 err
= request_irq(vp_dev
->msix_entries
[msix_vec
].vector
,
352 vp_dev
->msix_names
[msix_vec
],
366 /* the config->find_vqs() implementation */
367 int vp_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
368 struct virtqueue
*vqs
[],
369 vq_callback_t
*callbacks
[],
374 /* Try MSI-X with one vector per queue. */
375 err
= vp_try_to_find_vqs(vdev
, nvqs
, vqs
, callbacks
, names
, true, true);
378 /* Fallback: MSI-X with one vector for config, one shared for queues. */
379 err
= vp_try_to_find_vqs(vdev
, nvqs
, vqs
, callbacks
, names
,
383 /* Finally fall back to regular interrupts. */
384 return vp_try_to_find_vqs(vdev
, nvqs
, vqs
, callbacks
, names
,
388 const char *vp_bus_name(struct virtio_device
*vdev
)
390 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
392 return pci_name(vp_dev
->pci_dev
);
395 /* Setup the affinity for a virtqueue:
396 * - force the affinity for per vq vector
397 * - OR over all affinities for shared MSI
398 * - ignore the affinity request if we're using INTX
400 int vp_set_vq_affinity(struct virtqueue
*vq
, int cpu
)
402 struct virtio_device
*vdev
= vq
->vdev
;
403 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
404 struct virtio_pci_vq_info
*info
= vp_dev
->vqs
[vq
->index
];
405 struct cpumask
*mask
;
411 if (vp_dev
->msix_enabled
) {
412 mask
= vp_dev
->msix_affinity_masks
[info
->msix_vector
];
413 irq
= vp_dev
->msix_entries
[info
->msix_vector
].vector
;
415 irq_set_affinity_hint(irq
, NULL
);
417 cpumask_set_cpu(cpu
, mask
);
418 irq_set_affinity_hint(irq
, mask
);
424 void virtio_pci_release_dev(struct device
*_d
)
427 * No need for a release method as we allocate/free
428 * all devices together with the pci devices.
429 * Provide an empty one to avoid getting a warning from core.
433 #ifdef CONFIG_PM_SLEEP
434 static int virtio_pci_freeze(struct device
*dev
)
436 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
437 struct virtio_pci_device
*vp_dev
= pci_get_drvdata(pci_dev
);
440 ret
= virtio_device_freeze(&vp_dev
->vdev
);
443 pci_disable_device(pci_dev
);
447 static int virtio_pci_restore(struct device
*dev
)
449 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
450 struct virtio_pci_device
*vp_dev
= pci_get_drvdata(pci_dev
);
453 ret
= pci_enable_device(pci_dev
);
457 pci_set_master(pci_dev
);
458 return virtio_device_restore(&vp_dev
->vdev
);
461 const struct dev_pm_ops virtio_pci_pm_ops
= {
462 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze
, virtio_pci_restore
)