xref: /openbmc/linux/drivers/vdpa/vdpa_sim/vdpa_sim.c (revision 801543b2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VDPA device simulator core.
4  *
5  * Copyright (c) 2020, Red Hat Inc. All rights reserved.
6  *     Author: Jason Wang <jasowang@redhat.com>
7  *
8  */
9 
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>
21 
22 #include "vdpa_sim.h"
23 
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"
28 
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");
32 
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 for each address space. 0 means unlimited. (default: 2048)");
37 
38 #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
39 #define VDPASIM_QUEUE_MAX 256
40 #define VDPASIM_VENDOR_ID 0
41 
42 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
43 {
44 	return container_of(vdpa, struct vdpasim, vdpa);
45 }
46 
47 static struct vdpasim *dev_to_sim(struct device *dev)
48 {
49 	struct vdpa_device *vdpa = dev_to_vdpa(dev);
50 
51 	return vdpa_to_sim(vdpa);
52 }
53 
54 static void vdpasim_vq_notify(struct vringh *vring)
55 {
56 	struct vdpasim_virtqueue *vq =
57 		container_of(vring, struct vdpasim_virtqueue, vring);
58 
59 	if (!vq->cb)
60 		return;
61 
62 	vq->cb(vq->private);
63 }
64 
65 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
66 {
67 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
68 
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,
74 			  (struct vring_used *)
75 			  (uintptr_t)vq->device_addr);
76 
77 	vq->vring.notify = vdpasim_vq_notify;
78 }
79 
80 static void vdpasim_vq_reset(struct vdpasim *vdpasim,
81 			     struct vdpasim_virtqueue *vq)
82 {
83 	vq->ready = false;
84 	vq->desc_addr = 0;
85 	vq->driver_addr = 0;
86 	vq->device_addr = 0;
87 	vq->cb = NULL;
88 	vq->private = NULL;
89 	vringh_init_iotlb(&vq->vring, vdpasim->dev_attr.supported_features,
90 			  VDPASIM_QUEUE_MAX, false, NULL, NULL, NULL);
91 
92 	vq->vring.notify = NULL;
93 }
94 
95 static void vdpasim_do_reset(struct vdpasim *vdpasim)
96 {
97 	int i;
98 
99 	spin_lock(&vdpasim->iommu_lock);
100 
101 	for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
102 		vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
103 		vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
104 				 &vdpasim->iommu_lock);
105 	}
106 
107 	for (i = 0; i < vdpasim->dev_attr.nas; i++)
108 		vhost_iotlb_reset(&vdpasim->iommu[i]);
109 
110 	vdpasim->running = true;
111 	spin_unlock(&vdpasim->iommu_lock);
112 
113 	vdpasim->features = 0;
114 	vdpasim->status = 0;
115 	++vdpasim->generation;
116 }
117 
118 static int dir_to_perm(enum dma_data_direction dir)
119 {
120 	int perm = -EFAULT;
121 
122 	switch (dir) {
123 	case DMA_FROM_DEVICE:
124 		perm = VHOST_MAP_WO;
125 		break;
126 	case DMA_TO_DEVICE:
127 		perm = VHOST_MAP_RO;
128 		break;
129 	case DMA_BIDIRECTIONAL:
130 		perm = VHOST_MAP_RW;
131 		break;
132 	default:
133 		break;
134 	}
135 
136 	return perm;
137 }
138 
139 static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,
140 				    size_t size, unsigned int perm)
141 {
142 	struct iova *iova;
143 	dma_addr_t dma_addr;
144 	int ret;
145 
146 	/* We set the limit_pfn to the maximum (ULONG_MAX - 1) */
147 	iova = alloc_iova(&vdpasim->iova, size >> iova_shift(&vdpasim->iova),
148 			  ULONG_MAX - 1, true);
149 	if (!iova)
150 		return DMA_MAPPING_ERROR;
151 
152 	dma_addr = iova_dma_addr(&vdpasim->iova, iova);
153 
154 	spin_lock(&vdpasim->iommu_lock);
155 	ret = vhost_iotlb_add_range(&vdpasim->iommu[0], (u64)dma_addr,
156 				    (u64)dma_addr + size - 1, (u64)paddr, perm);
157 	spin_unlock(&vdpasim->iommu_lock);
158 
159 	if (ret) {
160 		__free_iova(&vdpasim->iova, iova);
161 		return DMA_MAPPING_ERROR;
162 	}
163 
164 	return dma_addr;
165 }
166 
167 static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
168 				size_t size)
169 {
170 	spin_lock(&vdpasim->iommu_lock);
171 	vhost_iotlb_del_range(&vdpasim->iommu[0], (u64)dma_addr,
172 			      (u64)dma_addr + size - 1);
173 	spin_unlock(&vdpasim->iommu_lock);
174 
175 	free_iova(&vdpasim->iova, iova_pfn(&vdpasim->iova, dma_addr));
176 }
177 
178 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
179 				   unsigned long offset, size_t size,
180 				   enum dma_data_direction dir,
181 				   unsigned long attrs)
182 {
183 	struct vdpasim *vdpasim = dev_to_sim(dev);
184 	phys_addr_t paddr = page_to_phys(page) + offset;
185 	int perm = dir_to_perm(dir);
186 
187 	if (perm < 0)
188 		return DMA_MAPPING_ERROR;
189 
190 	return vdpasim_map_range(vdpasim, paddr, size, perm);
191 }
192 
193 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
194 			       size_t size, enum dma_data_direction dir,
195 			       unsigned long attrs)
196 {
197 	struct vdpasim *vdpasim = dev_to_sim(dev);
198 
199 	vdpasim_unmap_range(vdpasim, dma_addr, size);
200 }
201 
202 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
203 				    dma_addr_t *dma_addr, gfp_t flag,
204 				    unsigned long attrs)
205 {
206 	struct vdpasim *vdpasim = dev_to_sim(dev);
207 	phys_addr_t paddr;
208 	void *addr;
209 
210 	addr = kmalloc(size, flag);
211 	if (!addr) {
212 		*dma_addr = DMA_MAPPING_ERROR;
213 		return NULL;
214 	}
215 
216 	paddr = virt_to_phys(addr);
217 
218 	*dma_addr = vdpasim_map_range(vdpasim, paddr, size, VHOST_MAP_RW);
219 	if (*dma_addr == DMA_MAPPING_ERROR) {
220 		kfree(addr);
221 		return NULL;
222 	}
223 
224 	return addr;
225 }
226 
227 static void vdpasim_free_coherent(struct device *dev, size_t size,
228 				  void *vaddr, dma_addr_t dma_addr,
229 				  unsigned long attrs)
230 {
231 	struct vdpasim *vdpasim = dev_to_sim(dev);
232 
233 	vdpasim_unmap_range(vdpasim, dma_addr, size);
234 
235 	kfree(vaddr);
236 }
237 
238 static const struct dma_map_ops vdpasim_dma_ops = {
239 	.map_page = vdpasim_map_page,
240 	.unmap_page = vdpasim_unmap_page,
241 	.alloc = vdpasim_alloc_coherent,
242 	.free = vdpasim_free_coherent,
243 };
244 
245 static const struct vdpa_config_ops vdpasim_config_ops;
246 static const struct vdpa_config_ops vdpasim_batch_config_ops;
247 
248 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
249 {
250 	const struct vdpa_config_ops *ops;
251 	struct vdpasim *vdpasim;
252 	struct device *dev;
253 	int i, ret = -ENOMEM;
254 
255 	if (batch_mapping)
256 		ops = &vdpasim_batch_config_ops;
257 	else
258 		ops = &vdpasim_config_ops;
259 
260 	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
261 				    dev_attr->ngroups, dev_attr->nas,
262 				    dev_attr->name, false);
263 	if (IS_ERR(vdpasim)) {
264 		ret = PTR_ERR(vdpasim);
265 		goto err_alloc;
266 	}
267 
268 	vdpasim->dev_attr = *dev_attr;
269 	INIT_WORK(&vdpasim->work, dev_attr->work_fn);
270 	spin_lock_init(&vdpasim->lock);
271 	spin_lock_init(&vdpasim->iommu_lock);
272 
273 	dev = &vdpasim->vdpa.dev;
274 	dev->dma_mask = &dev->coherent_dma_mask;
275 	if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
276 		goto err_iommu;
277 	set_dma_ops(dev, &vdpasim_dma_ops);
278 	vdpasim->vdpa.mdev = dev_attr->mgmt_dev;
279 
280 	vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
281 	if (!vdpasim->config)
282 		goto err_iommu;
283 
284 	vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
285 			       GFP_KERNEL);
286 	if (!vdpasim->vqs)
287 		goto err_iommu;
288 
289 	vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas,
290 				       sizeof(*vdpasim->iommu), GFP_KERNEL);
291 	if (!vdpasim->iommu)
292 		goto err_iommu;
293 
294 	for (i = 0; i < vdpasim->dev_attr.nas; i++)
295 		vhost_iotlb_init(&vdpasim->iommu[i], max_iotlb_entries, 0);
296 
297 	vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
298 	if (!vdpasim->buffer)
299 		goto err_iommu;
300 
301 	for (i = 0; i < dev_attr->nvqs; i++)
302 		vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
303 				 &vdpasim->iommu_lock);
304 
305 	ret = iova_cache_get();
306 	if (ret)
307 		goto err_iommu;
308 
309 	/* For simplicity we use an IOVA allocator with byte granularity */
310 	init_iova_domain(&vdpasim->iova, 1, 0);
311 
312 	vdpasim->vdpa.dma_dev = dev;
313 
314 	return vdpasim;
315 
316 err_iommu:
317 	put_device(dev);
318 err_alloc:
319 	return ERR_PTR(ret);
320 }
321 EXPORT_SYMBOL_GPL(vdpasim_create);
322 
323 static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
324 				  u64 desc_area, u64 driver_area,
325 				  u64 device_area)
326 {
327 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
328 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
329 
330 	vq->desc_addr = desc_area;
331 	vq->driver_addr = driver_area;
332 	vq->device_addr = device_area;
333 
334 	return 0;
335 }
336 
337 static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
338 {
339 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
340 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
341 
342 	vq->num = num;
343 }
344 
345 static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
346 {
347 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
348 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
349 
350 	if (vq->ready)
351 		schedule_work(&vdpasim->work);
352 }
353 
354 static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
355 			      struct vdpa_callback *cb)
356 {
357 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
358 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
359 
360 	vq->cb = cb->callback;
361 	vq->private = cb->private;
362 }
363 
364 static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
365 {
366 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
367 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
368 	bool old_ready;
369 
370 	spin_lock(&vdpasim->lock);
371 	old_ready = vq->ready;
372 	vq->ready = ready;
373 	if (vq->ready && !old_ready) {
374 		vdpasim_queue_ready(vdpasim, idx);
375 	}
376 	spin_unlock(&vdpasim->lock);
377 }
378 
379 static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
380 {
381 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
382 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
383 
384 	return vq->ready;
385 }
386 
387 static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
388 				const struct vdpa_vq_state *state)
389 {
390 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
391 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
392 	struct vringh *vrh = &vq->vring;
393 
394 	spin_lock(&vdpasim->lock);
395 	vrh->last_avail_idx = state->split.avail_index;
396 	spin_unlock(&vdpasim->lock);
397 
398 	return 0;
399 }
400 
401 static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
402 				struct vdpa_vq_state *state)
403 {
404 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
405 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
406 	struct vringh *vrh = &vq->vring;
407 
408 	state->split.avail_index = vrh->last_avail_idx;
409 	return 0;
410 }
411 
412 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
413 {
414 	return VDPASIM_QUEUE_ALIGN;
415 }
416 
417 static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
418 {
419 	/* RX and TX belongs to group 0, CVQ belongs to group 1 */
420 	if (idx == 2)
421 		return 1;
422 	else
423 		return 0;
424 }
425 
426 static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
427 {
428 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
429 
430 	return vdpasim->dev_attr.supported_features;
431 }
432 
433 static int vdpasim_set_driver_features(struct vdpa_device *vdpa, u64 features)
434 {
435 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
436 
437 	/* DMA mapping must be done by driver */
438 	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
439 		return -EINVAL;
440 
441 	vdpasim->features = features & vdpasim->dev_attr.supported_features;
442 
443 	return 0;
444 }
445 
446 static u64 vdpasim_get_driver_features(struct vdpa_device *vdpa)
447 {
448 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
449 
450 	return vdpasim->features;
451 }
452 
453 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
454 				  struct vdpa_callback *cb)
455 {
456 	/* We don't support config interrupt */
457 }
458 
459 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
460 {
461 	return VDPASIM_QUEUE_MAX;
462 }
463 
464 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
465 {
466 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
467 
468 	return vdpasim->dev_attr.id;
469 }
470 
471 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
472 {
473 	return VDPASIM_VENDOR_ID;
474 }
475 
476 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
477 {
478 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
479 	u8 status;
480 
481 	spin_lock(&vdpasim->lock);
482 	status = vdpasim->status;
483 	spin_unlock(&vdpasim->lock);
484 
485 	return status;
486 }
487 
488 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
489 {
490 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
491 
492 	spin_lock(&vdpasim->lock);
493 	vdpasim->status = status;
494 	spin_unlock(&vdpasim->lock);
495 }
496 
497 static int vdpasim_reset(struct vdpa_device *vdpa)
498 {
499 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
500 
501 	spin_lock(&vdpasim->lock);
502 	vdpasim->status = 0;
503 	vdpasim_do_reset(vdpasim);
504 	spin_unlock(&vdpasim->lock);
505 
506 	return 0;
507 }
508 
509 static int vdpasim_suspend(struct vdpa_device *vdpa)
510 {
511 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
512 
513 	spin_lock(&vdpasim->lock);
514 	vdpasim->running = false;
515 	spin_unlock(&vdpasim->lock);
516 
517 	return 0;
518 }
519 
520 static size_t vdpasim_get_config_size(struct vdpa_device *vdpa)
521 {
522 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
523 
524 	return vdpasim->dev_attr.config_size;
525 }
526 
527 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
528 			     void *buf, unsigned int len)
529 {
530 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
531 
532 	if (offset + len > vdpasim->dev_attr.config_size)
533 		return;
534 
535 	if (vdpasim->dev_attr.get_config)
536 		vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
537 
538 	memcpy(buf, vdpasim->config + offset, len);
539 }
540 
541 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
542 			     const void *buf, unsigned int len)
543 {
544 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
545 
546 	if (offset + len > vdpasim->dev_attr.config_size)
547 		return;
548 
549 	memcpy(vdpasim->config + offset, buf, len);
550 
551 	if (vdpasim->dev_attr.set_config)
552 		vdpasim->dev_attr.set_config(vdpasim, vdpasim->config);
553 }
554 
555 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
556 {
557 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
558 
559 	return vdpasim->generation;
560 }
561 
562 static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
563 {
564 	struct vdpa_iova_range range = {
565 		.first = 0ULL,
566 		.last = ULLONG_MAX,
567 	};
568 
569 	return range;
570 }
571 
572 static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
573 				  unsigned int asid)
574 {
575 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
576 	struct vhost_iotlb *iommu;
577 	int i;
578 
579 	if (group > vdpasim->dev_attr.ngroups)
580 		return -EINVAL;
581 
582 	if (asid >= vdpasim->dev_attr.nas)
583 		return -EINVAL;
584 
585 	iommu = &vdpasim->iommu[asid];
586 
587 	spin_lock(&vdpasim->lock);
588 
589 	for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
590 		if (vdpasim_get_vq_group(vdpa, i) == group)
591 			vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu,
592 					 &vdpasim->iommu_lock);
593 
594 	spin_unlock(&vdpasim->lock);
595 
596 	return 0;
597 }
598 
599 static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
600 			   struct vhost_iotlb *iotlb)
601 {
602 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
603 	struct vhost_iotlb_map *map;
604 	struct vhost_iotlb *iommu;
605 	u64 start = 0ULL, last = 0ULL - 1;
606 	int ret;
607 
608 	if (asid >= vdpasim->dev_attr.nas)
609 		return -EINVAL;
610 
611 	spin_lock(&vdpasim->iommu_lock);
612 
613 	iommu = &vdpasim->iommu[asid];
614 	vhost_iotlb_reset(iommu);
615 
616 	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
617 	     map = vhost_iotlb_itree_next(map, start, last)) {
618 		ret = vhost_iotlb_add_range(iommu, map->start,
619 					    map->last, map->addr, map->perm);
620 		if (ret)
621 			goto err;
622 	}
623 	spin_unlock(&vdpasim->iommu_lock);
624 	return 0;
625 
626 err:
627 	vhost_iotlb_reset(iommu);
628 	spin_unlock(&vdpasim->iommu_lock);
629 	return ret;
630 }
631 
632 static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid,
633 			   u64 iova, u64 size,
634 			   u64 pa, u32 perm, void *opaque)
635 {
636 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
637 	int ret;
638 
639 	if (asid >= vdpasim->dev_attr.nas)
640 		return -EINVAL;
641 
642 	spin_lock(&vdpasim->iommu_lock);
643 	ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
644 					iova + size - 1, pa, perm, opaque);
645 	spin_unlock(&vdpasim->iommu_lock);
646 
647 	return ret;
648 }
649 
650 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid,
651 			     u64 iova, u64 size)
652 {
653 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
654 
655 	if (asid >= vdpasim->dev_attr.nas)
656 		return -EINVAL;
657 
658 	spin_lock(&vdpasim->iommu_lock);
659 	vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
660 	spin_unlock(&vdpasim->iommu_lock);
661 
662 	return 0;
663 }
664 
665 static void vdpasim_free(struct vdpa_device *vdpa)
666 {
667 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
668 	int i;
669 
670 	cancel_work_sync(&vdpasim->work);
671 
672 	for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
673 		vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov);
674 		vringh_kiov_cleanup(&vdpasim->vqs[i].in_iov);
675 	}
676 
677 	if (vdpa_get_dma_dev(vdpa)) {
678 		put_iova_domain(&vdpasim->iova);
679 		iova_cache_put();
680 	}
681 
682 	kvfree(vdpasim->buffer);
683 	vhost_iotlb_free(vdpasim->iommu);
684 	kfree(vdpasim->vqs);
685 	kfree(vdpasim->config);
686 }
687 
688 static const struct vdpa_config_ops vdpasim_config_ops = {
689 	.set_vq_address         = vdpasim_set_vq_address,
690 	.set_vq_num             = vdpasim_set_vq_num,
691 	.kick_vq                = vdpasim_kick_vq,
692 	.set_vq_cb              = vdpasim_set_vq_cb,
693 	.set_vq_ready           = vdpasim_set_vq_ready,
694 	.get_vq_ready           = vdpasim_get_vq_ready,
695 	.set_vq_state           = vdpasim_set_vq_state,
696 	.get_vq_state           = vdpasim_get_vq_state,
697 	.get_vq_align           = vdpasim_get_vq_align,
698 	.get_vq_group           = vdpasim_get_vq_group,
699 	.get_device_features    = vdpasim_get_device_features,
700 	.set_driver_features    = vdpasim_set_driver_features,
701 	.get_driver_features    = vdpasim_get_driver_features,
702 	.set_config_cb          = vdpasim_set_config_cb,
703 	.get_vq_num_max         = vdpasim_get_vq_num_max,
704 	.get_device_id          = vdpasim_get_device_id,
705 	.get_vendor_id          = vdpasim_get_vendor_id,
706 	.get_status             = vdpasim_get_status,
707 	.set_status             = vdpasim_set_status,
708 	.reset			= vdpasim_reset,
709 	.suspend		= vdpasim_suspend,
710 	.get_config_size        = vdpasim_get_config_size,
711 	.get_config             = vdpasim_get_config,
712 	.set_config             = vdpasim_set_config,
713 	.get_generation         = vdpasim_get_generation,
714 	.get_iova_range         = vdpasim_get_iova_range,
715 	.set_group_asid         = vdpasim_set_group_asid,
716 	.dma_map                = vdpasim_dma_map,
717 	.dma_unmap              = vdpasim_dma_unmap,
718 	.free                   = vdpasim_free,
719 };
720 
721 static const struct vdpa_config_ops vdpasim_batch_config_ops = {
722 	.set_vq_address         = vdpasim_set_vq_address,
723 	.set_vq_num             = vdpasim_set_vq_num,
724 	.kick_vq                = vdpasim_kick_vq,
725 	.set_vq_cb              = vdpasim_set_vq_cb,
726 	.set_vq_ready           = vdpasim_set_vq_ready,
727 	.get_vq_ready           = vdpasim_get_vq_ready,
728 	.set_vq_state           = vdpasim_set_vq_state,
729 	.get_vq_state           = vdpasim_get_vq_state,
730 	.get_vq_align           = vdpasim_get_vq_align,
731 	.get_vq_group           = vdpasim_get_vq_group,
732 	.get_device_features    = vdpasim_get_device_features,
733 	.set_driver_features    = vdpasim_set_driver_features,
734 	.get_driver_features    = vdpasim_get_driver_features,
735 	.set_config_cb          = vdpasim_set_config_cb,
736 	.get_vq_num_max         = vdpasim_get_vq_num_max,
737 	.get_device_id          = vdpasim_get_device_id,
738 	.get_vendor_id          = vdpasim_get_vendor_id,
739 	.get_status             = vdpasim_get_status,
740 	.set_status             = vdpasim_set_status,
741 	.reset			= vdpasim_reset,
742 	.suspend		= vdpasim_suspend,
743 	.get_config_size        = vdpasim_get_config_size,
744 	.get_config             = vdpasim_get_config,
745 	.set_config             = vdpasim_set_config,
746 	.get_generation         = vdpasim_get_generation,
747 	.get_iova_range         = vdpasim_get_iova_range,
748 	.set_group_asid         = vdpasim_set_group_asid,
749 	.set_map                = vdpasim_set_map,
750 	.free                   = vdpasim_free,
751 };
752 
753 MODULE_VERSION(DRV_VERSION);
754 MODULE_LICENSE(DRV_LICENSE);
755 MODULE_AUTHOR(DRV_AUTHOR);
756 MODULE_DESCRIPTION(DRV_DESC);
757