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>
23 #define DRV_VERSION "0.1"
24 #define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>"
25 #define DRV_DESC "vDPA Device Simulator core"
26 #define DRV_LICENSE "GPL v2"
28 static int batch_mapping = 1;
29 module_param(batch_mapping, int, 0444);
30 MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");
32 static int max_iotlb_entries = 2048;
33 module_param(max_iotlb_entries, int, 0444);
34 MODULE_PARM_DESC(max_iotlb_entries,
35 "Maximum number of iotlb entries. 0 means unlimited. (default: 2048)");
37 #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
38 #define VDPASIM_QUEUE_MAX 256
39 #define VDPASIM_VENDOR_ID 0
41 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
43 return container_of(vdpa, struct vdpasim, vdpa);
46 static struct vdpasim *dev_to_sim(struct device *dev)
48 struct vdpa_device *vdpa = dev_to_vdpa(dev);
50 return vdpa_to_sim(vdpa);
53 static void vdpasim_vq_notify(struct vringh *vring)
55 struct vdpasim_virtqueue *vq =
56 container_of(vring, struct vdpasim_virtqueue, vring);
64 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
66 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
68 vringh_init_iotlb(&vq->vring, vdpasim->dev_attr.supported_features,
69 VDPASIM_QUEUE_MAX, false,
70 (struct vring_desc *)(uintptr_t)vq->desc_addr,
71 (struct vring_avail *)
72 (uintptr_t)vq->driver_addr,
74 (uintptr_t)vq->device_addr);
76 vq->vring.notify = vdpasim_vq_notify;
79 static void vdpasim_vq_reset(struct vdpasim *vdpasim,
80 struct vdpasim_virtqueue *vq)
88 vringh_init_iotlb(&vq->vring, vdpasim->dev_attr.supported_features,
89 VDPASIM_QUEUE_MAX, false, NULL, NULL, NULL);
91 vq->vring.notify = NULL;
94 static void vdpasim_reset(struct vdpasim *vdpasim)
98 for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
99 vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
101 spin_lock(&vdpasim->iommu_lock);
102 vhost_iotlb_reset(vdpasim->iommu);
103 spin_unlock(&vdpasim->iommu_lock);
105 vdpasim->features = 0;
107 ++vdpasim->generation;
110 static int dir_to_perm(enum dma_data_direction dir)
115 case DMA_FROM_DEVICE:
121 case DMA_BIDIRECTIONAL:
131 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
132 unsigned long offset, size_t size,
133 enum dma_data_direction dir,
136 struct vdpasim *vdpasim = dev_to_sim(dev);
137 struct vhost_iotlb *iommu = vdpasim->iommu;
138 u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
139 int ret, perm = dir_to_perm(dir);
142 return DMA_MAPPING_ERROR;
144 /* For simplicity, use identical mapping to avoid e.g iova
147 spin_lock(&vdpasim->iommu_lock);
148 ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
149 pa, dir_to_perm(dir));
150 spin_unlock(&vdpasim->iommu_lock);
152 return DMA_MAPPING_ERROR;
154 return (dma_addr_t)(pa);
157 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
158 size_t size, enum dma_data_direction dir,
161 struct vdpasim *vdpasim = dev_to_sim(dev);
162 struct vhost_iotlb *iommu = vdpasim->iommu;
164 spin_lock(&vdpasim->iommu_lock);
165 vhost_iotlb_del_range(iommu, (u64)dma_addr,
166 (u64)dma_addr + size - 1);
167 spin_unlock(&vdpasim->iommu_lock);
170 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
171 dma_addr_t *dma_addr, gfp_t flag,
174 struct vdpasim *vdpasim = dev_to_sim(dev);
175 struct vhost_iotlb *iommu = vdpasim->iommu;
176 void *addr = kmalloc(size, flag);
179 spin_lock(&vdpasim->iommu_lock);
181 *dma_addr = DMA_MAPPING_ERROR;
183 u64 pa = virt_to_phys(addr);
185 ret = vhost_iotlb_add_range(iommu, (u64)pa,
189 *dma_addr = DMA_MAPPING_ERROR;
193 *dma_addr = (dma_addr_t)pa;
195 spin_unlock(&vdpasim->iommu_lock);
200 static void vdpasim_free_coherent(struct device *dev, size_t size,
201 void *vaddr, dma_addr_t dma_addr,
204 struct vdpasim *vdpasim = dev_to_sim(dev);
205 struct vhost_iotlb *iommu = vdpasim->iommu;
207 spin_lock(&vdpasim->iommu_lock);
208 vhost_iotlb_del_range(iommu, (u64)dma_addr,
209 (u64)dma_addr + size - 1);
210 spin_unlock(&vdpasim->iommu_lock);
212 kfree(phys_to_virt((uintptr_t)dma_addr));
215 static const struct dma_map_ops vdpasim_dma_ops = {
216 .map_page = vdpasim_map_page,
217 .unmap_page = vdpasim_unmap_page,
218 .alloc = vdpasim_alloc_coherent,
219 .free = vdpasim_free_coherent,
222 static const struct vdpa_config_ops vdpasim_config_ops;
223 static const struct vdpa_config_ops vdpasim_batch_config_ops;
225 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
227 const struct vdpa_config_ops *ops;
228 struct vdpasim *vdpasim;
230 int i, ret = -ENOMEM;
233 ops = &vdpasim_batch_config_ops;
235 ops = &vdpasim_config_ops;
237 vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
242 vdpasim->dev_attr = *dev_attr;
243 INIT_WORK(&vdpasim->work, dev_attr->work_fn);
244 spin_lock_init(&vdpasim->lock);
245 spin_lock_init(&vdpasim->iommu_lock);
247 dev = &vdpasim->vdpa.dev;
248 dev->dma_mask = &dev->coherent_dma_mask;
249 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
251 set_dma_ops(dev, &vdpasim_dma_ops);
252 vdpasim->vdpa.mdev = dev_attr->mgmt_dev;
254 vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
255 if (!vdpasim->config)
258 vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
263 vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);
267 vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
268 if (!vdpasim->buffer)
271 for (i = 0; i < dev_attr->nvqs; i++)
272 vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu);
274 vdpasim->vdpa.dma_dev = dev;
283 EXPORT_SYMBOL_GPL(vdpasim_create);
285 static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
286 u64 desc_area, u64 driver_area,
289 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
290 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
292 vq->desc_addr = desc_area;
293 vq->driver_addr = driver_area;
294 vq->device_addr = device_area;
299 static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
301 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
302 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
307 static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
309 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
310 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
313 schedule_work(&vdpasim->work);
316 static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
317 struct vdpa_callback *cb)
319 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
320 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
322 vq->cb = cb->callback;
323 vq->private = cb->private;
326 static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
328 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
329 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
331 spin_lock(&vdpasim->lock);
334 vdpasim_queue_ready(vdpasim, idx);
335 spin_unlock(&vdpasim->lock);
338 static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
340 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
341 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
346 static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
347 const struct vdpa_vq_state *state)
349 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
350 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
351 struct vringh *vrh = &vq->vring;
353 spin_lock(&vdpasim->lock);
354 vrh->last_avail_idx = state->avail_index;
355 spin_unlock(&vdpasim->lock);
360 static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
361 struct vdpa_vq_state *state)
363 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
364 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
365 struct vringh *vrh = &vq->vring;
367 state->avail_index = vrh->last_avail_idx;
371 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
373 return VDPASIM_QUEUE_ALIGN;
376 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
378 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
380 return vdpasim->dev_attr.supported_features;
383 static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
385 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
387 /* DMA mapping must be done by driver */
388 if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
391 vdpasim->features = features & vdpasim->dev_attr.supported_features;
396 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
397 struct vdpa_callback *cb)
399 /* We don't support config interrupt */
402 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
404 return VDPASIM_QUEUE_MAX;
407 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
409 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
411 return vdpasim->dev_attr.id;
414 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
416 return VDPASIM_VENDOR_ID;
419 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
421 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
424 spin_lock(&vdpasim->lock);
425 status = vdpasim->status;
426 spin_unlock(&vdpasim->lock);
431 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
433 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
435 spin_lock(&vdpasim->lock);
436 vdpasim->status = status;
438 vdpasim_reset(vdpasim);
439 spin_unlock(&vdpasim->lock);
442 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
443 void *buf, unsigned int len)
445 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
447 if (offset + len > vdpasim->dev_attr.config_size)
450 if (vdpasim->dev_attr.get_config)
451 vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
453 memcpy(buf, vdpasim->config + offset, len);
456 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
457 const void *buf, unsigned int len)
459 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
461 if (offset + len > vdpasim->dev_attr.config_size)
464 memcpy(vdpasim->config + offset, buf, len);
466 if (vdpasim->dev_attr.set_config)
467 vdpasim->dev_attr.set_config(vdpasim, vdpasim->config);
470 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
472 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
474 return vdpasim->generation;
477 static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
479 struct vdpa_iova_range range = {
487 static int vdpasim_set_map(struct vdpa_device *vdpa,
488 struct vhost_iotlb *iotlb)
490 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
491 struct vhost_iotlb_map *map;
492 u64 start = 0ULL, last = 0ULL - 1;
495 spin_lock(&vdpasim->iommu_lock);
496 vhost_iotlb_reset(vdpasim->iommu);
498 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
499 map = vhost_iotlb_itree_next(map, start, last)) {
500 ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
501 map->last, map->addr, map->perm);
505 spin_unlock(&vdpasim->iommu_lock);
509 vhost_iotlb_reset(vdpasim->iommu);
510 spin_unlock(&vdpasim->iommu_lock);
514 static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
517 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
520 spin_lock(&vdpasim->iommu_lock);
521 ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
523 spin_unlock(&vdpasim->iommu_lock);
528 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
530 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
532 spin_lock(&vdpasim->iommu_lock);
533 vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
534 spin_unlock(&vdpasim->iommu_lock);
539 static void vdpasim_free(struct vdpa_device *vdpa)
541 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
543 cancel_work_sync(&vdpasim->work);
544 kvfree(vdpasim->buffer);
546 vhost_iotlb_free(vdpasim->iommu);
548 kfree(vdpasim->config);
551 static const struct vdpa_config_ops vdpasim_config_ops = {
552 .set_vq_address = vdpasim_set_vq_address,
553 .set_vq_num = vdpasim_set_vq_num,
554 .kick_vq = vdpasim_kick_vq,
555 .set_vq_cb = vdpasim_set_vq_cb,
556 .set_vq_ready = vdpasim_set_vq_ready,
557 .get_vq_ready = vdpasim_get_vq_ready,
558 .set_vq_state = vdpasim_set_vq_state,
559 .get_vq_state = vdpasim_get_vq_state,
560 .get_vq_align = vdpasim_get_vq_align,
561 .get_features = vdpasim_get_features,
562 .set_features = vdpasim_set_features,
563 .set_config_cb = vdpasim_set_config_cb,
564 .get_vq_num_max = vdpasim_get_vq_num_max,
565 .get_device_id = vdpasim_get_device_id,
566 .get_vendor_id = vdpasim_get_vendor_id,
567 .get_status = vdpasim_get_status,
568 .set_status = vdpasim_set_status,
569 .get_config = vdpasim_get_config,
570 .set_config = vdpasim_set_config,
571 .get_generation = vdpasim_get_generation,
572 .get_iova_range = vdpasim_get_iova_range,
573 .dma_map = vdpasim_dma_map,
574 .dma_unmap = vdpasim_dma_unmap,
575 .free = vdpasim_free,
578 static const struct vdpa_config_ops vdpasim_batch_config_ops = {
579 .set_vq_address = vdpasim_set_vq_address,
580 .set_vq_num = vdpasim_set_vq_num,
581 .kick_vq = vdpasim_kick_vq,
582 .set_vq_cb = vdpasim_set_vq_cb,
583 .set_vq_ready = vdpasim_set_vq_ready,
584 .get_vq_ready = vdpasim_get_vq_ready,
585 .set_vq_state = vdpasim_set_vq_state,
586 .get_vq_state = vdpasim_get_vq_state,
587 .get_vq_align = vdpasim_get_vq_align,
588 .get_features = vdpasim_get_features,
589 .set_features = vdpasim_set_features,
590 .set_config_cb = vdpasim_set_config_cb,
591 .get_vq_num_max = vdpasim_get_vq_num_max,
592 .get_device_id = vdpasim_get_device_id,
593 .get_vendor_id = vdpasim_get_vendor_id,
594 .get_status = vdpasim_get_status,
595 .set_status = vdpasim_set_status,
596 .get_config = vdpasim_get_config,
597 .set_config = vdpasim_set_config,
598 .get_generation = vdpasim_get_generation,
599 .get_iova_range = vdpasim_get_iova_range,
600 .set_map = vdpasim_set_map,
601 .free = vdpasim_free,
604 MODULE_VERSION(DRV_VERSION);
605 MODULE_LICENSE(DRV_LICENSE);
606 MODULE_AUTHOR(DRV_AUTHOR);
607 MODULE_DESCRIPTION(DRV_DESC);