1 // SPDX-License-Identifier: GPL-2.0-only
3 * VDPA networking device simulator.
5 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
6 * Author: Jason Wang <jasowang@redhat.com>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
15 #include <linux/poll.h>
16 #include <linux/slab.h>
17 #include <linux/sched.h>
18 #include <linux/wait.h>
19 #include <linux/uuid.h>
20 #include <linux/iommu.h>
21 #include <linux/dma-map-ops.h>
22 #include <linux/sysfs.h>
23 #include <linux/file.h>
24 #include <linux/etherdevice.h>
25 #include <linux/vringh.h>
26 #include <linux/vdpa.h>
27 #include <linux/virtio_byteorder.h>
28 #include <linux/vhost_iotlb.h>
29 #include <uapi/linux/virtio_config.h>
30 #include <uapi/linux/virtio_net.h>
32 #define DRV_VERSION "0.1"
33 #define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>"
34 #define DRV_DESC "vDPA Device Simulator"
35 #define DRV_LICENSE "GPL v2"
37 static int batch_mapping = 1;
38 module_param(batch_mapping, int, 0444);
39 MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");
41 struct vdpasim_virtqueue {
43 struct vringh_kiov iov;
51 irqreturn_t (*cb)(void *data);
54 #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
55 #define VDPASIM_QUEUE_MAX 256
56 #define VDPASIM_DEVICE_ID 0x1
57 #define VDPASIM_VENDOR_ID 0
58 #define VDPASIM_VQ_NUM 0x2
59 #define VDPASIM_NAME "vdpasim-netdev"
61 static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
62 (1ULL << VIRTIO_F_VERSION_1) |
63 (1ULL << VIRTIO_F_ACCESS_PLATFORM);
65 /* State of each vdpasim device */
67 struct vdpa_device vdpa;
68 struct vdpasim_virtqueue vqs[VDPASIM_VQ_NUM];
69 struct work_struct work;
70 /* spinlock to synchronize virtqueue state */
72 struct virtio_net_config config;
73 struct vhost_iotlb *iommu;
78 /* spinlock to synchronize iommu table */
79 spinlock_t iommu_lock;
82 /* TODO: cross-endian support */
83 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
85 return virtio_legacy_is_little_endian() ||
86 (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
89 static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
91 return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
94 static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
96 return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
99 static struct vdpasim *vdpasim_dev;
101 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
103 return container_of(vdpa, struct vdpasim, vdpa);
106 static struct vdpasim *dev_to_sim(struct device *dev)
108 struct vdpa_device *vdpa = dev_to_vdpa(dev);
110 return vdpa_to_sim(vdpa);
113 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
115 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
117 vringh_init_iotlb(&vq->vring, vdpasim_features,
118 VDPASIM_QUEUE_MAX, false,
119 (struct vring_desc *)(uintptr_t)vq->desc_addr,
120 (struct vring_avail *)
121 (uintptr_t)vq->driver_addr,
122 (struct vring_used *)
123 (uintptr_t)vq->device_addr);
126 static void vdpasim_vq_reset(struct vdpasim_virtqueue *vq)
134 vringh_init_iotlb(&vq->vring, vdpasim_features, VDPASIM_QUEUE_MAX,
135 false, NULL, NULL, NULL);
138 static void vdpasim_reset(struct vdpasim *vdpasim)
142 for (i = 0; i < VDPASIM_VQ_NUM; i++)
143 vdpasim_vq_reset(&vdpasim->vqs[i]);
145 spin_lock(&vdpasim->iommu_lock);
146 vhost_iotlb_reset(vdpasim->iommu);
147 spin_unlock(&vdpasim->iommu_lock);
149 vdpasim->features = 0;
151 ++vdpasim->generation;
154 static void vdpasim_work(struct work_struct *work)
156 struct vdpasim *vdpasim = container_of(work, struct
158 struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
159 struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
165 spin_lock(&vdpasim->lock);
167 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
170 if (!txq->ready || !rxq->ready)
175 err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
176 &txq->head, GFP_ATOMIC);
180 err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
181 &rxq->head, GFP_ATOMIC);
183 vringh_complete_iotlb(&txq->vring, txq->head, 0);
188 read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
194 write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
195 vdpasim->buffer, read);
199 total_write += write;
202 /* Make sure data is wrote before advancing index */
205 vringh_complete_iotlb(&txq->vring, txq->head, 0);
206 vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
208 /* Make sure used is visible before rasing the interrupt. */
213 txq->cb(txq->private);
215 rxq->cb(rxq->private);
219 schedule_work(&vdpasim->work);
225 spin_unlock(&vdpasim->lock);
228 static int dir_to_perm(enum dma_data_direction dir)
233 case DMA_FROM_DEVICE:
239 case DMA_BIDIRECTIONAL:
249 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
250 unsigned long offset, size_t size,
251 enum dma_data_direction dir,
254 struct vdpasim *vdpasim = dev_to_sim(dev);
255 struct vhost_iotlb *iommu = vdpasim->iommu;
256 u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
257 int ret, perm = dir_to_perm(dir);
260 return DMA_MAPPING_ERROR;
262 /* For simplicity, use identical mapping to avoid e.g iova
265 spin_lock(&vdpasim->iommu_lock);
266 ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
267 pa, dir_to_perm(dir));
268 spin_unlock(&vdpasim->iommu_lock);
270 return DMA_MAPPING_ERROR;
272 return (dma_addr_t)(pa);
275 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
276 size_t size, enum dma_data_direction dir,
279 struct vdpasim *vdpasim = dev_to_sim(dev);
280 struct vhost_iotlb *iommu = vdpasim->iommu;
282 spin_lock(&vdpasim->iommu_lock);
283 vhost_iotlb_del_range(iommu, (u64)dma_addr,
284 (u64)dma_addr + size - 1);
285 spin_unlock(&vdpasim->iommu_lock);
288 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
289 dma_addr_t *dma_addr, gfp_t flag,
292 struct vdpasim *vdpasim = dev_to_sim(dev);
293 struct vhost_iotlb *iommu = vdpasim->iommu;
294 void *addr = kmalloc(size, flag);
297 spin_lock(&vdpasim->iommu_lock);
299 *dma_addr = DMA_MAPPING_ERROR;
301 u64 pa = virt_to_phys(addr);
303 ret = vhost_iotlb_add_range(iommu, (u64)pa,
307 *dma_addr = DMA_MAPPING_ERROR;
311 *dma_addr = (dma_addr_t)pa;
313 spin_unlock(&vdpasim->iommu_lock);
318 static void vdpasim_free_coherent(struct device *dev, size_t size,
319 void *vaddr, dma_addr_t dma_addr,
322 struct vdpasim *vdpasim = dev_to_sim(dev);
323 struct vhost_iotlb *iommu = vdpasim->iommu;
325 spin_lock(&vdpasim->iommu_lock);
326 vhost_iotlb_del_range(iommu, (u64)dma_addr,
327 (u64)dma_addr + size - 1);
328 spin_unlock(&vdpasim->iommu_lock);
330 kfree(phys_to_virt((uintptr_t)dma_addr));
333 static const struct dma_map_ops vdpasim_dma_ops = {
334 .map_page = vdpasim_map_page,
335 .unmap_page = vdpasim_unmap_page,
336 .alloc = vdpasim_alloc_coherent,
337 .free = vdpasim_free_coherent,
340 static const struct vdpa_config_ops vdpasim_net_config_ops;
341 static const struct vdpa_config_ops vdpasim_net_batch_config_ops;
343 static struct vdpasim *vdpasim_create(void)
345 const struct vdpa_config_ops *ops;
346 struct vdpasim *vdpasim;
351 ops = &vdpasim_net_batch_config_ops;
353 ops = &vdpasim_net_config_ops;
355 vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, VDPASIM_VQ_NUM);
359 INIT_WORK(&vdpasim->work, vdpasim_work);
360 spin_lock_init(&vdpasim->lock);
361 spin_lock_init(&vdpasim->iommu_lock);
363 dev = &vdpasim->vdpa.dev;
364 dev->coherent_dma_mask = DMA_BIT_MASK(64);
365 set_dma_ops(dev, &vdpasim_dma_ops);
367 vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
371 vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
372 if (!vdpasim->buffer)
375 eth_random_addr(vdpasim->config.mac);
377 vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
378 vringh_set_iotlb(&vdpasim->vqs[1].vring, vdpasim->iommu);
380 vdpasim->vdpa.dma_dev = dev;
381 ret = vdpa_register_device(&vdpasim->vdpa);
393 static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
394 u64 desc_area, u64 driver_area,
397 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
398 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
400 vq->desc_addr = desc_area;
401 vq->driver_addr = driver_area;
402 vq->device_addr = device_area;
407 static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
409 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
410 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
415 static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
417 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
418 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
421 schedule_work(&vdpasim->work);
424 static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
425 struct vdpa_callback *cb)
427 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
428 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
430 vq->cb = cb->callback;
431 vq->private = cb->private;
434 static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
436 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
437 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
439 spin_lock(&vdpasim->lock);
442 vdpasim_queue_ready(vdpasim, idx);
443 spin_unlock(&vdpasim->lock);
446 static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
448 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
449 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
454 static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
455 const struct vdpa_vq_state *state)
457 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
458 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
459 struct vringh *vrh = &vq->vring;
461 spin_lock(&vdpasim->lock);
462 vrh->last_avail_idx = state->avail_index;
463 spin_unlock(&vdpasim->lock);
468 static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
469 struct vdpa_vq_state *state)
471 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
472 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
473 struct vringh *vrh = &vq->vring;
475 state->avail_index = vrh->last_avail_idx;
479 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
481 return VDPASIM_QUEUE_ALIGN;
484 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
486 return vdpasim_features;
489 static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
491 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
492 struct virtio_net_config *config = &vdpasim->config;
494 /* DMA mapping must be done by driver */
495 if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
498 vdpasim->features = features & vdpasim_features;
500 /* We generally only know whether guest is using the legacy interface
501 * here, so generally that's the earliest we can set config fields.
502 * Note: We actually require VIRTIO_F_ACCESS_PLATFORM above which
503 * implies VIRTIO_F_VERSION_1, but let's not try to be clever here.
506 config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
507 config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
511 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
512 struct vdpa_callback *cb)
514 /* We don't support config interrupt */
517 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
519 return VDPASIM_QUEUE_MAX;
522 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
524 return VDPASIM_DEVICE_ID;
527 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
529 return VDPASIM_VENDOR_ID;
532 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
534 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
537 spin_lock(&vdpasim->lock);
538 status = vdpasim->status;
539 spin_unlock(&vdpasim->lock);
544 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
546 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
548 spin_lock(&vdpasim->lock);
549 vdpasim->status = status;
551 vdpasim_reset(vdpasim);
552 spin_unlock(&vdpasim->lock);
555 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
556 void *buf, unsigned int len)
558 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
560 if (offset + len < sizeof(struct virtio_net_config))
561 memcpy(buf, (u8 *)&vdpasim->config + offset, len);
564 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
565 const void *buf, unsigned int len)
567 /* No writable config supportted by vdpasim */
570 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
572 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
574 return vdpasim->generation;
577 static int vdpasim_set_map(struct vdpa_device *vdpa,
578 struct vhost_iotlb *iotlb)
580 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
581 struct vhost_iotlb_map *map;
582 u64 start = 0ULL, last = 0ULL - 1;
585 spin_lock(&vdpasim->iommu_lock);
586 vhost_iotlb_reset(vdpasim->iommu);
588 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
589 map = vhost_iotlb_itree_next(map, start, last)) {
590 ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
591 map->last, map->addr, map->perm);
595 spin_unlock(&vdpasim->iommu_lock);
599 vhost_iotlb_reset(vdpasim->iommu);
600 spin_unlock(&vdpasim->iommu_lock);
604 static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
607 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
610 spin_lock(&vdpasim->iommu_lock);
611 ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
613 spin_unlock(&vdpasim->iommu_lock);
618 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
620 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
622 spin_lock(&vdpasim->iommu_lock);
623 vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
624 spin_unlock(&vdpasim->iommu_lock);
629 static void vdpasim_free(struct vdpa_device *vdpa)
631 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
633 cancel_work_sync(&vdpasim->work);
634 kfree(vdpasim->buffer);
636 vhost_iotlb_free(vdpasim->iommu);
639 static const struct vdpa_config_ops vdpasim_net_config_ops = {
640 .set_vq_address = vdpasim_set_vq_address,
641 .set_vq_num = vdpasim_set_vq_num,
642 .kick_vq = vdpasim_kick_vq,
643 .set_vq_cb = vdpasim_set_vq_cb,
644 .set_vq_ready = vdpasim_set_vq_ready,
645 .get_vq_ready = vdpasim_get_vq_ready,
646 .set_vq_state = vdpasim_set_vq_state,
647 .get_vq_state = vdpasim_get_vq_state,
648 .get_vq_align = vdpasim_get_vq_align,
649 .get_features = vdpasim_get_features,
650 .set_features = vdpasim_set_features,
651 .set_config_cb = vdpasim_set_config_cb,
652 .get_vq_num_max = vdpasim_get_vq_num_max,
653 .get_device_id = vdpasim_get_device_id,
654 .get_vendor_id = vdpasim_get_vendor_id,
655 .get_status = vdpasim_get_status,
656 .set_status = vdpasim_set_status,
657 .get_config = vdpasim_get_config,
658 .set_config = vdpasim_set_config,
659 .get_generation = vdpasim_get_generation,
660 .dma_map = vdpasim_dma_map,
661 .dma_unmap = vdpasim_dma_unmap,
662 .free = vdpasim_free,
665 static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
666 .set_vq_address = vdpasim_set_vq_address,
667 .set_vq_num = vdpasim_set_vq_num,
668 .kick_vq = vdpasim_kick_vq,
669 .set_vq_cb = vdpasim_set_vq_cb,
670 .set_vq_ready = vdpasim_set_vq_ready,
671 .get_vq_ready = vdpasim_get_vq_ready,
672 .set_vq_state = vdpasim_set_vq_state,
673 .get_vq_state = vdpasim_get_vq_state,
674 .get_vq_align = vdpasim_get_vq_align,
675 .get_features = vdpasim_get_features,
676 .set_features = vdpasim_set_features,
677 .set_config_cb = vdpasim_set_config_cb,
678 .get_vq_num_max = vdpasim_get_vq_num_max,
679 .get_device_id = vdpasim_get_device_id,
680 .get_vendor_id = vdpasim_get_vendor_id,
681 .get_status = vdpasim_get_status,
682 .set_status = vdpasim_set_status,
683 .get_config = vdpasim_get_config,
684 .set_config = vdpasim_set_config,
685 .get_generation = vdpasim_get_generation,
686 .set_map = vdpasim_set_map,
687 .free = vdpasim_free,
690 static int __init vdpasim_dev_init(void)
692 vdpasim_dev = vdpasim_create();
694 if (!IS_ERR(vdpasim_dev))
697 return PTR_ERR(vdpasim_dev);
700 static void __exit vdpasim_dev_exit(void)
702 struct vdpa_device *vdpa = &vdpasim_dev->vdpa;
704 vdpa_unregister_device(vdpa);
707 module_init(vdpasim_dev_init)
708 module_exit(vdpasim_dev_exit)
710 MODULE_VERSION(DRV_VERSION);
711 MODULE_LICENSE(DRV_LICENSE);
712 MODULE_AUTHOR(DRV_AUTHOR);
713 MODULE_DESCRIPTION(DRV_DESC);