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 static int __vp_check_common_size_one_feature(struct virtio_device *vdev, u32 fbit,
43 u32 offset, const char *fname)
45 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
47 if (!__virtio_test_bit(vdev, fbit))
50 if (likely(vp_dev->mdev.common_len >= offset))
54 "virtio: common cfg size(%zu) does not match the feature %s\n",
55 vp_dev->mdev.common_len, fname);
60 #define vp_check_common_size_one_feature(vdev, fbit, field) \
61 __vp_check_common_size_one_feature(vdev, fbit, \
62 offsetofend(struct virtio_pci_modern_common_cfg, field), #fbit)
64 static int vp_check_common_size(struct virtio_device *vdev)
66 if (vp_check_common_size_one_feature(vdev, VIRTIO_F_NOTIF_CONFIG_DATA, queue_notify_data))
69 if (vp_check_common_size_one_feature(vdev, VIRTIO_F_RING_RESET, queue_reset))
75 /* virtio config->finalize_features() implementation */
76 static int vp_finalize_features(struct virtio_device *vdev)
78 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
79 u64 features = vdev->features;
81 /* Give virtio_ring a chance to accept features. */
82 vring_transport_features(vdev);
84 /* Give virtio_pci a chance to accept features. */
85 vp_transport_features(vdev, features);
87 if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
88 dev_err(&vdev->dev, "virtio: device uses modern interface "
89 "but does not have VIRTIO_F_VERSION_1\n");
93 if (vp_check_common_size(vdev))
96 vp_modern_set_features(&vp_dev->mdev, vdev->features);
101 /* virtio config->get() implementation */
102 static void vp_get(struct virtio_device *vdev, unsigned int offset,
103 void *buf, unsigned int len)
105 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
106 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
107 void __iomem *device = mdev->device;
112 BUG_ON(offset + len > mdev->device_len);
116 b = ioread8(device + offset);
117 memcpy(buf, &b, sizeof b);
120 w = cpu_to_le16(ioread16(device + offset));
121 memcpy(buf, &w, sizeof w);
124 l = cpu_to_le32(ioread32(device + offset));
125 memcpy(buf, &l, sizeof l);
128 l = cpu_to_le32(ioread32(device + offset));
129 memcpy(buf, &l, sizeof l);
130 l = cpu_to_le32(ioread32(device + offset + sizeof l));
131 memcpy(buf + sizeof l, &l, sizeof l);
138 /* the config->set() implementation. it's symmetric to the config->get()
140 static void vp_set(struct virtio_device *vdev, unsigned int offset,
141 const void *buf, unsigned int len)
143 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
144 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
145 void __iomem *device = mdev->device;
150 BUG_ON(offset + len > mdev->device_len);
154 memcpy(&b, buf, sizeof b);
155 iowrite8(b, device + offset);
158 memcpy(&w, buf, sizeof w);
159 iowrite16(le16_to_cpu(w), device + offset);
162 memcpy(&l, buf, sizeof l);
163 iowrite32(le32_to_cpu(l), device + offset);
166 memcpy(&l, buf, sizeof l);
167 iowrite32(le32_to_cpu(l), device + offset);
168 memcpy(&l, buf + sizeof l, sizeof l);
169 iowrite32(le32_to_cpu(l), device + offset + sizeof l);
176 static u32 vp_generation(struct virtio_device *vdev)
178 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
180 return vp_modern_generation(&vp_dev->mdev);
183 /* config->{get,set}_status() implementations */
184 static u8 vp_get_status(struct virtio_device *vdev)
186 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
188 return vp_modern_get_status(&vp_dev->mdev);
191 static void vp_set_status(struct virtio_device *vdev, u8 status)
193 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
195 /* We should never be setting status to 0. */
197 vp_modern_set_status(&vp_dev->mdev, status);
200 static void vp_reset(struct virtio_device *vdev)
202 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
203 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
205 /* 0 status means a reset. */
206 vp_modern_set_status(mdev, 0);
207 /* After writing 0 to device_status, the driver MUST wait for a read of
208 * device_status to return 0 before reinitializing the device.
209 * This will flush out the status write, and flush in device writes,
210 * including MSI-X interrupts, if any.
212 while (vp_modern_get_status(mdev))
214 /* Flush pending VQ/configuration callbacks. */
215 vp_synchronize_vectors(vdev);
218 static int vp_active_vq(struct virtqueue *vq, u16 msix_vec)
220 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
221 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
226 /* activate the queue */
227 vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
228 vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
229 virtqueue_get_avail_addr(vq),
230 virtqueue_get_used_addr(vq));
232 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
233 msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
234 if (msix_vec == VIRTIO_MSI_NO_VECTOR)
241 static int vp_modern_disable_vq_and_reset(struct virtqueue *vq)
243 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
244 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
245 struct virtio_pci_vq_info *info;
248 if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
251 vp_modern_set_queue_reset(mdev, vq->index);
253 info = vp_dev->vqs[vq->index];
255 /* delete vq from irq handler */
256 spin_lock_irqsave(&vp_dev->lock, flags);
257 list_del(&info->node);
258 spin_unlock_irqrestore(&vp_dev->lock, flags);
260 INIT_LIST_HEAD(&info->node);
262 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
263 __virtqueue_break(vq);
266 /* For the case where vq has an exclusive irq, call synchronize_irq() to
267 * wait for completion.
269 * note: We can't use disable_irq() since it conflicts with the affinity
270 * managed IRQ that is used by some drivers.
272 if (vp_dev->per_vq_vectors && info->msix_vector != VIRTIO_MSI_NO_VECTOR)
273 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, info->msix_vector));
280 static int vp_modern_enable_vq_after_reset(struct virtqueue *vq)
282 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
283 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
284 struct virtio_pci_vq_info *info;
285 unsigned long flags, index;
292 info = vp_dev->vqs[index];
294 if (vp_modern_get_queue_reset(mdev, index))
297 if (vp_modern_get_queue_enable(mdev, index))
300 err = vp_active_vq(vq, info->msix_vector);
305 spin_lock_irqsave(&vp_dev->lock, flags);
306 list_add(&info->node, &vp_dev->virtqueues);
307 spin_unlock_irqrestore(&vp_dev->lock, flags);
309 INIT_LIST_HEAD(&info->node);
312 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
313 __virtqueue_unbreak(vq);
316 vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
322 static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
324 return vp_modern_config_vector(&vp_dev->mdev, vector);
327 static bool vp_notify_with_data(struct virtqueue *vq)
329 u32 data = vring_notification_data(vq);
331 iowrite32(data, (void __iomem *)vq->priv);
336 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
337 struct virtio_pci_vq_info *info,
339 void (*callback)(struct virtqueue *vq),
345 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
346 bool (*notify)(struct virtqueue *vq);
347 struct virtqueue *vq;
351 if (__virtio_test_bit(&vp_dev->vdev, VIRTIO_F_NOTIFICATION_DATA))
352 notify = vp_notify_with_data;
356 if (index >= vp_modern_get_num_queues(mdev))
357 return ERR_PTR(-EINVAL);
359 /* Check if queue is either not available or already active. */
360 num = vp_modern_get_queue_size(mdev, index);
361 if (!num || vp_modern_get_queue_enable(mdev, index))
362 return ERR_PTR(-ENOENT);
364 info->msix_vector = msix_vec;
366 /* create the vring */
367 vq = vring_create_virtqueue(index, num,
368 SMP_CACHE_BYTES, &vp_dev->vdev,
370 notify, callback, name);
372 return ERR_PTR(-ENOMEM);
376 err = vp_active_vq(vq, msix_vec);
380 vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
389 vring_del_virtqueue(vq);
393 static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
394 struct virtqueue *vqs[],
395 vq_callback_t *callbacks[],
396 const char * const names[], const bool *ctx,
397 struct irq_affinity *desc)
399 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
400 struct virtqueue *vq;
401 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
406 /* Select and activate all queues. Has to be done last: once we do
407 * this, there's no way to go back except reset.
409 list_for_each_entry(vq, &vdev->vqs, list)
410 vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
415 static void del_vq(struct virtio_pci_vq_info *info)
417 struct virtqueue *vq = info->vq;
418 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
419 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
421 if (vp_dev->msix_enabled)
422 vp_modern_queue_vector(mdev, vq->index,
423 VIRTIO_MSI_NO_VECTOR);
425 if (!mdev->notify_base)
426 pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
428 vring_del_virtqueue(vq);
431 static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
432 u8 *bar, u64 *offset, u64 *len)
436 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
437 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
438 u8 type, cap_len, id, res_bar;
440 u64 res_offset, res_length;
442 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
444 if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
447 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
449 if (cap_len != sizeof(struct virtio_pci_cap64)) {
450 dev_err(&dev->dev, "%s: shm cap with bad size offset:"
451 " %d size: %d\n", __func__, pos, cap_len);
455 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
457 if (id != required_id)
460 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
462 if (res_bar >= PCI_STD_NUM_BARS)
465 /* Type and ID match, and the BAR value isn't reserved.
469 /* Read the lower 32bit of length and offset */
470 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
473 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
477 /* and now the top half */
478 pci_read_config_dword(dev,
479 pos + offsetof(struct virtio_pci_cap64,
481 res_offset |= ((u64)tmp32) << 32;
482 pci_read_config_dword(dev,
483 pos + offsetof(struct virtio_pci_cap64,
485 res_length |= ((u64)tmp32) << 32;
488 *offset = res_offset;
496 static bool vp_get_shm_region(struct virtio_device *vdev,
497 struct virtio_shm_region *region, u8 id)
499 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
500 struct pci_dev *pci_dev = vp_dev->pci_dev;
503 phys_addr_t phys_addr;
506 if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
509 phys_addr = pci_resource_start(pci_dev, bar);
510 bar_len = pci_resource_len(pci_dev, bar);
512 if ((offset + len) < offset) {
513 dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
518 if (offset + len > bar_len) {
519 dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
525 region->addr = (u64) phys_addr + offset;
530 static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
533 .generation = vp_generation,
534 .get_status = vp_get_status,
535 .set_status = vp_set_status,
537 .find_vqs = vp_modern_find_vqs,
538 .del_vqs = vp_del_vqs,
539 .synchronize_cbs = vp_synchronize_vectors,
540 .get_features = vp_get_features,
541 .finalize_features = vp_finalize_features,
542 .bus_name = vp_bus_name,
543 .set_vq_affinity = vp_set_vq_affinity,
544 .get_vq_affinity = vp_get_vq_affinity,
545 .get_shm_region = vp_get_shm_region,
546 .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
547 .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
550 static const struct virtio_config_ops virtio_pci_config_ops = {
553 .generation = vp_generation,
554 .get_status = vp_get_status,
555 .set_status = vp_set_status,
557 .find_vqs = vp_modern_find_vqs,
558 .del_vqs = vp_del_vqs,
559 .synchronize_cbs = vp_synchronize_vectors,
560 .get_features = vp_get_features,
561 .finalize_features = vp_finalize_features,
562 .bus_name = vp_bus_name,
563 .set_vq_affinity = vp_set_vq_affinity,
564 .get_vq_affinity = vp_get_vq_affinity,
565 .get_shm_region = vp_get_shm_region,
566 .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
567 .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
570 /* the PCI probing function */
571 int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
573 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
574 struct pci_dev *pci_dev = vp_dev->pci_dev;
577 mdev->pci_dev = pci_dev;
579 err = vp_modern_probe(mdev);
584 vp_dev->vdev.config = &virtio_pci_config_ops;
586 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
588 vp_dev->config_vector = vp_config_vector;
589 vp_dev->setup_vq = setup_vq;
590 vp_dev->del_vq = del_vq;
591 vp_dev->isr = mdev->isr;
592 vp_dev->vdev.id = mdev->id;
597 void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
599 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
601 vp_modern_remove(mdev);