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 int gvt_dma_map_iova(struct intel_vgpu *vgpu, kvm_pfn_t pfn,
96 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
99 page = pfn_to_page(pfn);
100 if (is_error_page(page))
103 daddr = dma_map_page(dev, page, 0, PAGE_SIZE,
104 PCI_DMA_BIDIRECTIONAL);
105 if (dma_mapping_error(dev, daddr))
108 *iova = (unsigned long)(daddr >> PAGE_SHIFT);
112 static void gvt_dma_unmap_iova(struct intel_vgpu *vgpu, unsigned long iova)
114 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
117 daddr = (dma_addr_t)(iova << PAGE_SHIFT);
118 dma_unmap_page(dev, daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
121 static struct gvt_dma *__gvt_cache_find(struct intel_vgpu *vgpu, gfn_t gfn)
123 struct rb_node *node = vgpu->vdev.cache.rb_node;
124 struct gvt_dma *ret = NULL;
127 struct gvt_dma *itr = rb_entry(node, struct gvt_dma, node);
130 node = node->rb_left;
131 else if (gfn > itr->gfn)
132 node = node->rb_right;
143 static unsigned long gvt_cache_find(struct intel_vgpu *vgpu, gfn_t gfn)
145 struct gvt_dma *entry;
148 mutex_lock(&vgpu->vdev.cache_lock);
150 entry = __gvt_cache_find(vgpu, gfn);
151 iova = (entry == NULL) ? INTEL_GVT_INVALID_ADDR : entry->iova;
153 mutex_unlock(&vgpu->vdev.cache_lock);
157 static void gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
160 struct gvt_dma *new, *itr;
161 struct rb_node **link = &vgpu->vdev.cache.rb_node, *parent = NULL;
163 new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
170 mutex_lock(&vgpu->vdev.cache_lock);
173 itr = rb_entry(parent, struct gvt_dma, node);
177 else if (gfn < itr->gfn)
178 link = &parent->rb_left;
180 link = &parent->rb_right;
183 rb_link_node(&new->node, parent, link);
184 rb_insert_color(&new->node, &vgpu->vdev.cache);
185 mutex_unlock(&vgpu->vdev.cache_lock);
189 mutex_unlock(&vgpu->vdev.cache_lock);
193 static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
194 struct gvt_dma *entry)
196 rb_erase(&entry->node, &vgpu->vdev.cache);
200 static void gvt_cache_remove(struct intel_vgpu *vgpu, gfn_t gfn)
202 struct device *dev = mdev_dev(vgpu->vdev.mdev);
203 struct gvt_dma *this;
207 mutex_lock(&vgpu->vdev.cache_lock);
208 this = __gvt_cache_find(vgpu, gfn);
210 mutex_unlock(&vgpu->vdev.cache_lock);
215 gvt_dma_unmap_iova(vgpu, this->iova);
216 rc = vfio_unpin_pages(dev, &g1, 1);
218 __gvt_cache_remove_entry(vgpu, this);
219 mutex_unlock(&vgpu->vdev.cache_lock);
222 static void gvt_cache_init(struct intel_vgpu *vgpu)
224 vgpu->vdev.cache = RB_ROOT;
225 mutex_init(&vgpu->vdev.cache_lock);
228 static void gvt_cache_destroy(struct intel_vgpu *vgpu)
231 struct rb_node *node = NULL;
232 struct device *dev = mdev_dev(vgpu->vdev.mdev);
235 mutex_lock(&vgpu->vdev.cache_lock);
236 while ((node = rb_first(&vgpu->vdev.cache))) {
237 dma = rb_entry(node, struct gvt_dma, node);
238 gvt_dma_unmap_iova(vgpu, dma->iova);
241 vfio_unpin_pages(dev, &gfn, 1);
242 __gvt_cache_remove_entry(vgpu, dma);
244 mutex_unlock(&vgpu->vdev.cache_lock);
247 static struct intel_vgpu_type *intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
251 struct intel_vgpu_type *t;
252 const char *driver_name = dev_driver_string(
253 &gvt->dev_priv->drm.pdev->dev);
255 for (i = 0; i < gvt->num_types; i++) {
257 if (!strncmp(t->name, name + strlen(driver_name) + 1,
265 static ssize_t available_instances_show(struct kobject *kobj,
266 struct device *dev, char *buf)
268 struct intel_vgpu_type *type;
269 unsigned int num = 0;
270 void *gvt = kdev_to_i915(dev)->gvt;
272 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
276 num = type->avail_instance;
278 return sprintf(buf, "%u\n", num);
281 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
284 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
287 static ssize_t description_show(struct kobject *kobj, struct device *dev,
290 struct intel_vgpu_type *type;
291 void *gvt = kdev_to_i915(dev)->gvt;
293 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
297 return sprintf(buf, "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
299 BYTES_TO_MB(type->low_gm_size),
300 BYTES_TO_MB(type->high_gm_size),
304 static MDEV_TYPE_ATTR_RO(available_instances);
305 static MDEV_TYPE_ATTR_RO(device_api);
306 static MDEV_TYPE_ATTR_RO(description);
308 static struct attribute *type_attrs[] = {
309 &mdev_type_attr_available_instances.attr,
310 &mdev_type_attr_device_api.attr,
311 &mdev_type_attr_description.attr,
315 static struct attribute_group *intel_vgpu_type_groups[] = {
316 [0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
319 static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
322 struct intel_vgpu_type *type;
323 struct attribute_group *group;
325 for (i = 0; i < gvt->num_types; i++) {
326 type = &gvt->types[i];
328 group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
332 group->name = type->name;
333 group->attrs = type_attrs;
334 intel_vgpu_type_groups[i] = group;
340 for (j = 0; j < i; j++) {
341 group = intel_vgpu_type_groups[j];
348 static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
351 struct attribute_group *group;
353 for (i = 0; i < gvt->num_types; i++) {
354 group = intel_vgpu_type_groups[i];
359 static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
361 hash_init(info->ptable);
364 static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
366 struct kvmgt_pgfn *p;
367 struct hlist_node *tmp;
370 hash_for_each_safe(info->ptable, i, tmp, p, hnode) {
376 static struct kvmgt_pgfn *
377 __kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
379 struct kvmgt_pgfn *p, *res = NULL;
381 hash_for_each_possible(info->ptable, p, hnode, gfn) {
391 static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info,
394 struct kvmgt_pgfn *p;
396 p = __kvmgt_protect_table_find(info, gfn);
400 static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
402 struct kvmgt_pgfn *p;
404 if (kvmgt_gfn_is_write_protected(info, gfn))
407 p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC);
408 if (WARN(!p, "gfn: 0x%llx\n", gfn))
412 hash_add(info->ptable, &p->hnode, gfn);
415 static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
418 struct kvmgt_pgfn *p;
420 p = __kvmgt_protect_table_find(info, gfn);
427 static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
429 struct intel_vgpu *vgpu;
430 struct intel_vgpu_type *type;
435 pdev = mdev_parent_dev(mdev);
436 gvt = kdev_to_i915(pdev)->gvt;
438 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
440 gvt_err("failed to find type %s to create\n",
446 vgpu = intel_gvt_ops->vgpu_create(gvt, type);
447 if (IS_ERR_OR_NULL(vgpu)) {
448 ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu);
449 gvt_err("failed to create intel vgpu: %d\n", ret);
453 INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work);
455 vgpu->vdev.mdev = mdev;
456 mdev_set_drvdata(mdev, vgpu);
458 gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
459 dev_name(mdev_dev(mdev)));
466 static int intel_vgpu_remove(struct mdev_device *mdev)
468 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
470 if (handle_valid(vgpu->handle))
473 intel_gvt_ops->vgpu_destroy(vgpu);
477 static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
478 unsigned long action, void *data)
480 struct intel_vgpu *vgpu = container_of(nb,
482 vdev.iommu_notifier);
484 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
485 struct vfio_iommu_type1_dma_unmap *unmap = data;
486 unsigned long gfn, end_gfn;
488 gfn = unmap->iova >> PAGE_SHIFT;
489 end_gfn = gfn + unmap->size / PAGE_SIZE;
491 while (gfn < end_gfn)
492 gvt_cache_remove(vgpu, gfn++);
498 static int intel_vgpu_group_notifier(struct notifier_block *nb,
499 unsigned long action, void *data)
501 struct intel_vgpu *vgpu = container_of(nb,
503 vdev.group_notifier);
505 /* the only action we care about */
506 if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
507 vgpu->vdev.kvm = data;
510 schedule_work(&vgpu->vdev.release_work);
516 static int intel_vgpu_open(struct mdev_device *mdev)
518 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
519 unsigned long events;
522 vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
523 vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier;
525 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
526 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events,
527 &vgpu->vdev.iommu_notifier);
529 gvt_err("vfio_register_notifier for iommu failed: %d\n", ret);
533 events = VFIO_GROUP_NOTIFY_SET_KVM;
534 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events,
535 &vgpu->vdev.group_notifier);
537 gvt_err("vfio_register_notifier for group failed: %d\n", ret);
541 ret = kvmgt_guest_init(mdev);
545 atomic_set(&vgpu->vdev.released, 0);
549 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
550 &vgpu->vdev.group_notifier);
553 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
554 &vgpu->vdev.iommu_notifier);
559 static void __intel_vgpu_release(struct intel_vgpu *vgpu)
561 struct kvmgt_guest_info *info;
564 if (!handle_valid(vgpu->handle))
567 if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1))
570 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY,
571 &vgpu->vdev.iommu_notifier);
572 WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret);
574 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY,
575 &vgpu->vdev.group_notifier);
576 WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret);
578 info = (struct kvmgt_guest_info *)vgpu->handle;
579 kvmgt_guest_exit(info);
581 vgpu->vdev.kvm = NULL;
585 static void intel_vgpu_release(struct mdev_device *mdev)
587 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
589 __intel_vgpu_release(vgpu);
592 static void intel_vgpu_release_work(struct work_struct *work)
594 struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu,
597 __intel_vgpu_release(vgpu);
600 static uint64_t intel_vgpu_get_bar0_addr(struct intel_vgpu *vgpu)
602 u32 start_lo, start_hi;
604 int pos = PCI_BASE_ADDRESS_0;
606 start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + pos)) &
607 PCI_BASE_ADDRESS_MEM_MASK;
608 mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + pos)) &
609 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
612 case PCI_BASE_ADDRESS_MEM_TYPE_64:
613 start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space
616 case PCI_BASE_ADDRESS_MEM_TYPE_32:
617 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
618 /* 1M mem BAR treated as 32-bit BAR */
620 /* mem unknown type treated as 32-bit BAR */
625 return ((u64)start_hi << 32) | start_lo;
628 static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
629 size_t count, loff_t *ppos, bool is_write)
631 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
632 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
633 uint64_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
637 if (index >= VFIO_PCI_NUM_REGIONS) {
638 gvt_err("invalid index: %u\n", index);
643 case VFIO_PCI_CONFIG_REGION_INDEX:
645 ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos,
648 ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos,
651 case VFIO_PCI_BAR0_REGION_INDEX:
652 case VFIO_PCI_BAR1_REGION_INDEX:
654 uint64_t bar0_start = intel_vgpu_get_bar0_addr(vgpu);
656 ret = intel_gvt_ops->emulate_mmio_write(vgpu,
657 bar0_start + pos, buf, count);
659 uint64_t bar0_start = intel_vgpu_get_bar0_addr(vgpu);
661 ret = intel_gvt_ops->emulate_mmio_read(vgpu,
662 bar0_start + pos, buf, count);
665 case VFIO_PCI_BAR2_REGION_INDEX:
666 case VFIO_PCI_BAR3_REGION_INDEX:
667 case VFIO_PCI_BAR4_REGION_INDEX:
668 case VFIO_PCI_BAR5_REGION_INDEX:
669 case VFIO_PCI_VGA_REGION_INDEX:
670 case VFIO_PCI_ROM_REGION_INDEX:
672 gvt_err("unsupported region: %u\n", index);
675 return ret == 0 ? count : ret;
678 static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
679 size_t count, loff_t *ppos)
681 unsigned int done = 0;
687 if (count >= 4 && !(*ppos % 4)) {
690 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
695 if (copy_to_user(buf, &val, sizeof(val)))
699 } else if (count >= 2 && !(*ppos % 2)) {
702 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
707 if (copy_to_user(buf, &val, sizeof(val)))
714 ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos,
719 if (copy_to_user(buf, &val, sizeof(val)))
737 static ssize_t intel_vgpu_write(struct mdev_device *mdev,
738 const char __user *buf,
739 size_t count, loff_t *ppos)
741 unsigned int done = 0;
747 if (count >= 4 && !(*ppos % 4)) {
750 if (copy_from_user(&val, buf, sizeof(val)))
753 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
759 } else if (count >= 2 && !(*ppos % 2)) {
762 if (copy_from_user(&val, buf, sizeof(val)))
765 ret = intel_vgpu_rw(mdev, (char *)&val,
766 sizeof(val), ppos, true);
774 if (copy_from_user(&val, buf, sizeof(val)))
777 ret = intel_vgpu_rw(mdev, &val, sizeof(val),
796 static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
800 unsigned long req_size, pgoff = 0;
802 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
804 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
805 if (index >= VFIO_PCI_ROM_REGION_INDEX)
808 if (vma->vm_end < vma->vm_start)
810 if ((vma->vm_flags & VM_SHARED) == 0)
812 if (index != VFIO_PCI_BAR2_REGION_INDEX)
815 pg_prot = vma->vm_page_prot;
816 virtaddr = vma->vm_start;
817 req_size = vma->vm_end - vma->vm_start;
818 pgoff = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;
820 return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
823 static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type)
825 if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX)
831 static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu,
832 unsigned int index, unsigned int start,
833 unsigned int count, uint32_t flags,
839 static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu,
840 unsigned int index, unsigned int start,
841 unsigned int count, uint32_t flags, void *data)
846 static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu,
847 unsigned int index, unsigned int start, unsigned int count,
848 uint32_t flags, void *data)
853 static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu,
854 unsigned int index, unsigned int start, unsigned int count,
855 uint32_t flags, void *data)
857 struct eventfd_ctx *trigger;
859 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
860 int fd = *(int *)data;
862 trigger = eventfd_ctx_fdget(fd);
863 if (IS_ERR(trigger)) {
864 gvt_err("eventfd_ctx_fdget failed\n");
865 return PTR_ERR(trigger);
867 vgpu->vdev.msi_trigger = trigger;
873 static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, uint32_t flags,
874 unsigned int index, unsigned int start, unsigned int count,
877 int (*func)(struct intel_vgpu *vgpu, unsigned int index,
878 unsigned int start, unsigned int count, uint32_t flags,
882 case VFIO_PCI_INTX_IRQ_INDEX:
883 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
884 case VFIO_IRQ_SET_ACTION_MASK:
885 func = intel_vgpu_set_intx_mask;
887 case VFIO_IRQ_SET_ACTION_UNMASK:
888 func = intel_vgpu_set_intx_unmask;
890 case VFIO_IRQ_SET_ACTION_TRIGGER:
891 func = intel_vgpu_set_intx_trigger;
895 case VFIO_PCI_MSI_IRQ_INDEX:
896 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
897 case VFIO_IRQ_SET_ACTION_MASK:
898 case VFIO_IRQ_SET_ACTION_UNMASK:
899 /* XXX Need masking support exported */
901 case VFIO_IRQ_SET_ACTION_TRIGGER:
902 func = intel_vgpu_set_msi_trigger;
911 return func(vgpu, index, start, count, flags, data);
914 static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
917 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
920 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd);
922 if (cmd == VFIO_DEVICE_GET_INFO) {
923 struct vfio_device_info info;
925 minsz = offsetofend(struct vfio_device_info, num_irqs);
927 if (copy_from_user(&info, (void __user *)arg, minsz))
930 if (info.argsz < minsz)
933 info.flags = VFIO_DEVICE_FLAGS_PCI;
934 info.flags |= VFIO_DEVICE_FLAGS_RESET;
935 info.num_regions = VFIO_PCI_NUM_REGIONS;
936 info.num_irqs = VFIO_PCI_NUM_IRQS;
938 return copy_to_user((void __user *)arg, &info, minsz) ?
941 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
942 struct vfio_region_info info;
943 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
945 struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
950 minsz = offsetofend(struct vfio_region_info, offset);
952 if (copy_from_user(&info, (void __user *)arg, minsz))
955 if (info.argsz < minsz)
958 switch (info.index) {
959 case VFIO_PCI_CONFIG_REGION_INDEX:
960 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
961 info.size = INTEL_GVT_MAX_CFG_SPACE_SZ;
962 info.flags = VFIO_REGION_INFO_FLAG_READ |
963 VFIO_REGION_INFO_FLAG_WRITE;
965 case VFIO_PCI_BAR0_REGION_INDEX:
966 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
967 info.size = vgpu->cfg_space.bar[info.index].size;
973 info.flags = VFIO_REGION_INFO_FLAG_READ |
974 VFIO_REGION_INFO_FLAG_WRITE;
976 case VFIO_PCI_BAR1_REGION_INDEX:
977 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
981 case VFIO_PCI_BAR2_REGION_INDEX:
982 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
983 info.flags = VFIO_REGION_INFO_FLAG_CAPS |
984 VFIO_REGION_INFO_FLAG_MMAP |
985 VFIO_REGION_INFO_FLAG_READ |
986 VFIO_REGION_INFO_FLAG_WRITE;
987 info.size = gvt_aperture_sz(vgpu->gvt);
989 size = sizeof(*sparse) +
990 (nr_areas * sizeof(*sparse->areas));
991 sparse = kzalloc(size, GFP_KERNEL);
995 sparse->nr_areas = nr_areas;
996 cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
997 sparse->areas[0].offset =
998 PAGE_ALIGN(vgpu_aperture_offset(vgpu));
999 sparse->areas[0].size = vgpu_aperture_sz(vgpu);
1002 case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1003 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1007 gvt_dbg_core("get region info bar:%d\n", info.index);
1010 case VFIO_PCI_ROM_REGION_INDEX:
1011 case VFIO_PCI_VGA_REGION_INDEX:
1012 gvt_dbg_core("get region info index:%d\n", info.index);
1016 struct vfio_region_info_cap_type cap_type;
1018 if (info.index >= VFIO_PCI_NUM_REGIONS +
1019 vgpu->vdev.num_regions)
1022 i = info.index - VFIO_PCI_NUM_REGIONS;
1025 VFIO_PCI_INDEX_TO_OFFSET(info.index);
1026 info.size = vgpu->vdev.region[i].size;
1027 info.flags = vgpu->vdev.region[i].flags;
1029 cap_type.type = vgpu->vdev.region[i].type;
1030 cap_type.subtype = vgpu->vdev.region[i].subtype;
1032 ret = vfio_info_add_capability(&caps,
1033 VFIO_REGION_INFO_CAP_TYPE,
1040 if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) {
1041 switch (cap_type_id) {
1042 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1043 ret = vfio_info_add_capability(&caps,
1044 VFIO_REGION_INFO_CAP_SPARSE_MMAP,
1056 if (info.argsz < sizeof(info) + caps.size) {
1057 info.argsz = sizeof(info) + caps.size;
1058 info.cap_offset = 0;
1060 vfio_info_cap_shift(&caps, sizeof(info));
1061 if (copy_to_user((void __user *)arg +
1062 sizeof(info), caps.buf,
1067 info.cap_offset = sizeof(info);
1073 return copy_to_user((void __user *)arg, &info, minsz) ?
1075 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
1076 struct vfio_irq_info info;
1078 minsz = offsetofend(struct vfio_irq_info, count);
1080 if (copy_from_user(&info, (void __user *)arg, minsz))
1083 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
1086 switch (info.index) {
1087 case VFIO_PCI_INTX_IRQ_INDEX:
1088 case VFIO_PCI_MSI_IRQ_INDEX:
1094 info.flags = VFIO_IRQ_INFO_EVENTFD;
1096 info.count = intel_vgpu_get_irq_count(vgpu, info.index);
1098 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
1099 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
1100 VFIO_IRQ_INFO_AUTOMASKED);
1102 info.flags |= VFIO_IRQ_INFO_NORESIZE;
1104 return copy_to_user((void __user *)arg, &info, minsz) ?
1106 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
1107 struct vfio_irq_set hdr;
1110 size_t data_size = 0;
1112 minsz = offsetofend(struct vfio_irq_set, count);
1114 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1117 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
1118 int max = intel_vgpu_get_irq_count(vgpu, hdr.index);
1120 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
1121 VFIO_PCI_NUM_IRQS, &data_size);
1123 gvt_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
1127 data = memdup_user((void __user *)(arg + minsz),
1130 return PTR_ERR(data);
1134 ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index,
1135 hdr.start, hdr.count, data);
1139 } else if (cmd == VFIO_DEVICE_RESET) {
1140 intel_gvt_ops->vgpu_reset(vgpu);
1147 static const struct mdev_parent_ops intel_vgpu_ops = {
1148 .supported_type_groups = intel_vgpu_type_groups,
1149 .create = intel_vgpu_create,
1150 .remove = intel_vgpu_remove,
1152 .open = intel_vgpu_open,
1153 .release = intel_vgpu_release,
1155 .read = intel_vgpu_read,
1156 .write = intel_vgpu_write,
1157 .mmap = intel_vgpu_mmap,
1158 .ioctl = intel_vgpu_ioctl,
1161 static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)
1163 if (!intel_gvt_init_vgpu_type_groups(gvt))
1166 intel_gvt_ops = ops;
1168 return mdev_register_device(dev, &intel_vgpu_ops);
1171 static void kvmgt_host_exit(struct device *dev, void *gvt)
1173 intel_gvt_cleanup_vgpu_type_groups(gvt);
1174 mdev_unregister_device(dev);
1177 static int kvmgt_write_protect_add(unsigned long handle, u64 gfn)
1179 struct kvmgt_guest_info *info;
1181 struct kvm_memory_slot *slot;
1184 if (!handle_valid(handle))
1187 info = (struct kvmgt_guest_info *)handle;
1190 idx = srcu_read_lock(&kvm->srcu);
1191 slot = gfn_to_memslot(kvm, gfn);
1193 srcu_read_unlock(&kvm->srcu, idx);
1197 spin_lock(&kvm->mmu_lock);
1199 if (kvmgt_gfn_is_write_protected(info, gfn))
1202 kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1203 kvmgt_protect_table_add(info, gfn);
1206 spin_unlock(&kvm->mmu_lock);
1207 srcu_read_unlock(&kvm->srcu, idx);
1211 static int kvmgt_write_protect_remove(unsigned long handle, u64 gfn)
1213 struct kvmgt_guest_info *info;
1215 struct kvm_memory_slot *slot;
1218 if (!handle_valid(handle))
1221 info = (struct kvmgt_guest_info *)handle;
1224 idx = srcu_read_lock(&kvm->srcu);
1225 slot = gfn_to_memslot(kvm, gfn);
1227 srcu_read_unlock(&kvm->srcu, idx);
1231 spin_lock(&kvm->mmu_lock);
1233 if (!kvmgt_gfn_is_write_protected(info, gfn))
1236 kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1237 kvmgt_protect_table_del(info, gfn);
1240 spin_unlock(&kvm->mmu_lock);
1241 srcu_read_unlock(&kvm->srcu, idx);
1245 static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1246 const u8 *val, int len,
1247 struct kvm_page_track_notifier_node *node)
1249 struct kvmgt_guest_info *info = container_of(node,
1250 struct kvmgt_guest_info, track_node);
1252 if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
1253 intel_gvt_ops->emulate_mmio_write(info->vgpu, gpa,
1257 static void kvmgt_page_track_flush_slot(struct kvm *kvm,
1258 struct kvm_memory_slot *slot,
1259 struct kvm_page_track_notifier_node *node)
1263 struct kvmgt_guest_info *info = container_of(node,
1264 struct kvmgt_guest_info, track_node);
1266 spin_lock(&kvm->mmu_lock);
1267 for (i = 0; i < slot->npages; i++) {
1268 gfn = slot->base_gfn + i;
1269 if (kvmgt_gfn_is_write_protected(info, gfn)) {
1270 kvm_slot_page_track_remove_page(kvm, slot, gfn,
1271 KVM_PAGE_TRACK_WRITE);
1272 kvmgt_protect_table_del(info, gfn);
1275 spin_unlock(&kvm->mmu_lock);
1278 static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
1280 struct intel_vgpu *itr;
1281 struct kvmgt_guest_info *info;
1285 mutex_lock(&vgpu->gvt->lock);
1286 for_each_active_vgpu(vgpu->gvt, itr, id) {
1287 if (!handle_valid(itr->handle))
1290 info = (struct kvmgt_guest_info *)itr->handle;
1291 if (kvm && kvm == info->kvm) {
1297 mutex_unlock(&vgpu->gvt->lock);
1301 static int kvmgt_guest_init(struct mdev_device *mdev)
1303 struct kvmgt_guest_info *info;
1304 struct intel_vgpu *vgpu;
1307 vgpu = mdev_get_drvdata(mdev);
1308 if (handle_valid(vgpu->handle))
1311 kvm = vgpu->vdev.kvm;
1312 if (!kvm || kvm->mm != current->mm) {
1313 gvt_err("KVM is required to use Intel vGPU\n");
1317 if (__kvmgt_vgpu_exist(vgpu, kvm))
1320 info = vzalloc(sizeof(struct kvmgt_guest_info));
1324 vgpu->handle = (unsigned long)info;
1328 kvmgt_protect_table_init(info);
1329 gvt_cache_init(vgpu);
1331 info->track_node.track_write = kvmgt_page_track_write;
1332 info->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
1333 kvm_page_track_register_notifier(kvm, &info->track_node);
1338 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info)
1341 gvt_err("kvmgt_guest_info invalid\n");
1345 kvm_page_track_unregister_notifier(info->kvm, &info->track_node);
1346 kvmgt_protect_table_destroy(info);
1347 gvt_cache_destroy(info->vgpu);
1353 static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle)
1355 /* nothing to do here */
1359 static void kvmgt_detach_vgpu(unsigned long handle)
1361 /* nothing to do here */
1364 static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data)
1366 struct kvmgt_guest_info *info;
1367 struct intel_vgpu *vgpu;
1369 if (!handle_valid(handle))
1372 info = (struct kvmgt_guest_info *)handle;
1375 if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1)
1381 static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn)
1383 unsigned long iova, pfn;
1384 struct kvmgt_guest_info *info;
1388 if (!handle_valid(handle))
1389 return INTEL_GVT_INVALID_ADDR;
1391 info = (struct kvmgt_guest_info *)handle;
1392 iova = gvt_cache_find(info->vgpu, gfn);
1393 if (iova != INTEL_GVT_INVALID_ADDR)
1396 pfn = INTEL_GVT_INVALID_ADDR;
1397 dev = mdev_dev(info->vgpu->vdev.mdev);
1398 rc = vfio_pin_pages(dev, &gfn, 1, IOMMU_READ | IOMMU_WRITE, &pfn);
1400 gvt_err("vfio_pin_pages failed for gfn 0x%lx: %d\n", gfn, rc);
1401 return INTEL_GVT_INVALID_ADDR;
1403 /* transfer to host iova for GFX to use DMA */
1404 rc = gvt_dma_map_iova(info->vgpu, pfn, &iova);
1406 gvt_err("gvt_dma_map_iova failed for gfn: 0x%lx\n", gfn);
1407 vfio_unpin_pages(dev, &gfn, 1);
1408 return INTEL_GVT_INVALID_ADDR;
1411 gvt_cache_add(info->vgpu, gfn, iova);
1415 static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa,
1416 void *buf, unsigned long len, bool write)
1418 struct kvmgt_guest_info *info;
1421 bool kthread = current->mm == NULL;
1423 if (!handle_valid(handle))
1426 info = (struct kvmgt_guest_info *)handle;
1432 ret = write ? kvm_write_guest(kvm, gpa, buf, len) :
1433 kvm_read_guest(kvm, gpa, buf, len);
1441 static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa,
1442 void *buf, unsigned long len)
1444 return kvmgt_rw_gpa(handle, gpa, buf, len, false);
1447 static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa,
1448 void *buf, unsigned long len)
1450 return kvmgt_rw_gpa(handle, gpa, buf, len, true);
1453 static unsigned long kvmgt_virt_to_pfn(void *addr)
1455 return PFN_DOWN(__pa(addr));
1458 struct intel_gvt_mpt kvmgt_mpt = {
1459 .host_init = kvmgt_host_init,
1460 .host_exit = kvmgt_host_exit,
1461 .attach_vgpu = kvmgt_attach_vgpu,
1462 .detach_vgpu = kvmgt_detach_vgpu,
1463 .inject_msi = kvmgt_inject_msi,
1464 .from_virt_to_mfn = kvmgt_virt_to_pfn,
1465 .set_wp_page = kvmgt_write_protect_add,
1466 .unset_wp_page = kvmgt_write_protect_remove,
1467 .read_gpa = kvmgt_read_gpa,
1468 .write_gpa = kvmgt_write_gpa,
1469 .gfn_to_mfn = kvmgt_gfn_to_pfn,
1471 EXPORT_SYMBOL_GPL(kvmgt_mpt);
1473 static int __init kvmgt_init(void)
1478 static void __exit kvmgt_exit(void)
1482 module_init(kvmgt_init);
1483 module_exit(kvmgt_exit);
1485 MODULE_LICENSE("GPL and additional rights");
1486 MODULE_AUTHOR("Intel Corporation");