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);
39 /* virtio config->finalize_features() implementation */
40 static int vp_finalize_features(struct virtio_device *vdev)
42 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
43 u64 features = vdev->features;
45 /* Give virtio_ring a chance to accept features. */
46 vring_transport_features(vdev);
48 /* Give virtio_pci a chance to accept features. */
49 vp_transport_features(vdev, features);
51 if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
52 dev_err(&vdev->dev, "virtio: device uses modern interface "
53 "but does not have VIRTIO_F_VERSION_1\n");
57 vp_modern_set_features(&vp_dev->mdev, vdev->features);
62 /* virtio config->get() implementation */
63 static void vp_get(struct virtio_device *vdev, unsigned int offset,
64 void *buf, unsigned int len)
66 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
67 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
68 void __iomem *device = mdev->device;
73 BUG_ON(offset + len > mdev->device_len);
77 b = ioread8(device + offset);
78 memcpy(buf, &b, sizeof b);
81 w = cpu_to_le16(ioread16(device + offset));
82 memcpy(buf, &w, sizeof w);
85 l = cpu_to_le32(ioread32(device + offset));
86 memcpy(buf, &l, sizeof l);
89 l = cpu_to_le32(ioread32(device + offset));
90 memcpy(buf, &l, sizeof l);
91 l = cpu_to_le32(ioread32(device + offset + sizeof l));
92 memcpy(buf + sizeof l, &l, sizeof l);
99 /* the config->set() implementation. it's symmetric to the config->get()
101 static void vp_set(struct virtio_device *vdev, unsigned int offset,
102 const void *buf, unsigned int len)
104 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
105 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
106 void __iomem *device = mdev->device;
111 BUG_ON(offset + len > mdev->device_len);
115 memcpy(&b, buf, sizeof b);
116 iowrite8(b, device + offset);
119 memcpy(&w, buf, sizeof w);
120 iowrite16(le16_to_cpu(w), device + offset);
123 memcpy(&l, buf, sizeof l);
124 iowrite32(le32_to_cpu(l), device + offset);
127 memcpy(&l, buf, sizeof l);
128 iowrite32(le32_to_cpu(l), device + offset);
129 memcpy(&l, buf + sizeof l, sizeof l);
130 iowrite32(le32_to_cpu(l), device + offset + sizeof l);
137 static u32 vp_generation(struct virtio_device *vdev)
139 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
141 return vp_modern_generation(&vp_dev->mdev);
144 /* config->{get,set}_status() implementations */
145 static u8 vp_get_status(struct virtio_device *vdev)
147 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
149 return vp_modern_get_status(&vp_dev->mdev);
152 static void vp_set_status(struct virtio_device *vdev, u8 status)
154 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
156 /* We should never be setting status to 0. */
158 vp_modern_set_status(&vp_dev->mdev, status);
161 static void vp_reset(struct virtio_device *vdev)
163 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
164 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
166 /* 0 status means a reset. */
167 vp_modern_set_status(mdev, 0);
168 /* After writing 0 to device_status, the driver MUST wait for a read of
169 * device_status to return 0 before reinitializing the device.
170 * This will flush out the status write, and flush in device writes,
171 * including MSI-X interrupts, if any.
173 while (vp_modern_get_status(mdev))
175 /* Flush pending VQ/configuration callbacks. */
176 vp_synchronize_vectors(vdev);
179 static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
181 return vp_modern_config_vector(&vp_dev->mdev, vector);
184 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
185 struct virtio_pci_vq_info *info,
187 void (*callback)(struct virtqueue *vq),
193 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
194 struct virtqueue *vq;
198 if (index >= vp_modern_get_num_queues(mdev))
199 return ERR_PTR(-ENOENT);
201 /* Check if queue is either not available or already active. */
202 num = vp_modern_get_queue_size(mdev, index);
203 if (!num || vp_modern_get_queue_enable(mdev, index))
204 return ERR_PTR(-ENOENT);
206 if (num & (num - 1)) {
207 dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
208 return ERR_PTR(-EINVAL);
211 info->msix_vector = msix_vec;
213 /* create the vring */
214 vq = vring_create_virtqueue(index, num,
215 SMP_CACHE_BYTES, &vp_dev->vdev,
217 vp_notify, callback, name);
219 return ERR_PTR(-ENOMEM);
221 /* activate the queue */
222 vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
223 vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
224 virtqueue_get_avail_addr(vq),
225 virtqueue_get_used_addr(vq));
227 vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
233 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
234 msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
235 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
237 goto err_assign_vector;
244 if (!mdev->notify_base)
245 pci_iounmap(mdev->pci_dev, (void __iomem __force *)vq->priv);
247 vring_del_virtqueue(vq);
251 static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
252 struct virtqueue *vqs[],
253 vq_callback_t *callbacks[],
254 const char * const names[], const bool *ctx,
255 struct irq_affinity *desc)
257 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
258 struct virtqueue *vq;
259 int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
264 /* Select and activate all queues. Has to be done last: once we do
265 * this, there's no way to go back except reset.
267 list_for_each_entry(vq, &vdev->vqs, list)
268 vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
273 static void del_vq(struct virtio_pci_vq_info *info)
275 struct virtqueue *vq = info->vq;
276 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
277 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
279 if (vp_dev->msix_enabled)
280 vp_modern_queue_vector(mdev, vq->index,
281 VIRTIO_MSI_NO_VECTOR);
283 if (!mdev->notify_base)
284 pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
286 vring_del_virtqueue(vq);
289 static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
290 u8 *bar, u64 *offset, u64 *len)
294 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
295 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
296 u8 type, cap_len, id, res_bar;
298 u64 res_offset, res_length;
300 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
302 if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
305 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
307 if (cap_len != sizeof(struct virtio_pci_cap64)) {
308 dev_err(&dev->dev, "%s: shm cap with bad size offset:"
309 " %d size: %d\n", __func__, pos, cap_len);
313 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
315 if (id != required_id)
318 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
320 if (res_bar >= PCI_STD_NUM_BARS)
323 /* Type and ID match, and the BAR value isn't reserved.
327 /* Read the lower 32bit of length and offset */
328 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
331 pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
335 /* and now the top half */
336 pci_read_config_dword(dev,
337 pos + offsetof(struct virtio_pci_cap64,
339 res_offset |= ((u64)tmp32) << 32;
340 pci_read_config_dword(dev,
341 pos + offsetof(struct virtio_pci_cap64,
343 res_length |= ((u64)tmp32) << 32;
346 *offset = res_offset;
354 static bool vp_get_shm_region(struct virtio_device *vdev,
355 struct virtio_shm_region *region, u8 id)
357 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
358 struct pci_dev *pci_dev = vp_dev->pci_dev;
361 phys_addr_t phys_addr;
364 if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
367 phys_addr = pci_resource_start(pci_dev, bar);
368 bar_len = pci_resource_len(pci_dev, bar);
370 if ((offset + len) < offset) {
371 dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
376 if (offset + len > bar_len) {
377 dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
383 region->addr = (u64) phys_addr + offset;
388 static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
391 .generation = vp_generation,
392 .get_status = vp_get_status,
393 .set_status = vp_set_status,
395 .find_vqs = vp_modern_find_vqs,
396 .del_vqs = vp_del_vqs,
397 .synchronize_cbs = vp_synchronize_vectors,
398 .get_features = vp_get_features,
399 .finalize_features = vp_finalize_features,
400 .bus_name = vp_bus_name,
401 .set_vq_affinity = vp_set_vq_affinity,
402 .get_vq_affinity = vp_get_vq_affinity,
403 .get_shm_region = vp_get_shm_region,
406 static const struct virtio_config_ops virtio_pci_config_ops = {
409 .generation = vp_generation,
410 .get_status = vp_get_status,
411 .set_status = vp_set_status,
413 .find_vqs = vp_modern_find_vqs,
414 .del_vqs = vp_del_vqs,
415 .synchronize_cbs = vp_synchronize_vectors,
416 .get_features = vp_get_features,
417 .finalize_features = vp_finalize_features,
418 .bus_name = vp_bus_name,
419 .set_vq_affinity = vp_set_vq_affinity,
420 .get_vq_affinity = vp_get_vq_affinity,
421 .get_shm_region = vp_get_shm_region,
424 /* the PCI probing function */
425 int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
427 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
428 struct pci_dev *pci_dev = vp_dev->pci_dev;
431 mdev->pci_dev = pci_dev;
433 err = vp_modern_probe(mdev);
438 vp_dev->vdev.config = &virtio_pci_config_ops;
440 vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
442 vp_dev->config_vector = vp_config_vector;
443 vp_dev->setup_vq = setup_vq;
444 vp_dev->del_vq = del_vq;
445 vp_dev->isr = mdev->isr;
446 vp_dev->vdev.id = mdev->id;
451 void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
453 struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
455 vp_modern_remove(mdev);