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 if (unlikely(!pfn_valid(pfn)))
102 page = pfn_to_page(pfn);
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);
236 mutex_lock(&vgpu->vdev.cache_lock);
237 node = rb_first(&vgpu->vdev.cache);
239 mutex_unlock(&vgpu->vdev.cache_lock);
242 dma = rb_entry(node, struct gvt_dma, node);
243 gvt_dma_unmap_iova(vgpu, dma->iova);
245 __gvt_cache_remove_entry(vgpu, dma);
246 mutex_unlock(&vgpu->vdev.cache_lock);
247 vfio_unpin_pages(dev, &gfn, 1);
251 static struct intel_vgpu_type *intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
255 struct intel_vgpu_type *t;
256 const char *driver_name = dev_driver_string(
257 &gvt->dev_priv->drm.pdev->dev);
259 for (i = 0; i < gvt->num_types; i++) {
261 if (!strncmp(t->name, name + strlen(driver_name) + 1,
269 static ssize_t available_instances_show(struct kobject *kobj,
270 struct device *dev, char *buf)
272 struct intel_vgpu_type *type;
273 unsigned int num = 0;
274 void *gvt = kdev_to_i915(dev)->gvt;
276 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
280 num = type->avail_instance;
282 return sprintf(buf, "%u\n", num);
285 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
288 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
291 static ssize_t description_show(struct kobject *kobj, struct device *dev,
294 struct intel_vgpu_type *type;
295 void *gvt = kdev_to_i915(dev)->gvt;
297 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
301 return sprintf(buf, "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
302 "fence: %d\nresolution: %s\n"
304 BYTES_TO_MB(type->low_gm_size),
305 BYTES_TO_MB(type->high_gm_size),
306 type->fence, vgpu_edid_str(type->resolution),
310 static MDEV_TYPE_ATTR_RO(available_instances);
311 static MDEV_TYPE_ATTR_RO(device_api);
312 static MDEV_TYPE_ATTR_RO(description);
314 static struct attribute *type_attrs[] = {
315 &mdev_type_attr_available_instances.attr,
316 &mdev_type_attr_device_api.attr,
317 &mdev_type_attr_description.attr,
321 static struct attribute_group *intel_vgpu_type_groups[] = {
322 [0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
325 static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
328 struct intel_vgpu_type *type;
329 struct attribute_group *group;
331 for (i = 0; i < gvt->num_types; i++) {
332 type = &gvt->types[i];
334 group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
338 group->name = type->name;
339 group->attrs = type_attrs;
340 intel_vgpu_type_groups[i] = group;
346 for (j = 0; j < i; j++) {
347 group = intel_vgpu_type_groups[j];
354 static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
357 struct attribute_group *group;
359 for (i = 0; i < gvt->num_types; i++) {
360 group = intel_vgpu_type_groups[i];
365 static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
367 hash_init(info->ptable);
370 static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
372 struct kvmgt_pgfn *p;
373 struct hlist_node *tmp;
376 hash_for_each_safe(info->ptable, i, tmp, p, hnode) {
382 static struct kvmgt_pgfn *
383 __kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
385 struct kvmgt_pgfn *p, *res = NULL;
387 hash_for_each_possible(info->ptable, p, hnode, gfn) {
397 static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info,
400 struct kvmgt_pgfn *p;
402 p = __kvmgt_protect_table_find(info, gfn);
406 static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
408 struct kvmgt_pgfn *p;
410 if (kvmgt_gfn_is_write_protected(info, gfn))
413 p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC);
414 if (WARN(!p, "gfn: 0x%llx\n", gfn))
418 hash_add(info->ptable, &p->hnode, gfn);
421 static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
424 struct kvmgt_pgfn *p;
426 p = __kvmgt_protect_table_find(info, gfn);
433 static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
435 struct intel_vgpu *vgpu = NULL;
436 struct intel_vgpu_type *type;
441 pdev = mdev_parent_dev(mdev);
442 gvt = kdev_to_i915(pdev)->gvt;
444 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
446 gvt_vgpu_err("failed to find type %s to create\n",
452 vgpu = intel_gvt_ops->vgpu_create(gvt, type);
453 if (IS_ERR_OR_NULL(vgpu)) {
454 ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu);
455 gvt_vgpu_err("failed to create intel vgpu: %d\n", ret);
459 INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work);
461 vgpu->vdev.mdev = mdev;
462 mdev_set_drvdata(mdev, vgpu);
464 gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
465 dev_name(mdev_dev(mdev)));
472 static int intel_vgpu_remove(struct mdev_device *mdev)
474 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
476 if (handle_valid(vgpu->handle))
479 intel_gvt_ops->vgpu_destroy(vgpu);
483 static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
484 unsigned long action, void *data)
486 struct intel_vgpu *vgpu = container_of(nb,
488 vdev.iommu_notifier);
490 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
491 struct vfio_iommu_type1_dma_unmap *unmap = data;
492 unsigned long gfn, end_gfn;
494 gfn = unmap->iova >> PAGE_SHIFT;
495 end_gfn = gfn + unmap->size / PAGE_SIZE;
497 while (gfn < end_gfn)
498 gvt_cache_remove(vgpu, gfn++);
504 static int intel_vgpu_group_notifier(struct notifier_block *nb,
505 unsigned long action, void *data)
507 struct intel_vgpu *vgpu = container_of(nb,
509 vdev.group_notifier);
511 /* the only action we care about */
512 if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
513 vgpu->vdev.kvm = data;
516 schedule_work(&vgpu->vdev.release_work);
522 static int intel_vgpu_open(struct mdev_device *mdev)
524 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
525 unsigned long events;
528 vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
529 vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier;
531 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
532 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events,
533 &vgpu->vdev.iommu_notifier);
535 gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
540 events = VFIO_GROUP_NOTIFY_SET_KVM;
541 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events,
542 &vgpu->vdev.group_notifier);
544 gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
549 ret = kvmgt_guest_init(mdev);
553 intel_gvt_ops->vgpu_activate(vgpu);
555 atomic_set(&vgpu->vdev.released, 0);
559 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
560 &vgpu->vdev.group_notifier);
563 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
564 &vgpu->vdev.iommu_notifier);
569 static void __intel_vgpu_release(struct intel_vgpu *vgpu)
571 struct kvmgt_guest_info *info;
574 if (!handle_valid(vgpu->handle))
577 if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1))
580 intel_gvt_ops->vgpu_deactivate(vgpu);
582 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY,
583 &vgpu->vdev.iommu_notifier);
584 WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret);
586 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY,
587 &vgpu->vdev.group_notifier);
588 WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret);
590 info = (struct kvmgt_guest_info *)vgpu->handle;
591 kvmgt_guest_exit(info);
593 vgpu->vdev.kvm = NULL;
597 static void intel_vgpu_release(struct mdev_device *mdev)
599 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
601 __intel_vgpu_release(vgpu);
604 static void intel_vgpu_release_work(struct work_struct *work)
606 struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu,
609 __intel_vgpu_release(vgpu);
612 static uint64_t intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)
614 u32 start_lo, start_hi;
617 start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
618 PCI_BASE_ADDRESS_MEM_MASK;
619 mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
620 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
623 case PCI_BASE_ADDRESS_MEM_TYPE_64:
624 start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space
627 case PCI_BASE_ADDRESS_MEM_TYPE_32:
628 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
629 /* 1M mem BAR treated as 32-bit BAR */
631 /* mem unknown type treated as 32-bit BAR */
636 return ((u64)start_hi << 32) | start_lo;
639 static int intel_vgpu_bar_rw(struct intel_vgpu *vgpu, int bar, uint64_t off,
640 void *buf, unsigned int count, bool is_write)
642 uint64_t bar_start = intel_vgpu_get_bar_addr(vgpu, bar);
646 ret = intel_gvt_ops->emulate_mmio_write(vgpu,
647 bar_start + off, buf, count);
649 ret = intel_gvt_ops->emulate_mmio_read(vgpu,
650 bar_start + off, buf, count);
654 static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
655 size_t count, loff_t *ppos, bool is_write)
657 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
658 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
659 uint64_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
663 if (index >= VFIO_PCI_NUM_REGIONS) {
664 gvt_vgpu_err("invalid index: %u\n", index);
669 case VFIO_PCI_CONFIG_REGION_INDEX:
671 ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos,
674 ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos,
677 case VFIO_PCI_BAR0_REGION_INDEX:
678 ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_0, pos,
679 buf, count, is_write);
681 case VFIO_PCI_BAR2_REGION_INDEX:
682 ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_2, pos,
683 buf, count, is_write);
685 case VFIO_PCI_BAR1_REGION_INDEX:
686 case VFIO_PCI_BAR3_REGION_INDEX:
687 case VFIO_PCI_BAR4_REGION_INDEX:
688 case VFIO_PCI_BAR5_REGION_INDEX:
689 case VFIO_PCI_VGA_REGION_INDEX:
690 case VFIO_PCI_ROM_REGION_INDEX:
692 gvt_vgpu_err("unsupported region: %u\n", index);
695 return ret == 0 ? count : ret;
698 static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
699 size_t count, loff_t *ppos)
701 unsigned int done = 0;
707 if (count >= 4 && !(*ppos % 4)) {
710 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
715 if (copy_to_user(buf, &val, sizeof(val)))
719 } else if (count >= 2 && !(*ppos % 2)) {
722 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
727 if (copy_to_user(buf, &val, sizeof(val)))
734 ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos,
739 if (copy_to_user(buf, &val, sizeof(val)))
757 static ssize_t intel_vgpu_write(struct mdev_device *mdev,
758 const char __user *buf,
759 size_t count, loff_t *ppos)
761 unsigned int done = 0;
767 if (count >= 4 && !(*ppos % 4)) {
770 if (copy_from_user(&val, buf, sizeof(val)))
773 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
779 } else if (count >= 2 && !(*ppos % 2)) {
782 if (copy_from_user(&val, buf, sizeof(val)))
785 ret = intel_vgpu_rw(mdev, (char *)&val,
786 sizeof(val), ppos, true);
794 if (copy_from_user(&val, buf, sizeof(val)))
797 ret = intel_vgpu_rw(mdev, &val, sizeof(val),
816 static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
820 unsigned long req_size, pgoff = 0;
822 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
824 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
825 if (index >= VFIO_PCI_ROM_REGION_INDEX)
828 if (vma->vm_end < vma->vm_start)
830 if ((vma->vm_flags & VM_SHARED) == 0)
832 if (index != VFIO_PCI_BAR2_REGION_INDEX)
835 pg_prot = vma->vm_page_prot;
836 virtaddr = vma->vm_start;
837 req_size = vma->vm_end - vma->vm_start;
838 pgoff = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;
840 return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
843 static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type)
845 if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX)
851 static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu,
852 unsigned int index, unsigned int start,
853 unsigned int count, uint32_t flags,
859 static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu,
860 unsigned int index, unsigned int start,
861 unsigned int count, uint32_t flags, void *data)
866 static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu,
867 unsigned int index, unsigned int start, unsigned int count,
868 uint32_t flags, void *data)
873 static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu,
874 unsigned int index, unsigned int start, unsigned int count,
875 uint32_t flags, void *data)
877 struct eventfd_ctx *trigger;
879 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
880 int fd = *(int *)data;
882 trigger = eventfd_ctx_fdget(fd);
883 if (IS_ERR(trigger)) {
884 gvt_vgpu_err("eventfd_ctx_fdget failed\n");
885 return PTR_ERR(trigger);
887 vgpu->vdev.msi_trigger = trigger;
893 static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, uint32_t flags,
894 unsigned int index, unsigned int start, unsigned int count,
897 int (*func)(struct intel_vgpu *vgpu, unsigned int index,
898 unsigned int start, unsigned int count, uint32_t flags,
902 case VFIO_PCI_INTX_IRQ_INDEX:
903 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
904 case VFIO_IRQ_SET_ACTION_MASK:
905 func = intel_vgpu_set_intx_mask;
907 case VFIO_IRQ_SET_ACTION_UNMASK:
908 func = intel_vgpu_set_intx_unmask;
910 case VFIO_IRQ_SET_ACTION_TRIGGER:
911 func = intel_vgpu_set_intx_trigger;
915 case VFIO_PCI_MSI_IRQ_INDEX:
916 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
917 case VFIO_IRQ_SET_ACTION_MASK:
918 case VFIO_IRQ_SET_ACTION_UNMASK:
919 /* XXX Need masking support exported */
921 case VFIO_IRQ_SET_ACTION_TRIGGER:
922 func = intel_vgpu_set_msi_trigger;
931 return func(vgpu, index, start, count, flags, data);
934 static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
937 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
940 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd);
942 if (cmd == VFIO_DEVICE_GET_INFO) {
943 struct vfio_device_info info;
945 minsz = offsetofend(struct vfio_device_info, num_irqs);
947 if (copy_from_user(&info, (void __user *)arg, minsz))
950 if (info.argsz < minsz)
953 info.flags = VFIO_DEVICE_FLAGS_PCI;
954 info.flags |= VFIO_DEVICE_FLAGS_RESET;
955 info.num_regions = VFIO_PCI_NUM_REGIONS;
956 info.num_irqs = VFIO_PCI_NUM_IRQS;
958 return copy_to_user((void __user *)arg, &info, minsz) ?
961 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
962 struct vfio_region_info info;
963 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
965 struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
970 minsz = offsetofend(struct vfio_region_info, offset);
972 if (copy_from_user(&info, (void __user *)arg, minsz))
975 if (info.argsz < minsz)
978 switch (info.index) {
979 case VFIO_PCI_CONFIG_REGION_INDEX:
980 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
981 info.size = vgpu->gvt->device_info.cfg_space_size;
982 info.flags = VFIO_REGION_INFO_FLAG_READ |
983 VFIO_REGION_INFO_FLAG_WRITE;
985 case VFIO_PCI_BAR0_REGION_INDEX:
986 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
987 info.size = vgpu->cfg_space.bar[info.index].size;
993 info.flags = VFIO_REGION_INFO_FLAG_READ |
994 VFIO_REGION_INFO_FLAG_WRITE;
996 case VFIO_PCI_BAR1_REGION_INDEX:
997 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1001 case VFIO_PCI_BAR2_REGION_INDEX:
1002 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1003 info.flags = VFIO_REGION_INFO_FLAG_CAPS |
1004 VFIO_REGION_INFO_FLAG_MMAP |
1005 VFIO_REGION_INFO_FLAG_READ |
1006 VFIO_REGION_INFO_FLAG_WRITE;
1007 info.size = gvt_aperture_sz(vgpu->gvt);
1009 size = sizeof(*sparse) +
1010 (nr_areas * sizeof(*sparse->areas));
1011 sparse = kzalloc(size, GFP_KERNEL);
1015 sparse->nr_areas = nr_areas;
1016 cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1017 sparse->areas[0].offset =
1018 PAGE_ALIGN(vgpu_aperture_offset(vgpu));
1019 sparse->areas[0].size = vgpu_aperture_sz(vgpu);
1022 case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1023 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1027 gvt_dbg_core("get region info bar:%d\n", info.index);
1030 case VFIO_PCI_ROM_REGION_INDEX:
1031 case VFIO_PCI_VGA_REGION_INDEX:
1032 gvt_dbg_core("get region info index:%d\n", info.index);
1036 struct vfio_region_info_cap_type cap_type;
1038 if (info.index >= VFIO_PCI_NUM_REGIONS +
1039 vgpu->vdev.num_regions)
1042 i = info.index - VFIO_PCI_NUM_REGIONS;
1045 VFIO_PCI_INDEX_TO_OFFSET(info.index);
1046 info.size = vgpu->vdev.region[i].size;
1047 info.flags = vgpu->vdev.region[i].flags;
1049 cap_type.type = vgpu->vdev.region[i].type;
1050 cap_type.subtype = vgpu->vdev.region[i].subtype;
1052 ret = vfio_info_add_capability(&caps,
1053 VFIO_REGION_INFO_CAP_TYPE,
1060 if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) {
1061 switch (cap_type_id) {
1062 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1063 ret = vfio_info_add_capability(&caps,
1064 VFIO_REGION_INFO_CAP_SPARSE_MMAP,
1076 if (info.argsz < sizeof(info) + caps.size) {
1077 info.argsz = sizeof(info) + caps.size;
1078 info.cap_offset = 0;
1080 vfio_info_cap_shift(&caps, sizeof(info));
1081 if (copy_to_user((void __user *)arg +
1082 sizeof(info), caps.buf,
1087 info.cap_offset = sizeof(info);
1093 return copy_to_user((void __user *)arg, &info, minsz) ?
1095 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
1096 struct vfio_irq_info info;
1098 minsz = offsetofend(struct vfio_irq_info, count);
1100 if (copy_from_user(&info, (void __user *)arg, minsz))
1103 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
1106 switch (info.index) {
1107 case VFIO_PCI_INTX_IRQ_INDEX:
1108 case VFIO_PCI_MSI_IRQ_INDEX:
1114 info.flags = VFIO_IRQ_INFO_EVENTFD;
1116 info.count = intel_vgpu_get_irq_count(vgpu, info.index);
1118 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
1119 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
1120 VFIO_IRQ_INFO_AUTOMASKED);
1122 info.flags |= VFIO_IRQ_INFO_NORESIZE;
1124 return copy_to_user((void __user *)arg, &info, minsz) ?
1126 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
1127 struct vfio_irq_set hdr;
1130 size_t data_size = 0;
1132 minsz = offsetofend(struct vfio_irq_set, count);
1134 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1137 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
1138 int max = intel_vgpu_get_irq_count(vgpu, hdr.index);
1140 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
1141 VFIO_PCI_NUM_IRQS, &data_size);
1143 gvt_vgpu_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
1147 data = memdup_user((void __user *)(arg + minsz),
1150 return PTR_ERR(data);
1154 ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index,
1155 hdr.start, hdr.count, data);
1159 } else if (cmd == VFIO_DEVICE_RESET) {
1160 intel_gvt_ops->vgpu_reset(vgpu);
1168 vgpu_id_show(struct device *dev, struct device_attribute *attr,
1171 struct mdev_device *mdev = mdev_from_dev(dev);
1174 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1175 mdev_get_drvdata(mdev);
1176 return sprintf(buf, "%d\n", vgpu->id);
1178 return sprintf(buf, "\n");
1182 hw_id_show(struct device *dev, struct device_attribute *attr,
1185 struct mdev_device *mdev = mdev_from_dev(dev);
1188 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1189 mdev_get_drvdata(mdev);
1190 return sprintf(buf, "%u\n",
1191 vgpu->shadow_ctx->hw_id);
1193 return sprintf(buf, "\n");
1196 static DEVICE_ATTR_RO(vgpu_id);
1197 static DEVICE_ATTR_RO(hw_id);
1199 static struct attribute *intel_vgpu_attrs[] = {
1200 &dev_attr_vgpu_id.attr,
1201 &dev_attr_hw_id.attr,
1205 static const struct attribute_group intel_vgpu_group = {
1206 .name = "intel_vgpu",
1207 .attrs = intel_vgpu_attrs,
1210 static const struct attribute_group *intel_vgpu_groups[] = {
1215 static const struct mdev_parent_ops intel_vgpu_ops = {
1216 .supported_type_groups = intel_vgpu_type_groups,
1217 .mdev_attr_groups = intel_vgpu_groups,
1218 .create = intel_vgpu_create,
1219 .remove = intel_vgpu_remove,
1221 .open = intel_vgpu_open,
1222 .release = intel_vgpu_release,
1224 .read = intel_vgpu_read,
1225 .write = intel_vgpu_write,
1226 .mmap = intel_vgpu_mmap,
1227 .ioctl = intel_vgpu_ioctl,
1230 static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)
1232 if (!intel_gvt_init_vgpu_type_groups(gvt))
1235 intel_gvt_ops = ops;
1237 return mdev_register_device(dev, &intel_vgpu_ops);
1240 static void kvmgt_host_exit(struct device *dev, void *gvt)
1242 intel_gvt_cleanup_vgpu_type_groups(gvt);
1243 mdev_unregister_device(dev);
1246 static int kvmgt_write_protect_add(unsigned long handle, u64 gfn)
1248 struct kvmgt_guest_info *info;
1250 struct kvm_memory_slot *slot;
1253 if (!handle_valid(handle))
1256 info = (struct kvmgt_guest_info *)handle;
1259 idx = srcu_read_lock(&kvm->srcu);
1260 slot = gfn_to_memslot(kvm, gfn);
1262 srcu_read_unlock(&kvm->srcu, idx);
1266 spin_lock(&kvm->mmu_lock);
1268 if (kvmgt_gfn_is_write_protected(info, gfn))
1271 kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1272 kvmgt_protect_table_add(info, gfn);
1275 spin_unlock(&kvm->mmu_lock);
1276 srcu_read_unlock(&kvm->srcu, idx);
1280 static int kvmgt_write_protect_remove(unsigned long handle, u64 gfn)
1282 struct kvmgt_guest_info *info;
1284 struct kvm_memory_slot *slot;
1287 if (!handle_valid(handle))
1290 info = (struct kvmgt_guest_info *)handle;
1293 idx = srcu_read_lock(&kvm->srcu);
1294 slot = gfn_to_memslot(kvm, gfn);
1296 srcu_read_unlock(&kvm->srcu, idx);
1300 spin_lock(&kvm->mmu_lock);
1302 if (!kvmgt_gfn_is_write_protected(info, gfn))
1305 kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1306 kvmgt_protect_table_del(info, gfn);
1309 spin_unlock(&kvm->mmu_lock);
1310 srcu_read_unlock(&kvm->srcu, idx);
1314 static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1315 const u8 *val, int len,
1316 struct kvm_page_track_notifier_node *node)
1318 struct kvmgt_guest_info *info = container_of(node,
1319 struct kvmgt_guest_info, track_node);
1321 if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
1322 intel_gvt_ops->emulate_mmio_write(info->vgpu, gpa,
1326 static void kvmgt_page_track_flush_slot(struct kvm *kvm,
1327 struct kvm_memory_slot *slot,
1328 struct kvm_page_track_notifier_node *node)
1332 struct kvmgt_guest_info *info = container_of(node,
1333 struct kvmgt_guest_info, track_node);
1335 spin_lock(&kvm->mmu_lock);
1336 for (i = 0; i < slot->npages; i++) {
1337 gfn = slot->base_gfn + i;
1338 if (kvmgt_gfn_is_write_protected(info, gfn)) {
1339 kvm_slot_page_track_remove_page(kvm, slot, gfn,
1340 KVM_PAGE_TRACK_WRITE);
1341 kvmgt_protect_table_del(info, gfn);
1344 spin_unlock(&kvm->mmu_lock);
1347 static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
1349 struct intel_vgpu *itr;
1350 struct kvmgt_guest_info *info;
1354 mutex_lock(&vgpu->gvt->lock);
1355 for_each_active_vgpu(vgpu->gvt, itr, id) {
1356 if (!handle_valid(itr->handle))
1359 info = (struct kvmgt_guest_info *)itr->handle;
1360 if (kvm && kvm == info->kvm) {
1366 mutex_unlock(&vgpu->gvt->lock);
1370 static int kvmgt_guest_init(struct mdev_device *mdev)
1372 struct kvmgt_guest_info *info;
1373 struct intel_vgpu *vgpu;
1376 vgpu = mdev_get_drvdata(mdev);
1377 if (handle_valid(vgpu->handle))
1380 kvm = vgpu->vdev.kvm;
1381 if (!kvm || kvm->mm != current->mm) {
1382 gvt_vgpu_err("KVM is required to use Intel vGPU\n");
1386 if (__kvmgt_vgpu_exist(vgpu, kvm))
1389 info = vzalloc(sizeof(struct kvmgt_guest_info));
1393 vgpu->handle = (unsigned long)info;
1396 kvm_get_kvm(info->kvm);
1398 kvmgt_protect_table_init(info);
1399 gvt_cache_init(vgpu);
1401 info->track_node.track_write = kvmgt_page_track_write;
1402 info->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
1403 kvm_page_track_register_notifier(kvm, &info->track_node);
1408 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info)
1410 kvm_page_track_unregister_notifier(info->kvm, &info->track_node);
1411 kvm_put_kvm(info->kvm);
1412 kvmgt_protect_table_destroy(info);
1413 gvt_cache_destroy(info->vgpu);
1419 static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle)
1421 /* nothing to do here */
1425 static void kvmgt_detach_vgpu(unsigned long handle)
1427 /* nothing to do here */
1430 static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data)
1432 struct kvmgt_guest_info *info;
1433 struct intel_vgpu *vgpu;
1435 if (!handle_valid(handle))
1438 info = (struct kvmgt_guest_info *)handle;
1441 if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1)
1447 static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn)
1449 unsigned long iova, pfn;
1450 struct kvmgt_guest_info *info;
1452 struct intel_vgpu *vgpu;
1455 if (!handle_valid(handle))
1456 return INTEL_GVT_INVALID_ADDR;
1458 info = (struct kvmgt_guest_info *)handle;
1460 iova = gvt_cache_find(info->vgpu, gfn);
1461 if (iova != INTEL_GVT_INVALID_ADDR)
1464 pfn = INTEL_GVT_INVALID_ADDR;
1465 dev = mdev_dev(info->vgpu->vdev.mdev);
1466 rc = vfio_pin_pages(dev, &gfn, 1, IOMMU_READ | IOMMU_WRITE, &pfn);
1468 gvt_vgpu_err("vfio_pin_pages failed for gfn 0x%lx: %d\n",
1470 return INTEL_GVT_INVALID_ADDR;
1472 /* transfer to host iova for GFX to use DMA */
1473 rc = gvt_dma_map_iova(info->vgpu, pfn, &iova);
1475 gvt_vgpu_err("gvt_dma_map_iova failed for gfn: 0x%lx\n", gfn);
1476 vfio_unpin_pages(dev, &gfn, 1);
1477 return INTEL_GVT_INVALID_ADDR;
1480 gvt_cache_add(info->vgpu, gfn, iova);
1484 static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa,
1485 void *buf, unsigned long len, bool write)
1487 struct kvmgt_guest_info *info;
1490 bool kthread = current->mm == NULL;
1492 if (!handle_valid(handle))
1495 info = (struct kvmgt_guest_info *)handle;
1501 idx = srcu_read_lock(&kvm->srcu);
1502 ret = write ? kvm_write_guest(kvm, gpa, buf, len) :
1503 kvm_read_guest(kvm, gpa, buf, len);
1504 srcu_read_unlock(&kvm->srcu, idx);
1512 static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa,
1513 void *buf, unsigned long len)
1515 return kvmgt_rw_gpa(handle, gpa, buf, len, false);
1518 static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa,
1519 void *buf, unsigned long len)
1521 return kvmgt_rw_gpa(handle, gpa, buf, len, true);
1524 static unsigned long kvmgt_virt_to_pfn(void *addr)
1526 return PFN_DOWN(__pa(addr));
1529 struct intel_gvt_mpt kvmgt_mpt = {
1530 .host_init = kvmgt_host_init,
1531 .host_exit = kvmgt_host_exit,
1532 .attach_vgpu = kvmgt_attach_vgpu,
1533 .detach_vgpu = kvmgt_detach_vgpu,
1534 .inject_msi = kvmgt_inject_msi,
1535 .from_virt_to_mfn = kvmgt_virt_to_pfn,
1536 .set_wp_page = kvmgt_write_protect_add,
1537 .unset_wp_page = kvmgt_write_protect_remove,
1538 .read_gpa = kvmgt_read_gpa,
1539 .write_gpa = kvmgt_write_gpa,
1540 .gfn_to_mfn = kvmgt_gfn_to_pfn,
1542 EXPORT_SYMBOL_GPL(kvmgt_mpt);
1544 static int __init kvmgt_init(void)
1549 static void __exit kvmgt_exit(void)
1553 module_init(kvmgt_init);
1554 module_exit(kvmgt_exit);
1556 MODULE_LICENSE("GPL and additional rights");
1557 MODULE_AUTHOR("Intel Corporation");