2 * KVMGT - the implementation of Intel mediated pass-through framework for KVM
4 * Copyright(c) 2014-2016 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Kevin Tian <kevin.tian@intel.com>
27 * Jike Song <jike.song@intel.com>
28 * Xiaoguang Chen <xiaoguang.chen@intel.com>
31 #include <linux/init.h>
32 #include <linux/device.h>
34 #include <linux/mmu_context.h>
35 #include <linux/types.h>
36 #include <linux/list.h>
37 #include <linux/rbtree.h>
38 #include <linux/spinlock.h>
39 #include <linux/eventfd.h>
40 #include <linux/uuid.h>
41 #include <linux/kvm_host.h>
42 #include <linux/vfio.h>
43 #include <linux/mdev.h>
48 static const struct intel_gvt_ops *intel_gvt_ops;
50 /* helper macros copied from vfio-pci */
51 #define VFIO_PCI_OFFSET_SHIFT 40
52 #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
53 #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
54 #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
65 struct hlist_node hnode;
68 struct kvmgt_guest_info {
70 struct intel_vgpu *vgpu;
71 struct kvm_page_track_notifier_node track_node;
72 #define NR_BKT (1 << 18)
73 struct hlist_head ptable[NR_BKT];
83 static inline bool handle_valid(unsigned long handle)
85 return !!(handle & ~0xff);
88 static int kvmgt_guest_init(struct mdev_device *mdev);
89 static void intel_vgpu_release_work(struct work_struct *work);
90 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info);
92 static struct gvt_dma *__gvt_cache_find(struct intel_vgpu *vgpu, gfn_t gfn)
94 struct rb_node *node = vgpu->vdev.cache.rb_node;
95 struct gvt_dma *ret = NULL;
98 struct gvt_dma *itr = rb_entry(node, struct gvt_dma, node);
101 node = node->rb_left;
102 else if (gfn > itr->gfn)
103 node = node->rb_right;
114 static kvm_pfn_t gvt_cache_find(struct intel_vgpu *vgpu, gfn_t gfn)
116 struct gvt_dma *entry;
119 mutex_lock(&vgpu->vdev.cache_lock);
121 entry = __gvt_cache_find(vgpu, gfn);
122 pfn = (entry == NULL) ? 0 : entry->pfn;
124 mutex_unlock(&vgpu->vdev.cache_lock);
128 static void gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn, kvm_pfn_t pfn)
130 struct gvt_dma *new, *itr;
131 struct rb_node **link = &vgpu->vdev.cache.rb_node, *parent = NULL;
133 new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
140 mutex_lock(&vgpu->vdev.cache_lock);
143 itr = rb_entry(parent, struct gvt_dma, node);
147 else if (gfn < itr->gfn)
148 link = &parent->rb_left;
150 link = &parent->rb_right;
153 rb_link_node(&new->node, parent, link);
154 rb_insert_color(&new->node, &vgpu->vdev.cache);
155 mutex_unlock(&vgpu->vdev.cache_lock);
159 mutex_unlock(&vgpu->vdev.cache_lock);
163 static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
164 struct gvt_dma *entry)
166 rb_erase(&entry->node, &vgpu->vdev.cache);
170 static void gvt_cache_remove(struct intel_vgpu *vgpu, gfn_t gfn)
172 struct device *dev = mdev_dev(vgpu->vdev.mdev);
173 struct gvt_dma *this;
177 mutex_lock(&vgpu->vdev.cache_lock);
178 this = __gvt_cache_find(vgpu, gfn);
180 mutex_unlock(&vgpu->vdev.cache_lock);
185 rc = vfio_unpin_pages(dev, &g1, 1);
187 __gvt_cache_remove_entry(vgpu, this);
188 mutex_unlock(&vgpu->vdev.cache_lock);
191 static void gvt_cache_init(struct intel_vgpu *vgpu)
193 vgpu->vdev.cache = RB_ROOT;
194 mutex_init(&vgpu->vdev.cache_lock);
197 static void gvt_cache_destroy(struct intel_vgpu *vgpu)
200 struct rb_node *node = NULL;
201 struct device *dev = mdev_dev(vgpu->vdev.mdev);
204 mutex_lock(&vgpu->vdev.cache_lock);
205 while ((node = rb_first(&vgpu->vdev.cache))) {
206 dma = rb_entry(node, struct gvt_dma, node);
209 vfio_unpin_pages(dev, &gfn, 1);
210 __gvt_cache_remove_entry(vgpu, dma);
212 mutex_unlock(&vgpu->vdev.cache_lock);
215 static struct intel_vgpu_type *intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
219 struct intel_vgpu_type *t;
220 const char *driver_name = dev_driver_string(
221 &gvt->dev_priv->drm.pdev->dev);
223 for (i = 0; i < gvt->num_types; i++) {
225 if (!strncmp(t->name, name + strlen(driver_name) + 1,
233 static ssize_t available_instance_show(struct kobject *kobj, struct device *dev,
236 struct intel_vgpu_type *type;
237 unsigned int num = 0;
238 void *gvt = kdev_to_i915(dev)->gvt;
240 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
244 num = type->avail_instance;
246 return sprintf(buf, "%u\n", num);
249 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
252 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
255 static ssize_t description_show(struct kobject *kobj, struct device *dev,
258 struct intel_vgpu_type *type;
259 void *gvt = kdev_to_i915(dev)->gvt;
261 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
265 return sprintf(buf, "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
267 BYTES_TO_MB(type->low_gm_size),
268 BYTES_TO_MB(type->high_gm_size),
272 static MDEV_TYPE_ATTR_RO(available_instance);
273 static MDEV_TYPE_ATTR_RO(device_api);
274 static MDEV_TYPE_ATTR_RO(description);
276 static struct attribute *type_attrs[] = {
277 &mdev_type_attr_available_instance.attr,
278 &mdev_type_attr_device_api.attr,
279 &mdev_type_attr_description.attr,
283 static struct attribute_group *intel_vgpu_type_groups[] = {
284 [0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
287 static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
290 struct intel_vgpu_type *type;
291 struct attribute_group *group;
293 for (i = 0; i < gvt->num_types; i++) {
294 type = &gvt->types[i];
296 group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
300 group->name = type->name;
301 group->attrs = type_attrs;
302 intel_vgpu_type_groups[i] = group;
308 for (j = 0; j < i; j++) {
309 group = intel_vgpu_type_groups[j];
316 static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
319 struct attribute_group *group;
321 for (i = 0; i < gvt->num_types; i++) {
322 group = intel_vgpu_type_groups[i];
327 static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
329 hash_init(info->ptable);
332 static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
334 struct kvmgt_pgfn *p;
335 struct hlist_node *tmp;
338 hash_for_each_safe(info->ptable, i, tmp, p, hnode) {
344 static struct kvmgt_pgfn *
345 __kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
347 struct kvmgt_pgfn *p, *res = NULL;
349 hash_for_each_possible(info->ptable, p, hnode, gfn) {
359 static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info,
362 struct kvmgt_pgfn *p;
364 p = __kvmgt_protect_table_find(info, gfn);
368 static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
370 struct kvmgt_pgfn *p;
372 if (kvmgt_gfn_is_write_protected(info, gfn))
375 p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC);
376 if (WARN(!p, "gfn: 0x%llx\n", gfn))
380 hash_add(info->ptable, &p->hnode, gfn);
383 static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
386 struct kvmgt_pgfn *p;
388 p = __kvmgt_protect_table_find(info, gfn);
395 static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
397 struct intel_vgpu *vgpu;
398 struct intel_vgpu_type *type;
402 pdev = mdev_parent_dev(mdev);
403 gvt = kdev_to_i915(pdev)->gvt;
405 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
407 gvt_err("failed to find type %s to create\n",
412 vgpu = intel_gvt_ops->vgpu_create(gvt, type);
413 if (IS_ERR_OR_NULL(vgpu)) {
414 gvt_err("create intel vgpu failed\n");
418 INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work);
420 vgpu->vdev.mdev = mdev;
421 mdev_set_drvdata(mdev, vgpu);
423 gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
424 dev_name(mdev_dev(mdev)));
428 static int intel_vgpu_remove(struct mdev_device *mdev)
430 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
432 if (handle_valid(vgpu->handle))
435 intel_gvt_ops->vgpu_destroy(vgpu);
439 static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
440 unsigned long action, void *data)
442 struct intel_vgpu *vgpu = container_of(nb,
444 vdev.iommu_notifier);
446 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
447 struct vfio_iommu_type1_dma_unmap *unmap = data;
448 unsigned long gfn, end_gfn;
450 gfn = unmap->iova >> PAGE_SHIFT;
451 end_gfn = gfn + unmap->size / PAGE_SIZE;
453 while (gfn < end_gfn)
454 gvt_cache_remove(vgpu, gfn++);
460 static int intel_vgpu_group_notifier(struct notifier_block *nb,
461 unsigned long action, void *data)
463 struct intel_vgpu *vgpu = container_of(nb,
465 vdev.group_notifier);
467 /* the only action we care about */
468 if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
469 vgpu->vdev.kvm = data;
472 schedule_work(&vgpu->vdev.release_work);
478 static int intel_vgpu_open(struct mdev_device *mdev)
480 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
481 unsigned long events;
484 vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
485 vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier;
487 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
488 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events,
489 &vgpu->vdev.iommu_notifier);
491 gvt_err("vfio_register_notifier for iommu failed: %d\n", ret);
495 events = VFIO_GROUP_NOTIFY_SET_KVM;
496 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events,
497 &vgpu->vdev.group_notifier);
499 gvt_err("vfio_register_notifier for group failed: %d\n", ret);
503 ret = kvmgt_guest_init(mdev);
507 atomic_set(&vgpu->vdev.released, 0);
511 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
512 &vgpu->vdev.group_notifier);
515 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
516 &vgpu->vdev.iommu_notifier);
521 static void __intel_vgpu_release(struct intel_vgpu *vgpu)
523 struct kvmgt_guest_info *info;
526 if (!handle_valid(vgpu->handle))
529 if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1))
532 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY,
533 &vgpu->vdev.iommu_notifier);
534 WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret);
536 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY,
537 &vgpu->vdev.group_notifier);
538 WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret);
540 info = (struct kvmgt_guest_info *)vgpu->handle;
541 kvmgt_guest_exit(info);
543 vgpu->vdev.kvm = NULL;
547 static void intel_vgpu_release(struct mdev_device *mdev)
549 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
551 __intel_vgpu_release(vgpu);
554 static void intel_vgpu_release_work(struct work_struct *work)
556 struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu,
559 __intel_vgpu_release(vgpu);
562 static uint64_t intel_vgpu_get_bar0_addr(struct intel_vgpu *vgpu)
564 u32 start_lo, start_hi;
566 int pos = PCI_BASE_ADDRESS_0;
568 start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + pos)) &
569 PCI_BASE_ADDRESS_MEM_MASK;
570 mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + pos)) &
571 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
574 case PCI_BASE_ADDRESS_MEM_TYPE_64:
575 start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space
578 case PCI_BASE_ADDRESS_MEM_TYPE_32:
579 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
580 /* 1M mem BAR treated as 32-bit BAR */
582 /* mem unknown type treated as 32-bit BAR */
587 return ((u64)start_hi << 32) | start_lo;
590 static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
591 size_t count, loff_t *ppos, bool is_write)
593 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
594 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
595 uint64_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
599 if (index >= VFIO_PCI_NUM_REGIONS) {
600 gvt_err("invalid index: %u\n", index);
605 case VFIO_PCI_CONFIG_REGION_INDEX:
607 ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos,
610 ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos,
613 case VFIO_PCI_BAR0_REGION_INDEX:
614 case VFIO_PCI_BAR1_REGION_INDEX:
616 uint64_t bar0_start = intel_vgpu_get_bar0_addr(vgpu);
618 ret = intel_gvt_ops->emulate_mmio_write(vgpu,
619 bar0_start + pos, buf, count);
621 uint64_t bar0_start = intel_vgpu_get_bar0_addr(vgpu);
623 ret = intel_gvt_ops->emulate_mmio_read(vgpu,
624 bar0_start + pos, buf, count);
627 case VFIO_PCI_BAR2_REGION_INDEX:
628 case VFIO_PCI_BAR3_REGION_INDEX:
629 case VFIO_PCI_BAR4_REGION_INDEX:
630 case VFIO_PCI_BAR5_REGION_INDEX:
631 case VFIO_PCI_VGA_REGION_INDEX:
632 case VFIO_PCI_ROM_REGION_INDEX:
634 gvt_err("unsupported region: %u\n", index);
637 return ret == 0 ? count : ret;
640 static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
641 size_t count, loff_t *ppos)
643 unsigned int done = 0;
649 if (count >= 4 && !(*ppos % 4)) {
652 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
657 if (copy_to_user(buf, &val, sizeof(val)))
661 } else if (count >= 2 && !(*ppos % 2)) {
664 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
669 if (copy_to_user(buf, &val, sizeof(val)))
676 ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos,
681 if (copy_to_user(buf, &val, sizeof(val)))
699 static ssize_t intel_vgpu_write(struct mdev_device *mdev,
700 const char __user *buf,
701 size_t count, loff_t *ppos)
703 unsigned int done = 0;
709 if (count >= 4 && !(*ppos % 4)) {
712 if (copy_from_user(&val, buf, sizeof(val)))
715 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
721 } else if (count >= 2 && !(*ppos % 2)) {
724 if (copy_from_user(&val, buf, sizeof(val)))
727 ret = intel_vgpu_rw(mdev, (char *)&val,
728 sizeof(val), ppos, true);
736 if (copy_from_user(&val, buf, sizeof(val)))
739 ret = intel_vgpu_rw(mdev, &val, sizeof(val),
758 static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
762 unsigned long req_size, pgoff = 0;
764 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
766 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
767 if (index >= VFIO_PCI_ROM_REGION_INDEX)
770 if (vma->vm_end < vma->vm_start)
772 if ((vma->vm_flags & VM_SHARED) == 0)
774 if (index != VFIO_PCI_BAR2_REGION_INDEX)
777 pg_prot = vma->vm_page_prot;
778 virtaddr = vma->vm_start;
779 req_size = vma->vm_end - vma->vm_start;
780 pgoff = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;
782 return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
785 static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type)
787 if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX)
793 static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu,
794 unsigned int index, unsigned int start,
795 unsigned int count, uint32_t flags,
801 static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu,
802 unsigned int index, unsigned int start,
803 unsigned int count, uint32_t flags, void *data)
808 static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu,
809 unsigned int index, unsigned int start, unsigned int count,
810 uint32_t flags, void *data)
815 static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu,
816 unsigned int index, unsigned int start, unsigned int count,
817 uint32_t flags, void *data)
819 struct eventfd_ctx *trigger;
821 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
822 int fd = *(int *)data;
824 trigger = eventfd_ctx_fdget(fd);
825 if (IS_ERR(trigger)) {
826 gvt_err("eventfd_ctx_fdget failed\n");
827 return PTR_ERR(trigger);
829 vgpu->vdev.msi_trigger = trigger;
835 static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, uint32_t flags,
836 unsigned int index, unsigned int start, unsigned int count,
839 int (*func)(struct intel_vgpu *vgpu, unsigned int index,
840 unsigned int start, unsigned int count, uint32_t flags,
844 case VFIO_PCI_INTX_IRQ_INDEX:
845 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
846 case VFIO_IRQ_SET_ACTION_MASK:
847 func = intel_vgpu_set_intx_mask;
849 case VFIO_IRQ_SET_ACTION_UNMASK:
850 func = intel_vgpu_set_intx_unmask;
852 case VFIO_IRQ_SET_ACTION_TRIGGER:
853 func = intel_vgpu_set_intx_trigger;
857 case VFIO_PCI_MSI_IRQ_INDEX:
858 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
859 case VFIO_IRQ_SET_ACTION_MASK:
860 case VFIO_IRQ_SET_ACTION_UNMASK:
861 /* XXX Need masking support exported */
863 case VFIO_IRQ_SET_ACTION_TRIGGER:
864 func = intel_vgpu_set_msi_trigger;
873 return func(vgpu, index, start, count, flags, data);
876 static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
879 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
882 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd);
884 if (cmd == VFIO_DEVICE_GET_INFO) {
885 struct vfio_device_info info;
887 minsz = offsetofend(struct vfio_device_info, num_irqs);
889 if (copy_from_user(&info, (void __user *)arg, minsz))
892 if (info.argsz < minsz)
895 info.flags = VFIO_DEVICE_FLAGS_PCI;
896 info.flags |= VFIO_DEVICE_FLAGS_RESET;
897 info.num_regions = VFIO_PCI_NUM_REGIONS;
898 info.num_irqs = VFIO_PCI_NUM_IRQS;
900 return copy_to_user((void __user *)arg, &info, minsz) ?
903 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
904 struct vfio_region_info info;
905 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
907 struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
912 minsz = offsetofend(struct vfio_region_info, offset);
914 if (copy_from_user(&info, (void __user *)arg, minsz))
917 if (info.argsz < minsz)
920 switch (info.index) {
921 case VFIO_PCI_CONFIG_REGION_INDEX:
922 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
923 info.size = INTEL_GVT_MAX_CFG_SPACE_SZ;
924 info.flags = VFIO_REGION_INFO_FLAG_READ |
925 VFIO_REGION_INFO_FLAG_WRITE;
927 case VFIO_PCI_BAR0_REGION_INDEX:
928 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
929 info.size = vgpu->cfg_space.bar[info.index].size;
935 info.flags = VFIO_REGION_INFO_FLAG_READ |
936 VFIO_REGION_INFO_FLAG_WRITE;
938 case VFIO_PCI_BAR1_REGION_INDEX:
939 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
943 case VFIO_PCI_BAR2_REGION_INDEX:
944 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
945 info.flags = VFIO_REGION_INFO_FLAG_CAPS |
946 VFIO_REGION_INFO_FLAG_MMAP |
947 VFIO_REGION_INFO_FLAG_READ |
948 VFIO_REGION_INFO_FLAG_WRITE;
949 info.size = gvt_aperture_sz(vgpu->gvt);
951 size = sizeof(*sparse) +
952 (nr_areas * sizeof(*sparse->areas));
953 sparse = kzalloc(size, GFP_KERNEL);
957 sparse->nr_areas = nr_areas;
958 cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
959 sparse->areas[0].offset =
960 PAGE_ALIGN(vgpu_aperture_offset(vgpu));
961 sparse->areas[0].size = vgpu_aperture_sz(vgpu);
969 case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
970 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
974 gvt_dbg_core("get region info bar:%d\n", info.index);
977 case VFIO_PCI_ROM_REGION_INDEX:
978 case VFIO_PCI_VGA_REGION_INDEX:
979 gvt_dbg_core("get region info index:%d\n", info.index);
983 struct vfio_region_info_cap_type cap_type;
985 if (info.index >= VFIO_PCI_NUM_REGIONS +
986 vgpu->vdev.num_regions)
989 i = info.index - VFIO_PCI_NUM_REGIONS;
992 VFIO_PCI_INDEX_TO_OFFSET(info.index);
993 info.size = vgpu->vdev.region[i].size;
994 info.flags = vgpu->vdev.region[i].flags;
996 cap_type.type = vgpu->vdev.region[i].type;
997 cap_type.subtype = vgpu->vdev.region[i].subtype;
999 ret = vfio_info_add_capability(&caps,
1000 VFIO_REGION_INFO_CAP_TYPE,
1007 if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) {
1008 switch (cap_type_id) {
1009 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1010 ret = vfio_info_add_capability(&caps,
1011 VFIO_REGION_INFO_CAP_SPARSE_MMAP,
1023 if (info.argsz < sizeof(info) + caps.size) {
1024 info.argsz = sizeof(info) + caps.size;
1025 info.cap_offset = 0;
1027 vfio_info_cap_shift(&caps, sizeof(info));
1028 if (copy_to_user((void __user *)arg +
1029 sizeof(info), caps.buf,
1034 info.cap_offset = sizeof(info);
1040 return copy_to_user((void __user *)arg, &info, minsz) ?
1042 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
1043 struct vfio_irq_info info;
1045 minsz = offsetofend(struct vfio_irq_info, count);
1047 if (copy_from_user(&info, (void __user *)arg, minsz))
1050 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
1053 switch (info.index) {
1054 case VFIO_PCI_INTX_IRQ_INDEX:
1055 case VFIO_PCI_MSI_IRQ_INDEX:
1061 info.flags = VFIO_IRQ_INFO_EVENTFD;
1063 info.count = intel_vgpu_get_irq_count(vgpu, info.index);
1065 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
1066 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
1067 VFIO_IRQ_INFO_AUTOMASKED);
1069 info.flags |= VFIO_IRQ_INFO_NORESIZE;
1071 return copy_to_user((void __user *)arg, &info, minsz) ?
1073 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
1074 struct vfio_irq_set hdr;
1077 size_t data_size = 0;
1079 minsz = offsetofend(struct vfio_irq_set, count);
1081 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1084 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
1085 int max = intel_vgpu_get_irq_count(vgpu, hdr.index);
1087 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
1088 VFIO_PCI_NUM_IRQS, &data_size);
1090 gvt_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
1094 data = memdup_user((void __user *)(arg + minsz),
1097 return PTR_ERR(data);
1101 ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index,
1102 hdr.start, hdr.count, data);
1106 } else if (cmd == VFIO_DEVICE_RESET) {
1107 intel_gvt_ops->vgpu_reset(vgpu);
1114 static const struct mdev_parent_ops intel_vgpu_ops = {
1115 .supported_type_groups = intel_vgpu_type_groups,
1116 .create = intel_vgpu_create,
1117 .remove = intel_vgpu_remove,
1119 .open = intel_vgpu_open,
1120 .release = intel_vgpu_release,
1122 .read = intel_vgpu_read,
1123 .write = intel_vgpu_write,
1124 .mmap = intel_vgpu_mmap,
1125 .ioctl = intel_vgpu_ioctl,
1128 static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)
1130 if (!intel_gvt_init_vgpu_type_groups(gvt))
1133 intel_gvt_ops = ops;
1135 return mdev_register_device(dev, &intel_vgpu_ops);
1138 static void kvmgt_host_exit(struct device *dev, void *gvt)
1140 intel_gvt_cleanup_vgpu_type_groups(gvt);
1141 mdev_unregister_device(dev);
1144 static int kvmgt_write_protect_add(unsigned long handle, u64 gfn)
1146 struct kvmgt_guest_info *info;
1148 struct kvm_memory_slot *slot;
1151 if (!handle_valid(handle))
1154 info = (struct kvmgt_guest_info *)handle;
1157 idx = srcu_read_lock(&kvm->srcu);
1158 slot = gfn_to_memslot(kvm, gfn);
1160 srcu_read_unlock(&kvm->srcu, idx);
1164 spin_lock(&kvm->mmu_lock);
1166 if (kvmgt_gfn_is_write_protected(info, gfn))
1169 kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1170 kvmgt_protect_table_add(info, gfn);
1173 spin_unlock(&kvm->mmu_lock);
1174 srcu_read_unlock(&kvm->srcu, idx);
1178 static int kvmgt_write_protect_remove(unsigned long handle, u64 gfn)
1180 struct kvmgt_guest_info *info;
1182 struct kvm_memory_slot *slot;
1185 if (!handle_valid(handle))
1188 info = (struct kvmgt_guest_info *)handle;
1191 idx = srcu_read_lock(&kvm->srcu);
1192 slot = gfn_to_memslot(kvm, gfn);
1194 srcu_read_unlock(&kvm->srcu, idx);
1198 spin_lock(&kvm->mmu_lock);
1200 if (!kvmgt_gfn_is_write_protected(info, gfn))
1203 kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1204 kvmgt_protect_table_del(info, gfn);
1207 spin_unlock(&kvm->mmu_lock);
1208 srcu_read_unlock(&kvm->srcu, idx);
1212 static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1213 const u8 *val, int len,
1214 struct kvm_page_track_notifier_node *node)
1216 struct kvmgt_guest_info *info = container_of(node,
1217 struct kvmgt_guest_info, track_node);
1219 if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
1220 intel_gvt_ops->emulate_mmio_write(info->vgpu, gpa,
1224 static void kvmgt_page_track_flush_slot(struct kvm *kvm,
1225 struct kvm_memory_slot *slot,
1226 struct kvm_page_track_notifier_node *node)
1230 struct kvmgt_guest_info *info = container_of(node,
1231 struct kvmgt_guest_info, track_node);
1233 spin_lock(&kvm->mmu_lock);
1234 for (i = 0; i < slot->npages; i++) {
1235 gfn = slot->base_gfn + i;
1236 if (kvmgt_gfn_is_write_protected(info, gfn)) {
1237 kvm_slot_page_track_remove_page(kvm, slot, gfn,
1238 KVM_PAGE_TRACK_WRITE);
1239 kvmgt_protect_table_del(info, gfn);
1242 spin_unlock(&kvm->mmu_lock);
1245 static bool kvmgt_check_guest(void)
1247 unsigned int eax, ebx, ecx, edx;
1251 eax = KVM_CPUID_SIGNATURE;
1252 ebx = ecx = edx = 0;
1254 asm volatile ("cpuid"
1255 : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
1258 i = (unsigned int *)s;
1263 return !strncmp(s, "KVMKVMKVM", strlen("KVMKVMKVM"));
1268 * It's actually impossible to check if we are running in KVM host,
1269 * since the "KVM host" is simply native. So we only dectect guest here.
1271 static int kvmgt_detect_host(void)
1273 #ifdef CONFIG_INTEL_IOMMU
1274 if (intel_iommu_gfx_mapped) {
1275 gvt_err("Hardware IOMMU compatibility not yet supported, try to boot with intel_iommu=igfx_off\n");
1279 return kvmgt_check_guest() ? -ENODEV : 0;
1282 static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
1284 struct intel_vgpu *itr;
1285 struct kvmgt_guest_info *info;
1289 mutex_lock(&vgpu->gvt->lock);
1290 for_each_active_vgpu(vgpu->gvt, itr, id) {
1291 if (!handle_valid(itr->handle))
1294 info = (struct kvmgt_guest_info *)itr->handle;
1295 if (kvm && kvm == info->kvm) {
1301 mutex_unlock(&vgpu->gvt->lock);
1305 static int kvmgt_guest_init(struct mdev_device *mdev)
1307 struct kvmgt_guest_info *info;
1308 struct intel_vgpu *vgpu;
1311 vgpu = mdev_get_drvdata(mdev);
1312 if (handle_valid(vgpu->handle))
1315 kvm = vgpu->vdev.kvm;
1316 if (!kvm || kvm->mm != current->mm) {
1317 gvt_err("KVM is required to use Intel vGPU\n");
1321 if (__kvmgt_vgpu_exist(vgpu, kvm))
1324 info = vzalloc(sizeof(struct kvmgt_guest_info));
1328 vgpu->handle = (unsigned long)info;
1332 kvmgt_protect_table_init(info);
1333 gvt_cache_init(vgpu);
1335 info->track_node.track_write = kvmgt_page_track_write;
1336 info->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
1337 kvm_page_track_register_notifier(kvm, &info->track_node);
1342 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info)
1345 gvt_err("kvmgt_guest_info invalid\n");
1349 kvm_page_track_unregister_notifier(info->kvm, &info->track_node);
1350 kvmgt_protect_table_destroy(info);
1351 gvt_cache_destroy(info->vgpu);
1357 static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle)
1359 /* nothing to do here */
1363 static void kvmgt_detach_vgpu(unsigned long handle)
1365 /* nothing to do here */
1368 static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data)
1370 struct kvmgt_guest_info *info;
1371 struct intel_vgpu *vgpu;
1373 if (!handle_valid(handle))
1376 info = (struct kvmgt_guest_info *)handle;
1379 if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1)
1385 static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn)
1388 struct kvmgt_guest_info *info;
1392 if (!handle_valid(handle))
1393 return INTEL_GVT_INVALID_ADDR;
1395 info = (struct kvmgt_guest_info *)handle;
1396 pfn = gvt_cache_find(info->vgpu, gfn);
1400 pfn = INTEL_GVT_INVALID_ADDR;
1401 dev = mdev_dev(info->vgpu->vdev.mdev);
1402 rc = vfio_pin_pages(dev, &gfn, 1, IOMMU_READ | IOMMU_WRITE, &pfn);
1404 gvt_err("vfio_pin_pages failed for gfn 0x%lx: %d\n", gfn, rc);
1405 return INTEL_GVT_INVALID_ADDR;
1408 gvt_cache_add(info->vgpu, gfn, pfn);
1412 static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa,
1413 void *buf, unsigned long len, bool write)
1415 struct kvmgt_guest_info *info;
1418 bool kthread = current->mm == NULL;
1420 if (!handle_valid(handle))
1423 info = (struct kvmgt_guest_info *)handle;
1429 ret = write ? kvm_write_guest(kvm, gpa, buf, len) :
1430 kvm_read_guest(kvm, gpa, buf, len);
1438 static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa,
1439 void *buf, unsigned long len)
1441 return kvmgt_rw_gpa(handle, gpa, buf, len, false);
1444 static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa,
1445 void *buf, unsigned long len)
1447 return kvmgt_rw_gpa(handle, gpa, buf, len, true);
1450 static unsigned long kvmgt_virt_to_pfn(void *addr)
1452 return PFN_DOWN(__pa(addr));
1455 struct intel_gvt_mpt kvmgt_mpt = {
1456 .detect_host = kvmgt_detect_host,
1457 .host_init = kvmgt_host_init,
1458 .host_exit = kvmgt_host_exit,
1459 .attach_vgpu = kvmgt_attach_vgpu,
1460 .detach_vgpu = kvmgt_detach_vgpu,
1461 .inject_msi = kvmgt_inject_msi,
1462 .from_virt_to_mfn = kvmgt_virt_to_pfn,
1463 .set_wp_page = kvmgt_write_protect_add,
1464 .unset_wp_page = kvmgt_write_protect_remove,
1465 .read_gpa = kvmgt_read_gpa,
1466 .write_gpa = kvmgt_write_gpa,
1467 .gfn_to_mfn = kvmgt_gfn_to_pfn,
1469 EXPORT_SYMBOL_GPL(kvmgt_mpt);
1471 static int __init kvmgt_init(void)
1476 static void __exit kvmgt_exit(void)
1480 module_init(kvmgt_init);
1481 module_exit(kvmgt_exit);
1483 MODULE_LICENSE("GPL and additional rights");
1484 MODULE_AUTHOR("Intel Corporation");