1 // SPDX-License-Identifier: GPL-2.0-only
3 * VDPA device simulator core.
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>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/dma-map-ops.h>
17 #include <linux/vringh.h>
18 #include <linux/vdpa.h>
19 #include <linux/vhost_iotlb.h>
20 #include <linux/iova.h>
24 #define DRV_VERSION "0.1"
25 #define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>"
26 #define DRV_DESC "vDPA Device Simulator core"
27 #define DRV_LICENSE "GPL v2"
29 static int batch_mapping = 1;
30 module_param(batch_mapping, int, 0444);
31 MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");
33 static int max_iotlb_entries = 2048;
34 module_param(max_iotlb_entries, int, 0444);
35 MODULE_PARM_DESC(max_iotlb_entries,
36 "Maximum number of iotlb entries. 0 means unlimited. (default: 2048)");
38 #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
39 #define VDPASIM_QUEUE_MAX 256
40 #define VDPASIM_VENDOR_ID 0
42 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
44 return container_of(vdpa, struct vdpasim, vdpa);
47 static struct vdpasim *dev_to_sim(struct device *dev)
49 struct vdpa_device *vdpa = dev_to_vdpa(dev);
51 return vdpa_to_sim(vdpa);
54 static void vdpasim_vq_notify(struct vringh *vring)
56 struct vdpasim_virtqueue *vq =
57 container_of(vring, struct vdpasim_virtqueue, vring);
65 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
67 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
69 vringh_init_iotlb(&vq->vring, vdpasim->dev_attr.supported_features,
70 VDPASIM_QUEUE_MAX, false,
71 (struct vring_desc *)(uintptr_t)vq->desc_addr,
72 (struct vring_avail *)
73 (uintptr_t)vq->driver_addr,
75 (uintptr_t)vq->device_addr);
77 vq->vring.notify = vdpasim_vq_notify;
80 static void vdpasim_vq_reset(struct vdpasim *vdpasim,
81 struct vdpasim_virtqueue *vq)
89 vringh_init_iotlb(&vq->vring, vdpasim->dev_attr.supported_features,
90 VDPASIM_QUEUE_MAX, false, NULL, NULL, NULL);
92 vq->vring.notify = NULL;
95 static void vdpasim_do_reset(struct vdpasim *vdpasim)
99 for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
100 vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
102 spin_lock(&vdpasim->iommu_lock);
103 vhost_iotlb_reset(vdpasim->iommu);
104 spin_unlock(&vdpasim->iommu_lock);
106 vdpasim->features = 0;
108 ++vdpasim->generation;
111 static int dir_to_perm(enum dma_data_direction dir)
116 case DMA_FROM_DEVICE:
122 case DMA_BIDIRECTIONAL:
132 static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,
133 size_t size, unsigned int perm)
139 /* We set the limit_pfn to the maximum (ULONG_MAX - 1) */
140 iova = alloc_iova(&vdpasim->iova, size >> iova_shift(&vdpasim->iova),
141 ULONG_MAX - 1, true);
143 return DMA_MAPPING_ERROR;
145 dma_addr = iova_dma_addr(&vdpasim->iova, iova);
147 spin_lock(&vdpasim->iommu_lock);
148 ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,
149 (u64)dma_addr + size - 1, (u64)paddr, perm);
150 spin_unlock(&vdpasim->iommu_lock);
153 __free_iova(&vdpasim->iova, iova);
154 return DMA_MAPPING_ERROR;
160 static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
163 spin_lock(&vdpasim->iommu_lock);
164 vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,
165 (u64)dma_addr + size - 1);
166 spin_unlock(&vdpasim->iommu_lock);
168 free_iova(&vdpasim->iova, iova_pfn(&vdpasim->iova, dma_addr));
171 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
172 unsigned long offset, size_t size,
173 enum dma_data_direction dir,
176 struct vdpasim *vdpasim = dev_to_sim(dev);
177 phys_addr_t paddr = page_to_phys(page) + offset;
178 int perm = dir_to_perm(dir);
181 return DMA_MAPPING_ERROR;
183 return vdpasim_map_range(vdpasim, paddr, size, perm);
186 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
187 size_t size, enum dma_data_direction dir,
190 struct vdpasim *vdpasim = dev_to_sim(dev);
192 vdpasim_unmap_range(vdpasim, dma_addr, size);
195 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
196 dma_addr_t *dma_addr, gfp_t flag,
199 struct vdpasim *vdpasim = dev_to_sim(dev);
203 addr = kmalloc(size, flag);
205 *dma_addr = DMA_MAPPING_ERROR;
209 paddr = virt_to_phys(addr);
211 *dma_addr = vdpasim_map_range(vdpasim, paddr, size, VHOST_MAP_RW);
212 if (*dma_addr == DMA_MAPPING_ERROR) {
220 static void vdpasim_free_coherent(struct device *dev, size_t size,
221 void *vaddr, dma_addr_t dma_addr,
224 struct vdpasim *vdpasim = dev_to_sim(dev);
226 vdpasim_unmap_range(vdpasim, dma_addr, size);
231 static const struct dma_map_ops vdpasim_dma_ops = {
232 .map_page = vdpasim_map_page,
233 .unmap_page = vdpasim_unmap_page,
234 .alloc = vdpasim_alloc_coherent,
235 .free = vdpasim_free_coherent,
238 static const struct vdpa_config_ops vdpasim_config_ops;
239 static const struct vdpa_config_ops vdpasim_batch_config_ops;
241 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
243 const struct vdpa_config_ops *ops;
244 struct vdpasim *vdpasim;
246 int i, ret = -ENOMEM;
249 ops = &vdpasim_batch_config_ops;
251 ops = &vdpasim_config_ops;
253 vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
254 dev_attr->name, false);
255 if (IS_ERR(vdpasim)) {
256 ret = PTR_ERR(vdpasim);
260 vdpasim->dev_attr = *dev_attr;
261 INIT_WORK(&vdpasim->work, dev_attr->work_fn);
262 spin_lock_init(&vdpasim->lock);
263 spin_lock_init(&vdpasim->iommu_lock);
265 dev = &vdpasim->vdpa.dev;
266 dev->dma_mask = &dev->coherent_dma_mask;
267 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
269 set_dma_ops(dev, &vdpasim_dma_ops);
270 vdpasim->vdpa.mdev = dev_attr->mgmt_dev;
272 vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
273 if (!vdpasim->config)
276 vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
281 vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);
285 vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
286 if (!vdpasim->buffer)
289 for (i = 0; i < dev_attr->nvqs; i++)
290 vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu,
291 &vdpasim->iommu_lock);
293 ret = iova_cache_get();
297 /* For simplicity we use an IOVA allocator with byte granularity */
298 init_iova_domain(&vdpasim->iova, 1, 0);
300 vdpasim->vdpa.dma_dev = dev;
309 EXPORT_SYMBOL_GPL(vdpasim_create);
311 static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
312 u64 desc_area, u64 driver_area,
315 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
316 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
318 vq->desc_addr = desc_area;
319 vq->driver_addr = driver_area;
320 vq->device_addr = device_area;
325 static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
327 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
328 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
333 static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
335 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
336 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
339 schedule_work(&vdpasim->work);
342 static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
343 struct vdpa_callback *cb)
345 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
346 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
348 vq->cb = cb->callback;
349 vq->private = cb->private;
352 static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
354 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
355 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
358 spin_lock(&vdpasim->lock);
359 old_ready = vq->ready;
361 if (vq->ready && !old_ready) {
362 vdpasim_queue_ready(vdpasim, idx);
364 spin_unlock(&vdpasim->lock);
367 static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
369 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
370 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
375 static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
376 const struct vdpa_vq_state *state)
378 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
379 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
380 struct vringh *vrh = &vq->vring;
382 spin_lock(&vdpasim->lock);
383 vrh->last_avail_idx = state->split.avail_index;
384 spin_unlock(&vdpasim->lock);
389 static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
390 struct vdpa_vq_state *state)
392 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
393 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
394 struct vringh *vrh = &vq->vring;
396 state->split.avail_index = vrh->last_avail_idx;
400 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
402 return VDPASIM_QUEUE_ALIGN;
405 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
407 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
409 return vdpasim->dev_attr.supported_features;
412 static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
414 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
416 /* DMA mapping must be done by driver */
417 if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
420 vdpasim->features = features & vdpasim->dev_attr.supported_features;
425 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
426 struct vdpa_callback *cb)
428 /* We don't support config interrupt */
431 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
433 return VDPASIM_QUEUE_MAX;
436 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
438 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
440 return vdpasim->dev_attr.id;
443 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
445 return VDPASIM_VENDOR_ID;
448 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
450 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
453 spin_lock(&vdpasim->lock);
454 status = vdpasim->status;
455 spin_unlock(&vdpasim->lock);
460 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
462 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
464 spin_lock(&vdpasim->lock);
465 vdpasim->status = status;
466 spin_unlock(&vdpasim->lock);
469 static int vdpasim_reset(struct vdpa_device *vdpa)
471 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
473 spin_lock(&vdpasim->lock);
475 vdpasim_do_reset(vdpasim);
476 spin_unlock(&vdpasim->lock);
481 static size_t vdpasim_get_config_size(struct vdpa_device *vdpa)
483 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
485 return vdpasim->dev_attr.config_size;
488 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
489 void *buf, unsigned int len)
491 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
493 if (offset + len > vdpasim->dev_attr.config_size)
496 if (vdpasim->dev_attr.get_config)
497 vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
499 memcpy(buf, vdpasim->config + offset, len);
502 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
503 const void *buf, unsigned int len)
505 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
507 if (offset + len > vdpasim->dev_attr.config_size)
510 memcpy(vdpasim->config + offset, buf, len);
512 if (vdpasim->dev_attr.set_config)
513 vdpasim->dev_attr.set_config(vdpasim, vdpasim->config);
516 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
518 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
520 return vdpasim->generation;
523 static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
525 struct vdpa_iova_range range = {
533 static int vdpasim_set_map(struct vdpa_device *vdpa,
534 struct vhost_iotlb *iotlb)
536 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
537 struct vhost_iotlb_map *map;
538 u64 start = 0ULL, last = 0ULL - 1;
541 spin_lock(&vdpasim->iommu_lock);
542 vhost_iotlb_reset(vdpasim->iommu);
544 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
545 map = vhost_iotlb_itree_next(map, start, last)) {
546 ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
547 map->last, map->addr, map->perm);
551 spin_unlock(&vdpasim->iommu_lock);
555 vhost_iotlb_reset(vdpasim->iommu);
556 spin_unlock(&vdpasim->iommu_lock);
560 static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
561 u64 pa, u32 perm, void *opaque)
563 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
566 spin_lock(&vdpasim->iommu_lock);
567 ret = vhost_iotlb_add_range_ctx(vdpasim->iommu, iova, iova + size - 1,
569 spin_unlock(&vdpasim->iommu_lock);
574 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
576 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
578 spin_lock(&vdpasim->iommu_lock);
579 vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
580 spin_unlock(&vdpasim->iommu_lock);
585 static void vdpasim_free(struct vdpa_device *vdpa)
587 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
590 cancel_work_sync(&vdpasim->work);
592 for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
593 vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov);
594 vringh_kiov_cleanup(&vdpasim->vqs[i].in_iov);
597 if (vdpa_get_dma_dev(vdpa)) {
598 put_iova_domain(&vdpasim->iova);
602 kvfree(vdpasim->buffer);
604 vhost_iotlb_free(vdpasim->iommu);
606 kfree(vdpasim->config);
609 static const struct vdpa_config_ops vdpasim_config_ops = {
610 .set_vq_address = vdpasim_set_vq_address,
611 .set_vq_num = vdpasim_set_vq_num,
612 .kick_vq = vdpasim_kick_vq,
613 .set_vq_cb = vdpasim_set_vq_cb,
614 .set_vq_ready = vdpasim_set_vq_ready,
615 .get_vq_ready = vdpasim_get_vq_ready,
616 .set_vq_state = vdpasim_set_vq_state,
617 .get_vq_state = vdpasim_get_vq_state,
618 .get_vq_align = vdpasim_get_vq_align,
619 .get_features = vdpasim_get_features,
620 .set_features = vdpasim_set_features,
621 .set_config_cb = vdpasim_set_config_cb,
622 .get_vq_num_max = vdpasim_get_vq_num_max,
623 .get_device_id = vdpasim_get_device_id,
624 .get_vendor_id = vdpasim_get_vendor_id,
625 .get_status = vdpasim_get_status,
626 .set_status = vdpasim_set_status,
627 .reset = vdpasim_reset,
628 .get_config_size = vdpasim_get_config_size,
629 .get_config = vdpasim_get_config,
630 .set_config = vdpasim_set_config,
631 .get_generation = vdpasim_get_generation,
632 .get_iova_range = vdpasim_get_iova_range,
633 .dma_map = vdpasim_dma_map,
634 .dma_unmap = vdpasim_dma_unmap,
635 .free = vdpasim_free,
638 static const struct vdpa_config_ops vdpasim_batch_config_ops = {
639 .set_vq_address = vdpasim_set_vq_address,
640 .set_vq_num = vdpasim_set_vq_num,
641 .kick_vq = vdpasim_kick_vq,
642 .set_vq_cb = vdpasim_set_vq_cb,
643 .set_vq_ready = vdpasim_set_vq_ready,
644 .get_vq_ready = vdpasim_get_vq_ready,
645 .set_vq_state = vdpasim_set_vq_state,
646 .get_vq_state = vdpasim_get_vq_state,
647 .get_vq_align = vdpasim_get_vq_align,
648 .get_features = vdpasim_get_features,
649 .set_features = vdpasim_set_features,
650 .set_config_cb = vdpasim_set_config_cb,
651 .get_vq_num_max = vdpasim_get_vq_num_max,
652 .get_device_id = vdpasim_get_device_id,
653 .get_vendor_id = vdpasim_get_vendor_id,
654 .get_status = vdpasim_get_status,
655 .set_status = vdpasim_set_status,
656 .reset = vdpasim_reset,
657 .get_config_size = vdpasim_get_config_size,
658 .get_config = vdpasim_get_config,
659 .set_config = vdpasim_set_config,
660 .get_generation = vdpasim_get_generation,
661 .get_iova_range = vdpasim_get_iova_range,
662 .set_map = vdpasim_set_map,
663 .free = vdpasim_free,
666 MODULE_VERSION(DRV_VERSION);
667 MODULE_LICENSE(DRV_LICENSE);
668 MODULE_AUTHOR(DRV_AUTHOR);
669 MODULE_DESCRIPTION(DRV_DESC);