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:
502 if (!ops->get_vq_group)
505 s.num = ops->get_vq_group(vdpa, idx);
506 if (s.num >= vdpa->ngroups)
508 else if (copy_to_user(argp, &s, sizeof(s)))
511 case VHOST_VDPA_SET_GROUP_ASID:
512 if (copy_from_user(&s, argp, sizeof(s)))
514 if (s.num >= vdpa->nas)
516 if (!ops->set_group_asid)
518 return ops->set_group_asid(vdpa, idx, s.num);
519 case VHOST_GET_VRING_BASE:
520 r = ops->get_vq_state(v->vdpa, idx, &vq_state);
524 vq->last_avail_idx = vq_state.split.avail_index;
528 r = vhost_vring_ioctl(&v->vdev, cmd, argp);
533 case VHOST_SET_VRING_ADDR:
534 if (ops->set_vq_address(vdpa, idx,
535 (u64)(uintptr_t)vq->desc,
536 (u64)(uintptr_t)vq->avail,
537 (u64)(uintptr_t)vq->used))
541 case VHOST_SET_VRING_BASE:
542 vq_state.split.avail_index = vq->last_avail_idx;
543 if (ops->set_vq_state(vdpa, idx, &vq_state))
547 case VHOST_SET_VRING_CALL:
548 if (vq->call_ctx.ctx) {
549 cb.callback = vhost_vdpa_virtqueue_cb;
555 ops->set_vq_cb(vdpa, idx, &cb);
556 vhost_vdpa_setup_vq_irq(v, idx);
559 case VHOST_SET_VRING_NUM:
560 ops->set_vq_num(vdpa, idx, vq->num);
567 static long vhost_vdpa_unlocked_ioctl(struct file *filep,
568 unsigned int cmd, unsigned long arg)
570 struct vhost_vdpa *v = filep->private_data;
571 struct vhost_dev *d = &v->vdev;
572 void __user *argp = (void __user *)arg;
573 u64 __user *featurep = argp;
577 if (cmd == VHOST_SET_BACKEND_FEATURES) {
578 if (copy_from_user(&features, featurep, sizeof(features)))
580 if (features & ~VHOST_VDPA_BACKEND_FEATURES)
582 vhost_set_backend_features(&v->vdev, features);
586 mutex_lock(&d->mutex);
589 case VHOST_VDPA_GET_DEVICE_ID:
590 r = vhost_vdpa_get_device_id(v, argp);
592 case VHOST_VDPA_GET_STATUS:
593 r = vhost_vdpa_get_status(v, argp);
595 case VHOST_VDPA_SET_STATUS:
596 r = vhost_vdpa_set_status(v, argp);
598 case VHOST_VDPA_GET_CONFIG:
599 r = vhost_vdpa_get_config(v, argp);
601 case VHOST_VDPA_SET_CONFIG:
602 r = vhost_vdpa_set_config(v, argp);
604 case VHOST_GET_FEATURES:
605 r = vhost_vdpa_get_features(v, argp);
607 case VHOST_SET_FEATURES:
608 r = vhost_vdpa_set_features(v, argp);
610 case VHOST_VDPA_GET_VRING_NUM:
611 r = vhost_vdpa_get_vring_num(v, argp);
613 case VHOST_VDPA_GET_GROUP_NUM:
614 if (copy_to_user(argp, &v->vdpa->ngroups,
615 sizeof(v->vdpa->ngroups)))
618 case VHOST_VDPA_GET_AS_NUM:
619 if (copy_to_user(argp, &v->vdpa->nas, sizeof(v->vdpa->nas)))
622 case VHOST_SET_LOG_BASE:
623 case VHOST_SET_LOG_FD:
626 case VHOST_VDPA_SET_CONFIG_CALL:
627 r = vhost_vdpa_set_config_call(v, argp);
629 case VHOST_GET_BACKEND_FEATURES:
630 features = VHOST_VDPA_BACKEND_FEATURES;
631 if (copy_to_user(featurep, &features, sizeof(features)))
634 case VHOST_VDPA_GET_IOVA_RANGE:
635 r = vhost_vdpa_get_iova_range(v, argp);
637 case VHOST_VDPA_GET_CONFIG_SIZE:
638 r = vhost_vdpa_get_config_size(v, argp);
640 case VHOST_VDPA_GET_VQS_COUNT:
641 r = vhost_vdpa_get_vqs_count(v, argp);
644 r = vhost_dev_ioctl(&v->vdev, cmd, argp);
645 if (r == -ENOIOCTLCMD)
646 r = vhost_vdpa_vring_ioctl(v, cmd, argp);
650 mutex_unlock(&d->mutex);
654 static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v,
655 struct vhost_iotlb *iotlb,
658 struct vhost_dev *dev = &v->vdev;
659 struct vhost_iotlb_map *map;
661 unsigned long pfn, pinned;
663 while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) {
664 pinned = PFN_DOWN(map->size);
665 for (pfn = PFN_DOWN(map->addr);
666 pinned > 0; pfn++, pinned--) {
667 page = pfn_to_page(pfn);
668 if (map->perm & VHOST_ACCESS_WO)
669 set_page_dirty_lock(page);
670 unpin_user_page(page);
672 atomic64_sub(PFN_DOWN(map->size), &dev->mm->pinned_vm);
673 vhost_iotlb_map_free(iotlb, map);
677 static void vhost_vdpa_va_unmap(struct vhost_vdpa *v,
678 struct vhost_iotlb *iotlb,
681 struct vhost_iotlb_map *map;
682 struct vdpa_map_file *map_file;
684 while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) {
685 map_file = (struct vdpa_map_file *)map->opaque;
686 fput(map_file->file);
688 vhost_iotlb_map_free(iotlb, map);
692 static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
693 struct vhost_iotlb *iotlb,
696 struct vdpa_device *vdpa = v->vdpa;
699 return vhost_vdpa_va_unmap(v, iotlb, start, last);
701 return vhost_vdpa_pa_unmap(v, iotlb, start, last);
704 static int perm_to_iommu_flags(u32 perm)
709 case VHOST_ACCESS_WO:
710 flags |= IOMMU_WRITE;
712 case VHOST_ACCESS_RO:
715 case VHOST_ACCESS_RW:
716 flags |= (IOMMU_WRITE | IOMMU_READ);
719 WARN(1, "invalidate vhost IOTLB permission\n");
723 return flags | IOMMU_CACHE;
726 static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
727 u64 iova, u64 size, u64 pa, u32 perm, void *opaque)
729 struct vhost_dev *dev = &v->vdev;
730 struct vdpa_device *vdpa = v->vdpa;
731 const struct vdpa_config_ops *ops = vdpa->config;
732 u32 asid = iotlb_to_asid(iotlb);
735 r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1,
741 r = ops->dma_map(vdpa, asid, iova, size, pa, perm, opaque);
742 } else if (ops->set_map) {
744 r = ops->set_map(vdpa, asid, iotlb);
746 r = iommu_map(v->domain, iova, pa, size,
747 perm_to_iommu_flags(perm));
750 vhost_iotlb_del_range(iotlb, iova, iova + size - 1);
755 atomic64_add(PFN_DOWN(size), &dev->mm->pinned_vm);
760 static void vhost_vdpa_unmap(struct vhost_vdpa *v,
761 struct vhost_iotlb *iotlb,
764 struct vdpa_device *vdpa = v->vdpa;
765 const struct vdpa_config_ops *ops = vdpa->config;
766 u32 asid = iotlb_to_asid(iotlb);
768 vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
771 ops->dma_unmap(vdpa, asid, iova, size);
772 } else if (ops->set_map) {
774 ops->set_map(vdpa, asid, iotlb);
776 iommu_unmap(v->domain, iova, size);
779 /* If we are in the middle of batch processing, delay the free
780 * of AS until BATCH_END.
782 if (!v->in_batch && !iotlb->nmaps)
783 vhost_vdpa_remove_as(v, asid);
786 static int vhost_vdpa_va_map(struct vhost_vdpa *v,
787 struct vhost_iotlb *iotlb,
788 u64 iova, u64 size, u64 uaddr, u32 perm)
790 struct vhost_dev *dev = &v->vdev;
791 u64 offset, map_size, map_iova = iova;
792 struct vdpa_map_file *map_file;
793 struct vm_area_struct *vma;
796 mmap_read_lock(dev->mm);
799 vma = find_vma(dev->mm, uaddr);
804 map_size = min(size, vma->vm_end - uaddr);
805 if (!(vma->vm_file && (vma->vm_flags & VM_SHARED) &&
806 !(vma->vm_flags & (VM_IO | VM_PFNMAP))))
809 map_file = kzalloc(sizeof(*map_file), GFP_KERNEL);
814 offset = (vma->vm_pgoff << PAGE_SHIFT) + uaddr - vma->vm_start;
815 map_file->offset = offset;
816 map_file->file = get_file(vma->vm_file);
817 ret = vhost_vdpa_map(v, iotlb, map_iova, map_size, uaddr,
820 fput(map_file->file);
827 map_iova += map_size;
830 vhost_vdpa_unmap(v, iotlb, iova, map_iova - iova);
832 mmap_read_unlock(dev->mm);
837 static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
838 struct vhost_iotlb *iotlb,
839 u64 iova, u64 size, u64 uaddr, u32 perm)
841 struct vhost_dev *dev = &v->vdev;
842 struct page **page_list;
843 unsigned long list_size = PAGE_SIZE / sizeof(struct page *);
844 unsigned int gup_flags = FOLL_LONGTERM;
845 unsigned long npages, cur_base, map_pfn, last_pfn = 0;
846 unsigned long lock_limit, sz2pin, nchunks, i;
851 /* Limit the use of memory for bookkeeping */
852 page_list = (struct page **) __get_free_page(GFP_KERNEL);
856 if (perm & VHOST_ACCESS_WO)
857 gup_flags |= FOLL_WRITE;
859 npages = PFN_UP(size + (iova & ~PAGE_MASK));
865 mmap_read_lock(dev->mm);
867 lock_limit = PFN_DOWN(rlimit(RLIMIT_MEMLOCK));
868 if (npages + atomic64_read(&dev->mm->pinned_vm) > lock_limit) {
873 cur_base = uaddr & PAGE_MASK;
878 sz2pin = min_t(unsigned long, npages, list_size);
879 pinned = pin_user_pages(cur_base, sz2pin,
880 gup_flags, page_list, NULL);
881 if (sz2pin != pinned) {
885 unpin_user_pages(page_list, pinned);
893 map_pfn = page_to_pfn(page_list[0]);
895 for (i = 0; i < pinned; i++) {
896 unsigned long this_pfn = page_to_pfn(page_list[i]);
899 if (last_pfn && (this_pfn != last_pfn + 1)) {
900 /* Pin a contiguous chunk of memory */
901 csize = PFN_PHYS(last_pfn - map_pfn + 1);
902 ret = vhost_vdpa_map(v, iotlb, iova, csize,
907 * Unpin the pages that are left unmapped
908 * from this point on in the current
909 * page_list. The remaining outstanding
910 * ones which may stride across several
911 * chunks will be covered in the common
912 * error path subsequently.
914 unpin_user_pages(&page_list[i],
927 cur_base += PFN_PHYS(pinned);
931 /* Pin the rest chunk */
932 ret = vhost_vdpa_map(v, iotlb, iova, PFN_PHYS(last_pfn - map_pfn + 1),
933 PFN_PHYS(map_pfn), perm, NULL);
940 * Unpin the outstanding pages which are yet to be
941 * mapped but haven't due to vdpa_map() or
942 * pin_user_pages() failure.
944 * Mapped pages are accounted in vdpa_map(), hence
945 * the corresponding unpinning will be handled by
949 for (pfn = map_pfn; pfn <= last_pfn; pfn++)
950 unpin_user_page(pfn_to_page(pfn));
952 vhost_vdpa_unmap(v, iotlb, start, size);
955 mmap_read_unlock(dev->mm);
957 free_page((unsigned long)page_list);
962 static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
963 struct vhost_iotlb *iotlb,
964 struct vhost_iotlb_msg *msg)
966 struct vdpa_device *vdpa = v->vdpa;
968 if (msg->iova < v->range.first || !msg->size ||
969 msg->iova > U64_MAX - msg->size + 1 ||
970 msg->iova + msg->size - 1 > v->range.last)
973 if (vhost_iotlb_itree_first(iotlb, msg->iova,
974 msg->iova + msg->size - 1))
978 return vhost_vdpa_va_map(v, iotlb, msg->iova, msg->size,
979 msg->uaddr, msg->perm);
981 return vhost_vdpa_pa_map(v, iotlb, msg->iova, msg->size, msg->uaddr,
985 static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
986 struct vhost_iotlb_msg *msg)
988 struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
989 struct vdpa_device *vdpa = v->vdpa;
990 const struct vdpa_config_ops *ops = vdpa->config;
991 struct vhost_iotlb *iotlb = NULL;
992 struct vhost_vdpa_as *as = NULL;
995 mutex_lock(&dev->mutex);
997 r = vhost_dev_check_owner(dev);
1001 if (msg->type == VHOST_IOTLB_UPDATE ||
1002 msg->type == VHOST_IOTLB_BATCH_BEGIN) {
1003 as = vhost_vdpa_find_alloc_as(v, asid);
1005 dev_err(&v->dev, "can't find and alloc asid %d\n",
1012 iotlb = asid_to_iotlb(v, asid);
1014 if ((v->in_batch && v->batch_asid != asid) || !iotlb) {
1015 if (v->in_batch && v->batch_asid != asid) {
1016 dev_info(&v->dev, "batch id %d asid %d\n",
1017 v->batch_asid, asid);
1020 dev_err(&v->dev, "no iotlb for asid %d\n", asid);
1025 switch (msg->type) {
1026 case VHOST_IOTLB_UPDATE:
1027 r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
1029 case VHOST_IOTLB_INVALIDATE:
1030 vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
1032 case VHOST_IOTLB_BATCH_BEGIN:
1033 v->batch_asid = asid;
1036 case VHOST_IOTLB_BATCH_END:
1037 if (v->in_batch && ops->set_map)
1038 ops->set_map(vdpa, asid, iotlb);
1039 v->in_batch = false;
1041 vhost_vdpa_remove_as(v, asid);
1048 mutex_unlock(&dev->mutex);
1053 static ssize_t vhost_vdpa_chr_write_iter(struct kiocb *iocb,
1054 struct iov_iter *from)
1056 struct file *file = iocb->ki_filp;
1057 struct vhost_vdpa *v = file->private_data;
1058 struct vhost_dev *dev = &v->vdev;
1060 return vhost_chr_write_iter(dev, from);
1063 static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v)
1065 struct vdpa_device *vdpa = v->vdpa;
1066 const struct vdpa_config_ops *ops = vdpa->config;
1067 struct device *dma_dev = vdpa_get_dma_dev(vdpa);
1068 struct bus_type *bus;
1071 /* Device want to do DMA by itself */
1072 if (ops->set_map || ops->dma_map)
1079 if (!iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
1082 v->domain = iommu_domain_alloc(bus);
1086 ret = iommu_attach_device(v->domain, dma_dev);
1093 iommu_domain_free(v->domain);
1097 static void vhost_vdpa_free_domain(struct vhost_vdpa *v)
1099 struct vdpa_device *vdpa = v->vdpa;
1100 struct device *dma_dev = vdpa_get_dma_dev(vdpa);
1103 iommu_detach_device(v->domain, dma_dev);
1104 iommu_domain_free(v->domain);
1110 static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
1112 struct vdpa_iova_range *range = &v->range;
1113 struct vdpa_device *vdpa = v->vdpa;
1114 const struct vdpa_config_ops *ops = vdpa->config;
1116 if (ops->get_iova_range) {
1117 *range = ops->get_iova_range(vdpa);
1118 } else if (v->domain && v->domain->geometry.force_aperture) {
1119 range->first = v->domain->geometry.aperture_start;
1120 range->last = v->domain->geometry.aperture_end;
1123 range->last = ULLONG_MAX;
1127 static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
1129 struct vhost_vdpa_as *as;
1132 vhost_dev_cleanup(&v->vdev);
1135 for (asid = 0; asid < v->vdpa->nas; asid++) {
1136 as = asid_to_as(v, asid);
1138 vhost_vdpa_remove_as(v, asid);
1142 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
1144 struct vhost_vdpa *v;
1145 struct vhost_dev *dev;
1146 struct vhost_virtqueue **vqs;
1150 v = container_of(inode->i_cdev, struct vhost_vdpa, cdev);
1152 opened = atomic_cmpxchg(&v->opened, 0, 1);
1157 r = vhost_vdpa_reset(v);
1161 vqs = kmalloc_array(nvqs, sizeof(*vqs), GFP_KERNEL);
1168 for (i = 0; i < nvqs; i++) {
1169 vqs[i] = &v->vqs[i];
1170 vqs[i]->handle_kick = handle_vq_kick;
1172 vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
1173 vhost_vdpa_process_iotlb_msg);
1175 r = vhost_vdpa_alloc_domain(v);
1177 goto err_alloc_domain;
1179 vhost_vdpa_set_iova_range(v);
1181 filep->private_data = v;
1186 vhost_vdpa_cleanup(v);
1188 atomic_dec(&v->opened);
1192 static void vhost_vdpa_clean_irq(struct vhost_vdpa *v)
1196 for (i = 0; i < v->nvqs; i++)
1197 vhost_vdpa_unsetup_vq_irq(v, i);
1200 static int vhost_vdpa_release(struct inode *inode, struct file *filep)
1202 struct vhost_vdpa *v = filep->private_data;
1203 struct vhost_dev *d = &v->vdev;
1205 mutex_lock(&d->mutex);
1206 filep->private_data = NULL;
1207 vhost_vdpa_clean_irq(v);
1208 vhost_vdpa_reset(v);
1209 vhost_dev_stop(&v->vdev);
1210 vhost_vdpa_free_domain(v);
1211 vhost_vdpa_config_put(v);
1212 vhost_vdpa_cleanup(v);
1213 mutex_unlock(&d->mutex);
1215 atomic_dec(&v->opened);
1216 complete(&v->completion);
1222 static vm_fault_t vhost_vdpa_fault(struct vm_fault *vmf)
1224 struct vhost_vdpa *v = vmf->vma->vm_file->private_data;
1225 struct vdpa_device *vdpa = v->vdpa;
1226 const struct vdpa_config_ops *ops = vdpa->config;
1227 struct vdpa_notification_area notify;
1228 struct vm_area_struct *vma = vmf->vma;
1229 u16 index = vma->vm_pgoff;
1231 notify = ops->get_vq_notification(vdpa, index);
1233 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1234 if (remap_pfn_range(vma, vmf->address & PAGE_MASK,
1235 PFN_DOWN(notify.addr), PAGE_SIZE,
1237 return VM_FAULT_SIGBUS;
1239 return VM_FAULT_NOPAGE;
1242 static const struct vm_operations_struct vhost_vdpa_vm_ops = {
1243 .fault = vhost_vdpa_fault,
1246 static int vhost_vdpa_mmap(struct file *file, struct vm_area_struct *vma)
1248 struct vhost_vdpa *v = vma->vm_file->private_data;
1249 struct vdpa_device *vdpa = v->vdpa;
1250 const struct vdpa_config_ops *ops = vdpa->config;
1251 struct vdpa_notification_area notify;
1252 unsigned long index = vma->vm_pgoff;
1254 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1256 if ((vma->vm_flags & VM_SHARED) == 0)
1258 if (vma->vm_flags & VM_READ)
1262 if (!ops->get_vq_notification)
1265 /* To be safe and easily modelled by userspace, We only
1266 * support the doorbell which sits on the page boundary and
1267 * does not share the page with other registers.
1269 notify = ops->get_vq_notification(vdpa, index);
1270 if (notify.addr & (PAGE_SIZE - 1))
1272 if (vma->vm_end - vma->vm_start != notify.size)
1275 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1276 vma->vm_ops = &vhost_vdpa_vm_ops;
1279 #endif /* CONFIG_MMU */
1281 static const struct file_operations vhost_vdpa_fops = {
1282 .owner = THIS_MODULE,
1283 .open = vhost_vdpa_open,
1284 .release = vhost_vdpa_release,
1285 .write_iter = vhost_vdpa_chr_write_iter,
1286 .unlocked_ioctl = vhost_vdpa_unlocked_ioctl,
1288 .mmap = vhost_vdpa_mmap,
1289 #endif /* CONFIG_MMU */
1290 .compat_ioctl = compat_ptr_ioctl,
1293 static void vhost_vdpa_release_dev(struct device *device)
1295 struct vhost_vdpa *v =
1296 container_of(device, struct vhost_vdpa, dev);
1298 ida_simple_remove(&vhost_vdpa_ida, v->minor);
1303 static int vhost_vdpa_probe(struct vdpa_device *vdpa)
1305 const struct vdpa_config_ops *ops = vdpa->config;
1306 struct vhost_vdpa *v;
1310 /* We can't support platform IOMMU device with more than 1
1313 if (!ops->set_map && !ops->dma_map &&
1314 (vdpa->ngroups > 1 || vdpa->nas > 1))
1317 v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
1321 minor = ida_simple_get(&vhost_vdpa_ida, 0,
1322 VHOST_VDPA_DEV_MAX, GFP_KERNEL);
1328 atomic_set(&v->opened, 0);
1331 v->nvqs = vdpa->nvqs;
1332 v->virtio_id = ops->get_device_id(vdpa);
1334 device_initialize(&v->dev);
1335 v->dev.release = vhost_vdpa_release_dev;
1336 v->dev.parent = &vdpa->dev;
1337 v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
1338 v->vqs = kmalloc_array(v->nvqs, sizeof(struct vhost_virtqueue),
1345 r = dev_set_name(&v->dev, "vhost-vdpa-%u", minor);
1349 cdev_init(&v->cdev, &vhost_vdpa_fops);
1350 v->cdev.owner = THIS_MODULE;
1352 r = cdev_device_add(&v->cdev, &v->dev);
1356 init_completion(&v->completion);
1357 vdpa_set_drvdata(vdpa, v);
1359 for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
1360 INIT_HLIST_HEAD(&v->as[i]);
1365 put_device(&v->dev);
1369 static void vhost_vdpa_remove(struct vdpa_device *vdpa)
1371 struct vhost_vdpa *v = vdpa_get_drvdata(vdpa);
1374 cdev_device_del(&v->cdev, &v->dev);
1377 opened = atomic_cmpxchg(&v->opened, 0, 1);
1380 wait_for_completion(&v->completion);
1383 put_device(&v->dev);
1386 static struct vdpa_driver vhost_vdpa_driver = {
1388 .name = "vhost_vdpa",
1390 .probe = vhost_vdpa_probe,
1391 .remove = vhost_vdpa_remove,
1394 static int __init vhost_vdpa_init(void)
1398 r = alloc_chrdev_region(&vhost_vdpa_major, 0, VHOST_VDPA_DEV_MAX,
1401 goto err_alloc_chrdev;
1403 r = vdpa_register_driver(&vhost_vdpa_driver);
1405 goto err_vdpa_register_driver;
1409 err_vdpa_register_driver:
1410 unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX);
1414 module_init(vhost_vdpa_init);
1416 static void __exit vhost_vdpa_exit(void)
1418 vdpa_unregister_driver(&vhost_vdpa_driver);
1419 unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX);
1421 module_exit(vhost_vdpa_exit);
1423 MODULE_VERSION("0.0.1");
1424 MODULE_LICENSE("GPL v2");
1425 MODULE_AUTHOR("Intel Corporation");
1426 MODULE_DESCRIPTION("vDPA-based vhost backend for virtio");