1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Virtio PCI driver - modern (virtio 1.0) device support
5 * This module allows virtio devices to be used over a virtual PCI device.
6 * This can be used with QEMU based VMMs like KVM or Xen.
8 * Copyright IBM Corp. 2007
9 * Copyright Red Hat, Inc. 2014
12 * Anthony Liguori <aliguori@us.ibm.com>
13 * Rusty Russell <rusty@rustcorp.com.au>
14 * Michael S. Tsirkin <mst@redhat.com>
17 #include <linux/delay.h>
18 #define VIRTIO_PCI_NO_LEGACY
19 #define VIRTIO_RING_NO_LEGACY
20 #include "virtio_pci_common.h"
22 static u64 vp_get_features(struct virtio_device *vdev)
24 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
26 return vp_modern_get_features(&vp_dev->mdev);
29 static void vp_transport_features(struct virtio_device *vdev, u64 features)
31 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
32 struct pci_dev *pci_dev = vp_dev->pci_dev;
34 if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
35 pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
36 __virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
38 if (features & BIT_ULL(VIRTIO_F_RING_RESET))
39 __virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
42 /* virtio config->finalize_features() implementation */
43 static int vp_finalize_features(struct virtio_device *vdev)
45 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
46 u64 features = vdev->features;
48 /* Give virtio_ring a chance to accept features. */
49 vring_transport_features(vdev);
51 /* Give virtio_pci a chance to accept features. */
52 vp_transport_features(vdev, features);
54 if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
55 dev_err(&vdev->dev, "virtio: device uses modern interface "
56 "but does not have VIRTIO_F_VERSION_1\n");
60 vp_modern_set_features(&vp_dev->mdev, vdev->features);
65 /* virtio config->get() implementation */
66 static void vp_get(struct virtio_device *vdev, unsigned int offset,
67 void *buf, unsigned int len)
69 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
70 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
71 void __iomem *device = mdev->device;
76 BUG_ON(offset + len > mdev->device_len);
80 b = ioread8(device + offset);
81 memcpy(buf, &b, sizeof b);
84 w = cpu_to_le16(ioread16(device + offset));
85 memcpy(buf, &w, sizeof w);
88 l = cpu_to_le32(ioread32(device + offset));
89 memcpy(buf, &l, sizeof l);
92 l = cpu_to_le32(ioread32(device + offset));
93 memcpy(buf, &l, sizeof l);
94 l = cpu_to_le32(ioread32(device + offset + sizeof l));
95 memcpy(buf + sizeof l, &l, sizeof l);
102 /* the config->set() implementation. it's symmetric to the config->get()
104 static void vp_set(struct virtio_device *vdev, unsigned int offset,
105 const void *buf, unsigned int len)
107 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
108 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
109 void __iomem *device = mdev->device;
114 BUG_ON(offset + len > mdev->device_len);
118 memcpy(&b, buf, sizeof b);
119 iowrite8(b, device + offset);
122 memcpy(&w, buf, sizeof w);
123 iowrite16(le16_to_cpu(w), device + offset);
126 memcpy(&l, buf, sizeof l);
127 iowrite32(le32_to_cpu(l), device + offset);
130 memcpy(&l, buf, sizeof l);
131 iowrite32(le32_to_cpu(l), device + offset);
132 memcpy(&l, buf + sizeof l, sizeof l);
133 iowrite32(le32_to_cpu(l), device + offset + sizeof l);
140 static u32 vp_generation(struct virtio_device *vdev)
142 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
144 return vp_modern_generation(&vp_dev->mdev);
147 /* config->{get,set}_status() implementations */
148 static u8 vp_get_status(struct virtio_device *vdev)
150 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
152 return vp_modern_get_status(&vp_dev->mdev);
155 static void vp_set_status(struct virtio_device *vdev, u8 status)
157 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
159 /* We should never be setting status to 0. */
161 vp_modern_set_status(&vp_dev->mdev, status);
164 static void vp_reset(struct virtio_device *vdev)
166 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
167 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
169 /* 0 status means a reset. */
170 vp_modern_set_status(mdev, 0);
171 /* After writing 0 to device_status, the driver MUST wait for a read of
172 * device_status to return 0 before reinitializing the device.
173 * This will flush out the status write, and flush in device writes,
174 * including MSI-X interrupts, if any.
176 while (vp_modern_get_status(mdev))
178 /* Flush pending VQ/configuration callbacks. */
179 vp_synchronize_vectors(vdev);
182 static int vp_active_vq(struct virtqueue *vq, u16 msix_vec)
184 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
185 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
190 /* activate the queue */
191 vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
192 vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
193 virtqueue_get_avail_addr(vq),
194 virtqueue_get_used_addr(vq));
196 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
197 msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
198 if (msix_vec == VIRTIO_MSI_NO_VECTOR)
205 static int vp_modern_disable_vq_and_reset(struct virtqueue *vq)
207 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
208 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
209 struct virtio_pci_vq_info *info;
212 if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
215 vp_modern_set_queue_reset(mdev, vq->index);
217 info = vp_dev->vqs[vq->index];
219 /* delete vq from irq handler */
220 spin_lock_irqsave(&vp_dev->lock, flags);
221 list_del(&info->node);
222 spin_unlock_irqrestore(&vp_dev->lock, flags);
224 INIT_LIST_HEAD(&info->node);
226 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
227 __virtqueue_break(vq);
230 /* For the case where vq has an exclusive irq, call synchronize_irq() to
231 * wait for completion.
233 * note: We can't use disable_irq() since it conflicts with the affinity
234 * managed IRQ that is used by some drivers.
236 if (vp_dev->per_vq_vectors && info->msix_vector != VIRTIO_MSI_NO_VECTOR)
237 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, info->msix_vector));
244 static int vp_modern_enable_vq_after_reset(struct virtqueue *vq)
246 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
247 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
248 struct virtio_pci_vq_info *info;
249 unsigned long flags, index;
256 info = vp_dev->vqs[index];
258 if (vp_modern_get_queue_reset(mdev, index))
261 if (vp_modern_get_queue_enable(mdev, index))
264 err = vp_active_vq(vq, info->msix_vector);
269 spin_lock_irqsave(&vp_dev->lock, flags);
270 list_add(&info->node, &vp_dev->virtqueues);
271 spin_unlock_irqrestore(&vp_dev->lock, flags);
273 INIT_LIST_HEAD(&info->node);
276 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
277 __virtqueue_unbreak(vq);
280 vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
286 static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
288 return vp_modern_config_vector(&vp_dev->mdev, vector);
291 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
292 struct virtio_pci_vq_info *info,
294 void (*callback)(struct virtqueue *vq),
301 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
302 struct virtqueue *vq;
306 if (index >= vp_modern_get_num_queues(mdev))
307 return ERR_PTR(-ENOENT);
309 /* Check if queue is either not available or already active. */
310 num = vp_modern_get_queue_size(mdev, index);
311 if (!num || vp_modern_get_queue_enable(mdev, index))
312 return ERR_PTR(-ENOENT);
314 if (!size || size > num)
317 if (size & (size - 1)) {
318 dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", size);
319 return ERR_PTR(-EINVAL);
322 info->msix_vector = msix_vec;
324 /* create the vring */
325 vq = vring_create_virtqueue(index, size,
326 SMP_CACHE_BYTES, &vp_dev->vdev,
328 vp_notify, callback, name);
330 return ERR_PTR(-ENOMEM);
334 err = vp_active_vq(vq, msix_vec);
338 vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
347 vring_del_virtqueue(vq);
351 static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
352 struct virtqueue *vqs[],
353 vq_callback_t *callbacks[],
354 const char * const names[],
357 struct irq_affinity *desc)
359 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
360 struct virtqueue *vq;
361 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, sizes, ctx,
367 /* Select and activate all queues. Has to be done last: once we do
368 * this, there's no way to go back except reset.
370 list_for_each_entry(vq, &vdev->vqs, list)
371 vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
376 static void del_vq(struct virtio_pci_vq_info *info)
378 struct virtqueue *vq = info->vq;
379 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
380 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
382 if (vp_dev->msix_enabled)
383 vp_modern_queue_vector(mdev, vq->index,
384 VIRTIO_MSI_NO_VECTOR);
386 if (!mdev->notify_base)
387 pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
389 vring_del_virtqueue(vq);
392 static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
393 u8 *bar, u64 *offset, u64 *len)
397 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
398 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
399 u8 type, cap_len, id, res_bar;
401 u64 res_offset, res_length;
403 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
405 if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
408 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
410 if (cap_len != sizeof(struct virtio_pci_cap64)) {
411 dev_err(&dev->dev, "%s: shm cap with bad size offset:"
412 " %d size: %d\n", __func__, pos, cap_len);
416 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
418 if (id != required_id)
421 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
423 if (res_bar >= PCI_STD_NUM_BARS)
426 /* Type and ID match, and the BAR value isn't reserved.
430 /* Read the lower 32bit of length and offset */
431 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
434 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
438 /* and now the top half */
439 pci_read_config_dword(dev,
440 pos + offsetof(struct virtio_pci_cap64,
442 res_offset |= ((u64)tmp32) << 32;
443 pci_read_config_dword(dev,
444 pos + offsetof(struct virtio_pci_cap64,
446 res_length |= ((u64)tmp32) << 32;
449 *offset = res_offset;
457 static bool vp_get_shm_region(struct virtio_device *vdev,
458 struct virtio_shm_region *region, u8 id)
460 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
461 struct pci_dev *pci_dev = vp_dev->pci_dev;
464 phys_addr_t phys_addr;
467 if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
470 phys_addr = pci_resource_start(pci_dev, bar);
471 bar_len = pci_resource_len(pci_dev, bar);
473 if ((offset + len) < offset) {
474 dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
479 if (offset + len > bar_len) {
480 dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
486 region->addr = (u64) phys_addr + offset;
491 static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
494 .generation = vp_generation,
495 .get_status = vp_get_status,
496 .set_status = vp_set_status,
498 .find_vqs = vp_modern_find_vqs,
499 .del_vqs = vp_del_vqs,
500 .synchronize_cbs = vp_synchronize_vectors,
501 .get_features = vp_get_features,
502 .finalize_features = vp_finalize_features,
503 .bus_name = vp_bus_name,
504 .set_vq_affinity = vp_set_vq_affinity,
505 .get_vq_affinity = vp_get_vq_affinity,
506 .get_shm_region = vp_get_shm_region,
507 .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
508 .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
511 static const struct virtio_config_ops virtio_pci_config_ops = {
514 .generation = vp_generation,
515 .get_status = vp_get_status,
516 .set_status = vp_set_status,
518 .find_vqs = vp_modern_find_vqs,
519 .del_vqs = vp_del_vqs,
520 .synchronize_cbs = vp_synchronize_vectors,
521 .get_features = vp_get_features,
522 .finalize_features = vp_finalize_features,
523 .bus_name = vp_bus_name,
524 .set_vq_affinity = vp_set_vq_affinity,
525 .get_vq_affinity = vp_get_vq_affinity,
526 .get_shm_region = vp_get_shm_region,
527 .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
528 .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
531 /* the PCI probing function */
532 int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
534 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
535 struct pci_dev *pci_dev = vp_dev->pci_dev;
538 mdev->pci_dev = pci_dev;
540 err = vp_modern_probe(mdev);
545 vp_dev->vdev.config = &virtio_pci_config_ops;
547 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
549 vp_dev->config_vector = vp_config_vector;
550 vp_dev->setup_vq = setup_vq;
551 vp_dev->del_vq = del_vq;
552 vp_dev->isr = mdev->isr;
553 vp_dev->vdev.id = mdev->id;
558 void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
560 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
562 vp_modern_remove(mdev);