1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018-2020 Intel Corporation.
4 * Copyright (C) 2020 Red Hat, Inc.
6 * Author: Tiwei Bie <tiwei.bie@intel.com>
7 * Jason Wang <jasowang@redhat.com>
9 * Thanks Michael S. Tsirkin for the valuable comments and
10 * suggestions. And thanks to Cunming Liang and Zhihong Wang for all
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/cdev.h>
17 #include <linux/device.h>
19 #include <linux/slab.h>
20 #include <linux/iommu.h>
21 #include <linux/uuid.h>
22 #include <linux/vdpa.h>
23 #include <linux/nospec.h>
24 #include <linux/vhost.h>
29 VHOST_VDPA_BACKEND_FEATURES =
30 (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
31 (1ULL << VHOST_BACKEND_F_IOTLB_BATCH) |
32 (1ULL << VHOST_BACKEND_F_IOTLB_ASID),
35 #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
37 #define VHOST_VDPA_IOTLB_BUCKETS 16
39 struct vhost_vdpa_as {
40 struct hlist_node hash_link;
41 struct vhost_iotlb iotlb;
46 struct vhost_dev vdev;
47 struct iommu_domain *domain;
48 struct vhost_virtqueue *vqs;
49 struct completion completion;
50 struct vdpa_device *vdpa;
51 struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
58 struct eventfd_ctx *config_ctx;
60 struct vdpa_iova_range range;
64 static DEFINE_IDA(vhost_vdpa_ida);
66 static dev_t vhost_vdpa_major;
68 static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb)
70 struct vhost_vdpa_as *as = container_of(iotlb, struct
71 vhost_vdpa_as, iotlb);
75 static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
77 struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
78 struct vhost_vdpa_as *as;
80 hlist_for_each_entry(as, head, hash_link)
87 static struct vhost_iotlb *asid_to_iotlb(struct vhost_vdpa *v, u32 asid)
89 struct vhost_vdpa_as *as = asid_to_as(v, asid);
97 static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
99 struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
100 struct vhost_vdpa_as *as;
102 if (asid_to_as(v, asid))
105 if (asid >= v->vdpa->nas)
108 as = kmalloc(sizeof(*as), GFP_KERNEL);
112 vhost_iotlb_init(&as->iotlb, 0, 0);
114 hlist_add_head(&as->hash_link, head);
119 static struct vhost_vdpa_as *vhost_vdpa_find_alloc_as(struct vhost_vdpa *v,
122 struct vhost_vdpa_as *as = asid_to_as(v, asid);
127 return vhost_vdpa_alloc_as(v, asid);
130 static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
132 struct vhost_vdpa_as *as = asid_to_as(v, asid);
137 hlist_del(&as->hash_link);
138 vhost_iotlb_reset(&as->iotlb);
144 static void handle_vq_kick(struct vhost_work *work)
146 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
148 struct vhost_vdpa *v = container_of(vq->dev, struct vhost_vdpa, vdev);
149 const struct vdpa_config_ops *ops = v->vdpa->config;
151 ops->kick_vq(v->vdpa, vq - v->vqs);
154 static irqreturn_t vhost_vdpa_virtqueue_cb(void *private)
156 struct vhost_virtqueue *vq = private;
157 struct eventfd_ctx *call_ctx = vq->call_ctx.ctx;
160 eventfd_signal(call_ctx, 1);
165 static irqreturn_t vhost_vdpa_config_cb(void *private)
167 struct vhost_vdpa *v = private;
168 struct eventfd_ctx *config_ctx = v->config_ctx;
171 eventfd_signal(config_ctx, 1);
176 static void vhost_vdpa_setup_vq_irq(struct vhost_vdpa *v, u16 qid)
178 struct vhost_virtqueue *vq = &v->vqs[qid];
179 const struct vdpa_config_ops *ops = v->vdpa->config;
180 struct vdpa_device *vdpa = v->vdpa;
183 if (!ops->get_vq_irq)
186 irq = ops->get_vq_irq(vdpa, qid);
190 irq_bypass_unregister_producer(&vq->call_ctx.producer);
191 if (!vq->call_ctx.ctx)
194 vq->call_ctx.producer.token = vq->call_ctx.ctx;
195 vq->call_ctx.producer.irq = irq;
196 ret = irq_bypass_register_producer(&vq->call_ctx.producer);
198 dev_info(&v->dev, "vq %u, irq bypass producer (token %p) registration fails, ret = %d\n",
199 qid, vq->call_ctx.producer.token, ret);
202 static void vhost_vdpa_unsetup_vq_irq(struct vhost_vdpa *v, u16 qid)
204 struct vhost_virtqueue *vq = &v->vqs[qid];
206 irq_bypass_unregister_producer(&vq->call_ctx.producer);
209 static int vhost_vdpa_reset(struct vhost_vdpa *v)
211 struct vdpa_device *vdpa = v->vdpa;
215 return vdpa_reset(vdpa);
218 static long vhost_vdpa_get_device_id(struct vhost_vdpa *v, u8 __user *argp)
220 struct vdpa_device *vdpa = v->vdpa;
221 const struct vdpa_config_ops *ops = vdpa->config;
224 device_id = ops->get_device_id(vdpa);
226 if (copy_to_user(argp, &device_id, sizeof(device_id)))
232 static long vhost_vdpa_get_status(struct vhost_vdpa *v, u8 __user *statusp)
234 struct vdpa_device *vdpa = v->vdpa;
235 const struct vdpa_config_ops *ops = vdpa->config;
238 status = ops->get_status(vdpa);
240 if (copy_to_user(statusp, &status, sizeof(status)))
246 static long vhost_vdpa_set_status(struct vhost_vdpa *v, u8 __user *statusp)
248 struct vdpa_device *vdpa = v->vdpa;
249 const struct vdpa_config_ops *ops = vdpa->config;
250 u8 status, status_old;
255 if (copy_from_user(&status, statusp, sizeof(status)))
258 status_old = ops->get_status(vdpa);
261 * Userspace shouldn't remove status bits unless reset the
264 if (status != 0 && (status_old & ~status) != 0)
267 if ((status_old & VIRTIO_CONFIG_S_DRIVER_OK) && !(status & VIRTIO_CONFIG_S_DRIVER_OK))
268 for (i = 0; i < nvqs; i++)
269 vhost_vdpa_unsetup_vq_irq(v, i);
272 ret = vdpa_reset(vdpa);
276 vdpa_set_status(vdpa, status);
278 if ((status & VIRTIO_CONFIG_S_DRIVER_OK) && !(status_old & VIRTIO_CONFIG_S_DRIVER_OK))
279 for (i = 0; i < nvqs; i++)
280 vhost_vdpa_setup_vq_irq(v, i);
285 static int vhost_vdpa_config_validate(struct vhost_vdpa *v,
286 struct vhost_vdpa_config *c)
288 struct vdpa_device *vdpa = v->vdpa;
289 size_t size = vdpa->config->get_config_size(vdpa);
291 if (c->len == 0 || c->off > size)
294 if (c->len > size - c->off)
300 static long vhost_vdpa_get_config(struct vhost_vdpa *v,
301 struct vhost_vdpa_config __user *c)
303 struct vdpa_device *vdpa = v->vdpa;
304 struct vhost_vdpa_config config;
305 unsigned long size = offsetof(struct vhost_vdpa_config, buf);
308 if (copy_from_user(&config, c, size))
310 if (vhost_vdpa_config_validate(v, &config))
312 buf = kvzalloc(config.len, GFP_KERNEL);
316 vdpa_get_config(vdpa, config.off, buf, config.len);
318 if (copy_to_user(c->buf, buf, config.len)) {
327 static long vhost_vdpa_set_config(struct vhost_vdpa *v,
328 struct vhost_vdpa_config __user *c)
330 struct vdpa_device *vdpa = v->vdpa;
331 struct vhost_vdpa_config config;
332 unsigned long size = offsetof(struct vhost_vdpa_config, buf);
335 if (copy_from_user(&config, c, size))
337 if (vhost_vdpa_config_validate(v, &config))
340 buf = vmemdup_user(c->buf, config.len);
344 vdpa_set_config(vdpa, config.off, buf, config.len);
350 static long vhost_vdpa_get_features(struct vhost_vdpa *v, u64 __user *featurep)
352 struct vdpa_device *vdpa = v->vdpa;
353 const struct vdpa_config_ops *ops = vdpa->config;
356 features = ops->get_device_features(vdpa);
358 if (copy_to_user(featurep, &features, sizeof(features)))
364 static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep)
366 struct vdpa_device *vdpa = v->vdpa;
367 const struct vdpa_config_ops *ops = vdpa->config;
371 * It's not allowed to change the features after they have
374 if (ops->get_status(vdpa) & VIRTIO_CONFIG_S_FEATURES_OK)
377 if (copy_from_user(&features, featurep, sizeof(features)))
380 if (vdpa_set_features(vdpa, features))
386 static long vhost_vdpa_get_vring_num(struct vhost_vdpa *v, u16 __user *argp)
388 struct vdpa_device *vdpa = v->vdpa;
389 const struct vdpa_config_ops *ops = vdpa->config;
392 num = ops->get_vq_num_max(vdpa);
394 if (copy_to_user(argp, &num, sizeof(num)))
400 static void vhost_vdpa_config_put(struct vhost_vdpa *v)
403 eventfd_ctx_put(v->config_ctx);
404 v->config_ctx = NULL;
408 static long vhost_vdpa_set_config_call(struct vhost_vdpa *v, u32 __user *argp)
410 struct vdpa_callback cb;
412 struct eventfd_ctx *ctx;
414 cb.callback = vhost_vdpa_config_cb;
416 if (copy_from_user(&fd, argp, sizeof(fd)))
419 ctx = fd == VHOST_FILE_UNBIND ? NULL : eventfd_ctx_fdget(fd);
420 swap(ctx, v->config_ctx);
422 if (!IS_ERR_OR_NULL(ctx))
423 eventfd_ctx_put(ctx);
425 if (IS_ERR(v->config_ctx)) {
426 long ret = PTR_ERR(v->config_ctx);
428 v->config_ctx = NULL;
432 v->vdpa->config->set_config_cb(v->vdpa, &cb);
437 static long vhost_vdpa_get_iova_range(struct vhost_vdpa *v, u32 __user *argp)
439 struct vhost_vdpa_iova_range range = {
440 .first = v->range.first,
441 .last = v->range.last,
444 if (copy_to_user(argp, &range, sizeof(range)))
449 static long vhost_vdpa_get_config_size(struct vhost_vdpa *v, u32 __user *argp)
451 struct vdpa_device *vdpa = v->vdpa;
452 const struct vdpa_config_ops *ops = vdpa->config;
455 size = ops->get_config_size(vdpa);
457 if (copy_to_user(argp, &size, sizeof(size)))
463 static long vhost_vdpa_get_vqs_count(struct vhost_vdpa *v, u32 __user *argp)
465 struct vdpa_device *vdpa = v->vdpa;
467 if (copy_to_user(argp, &vdpa->nvqs, sizeof(vdpa->nvqs)))
473 static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
476 struct vdpa_device *vdpa = v->vdpa;
477 const struct vdpa_config_ops *ops = vdpa->config;
478 struct vdpa_vq_state vq_state;
479 struct vdpa_callback cb;
480 struct vhost_virtqueue *vq;
481 struct vhost_vring_state s;
485 r = get_user(idx, (u32 __user *)argp);
492 idx = array_index_nospec(idx, v->nvqs);
496 case VHOST_VDPA_SET_VRING_ENABLE:
497 if (copy_from_user(&s, argp, sizeof(s)))
499 ops->set_vq_ready(vdpa, idx, s.num);
501 case VHOST_VDPA_GET_VRING_GROUP:
503 s.num = ops->get_vq_group(vdpa, idx);
504 if (s.num >= vdpa->ngroups)
506 else if (copy_to_user(argp, &s, sizeof(s)))
509 case VHOST_VDPA_SET_GROUP_ASID:
510 if (copy_from_user(&s, argp, sizeof(s)))
512 if (s.num >= vdpa->nas)
514 if (!ops->set_group_asid)
516 return ops->set_group_asid(vdpa, idx, s.num);
517 case VHOST_GET_VRING_BASE:
518 r = ops->get_vq_state(v->vdpa, idx, &vq_state);
522 vq->last_avail_idx = vq_state.split.avail_index;
526 r = vhost_vring_ioctl(&v->vdev, cmd, argp);
531 case VHOST_SET_VRING_ADDR:
532 if (ops->set_vq_address(vdpa, idx,
533 (u64)(uintptr_t)vq->desc,
534 (u64)(uintptr_t)vq->avail,
535 (u64)(uintptr_t)vq->used))
539 case VHOST_SET_VRING_BASE:
540 vq_state.split.avail_index = vq->last_avail_idx;
541 if (ops->set_vq_state(vdpa, idx, &vq_state))
545 case VHOST_SET_VRING_CALL:
546 if (vq->call_ctx.ctx) {
547 cb.callback = vhost_vdpa_virtqueue_cb;
553 ops->set_vq_cb(vdpa, idx, &cb);
554 vhost_vdpa_setup_vq_irq(v, idx);
557 case VHOST_SET_VRING_NUM:
558 ops->set_vq_num(vdpa, idx, vq->num);
565 static long vhost_vdpa_unlocked_ioctl(struct file *filep,
566 unsigned int cmd, unsigned long arg)
568 struct vhost_vdpa *v = filep->private_data;
569 struct vhost_dev *d = &v->vdev;
570 void __user *argp = (void __user *)arg;
571 u64 __user *featurep = argp;
575 if (cmd == VHOST_SET_BACKEND_FEATURES) {
576 if (copy_from_user(&features, featurep, sizeof(features)))
578 if (features & ~VHOST_VDPA_BACKEND_FEATURES)
580 vhost_set_backend_features(&v->vdev, features);
584 mutex_lock(&d->mutex);
587 case VHOST_VDPA_GET_DEVICE_ID:
588 r = vhost_vdpa_get_device_id(v, argp);
590 case VHOST_VDPA_GET_STATUS:
591 r = vhost_vdpa_get_status(v, argp);
593 case VHOST_VDPA_SET_STATUS:
594 r = vhost_vdpa_set_status(v, argp);
596 case VHOST_VDPA_GET_CONFIG:
597 r = vhost_vdpa_get_config(v, argp);
599 case VHOST_VDPA_SET_CONFIG:
600 r = vhost_vdpa_set_config(v, argp);
602 case VHOST_GET_FEATURES:
603 r = vhost_vdpa_get_features(v, argp);
605 case VHOST_SET_FEATURES:
606 r = vhost_vdpa_set_features(v, argp);
608 case VHOST_VDPA_GET_VRING_NUM:
609 r = vhost_vdpa_get_vring_num(v, argp);
611 case VHOST_VDPA_GET_GROUP_NUM:
612 if (copy_to_user(argp, &v->vdpa->ngroups,
613 sizeof(v->vdpa->ngroups)))
616 case VHOST_VDPA_GET_AS_NUM:
617 if (copy_to_user(argp, &v->vdpa->nas, sizeof(v->vdpa->nas)))
620 case VHOST_SET_LOG_BASE:
621 case VHOST_SET_LOG_FD:
624 case VHOST_VDPA_SET_CONFIG_CALL:
625 r = vhost_vdpa_set_config_call(v, argp);
627 case VHOST_GET_BACKEND_FEATURES:
628 features = VHOST_VDPA_BACKEND_FEATURES;
629 if (copy_to_user(featurep, &features, sizeof(features)))
632 case VHOST_VDPA_GET_IOVA_RANGE:
633 r = vhost_vdpa_get_iova_range(v, argp);
635 case VHOST_VDPA_GET_CONFIG_SIZE:
636 r = vhost_vdpa_get_config_size(v, argp);
638 case VHOST_VDPA_GET_VQS_COUNT:
639 r = vhost_vdpa_get_vqs_count(v, argp);
642 r = vhost_dev_ioctl(&v->vdev, cmd, argp);
643 if (r == -ENOIOCTLCMD)
644 r = vhost_vdpa_vring_ioctl(v, cmd, argp);
648 mutex_unlock(&d->mutex);
652 static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v,
653 struct vhost_iotlb *iotlb,
656 struct vhost_dev *dev = &v->vdev;
657 struct vhost_iotlb_map *map;
659 unsigned long pfn, pinned;
661 while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) {
662 pinned = PFN_DOWN(map->size);
663 for (pfn = PFN_DOWN(map->addr);
664 pinned > 0; pfn++, pinned--) {
665 page = pfn_to_page(pfn);
666 if (map->perm & VHOST_ACCESS_WO)
667 set_page_dirty_lock(page);
668 unpin_user_page(page);
670 atomic64_sub(PFN_DOWN(map->size), &dev->mm->pinned_vm);
671 vhost_iotlb_map_free(iotlb, map);
675 static void vhost_vdpa_va_unmap(struct vhost_vdpa *v,
676 struct vhost_iotlb *iotlb,
679 struct vhost_iotlb_map *map;
680 struct vdpa_map_file *map_file;
682 while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) {
683 map_file = (struct vdpa_map_file *)map->opaque;
684 fput(map_file->file);
686 vhost_iotlb_map_free(iotlb, map);
690 static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
691 struct vhost_iotlb *iotlb,
694 struct vdpa_device *vdpa = v->vdpa;
697 return vhost_vdpa_va_unmap(v, iotlb, start, last);
699 return vhost_vdpa_pa_unmap(v, iotlb, start, last);
702 static int perm_to_iommu_flags(u32 perm)
707 case VHOST_ACCESS_WO:
708 flags |= IOMMU_WRITE;
710 case VHOST_ACCESS_RO:
713 case VHOST_ACCESS_RW:
714 flags |= (IOMMU_WRITE | IOMMU_READ);
717 WARN(1, "invalidate vhost IOTLB permission\n");
721 return flags | IOMMU_CACHE;
724 static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
725 u64 iova, u64 size, u64 pa, u32 perm, void *opaque)
727 struct vhost_dev *dev = &v->vdev;
728 struct vdpa_device *vdpa = v->vdpa;
729 const struct vdpa_config_ops *ops = vdpa->config;
730 u32 asid = iotlb_to_asid(iotlb);
733 r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1,
739 r = ops->dma_map(vdpa, asid, iova, size, pa, perm, opaque);
740 } else if (ops->set_map) {
742 r = ops->set_map(vdpa, asid, iotlb);
744 r = iommu_map(v->domain, iova, pa, size,
745 perm_to_iommu_flags(perm));
748 vhost_iotlb_del_range(iotlb, iova, iova + size - 1);
753 atomic64_add(PFN_DOWN(size), &dev->mm->pinned_vm);
758 static void vhost_vdpa_unmap(struct vhost_vdpa *v,
759 struct vhost_iotlb *iotlb,
762 struct vdpa_device *vdpa = v->vdpa;
763 const struct vdpa_config_ops *ops = vdpa->config;
764 u32 asid = iotlb_to_asid(iotlb);
766 vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
769 ops->dma_unmap(vdpa, asid, iova, size);
770 } else if (ops->set_map) {
772 ops->set_map(vdpa, asid, iotlb);
774 iommu_unmap(v->domain, iova, size);
777 /* If we are in the middle of batch processing, delay the free
778 * of AS until BATCH_END.
780 if (!v->in_batch && !iotlb->nmaps)
781 vhost_vdpa_remove_as(v, asid);
784 static int vhost_vdpa_va_map(struct vhost_vdpa *v,
785 struct vhost_iotlb *iotlb,
786 u64 iova, u64 size, u64 uaddr, u32 perm)
788 struct vhost_dev *dev = &v->vdev;
789 u64 offset, map_size, map_iova = iova;
790 struct vdpa_map_file *map_file;
791 struct vm_area_struct *vma;
794 mmap_read_lock(dev->mm);
797 vma = find_vma(dev->mm, uaddr);
802 map_size = min(size, vma->vm_end - uaddr);
803 if (!(vma->vm_file && (vma->vm_flags & VM_SHARED) &&
804 !(vma->vm_flags & (VM_IO | VM_PFNMAP))))
807 map_file = kzalloc(sizeof(*map_file), GFP_KERNEL);
812 offset = (vma->vm_pgoff << PAGE_SHIFT) + uaddr - vma->vm_start;
813 map_file->offset = offset;
814 map_file->file = get_file(vma->vm_file);
815 ret = vhost_vdpa_map(v, iotlb, map_iova, map_size, uaddr,
818 fput(map_file->file);
825 map_iova += map_size;
828 vhost_vdpa_unmap(v, iotlb, iova, map_iova - iova);
830 mmap_read_unlock(dev->mm);
835 static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
836 struct vhost_iotlb *iotlb,
837 u64 iova, u64 size, u64 uaddr, u32 perm)
839 struct vhost_dev *dev = &v->vdev;
840 struct page **page_list;
841 unsigned long list_size = PAGE_SIZE / sizeof(struct page *);
842 unsigned int gup_flags = FOLL_LONGTERM;
843 unsigned long npages, cur_base, map_pfn, last_pfn = 0;
844 unsigned long lock_limit, sz2pin, nchunks, i;
849 /* Limit the use of memory for bookkeeping */
850 page_list = (struct page **) __get_free_page(GFP_KERNEL);
854 if (perm & VHOST_ACCESS_WO)
855 gup_flags |= FOLL_WRITE;
857 npages = PFN_UP(size + (iova & ~PAGE_MASK));
863 mmap_read_lock(dev->mm);
865 lock_limit = PFN_DOWN(rlimit(RLIMIT_MEMLOCK));
866 if (npages + atomic64_read(&dev->mm->pinned_vm) > lock_limit) {
871 cur_base = uaddr & PAGE_MASK;
876 sz2pin = min_t(unsigned long, npages, list_size);
877 pinned = pin_user_pages(cur_base, sz2pin,
878 gup_flags, page_list, NULL);
879 if (sz2pin != pinned) {
883 unpin_user_pages(page_list, pinned);
891 map_pfn = page_to_pfn(page_list[0]);
893 for (i = 0; i < pinned; i++) {
894 unsigned long this_pfn = page_to_pfn(page_list[i]);
897 if (last_pfn && (this_pfn != last_pfn + 1)) {
898 /* Pin a contiguous chunk of memory */
899 csize = PFN_PHYS(last_pfn - map_pfn + 1);
900 ret = vhost_vdpa_map(v, iotlb, iova, csize,
905 * Unpin the pages that are left unmapped
906 * from this point on in the current
907 * page_list. The remaining outstanding
908 * ones which may stride across several
909 * chunks will be covered in the common
910 * error path subsequently.
912 unpin_user_pages(&page_list[i],
925 cur_base += PFN_PHYS(pinned);
929 /* Pin the rest chunk */
930 ret = vhost_vdpa_map(v, iotlb, iova, PFN_PHYS(last_pfn - map_pfn + 1),
931 PFN_PHYS(map_pfn), perm, NULL);
938 * Unpin the outstanding pages which are yet to be
939 * mapped but haven't due to vdpa_map() or
940 * pin_user_pages() failure.
942 * Mapped pages are accounted in vdpa_map(), hence
943 * the corresponding unpinning will be handled by
947 for (pfn = map_pfn; pfn <= last_pfn; pfn++)
948 unpin_user_page(pfn_to_page(pfn));
950 vhost_vdpa_unmap(v, iotlb, start, size);
953 mmap_read_unlock(dev->mm);
955 free_page((unsigned long)page_list);
960 static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
961 struct vhost_iotlb *iotlb,
962 struct vhost_iotlb_msg *msg)
964 struct vdpa_device *vdpa = v->vdpa;
966 if (msg->iova < v->range.first || !msg->size ||
967 msg->iova > U64_MAX - msg->size + 1 ||
968 msg->iova + msg->size - 1 > v->range.last)
971 if (vhost_iotlb_itree_first(iotlb, msg->iova,
972 msg->iova + msg->size - 1))
976 return vhost_vdpa_va_map(v, iotlb, msg->iova, msg->size,
977 msg->uaddr, msg->perm);
979 return vhost_vdpa_pa_map(v, iotlb, msg->iova, msg->size, msg->uaddr,
983 static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
984 struct vhost_iotlb_msg *msg)
986 struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
987 struct vdpa_device *vdpa = v->vdpa;
988 const struct vdpa_config_ops *ops = vdpa->config;
989 struct vhost_iotlb *iotlb = NULL;
990 struct vhost_vdpa_as *as = NULL;
993 mutex_lock(&dev->mutex);
995 r = vhost_dev_check_owner(dev);
999 if (msg->type == VHOST_IOTLB_UPDATE ||
1000 msg->type == VHOST_IOTLB_BATCH_BEGIN) {
1001 as = vhost_vdpa_find_alloc_as(v, asid);
1003 dev_err(&v->dev, "can't find and alloc asid %d\n",
1010 iotlb = asid_to_iotlb(v, asid);
1012 if ((v->in_batch && v->batch_asid != asid) || !iotlb) {
1013 if (v->in_batch && v->batch_asid != asid) {
1014 dev_info(&v->dev, "batch id %d asid %d\n",
1015 v->batch_asid, asid);
1018 dev_err(&v->dev, "no iotlb for asid %d\n", asid);
1023 switch (msg->type) {
1024 case VHOST_IOTLB_UPDATE:
1025 r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
1027 case VHOST_IOTLB_INVALIDATE:
1028 vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
1030 case VHOST_IOTLB_BATCH_BEGIN:
1031 v->batch_asid = asid;
1034 case VHOST_IOTLB_BATCH_END:
1035 if (v->in_batch && ops->set_map)
1036 ops->set_map(vdpa, asid, iotlb);
1037 v->in_batch = false;
1039 vhost_vdpa_remove_as(v, asid);
1046 mutex_unlock(&dev->mutex);
1051 static ssize_t vhost_vdpa_chr_write_iter(struct kiocb *iocb,
1052 struct iov_iter *from)
1054 struct file *file = iocb->ki_filp;
1055 struct vhost_vdpa *v = file->private_data;
1056 struct vhost_dev *dev = &v->vdev;
1058 return vhost_chr_write_iter(dev, from);
1061 static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v)
1063 struct vdpa_device *vdpa = v->vdpa;
1064 const struct vdpa_config_ops *ops = vdpa->config;
1065 struct device *dma_dev = vdpa_get_dma_dev(vdpa);
1066 struct bus_type *bus;
1069 /* Device want to do DMA by itself */
1070 if (ops->set_map || ops->dma_map)
1077 if (!iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
1080 v->domain = iommu_domain_alloc(bus);
1084 ret = iommu_attach_device(v->domain, dma_dev);
1091 iommu_domain_free(v->domain);
1095 static void vhost_vdpa_free_domain(struct vhost_vdpa *v)
1097 struct vdpa_device *vdpa = v->vdpa;
1098 struct device *dma_dev = vdpa_get_dma_dev(vdpa);
1101 iommu_detach_device(v->domain, dma_dev);
1102 iommu_domain_free(v->domain);
1108 static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
1110 struct vdpa_iova_range *range = &v->range;
1111 struct vdpa_device *vdpa = v->vdpa;
1112 const struct vdpa_config_ops *ops = vdpa->config;
1114 if (ops->get_iova_range) {
1115 *range = ops->get_iova_range(vdpa);
1116 } else if (v->domain && v->domain->geometry.force_aperture) {
1117 range->first = v->domain->geometry.aperture_start;
1118 range->last = v->domain->geometry.aperture_end;
1121 range->last = ULLONG_MAX;
1125 static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
1127 struct vhost_vdpa_as *as;
1130 vhost_dev_cleanup(&v->vdev);
1133 for (asid = 0; asid < v->vdpa->nas; asid++) {
1134 as = asid_to_as(v, asid);
1136 vhost_vdpa_remove_as(v, asid);
1140 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
1142 struct vhost_vdpa *v;
1143 struct vhost_dev *dev;
1144 struct vhost_virtqueue **vqs;
1148 v = container_of(inode->i_cdev, struct vhost_vdpa, cdev);
1150 opened = atomic_cmpxchg(&v->opened, 0, 1);
1155 r = vhost_vdpa_reset(v);
1159 vqs = kmalloc_array(nvqs, sizeof(*vqs), GFP_KERNEL);
1166 for (i = 0; i < nvqs; i++) {
1167 vqs[i] = &v->vqs[i];
1168 vqs[i]->handle_kick = handle_vq_kick;
1170 vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
1171 vhost_vdpa_process_iotlb_msg);
1173 r = vhost_vdpa_alloc_domain(v);
1175 goto err_alloc_domain;
1177 vhost_vdpa_set_iova_range(v);
1179 filep->private_data = v;
1184 vhost_vdpa_cleanup(v);
1186 atomic_dec(&v->opened);
1190 static void vhost_vdpa_clean_irq(struct vhost_vdpa *v)
1194 for (i = 0; i < v->nvqs; i++)
1195 vhost_vdpa_unsetup_vq_irq(v, i);
1198 static int vhost_vdpa_release(struct inode *inode, struct file *filep)
1200 struct vhost_vdpa *v = filep->private_data;
1201 struct vhost_dev *d = &v->vdev;
1203 mutex_lock(&d->mutex);
1204 filep->private_data = NULL;
1205 vhost_vdpa_clean_irq(v);
1206 vhost_vdpa_reset(v);
1207 vhost_dev_stop(&v->vdev);
1208 vhost_vdpa_free_domain(v);
1209 vhost_vdpa_config_put(v);
1210 vhost_dev_cleanup(&v->vdev);
1211 mutex_unlock(&d->mutex);
1213 atomic_dec(&v->opened);
1214 complete(&v->completion);
1220 static vm_fault_t vhost_vdpa_fault(struct vm_fault *vmf)
1222 struct vhost_vdpa *v = vmf->vma->vm_file->private_data;
1223 struct vdpa_device *vdpa = v->vdpa;
1224 const struct vdpa_config_ops *ops = vdpa->config;
1225 struct vdpa_notification_area notify;
1226 struct vm_area_struct *vma = vmf->vma;
1227 u16 index = vma->vm_pgoff;
1229 notify = ops->get_vq_notification(vdpa, index);
1231 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1232 if (remap_pfn_range(vma, vmf->address & PAGE_MASK,
1233 PFN_DOWN(notify.addr), PAGE_SIZE,
1235 return VM_FAULT_SIGBUS;
1237 return VM_FAULT_NOPAGE;
1240 static const struct vm_operations_struct vhost_vdpa_vm_ops = {
1241 .fault = vhost_vdpa_fault,
1244 static int vhost_vdpa_mmap(struct file *file, struct vm_area_struct *vma)
1246 struct vhost_vdpa *v = vma->vm_file->private_data;
1247 struct vdpa_device *vdpa = v->vdpa;
1248 const struct vdpa_config_ops *ops = vdpa->config;
1249 struct vdpa_notification_area notify;
1250 unsigned long index = vma->vm_pgoff;
1252 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1254 if ((vma->vm_flags & VM_SHARED) == 0)
1256 if (vma->vm_flags & VM_READ)
1260 if (!ops->get_vq_notification)
1263 /* To be safe and easily modelled by userspace, We only
1264 * support the doorbell which sits on the page boundary and
1265 * does not share the page with other registers.
1267 notify = ops->get_vq_notification(vdpa, index);
1268 if (notify.addr & (PAGE_SIZE - 1))
1270 if (vma->vm_end - vma->vm_start != notify.size)
1273 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1274 vma->vm_ops = &vhost_vdpa_vm_ops;
1277 #endif /* CONFIG_MMU */
1279 static const struct file_operations vhost_vdpa_fops = {
1280 .owner = THIS_MODULE,
1281 .open = vhost_vdpa_open,
1282 .release = vhost_vdpa_release,
1283 .write_iter = vhost_vdpa_chr_write_iter,
1284 .unlocked_ioctl = vhost_vdpa_unlocked_ioctl,
1286 .mmap = vhost_vdpa_mmap,
1287 #endif /* CONFIG_MMU */
1288 .compat_ioctl = compat_ptr_ioctl,
1291 static void vhost_vdpa_release_dev(struct device *device)
1293 struct vhost_vdpa *v =
1294 container_of(device, struct vhost_vdpa, dev);
1296 ida_simple_remove(&vhost_vdpa_ida, v->minor);
1301 static int vhost_vdpa_probe(struct vdpa_device *vdpa)
1303 const struct vdpa_config_ops *ops = vdpa->config;
1304 struct vhost_vdpa *v;
1308 /* We can't support platform IOMMU device with more than 1
1311 if (!ops->set_map && !ops->dma_map &&
1312 (vdpa->ngroups > 1 || vdpa->nas > 1))
1315 v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
1319 minor = ida_simple_get(&vhost_vdpa_ida, 0,
1320 VHOST_VDPA_DEV_MAX, GFP_KERNEL);
1326 atomic_set(&v->opened, 0);
1329 v->nvqs = vdpa->nvqs;
1330 v->virtio_id = ops->get_device_id(vdpa);
1332 device_initialize(&v->dev);
1333 v->dev.release = vhost_vdpa_release_dev;
1334 v->dev.parent = &vdpa->dev;
1335 v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
1336 v->vqs = kmalloc_array(v->nvqs, sizeof(struct vhost_virtqueue),
1343 r = dev_set_name(&v->dev, "vhost-vdpa-%u", minor);
1347 cdev_init(&v->cdev, &vhost_vdpa_fops);
1348 v->cdev.owner = THIS_MODULE;
1350 r = cdev_device_add(&v->cdev, &v->dev);
1354 init_completion(&v->completion);
1355 vdpa_set_drvdata(vdpa, v);
1357 for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
1358 INIT_HLIST_HEAD(&v->as[i]);
1363 put_device(&v->dev);
1367 static void vhost_vdpa_remove(struct vdpa_device *vdpa)
1369 struct vhost_vdpa *v = vdpa_get_drvdata(vdpa);
1372 cdev_device_del(&v->cdev, &v->dev);
1375 opened = atomic_cmpxchg(&v->opened, 0, 1);
1378 wait_for_completion(&v->completion);
1381 put_device(&v->dev);
1384 static struct vdpa_driver vhost_vdpa_driver = {
1386 .name = "vhost_vdpa",
1388 .probe = vhost_vdpa_probe,
1389 .remove = vhost_vdpa_remove,
1392 static int __init vhost_vdpa_init(void)
1396 r = alloc_chrdev_region(&vhost_vdpa_major, 0, VHOST_VDPA_DEV_MAX,
1399 goto err_alloc_chrdev;
1401 r = vdpa_register_driver(&vhost_vdpa_driver);
1403 goto err_vdpa_register_driver;
1407 err_vdpa_register_driver:
1408 unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX);
1412 module_init(vhost_vdpa_init);
1414 static void __exit vhost_vdpa_exit(void)
1416 vdpa_unregister_driver(&vhost_vdpa_driver);
1417 unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX);
1419 module_exit(vhost_vdpa_exit);
1421 MODULE_VERSION("0.0.1");
1422 MODULE_LICENSE("GPL v2");
1423 MODULE_AUTHOR("Intel Corporation");
1424 MODULE_DESCRIPTION("vDPA-based vhost backend for virtio");