• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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. 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 
vdpa_to_sim(struct vdpa_device * vdpa)42 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
43 {
44 	return container_of(vdpa, struct vdpasim, vdpa);
45 }
46 
dev_to_sim(struct device * dev)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 
vdpasim_vq_notify(struct vringh * vring)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 
vdpasim_queue_ready(struct vdpasim * vdpasim,unsigned int idx)65 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
66 {
67 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
68 	uint16_t last_avail_idx = vq->vring.last_avail_idx;
69 
70 	vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num, 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.last_avail_idx = last_avail_idx;
78 
79 	/*
80 	 * Since vdpa_sim does not support receive inflight descriptors as a
81 	 * destination of a migration, let's set both avail_idx and used_idx
82 	 * the same at vq start.  This is how vhost-user works in a
83 	 * VHOST_SET_VRING_BASE call.
84 	 *
85 	 * Although the simple fix is to set last_used_idx at
86 	 * vdpasim_set_vq_state, it would be reset at vdpasim_queue_ready.
87 	 */
88 	vq->vring.last_used_idx = last_avail_idx;
89 	vq->vring.notify = vdpasim_vq_notify;
90 }
91 
vdpasim_vq_reset(struct vdpasim * vdpasim,struct vdpasim_virtqueue * vq)92 static void vdpasim_vq_reset(struct vdpasim *vdpasim,
93 			     struct vdpasim_virtqueue *vq)
94 {
95 	vq->ready = false;
96 	vq->desc_addr = 0;
97 	vq->driver_addr = 0;
98 	vq->device_addr = 0;
99 	vq->cb = NULL;
100 	vq->private = NULL;
101 	vringh_init_iotlb(&vq->vring, vdpasim->dev_attr.supported_features,
102 			  VDPASIM_QUEUE_MAX, false, NULL, NULL, NULL);
103 
104 	vq->vring.notify = NULL;
105 }
106 
vdpasim_do_reset(struct vdpasim * vdpasim)107 static void vdpasim_do_reset(struct vdpasim *vdpasim)
108 {
109 	int i;
110 
111 	for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
112 		vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
113 
114 	spin_lock(&vdpasim->iommu_lock);
115 	vhost_iotlb_reset(vdpasim->iommu);
116 	spin_unlock(&vdpasim->iommu_lock);
117 
118 	vdpasim->features = 0;
119 	vdpasim->status = 0;
120 	++vdpasim->generation;
121 }
122 
dir_to_perm(enum dma_data_direction dir)123 static int dir_to_perm(enum dma_data_direction dir)
124 {
125 	int perm = -EFAULT;
126 
127 	switch (dir) {
128 	case DMA_FROM_DEVICE:
129 		perm = VHOST_MAP_WO;
130 		break;
131 	case DMA_TO_DEVICE:
132 		perm = VHOST_MAP_RO;
133 		break;
134 	case DMA_BIDIRECTIONAL:
135 		perm = VHOST_MAP_RW;
136 		break;
137 	default:
138 		break;
139 	}
140 
141 	return perm;
142 }
143 
vdpasim_map_range(struct vdpasim * vdpasim,phys_addr_t paddr,size_t size,unsigned int perm)144 static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr,
145 				    size_t size, unsigned int perm)
146 {
147 	struct iova *iova;
148 	dma_addr_t dma_addr;
149 	int ret;
150 
151 	/* We set the limit_pfn to the maximum (ULONG_MAX - 1) */
152 	iova = alloc_iova(&vdpasim->iova, size >> iova_shift(&vdpasim->iova),
153 			  ULONG_MAX - 1, true);
154 	if (!iova)
155 		return DMA_MAPPING_ERROR;
156 
157 	dma_addr = iova_dma_addr(&vdpasim->iova, iova);
158 
159 	spin_lock(&vdpasim->iommu_lock);
160 	ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,
161 				    (u64)dma_addr + size - 1, (u64)paddr, perm);
162 	spin_unlock(&vdpasim->iommu_lock);
163 
164 	if (ret) {
165 		__free_iova(&vdpasim->iova, iova);
166 		return DMA_MAPPING_ERROR;
167 	}
168 
169 	return dma_addr;
170 }
171 
vdpasim_unmap_range(struct vdpasim * vdpasim,dma_addr_t dma_addr,size_t size)172 static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr,
173 				size_t size)
174 {
175 	spin_lock(&vdpasim->iommu_lock);
176 	vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,
177 			      (u64)dma_addr + size - 1);
178 	spin_unlock(&vdpasim->iommu_lock);
179 
180 	free_iova(&vdpasim->iova, iova_pfn(&vdpasim->iova, dma_addr));
181 }
182 
vdpasim_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,unsigned long attrs)183 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
184 				   unsigned long offset, size_t size,
185 				   enum dma_data_direction dir,
186 				   unsigned long attrs)
187 {
188 	struct vdpasim *vdpasim = dev_to_sim(dev);
189 	phys_addr_t paddr = page_to_phys(page) + offset;
190 	int perm = dir_to_perm(dir);
191 
192 	if (perm < 0)
193 		return DMA_MAPPING_ERROR;
194 
195 	return vdpasim_map_range(vdpasim, paddr, size, perm);
196 }
197 
vdpasim_unmap_page(struct device * dev,dma_addr_t dma_addr,size_t size,enum dma_data_direction dir,unsigned long attrs)198 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
199 			       size_t size, enum dma_data_direction dir,
200 			       unsigned long attrs)
201 {
202 	struct vdpasim *vdpasim = dev_to_sim(dev);
203 
204 	vdpasim_unmap_range(vdpasim, dma_addr, size);
205 }
206 
vdpasim_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_addr,gfp_t flag,unsigned long attrs)207 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
208 				    dma_addr_t *dma_addr, gfp_t flag,
209 				    unsigned long attrs)
210 {
211 	struct vdpasim *vdpasim = dev_to_sim(dev);
212 	phys_addr_t paddr;
213 	void *addr;
214 
215 	addr = kmalloc(size, flag);
216 	if (!addr) {
217 		*dma_addr = DMA_MAPPING_ERROR;
218 		return NULL;
219 	}
220 
221 	paddr = virt_to_phys(addr);
222 
223 	*dma_addr = vdpasim_map_range(vdpasim, paddr, size, VHOST_MAP_RW);
224 	if (*dma_addr == DMA_MAPPING_ERROR) {
225 		kfree(addr);
226 		return NULL;
227 	}
228 
229 	return addr;
230 }
231 
vdpasim_free_coherent(struct device * dev,size_t size,void * vaddr,dma_addr_t dma_addr,unsigned long attrs)232 static void vdpasim_free_coherent(struct device *dev, size_t size,
233 				  void *vaddr, dma_addr_t dma_addr,
234 				  unsigned long attrs)
235 {
236 	struct vdpasim *vdpasim = dev_to_sim(dev);
237 
238 	vdpasim_unmap_range(vdpasim, dma_addr, size);
239 
240 	kfree(vaddr);
241 }
242 
243 static const struct dma_map_ops vdpasim_dma_ops = {
244 	.map_page = vdpasim_map_page,
245 	.unmap_page = vdpasim_unmap_page,
246 	.alloc = vdpasim_alloc_coherent,
247 	.free = vdpasim_free_coherent,
248 };
249 
250 static const struct vdpa_config_ops vdpasim_config_ops;
251 static const struct vdpa_config_ops vdpasim_batch_config_ops;
252 
vdpasim_create(struct vdpasim_dev_attr * dev_attr)253 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
254 {
255 	const struct vdpa_config_ops *ops;
256 	struct vdpasim *vdpasim;
257 	struct device *dev;
258 	int i, ret = -ENOMEM;
259 
260 	if (batch_mapping)
261 		ops = &vdpasim_batch_config_ops;
262 	else
263 		ops = &vdpasim_config_ops;
264 
265 	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
266 				    dev_attr->name, false);
267 	if (IS_ERR(vdpasim)) {
268 		ret = PTR_ERR(vdpasim);
269 		goto err_alloc;
270 	}
271 
272 	vdpasim->dev_attr = *dev_attr;
273 	INIT_WORK(&vdpasim->work, dev_attr->work_fn);
274 	spin_lock_init(&vdpasim->lock);
275 	spin_lock_init(&vdpasim->iommu_lock);
276 
277 	dev = &vdpasim->vdpa.dev;
278 	dev->dma_mask = &dev->coherent_dma_mask;
279 	if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
280 		goto err_iommu;
281 	set_dma_ops(dev, &vdpasim_dma_ops);
282 	vdpasim->vdpa.mdev = dev_attr->mgmt_dev;
283 
284 	vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
285 	if (!vdpasim->config)
286 		goto err_iommu;
287 
288 	vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
289 			       GFP_KERNEL);
290 	if (!vdpasim->vqs)
291 		goto err_iommu;
292 
293 	vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);
294 	if (!vdpasim->iommu)
295 		goto err_iommu;
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,
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 
vdpasim_set_vq_address(struct vdpa_device * vdpa,u16 idx,u64 desc_area,u64 driver_area,u64 device_area)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 
vdpasim_set_vq_num(struct vdpa_device * vdpa,u16 idx,u32 num)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 
vdpasim_kick_vq(struct vdpa_device * vdpa,u16 idx)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 
vdpasim_set_vq_cb(struct vdpa_device * vdpa,u16 idx,struct vdpa_callback * cb)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 
vdpasim_set_vq_ready(struct vdpa_device * vdpa,u16 idx,bool ready)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 
vdpasim_get_vq_ready(struct vdpa_device * vdpa,u16 idx)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 
vdpasim_set_vq_state(struct vdpa_device * vdpa,u16 idx,const struct vdpa_vq_state * state)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 
vdpasim_get_vq_state(struct vdpa_device * vdpa,u16 idx,struct vdpa_vq_state * state)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 
vdpasim_get_vq_align(struct vdpa_device * vdpa)412 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
413 {
414 	return VDPASIM_QUEUE_ALIGN;
415 }
416 
vdpasim_get_features(struct vdpa_device * vdpa)417 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
418 {
419 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
420 
421 	return vdpasim->dev_attr.supported_features;
422 }
423 
vdpasim_set_features(struct vdpa_device * vdpa,u64 features)424 static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
425 {
426 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
427 
428 	/* DMA mapping must be done by driver */
429 	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
430 		return -EINVAL;
431 
432 	vdpasim->features = features & vdpasim->dev_attr.supported_features;
433 
434 	return 0;
435 }
436 
vdpasim_set_config_cb(struct vdpa_device * vdpa,struct vdpa_callback * cb)437 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
438 				  struct vdpa_callback *cb)
439 {
440 	/* We don't support config interrupt */
441 }
442 
vdpasim_get_vq_num_max(struct vdpa_device * vdpa)443 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
444 {
445 	return VDPASIM_QUEUE_MAX;
446 }
447 
vdpasim_get_device_id(struct vdpa_device * vdpa)448 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
449 {
450 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
451 
452 	return vdpasim->dev_attr.id;
453 }
454 
vdpasim_get_vendor_id(struct vdpa_device * vdpa)455 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
456 {
457 	return VDPASIM_VENDOR_ID;
458 }
459 
vdpasim_get_status(struct vdpa_device * vdpa)460 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
461 {
462 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
463 	u8 status;
464 
465 	spin_lock(&vdpasim->lock);
466 	status = vdpasim->status;
467 	spin_unlock(&vdpasim->lock);
468 
469 	return status;
470 }
471 
vdpasim_set_status(struct vdpa_device * vdpa,u8 status)472 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
473 {
474 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
475 
476 	spin_lock(&vdpasim->lock);
477 	vdpasim->status = status;
478 	spin_unlock(&vdpasim->lock);
479 }
480 
vdpasim_reset(struct vdpa_device * vdpa)481 static int vdpasim_reset(struct vdpa_device *vdpa)
482 {
483 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
484 
485 	spin_lock(&vdpasim->lock);
486 	vdpasim->status = 0;
487 	vdpasim_do_reset(vdpasim);
488 	spin_unlock(&vdpasim->lock);
489 
490 	return 0;
491 }
492 
vdpasim_get_config_size(struct vdpa_device * vdpa)493 static size_t vdpasim_get_config_size(struct vdpa_device *vdpa)
494 {
495 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
496 
497 	return vdpasim->dev_attr.config_size;
498 }
499 
vdpasim_get_config(struct vdpa_device * vdpa,unsigned int offset,void * buf,unsigned int len)500 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
501 			     void *buf, unsigned int len)
502 {
503 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
504 
505 	if (offset + len > vdpasim->dev_attr.config_size)
506 		return;
507 
508 	if (vdpasim->dev_attr.get_config)
509 		vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
510 
511 	memcpy(buf, vdpasim->config + offset, len);
512 }
513 
vdpasim_set_config(struct vdpa_device * vdpa,unsigned int offset,const void * buf,unsigned int len)514 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
515 			     const void *buf, unsigned int len)
516 {
517 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
518 
519 	if (offset + len > vdpasim->dev_attr.config_size)
520 		return;
521 
522 	memcpy(vdpasim->config + offset, buf, len);
523 
524 	if (vdpasim->dev_attr.set_config)
525 		vdpasim->dev_attr.set_config(vdpasim, vdpasim->config);
526 }
527 
vdpasim_get_generation(struct vdpa_device * vdpa)528 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
529 {
530 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
531 
532 	return vdpasim->generation;
533 }
534 
vdpasim_get_iova_range(struct vdpa_device * vdpa)535 static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
536 {
537 	struct vdpa_iova_range range = {
538 		.first = 0ULL,
539 		.last = ULLONG_MAX,
540 	};
541 
542 	return range;
543 }
544 
vdpasim_set_map(struct vdpa_device * vdpa,struct vhost_iotlb * iotlb)545 static int vdpasim_set_map(struct vdpa_device *vdpa,
546 			   struct vhost_iotlb *iotlb)
547 {
548 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
549 	struct vhost_iotlb_map *map;
550 	u64 start = 0ULL, last = 0ULL - 1;
551 	int ret;
552 
553 	spin_lock(&vdpasim->iommu_lock);
554 	vhost_iotlb_reset(vdpasim->iommu);
555 
556 	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
557 	     map = vhost_iotlb_itree_next(map, start, last)) {
558 		ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
559 					    map->last, map->addr, map->perm);
560 		if (ret)
561 			goto err;
562 	}
563 	spin_unlock(&vdpasim->iommu_lock);
564 	return 0;
565 
566 err:
567 	vhost_iotlb_reset(vdpasim->iommu);
568 	spin_unlock(&vdpasim->iommu_lock);
569 	return ret;
570 }
571 
vdpasim_dma_map(struct vdpa_device * vdpa,u64 iova,u64 size,u64 pa,u32 perm,void * opaque)572 static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
573 			   u64 pa, u32 perm, void *opaque)
574 {
575 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
576 	int ret;
577 
578 	spin_lock(&vdpasim->iommu_lock);
579 	ret = vhost_iotlb_add_range_ctx(vdpasim->iommu, iova, iova + size - 1,
580 					pa, perm, opaque);
581 	spin_unlock(&vdpasim->iommu_lock);
582 
583 	return ret;
584 }
585 
vdpasim_dma_unmap(struct vdpa_device * vdpa,u64 iova,u64 size)586 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
587 {
588 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
589 
590 	spin_lock(&vdpasim->iommu_lock);
591 	vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
592 	spin_unlock(&vdpasim->iommu_lock);
593 
594 	return 0;
595 }
596 
vdpasim_free(struct vdpa_device * vdpa)597 static void vdpasim_free(struct vdpa_device *vdpa)
598 {
599 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
600 	int i;
601 
602 	cancel_work_sync(&vdpasim->work);
603 
604 	for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
605 		vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov);
606 		vringh_kiov_cleanup(&vdpasim->vqs[i].in_iov);
607 	}
608 
609 	if (vdpa_get_dma_dev(vdpa)) {
610 		put_iova_domain(&vdpasim->iova);
611 		iova_cache_put();
612 	}
613 
614 	kvfree(vdpasim->buffer);
615 	if (vdpasim->iommu)
616 		vhost_iotlb_free(vdpasim->iommu);
617 	kfree(vdpasim->vqs);
618 	kfree(vdpasim->config);
619 }
620 
621 static const struct vdpa_config_ops vdpasim_config_ops = {
622 	.set_vq_address         = vdpasim_set_vq_address,
623 	.set_vq_num             = vdpasim_set_vq_num,
624 	.kick_vq                = vdpasim_kick_vq,
625 	.set_vq_cb              = vdpasim_set_vq_cb,
626 	.set_vq_ready           = vdpasim_set_vq_ready,
627 	.get_vq_ready           = vdpasim_get_vq_ready,
628 	.set_vq_state           = vdpasim_set_vq_state,
629 	.get_vq_state           = vdpasim_get_vq_state,
630 	.get_vq_align           = vdpasim_get_vq_align,
631 	.get_features           = vdpasim_get_features,
632 	.set_features           = vdpasim_set_features,
633 	.set_config_cb          = vdpasim_set_config_cb,
634 	.get_vq_num_max         = vdpasim_get_vq_num_max,
635 	.get_device_id          = vdpasim_get_device_id,
636 	.get_vendor_id          = vdpasim_get_vendor_id,
637 	.get_status             = vdpasim_get_status,
638 	.set_status             = vdpasim_set_status,
639 	.reset			= vdpasim_reset,
640 	.get_config_size        = vdpasim_get_config_size,
641 	.get_config             = vdpasim_get_config,
642 	.set_config             = vdpasim_set_config,
643 	.get_generation         = vdpasim_get_generation,
644 	.get_iova_range         = vdpasim_get_iova_range,
645 	.dma_map                = vdpasim_dma_map,
646 	.dma_unmap              = vdpasim_dma_unmap,
647 	.free                   = vdpasim_free,
648 };
649 
650 static const struct vdpa_config_ops vdpasim_batch_config_ops = {
651 	.set_vq_address         = vdpasim_set_vq_address,
652 	.set_vq_num             = vdpasim_set_vq_num,
653 	.kick_vq                = vdpasim_kick_vq,
654 	.set_vq_cb              = vdpasim_set_vq_cb,
655 	.set_vq_ready           = vdpasim_set_vq_ready,
656 	.get_vq_ready           = vdpasim_get_vq_ready,
657 	.set_vq_state           = vdpasim_set_vq_state,
658 	.get_vq_state           = vdpasim_get_vq_state,
659 	.get_vq_align           = vdpasim_get_vq_align,
660 	.get_features           = vdpasim_get_features,
661 	.set_features           = vdpasim_set_features,
662 	.set_config_cb          = vdpasim_set_config_cb,
663 	.get_vq_num_max         = vdpasim_get_vq_num_max,
664 	.get_device_id          = vdpasim_get_device_id,
665 	.get_vendor_id          = vdpasim_get_vendor_id,
666 	.get_status             = vdpasim_get_status,
667 	.set_status             = vdpasim_set_status,
668 	.reset			= vdpasim_reset,
669 	.get_config_size        = vdpasim_get_config_size,
670 	.get_config             = vdpasim_get_config,
671 	.set_config             = vdpasim_set_config,
672 	.get_generation         = vdpasim_get_generation,
673 	.get_iova_range         = vdpasim_get_iova_range,
674 	.set_map                = vdpasim_set_map,
675 	.free                   = vdpasim_free,
676 };
677 
678 MODULE_VERSION(DRV_VERSION);
679 MODULE_LICENSE(DRV_LICENSE);
680 MODULE_AUTHOR(DRV_AUTHOR);
681 MODULE_DESCRIPTION(DRV_DESC);
682