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>
44 #include <linux/debugfs.h>
49 static const struct intel_gvt_ops *intel_gvt_ops;
51 /* helper macros copied from vfio-pci */
52 #define VFIO_PCI_OFFSET_SHIFT 40
53 #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
54 #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
55 #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
57 #define OPREGION_SIGNATURE "IntelGraphicsMem"
60 struct intel_vgpu_regops {
61 size_t (*rw)(struct intel_vgpu *vgpu, char *buf,
62 size_t count, loff_t *ppos, bool iswrite);
63 void (*release)(struct intel_vgpu *vgpu,
64 struct vfio_region *region);
72 const struct intel_vgpu_regops *ops;
78 struct hlist_node hnode;
81 struct kvmgt_guest_info {
83 struct intel_vgpu *vgpu;
84 struct kvm_page_track_notifier_node track_node;
85 #define NR_BKT (1 << 18)
86 struct hlist_head ptable[NR_BKT];
88 struct dentry *debugfs_cache_entries;
92 struct intel_vgpu *vgpu;
93 struct rb_node gfn_node;
94 struct rb_node dma_addr_node;
100 static inline bool handle_valid(unsigned long handle)
102 return !!(handle & ~0xff);
105 static int kvmgt_guest_init(struct mdev_device *mdev);
106 static void intel_vgpu_release_work(struct work_struct *work);
107 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info);
109 static int gvt_dma_map_page(struct intel_vgpu *vgpu, unsigned long gfn,
110 dma_addr_t *dma_addr)
112 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
117 /* Pin the page first. */
118 ret = vfio_pin_pages(mdev_dev(vgpu->vdev.mdev), &gfn, 1,
119 IOMMU_READ | IOMMU_WRITE, &pfn);
121 gvt_vgpu_err("vfio_pin_pages failed for gfn 0x%lx: %d\n",
126 /* Setup DMA mapping. */
127 page = pfn_to_page(pfn);
128 *dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE,
129 PCI_DMA_BIDIRECTIONAL);
130 if (dma_mapping_error(dev, *dma_addr)) {
131 gvt_vgpu_err("DMA mapping failed for gfn 0x%lx\n", gfn);
132 vfio_unpin_pages(mdev_dev(vgpu->vdev.mdev), &gfn, 1);
139 static void gvt_dma_unmap_page(struct intel_vgpu *vgpu, unsigned long gfn,
142 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
145 dma_unmap_page(dev, dma_addr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
146 ret = vfio_unpin_pages(mdev_dev(vgpu->vdev.mdev), &gfn, 1);
150 static struct gvt_dma *__gvt_cache_find_dma_addr(struct intel_vgpu *vgpu,
153 struct rb_node *node = vgpu->vdev.dma_addr_cache.rb_node;
157 itr = rb_entry(node, struct gvt_dma, dma_addr_node);
159 if (dma_addr < itr->dma_addr)
160 node = node->rb_left;
161 else if (dma_addr > itr->dma_addr)
162 node = node->rb_right;
169 static struct gvt_dma *__gvt_cache_find_gfn(struct intel_vgpu *vgpu, gfn_t gfn)
171 struct rb_node *node = vgpu->vdev.gfn_cache.rb_node;
175 itr = rb_entry(node, struct gvt_dma, gfn_node);
178 node = node->rb_left;
179 else if (gfn > itr->gfn)
180 node = node->rb_right;
187 static int __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
190 struct gvt_dma *new, *itr;
191 struct rb_node **link, *parent = NULL;
193 new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
199 new->dma_addr = dma_addr;
200 kref_init(&new->ref);
202 /* gfn_cache maps gfn to struct gvt_dma. */
203 link = &vgpu->vdev.gfn_cache.rb_node;
206 itr = rb_entry(parent, struct gvt_dma, gfn_node);
209 link = &parent->rb_left;
211 link = &parent->rb_right;
213 rb_link_node(&new->gfn_node, parent, link);
214 rb_insert_color(&new->gfn_node, &vgpu->vdev.gfn_cache);
216 /* dma_addr_cache maps dma addr to struct gvt_dma. */
218 link = &vgpu->vdev.dma_addr_cache.rb_node;
221 itr = rb_entry(parent, struct gvt_dma, dma_addr_node);
223 if (dma_addr < itr->dma_addr)
224 link = &parent->rb_left;
226 link = &parent->rb_right;
228 rb_link_node(&new->dma_addr_node, parent, link);
229 rb_insert_color(&new->dma_addr_node, &vgpu->vdev.dma_addr_cache);
231 vgpu->vdev.nr_cache_entries++;
235 static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
236 struct gvt_dma *entry)
238 rb_erase(&entry->gfn_node, &vgpu->vdev.gfn_cache);
239 rb_erase(&entry->dma_addr_node, &vgpu->vdev.dma_addr_cache);
241 vgpu->vdev.nr_cache_entries--;
244 static void gvt_cache_destroy(struct intel_vgpu *vgpu)
247 struct rb_node *node = NULL;
250 mutex_lock(&vgpu->vdev.cache_lock);
251 node = rb_first(&vgpu->vdev.gfn_cache);
253 mutex_unlock(&vgpu->vdev.cache_lock);
256 dma = rb_entry(node, struct gvt_dma, gfn_node);
257 gvt_dma_unmap_page(vgpu, dma->gfn, dma->dma_addr);
258 __gvt_cache_remove_entry(vgpu, dma);
259 mutex_unlock(&vgpu->vdev.cache_lock);
263 static void gvt_cache_init(struct intel_vgpu *vgpu)
265 vgpu->vdev.gfn_cache = RB_ROOT;
266 vgpu->vdev.dma_addr_cache = RB_ROOT;
267 vgpu->vdev.nr_cache_entries = 0;
268 mutex_init(&vgpu->vdev.cache_lock);
271 static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
273 hash_init(info->ptable);
276 static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
278 struct kvmgt_pgfn *p;
279 struct hlist_node *tmp;
282 hash_for_each_safe(info->ptable, i, tmp, p, hnode) {
288 static struct kvmgt_pgfn *
289 __kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
291 struct kvmgt_pgfn *p, *res = NULL;
293 hash_for_each_possible(info->ptable, p, hnode, gfn) {
303 static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info,
306 struct kvmgt_pgfn *p;
308 p = __kvmgt_protect_table_find(info, gfn);
312 static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
314 struct kvmgt_pgfn *p;
316 if (kvmgt_gfn_is_write_protected(info, gfn))
319 p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC);
320 if (WARN(!p, "gfn: 0x%llx\n", gfn))
324 hash_add(info->ptable, &p->hnode, gfn);
327 static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
330 struct kvmgt_pgfn *p;
332 p = __kvmgt_protect_table_find(info, gfn);
339 static size_t intel_vgpu_reg_rw_opregion(struct intel_vgpu *vgpu, char *buf,
340 size_t count, loff_t *ppos, bool iswrite)
342 unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) -
343 VFIO_PCI_NUM_REGIONS;
344 void *base = vgpu->vdev.region[i].data;
345 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
347 if (pos >= vgpu->vdev.region[i].size || iswrite) {
348 gvt_vgpu_err("invalid op or offset for Intel vgpu OpRegion\n");
351 count = min(count, (size_t)(vgpu->vdev.region[i].size - pos));
352 memcpy(buf, base + pos, count);
357 static void intel_vgpu_reg_release_opregion(struct intel_vgpu *vgpu,
358 struct vfio_region *region)
362 static const struct intel_vgpu_regops intel_vgpu_regops_opregion = {
363 .rw = intel_vgpu_reg_rw_opregion,
364 .release = intel_vgpu_reg_release_opregion,
367 static int intel_vgpu_register_reg(struct intel_vgpu *vgpu,
368 unsigned int type, unsigned int subtype,
369 const struct intel_vgpu_regops *ops,
370 size_t size, u32 flags, void *data)
372 struct vfio_region *region;
374 region = krealloc(vgpu->vdev.region,
375 (vgpu->vdev.num_regions + 1) * sizeof(*region),
380 vgpu->vdev.region = region;
381 vgpu->vdev.region[vgpu->vdev.num_regions].type = type;
382 vgpu->vdev.region[vgpu->vdev.num_regions].subtype = subtype;
383 vgpu->vdev.region[vgpu->vdev.num_regions].ops = ops;
384 vgpu->vdev.region[vgpu->vdev.num_regions].size = size;
385 vgpu->vdev.region[vgpu->vdev.num_regions].flags = flags;
386 vgpu->vdev.region[vgpu->vdev.num_regions].data = data;
387 vgpu->vdev.num_regions++;
391 static int kvmgt_get_vfio_device(void *p_vgpu)
393 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
395 vgpu->vdev.vfio_device = vfio_device_get_from_dev(
396 mdev_dev(vgpu->vdev.mdev));
397 if (!vgpu->vdev.vfio_device) {
398 gvt_vgpu_err("failed to get vfio device\n");
405 static int kvmgt_set_opregion(void *p_vgpu)
407 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
411 /* Each vgpu has its own opregion, although VFIO would create another
412 * one later. This one is used to expose opregion to VFIO. And the
413 * other one created by VFIO later, is used by guest actually.
415 base = vgpu_opregion(vgpu)->va;
419 if (memcmp(base, OPREGION_SIGNATURE, 16)) {
424 ret = intel_vgpu_register_reg(vgpu,
425 PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
426 VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION,
427 &intel_vgpu_regops_opregion, OPREGION_SIZE,
428 VFIO_REGION_INFO_FLAG_READ, base);
433 static void kvmgt_put_vfio_device(void *vgpu)
435 if (WARN_ON(!((struct intel_vgpu *)vgpu)->vdev.vfio_device))
438 vfio_device_put(((struct intel_vgpu *)vgpu)->vdev.vfio_device);
441 static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
443 struct intel_vgpu *vgpu = NULL;
444 struct intel_vgpu_type *type;
449 pdev = mdev_parent_dev(mdev);
450 gvt = kdev_to_i915(pdev)->gvt;
452 type = intel_gvt_ops->gvt_find_vgpu_type(gvt, kobject_name(kobj));
454 gvt_vgpu_err("failed to find type %s to create\n",
460 vgpu = intel_gvt_ops->vgpu_create(gvt, type);
461 if (IS_ERR_OR_NULL(vgpu)) {
462 ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu);
463 gvt_err("failed to create intel vgpu: %d\n", ret);
467 INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work);
469 vgpu->vdev.mdev = mdev;
470 mdev_set_drvdata(mdev, vgpu);
472 gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
473 dev_name(mdev_dev(mdev)));
480 static int intel_vgpu_remove(struct mdev_device *mdev)
482 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
484 if (handle_valid(vgpu->handle))
487 intel_gvt_ops->vgpu_destroy(vgpu);
491 static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
492 unsigned long action, void *data)
494 struct intel_vgpu *vgpu = container_of(nb,
496 vdev.iommu_notifier);
498 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
499 struct vfio_iommu_type1_dma_unmap *unmap = data;
500 struct gvt_dma *entry;
501 unsigned long iov_pfn, end_iov_pfn;
503 iov_pfn = unmap->iova >> PAGE_SHIFT;
504 end_iov_pfn = iov_pfn + unmap->size / PAGE_SIZE;
506 mutex_lock(&vgpu->vdev.cache_lock);
507 for (; iov_pfn < end_iov_pfn; iov_pfn++) {
508 entry = __gvt_cache_find_gfn(vgpu, iov_pfn);
512 gvt_dma_unmap_page(vgpu, entry->gfn, entry->dma_addr);
513 __gvt_cache_remove_entry(vgpu, entry);
515 mutex_unlock(&vgpu->vdev.cache_lock);
521 static int intel_vgpu_group_notifier(struct notifier_block *nb,
522 unsigned long action, void *data)
524 struct intel_vgpu *vgpu = container_of(nb,
526 vdev.group_notifier);
528 /* the only action we care about */
529 if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
530 vgpu->vdev.kvm = data;
533 schedule_work(&vgpu->vdev.release_work);
539 static int intel_vgpu_open(struct mdev_device *mdev)
541 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
542 unsigned long events;
545 vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
546 vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier;
548 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
549 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events,
550 &vgpu->vdev.iommu_notifier);
552 gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
557 events = VFIO_GROUP_NOTIFY_SET_KVM;
558 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events,
559 &vgpu->vdev.group_notifier);
561 gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
566 ret = kvmgt_guest_init(mdev);
570 intel_gvt_ops->vgpu_activate(vgpu);
572 atomic_set(&vgpu->vdev.released, 0);
576 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
577 &vgpu->vdev.group_notifier);
580 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
581 &vgpu->vdev.iommu_notifier);
586 static void __intel_vgpu_release(struct intel_vgpu *vgpu)
588 struct kvmgt_guest_info *info;
591 if (!handle_valid(vgpu->handle))
594 if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1))
597 intel_gvt_ops->vgpu_deactivate(vgpu);
599 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY,
600 &vgpu->vdev.iommu_notifier);
601 WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret);
603 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY,
604 &vgpu->vdev.group_notifier);
605 WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret);
607 info = (struct kvmgt_guest_info *)vgpu->handle;
608 kvmgt_guest_exit(info);
610 vgpu->vdev.kvm = NULL;
614 static void intel_vgpu_release(struct mdev_device *mdev)
616 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
618 __intel_vgpu_release(vgpu);
621 static void intel_vgpu_release_work(struct work_struct *work)
623 struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu,
626 __intel_vgpu_release(vgpu);
629 static uint64_t intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)
631 u32 start_lo, start_hi;
634 start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
635 PCI_BASE_ADDRESS_MEM_MASK;
636 mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
637 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
640 case PCI_BASE_ADDRESS_MEM_TYPE_64:
641 start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space
644 case PCI_BASE_ADDRESS_MEM_TYPE_32:
645 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
646 /* 1M mem BAR treated as 32-bit BAR */
648 /* mem unknown type treated as 32-bit BAR */
653 return ((u64)start_hi << 32) | start_lo;
656 static int intel_vgpu_bar_rw(struct intel_vgpu *vgpu, int bar, uint64_t off,
657 void *buf, unsigned int count, bool is_write)
659 uint64_t bar_start = intel_vgpu_get_bar_addr(vgpu, bar);
663 ret = intel_gvt_ops->emulate_mmio_write(vgpu,
664 bar_start + off, buf, count);
666 ret = intel_gvt_ops->emulate_mmio_read(vgpu,
667 bar_start + off, buf, count);
671 static inline bool intel_vgpu_in_aperture(struct intel_vgpu *vgpu, uint64_t off)
673 return off >= vgpu_aperture_offset(vgpu) &&
674 off < vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu);
677 static int intel_vgpu_aperture_rw(struct intel_vgpu *vgpu, uint64_t off,
678 void *buf, unsigned long count, bool is_write)
682 if (!intel_vgpu_in_aperture(vgpu, off) ||
683 !intel_vgpu_in_aperture(vgpu, off + count)) {
684 gvt_vgpu_err("Invalid aperture offset %llu\n", off);
688 aperture_va = io_mapping_map_wc(&vgpu->gvt->dev_priv->ggtt.iomap,
689 ALIGN_DOWN(off, PAGE_SIZE),
690 count + offset_in_page(off));
695 memcpy(aperture_va + offset_in_page(off), buf, count);
697 memcpy(buf, aperture_va + offset_in_page(off), count);
699 io_mapping_unmap(aperture_va);
704 static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
705 size_t count, loff_t *ppos, bool is_write)
707 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
708 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
709 uint64_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
713 if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions) {
714 gvt_vgpu_err("invalid index: %u\n", index);
719 case VFIO_PCI_CONFIG_REGION_INDEX:
721 ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos,
724 ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos,
727 case VFIO_PCI_BAR0_REGION_INDEX:
728 ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_0, pos,
729 buf, count, is_write);
731 case VFIO_PCI_BAR2_REGION_INDEX:
732 ret = intel_vgpu_aperture_rw(vgpu, pos, buf, count, is_write);
734 case VFIO_PCI_BAR1_REGION_INDEX:
735 case VFIO_PCI_BAR3_REGION_INDEX:
736 case VFIO_PCI_BAR4_REGION_INDEX:
737 case VFIO_PCI_BAR5_REGION_INDEX:
738 case VFIO_PCI_VGA_REGION_INDEX:
739 case VFIO_PCI_ROM_REGION_INDEX:
742 if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions)
745 index -= VFIO_PCI_NUM_REGIONS;
746 return vgpu->vdev.region[index].ops->rw(vgpu, buf, count,
750 return ret == 0 ? count : ret;
753 static bool gtt_entry(struct mdev_device *mdev, loff_t *ppos)
755 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
756 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
757 struct intel_gvt *gvt = vgpu->gvt;
760 /* Only allow MMIO GGTT entry access */
761 if (index != PCI_BASE_ADDRESS_0)
764 offset = (u64)(*ppos & VFIO_PCI_OFFSET_MASK) -
765 intel_vgpu_get_bar_gpa(vgpu, PCI_BASE_ADDRESS_0);
767 return (offset >= gvt->device_info.gtt_start_offset &&
768 offset < gvt->device_info.gtt_start_offset + gvt_ggtt_sz(gvt)) ?
772 static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
773 size_t count, loff_t *ppos)
775 unsigned int done = 0;
781 /* Only support GGTT entry 8 bytes read */
782 if (count >= 8 && !(*ppos % 8) &&
783 gtt_entry(mdev, ppos)) {
786 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
791 if (copy_to_user(buf, &val, sizeof(val)))
795 } else if (count >= 4 && !(*ppos % 4)) {
798 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
803 if (copy_to_user(buf, &val, sizeof(val)))
807 } else if (count >= 2 && !(*ppos % 2)) {
810 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
815 if (copy_to_user(buf, &val, sizeof(val)))
822 ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos,
827 if (copy_to_user(buf, &val, sizeof(val)))
845 static ssize_t intel_vgpu_write(struct mdev_device *mdev,
846 const char __user *buf,
847 size_t count, loff_t *ppos)
849 unsigned int done = 0;
855 /* Only support GGTT entry 8 bytes write */
856 if (count >= 8 && !(*ppos % 8) &&
857 gtt_entry(mdev, ppos)) {
860 if (copy_from_user(&val, buf, sizeof(val)))
863 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
869 } else if (count >= 4 && !(*ppos % 4)) {
872 if (copy_from_user(&val, buf, sizeof(val)))
875 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
881 } else if (count >= 2 && !(*ppos % 2)) {
884 if (copy_from_user(&val, buf, sizeof(val)))
887 ret = intel_vgpu_rw(mdev, (char *)&val,
888 sizeof(val), ppos, true);
896 if (copy_from_user(&val, buf, sizeof(val)))
899 ret = intel_vgpu_rw(mdev, &val, sizeof(val),
918 static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
922 unsigned long req_size, pgoff = 0;
924 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
926 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
927 if (index >= VFIO_PCI_ROM_REGION_INDEX)
930 if (vma->vm_end < vma->vm_start)
932 if ((vma->vm_flags & VM_SHARED) == 0)
934 if (index != VFIO_PCI_BAR2_REGION_INDEX)
937 pg_prot = vma->vm_page_prot;
938 virtaddr = vma->vm_start;
939 req_size = vma->vm_end - vma->vm_start;
940 pgoff = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;
942 return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
945 static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type)
947 if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX)
953 static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu,
954 unsigned int index, unsigned int start,
955 unsigned int count, uint32_t flags,
961 static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu,
962 unsigned int index, unsigned int start,
963 unsigned int count, uint32_t flags, void *data)
968 static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu,
969 unsigned int index, unsigned int start, unsigned int count,
970 uint32_t flags, void *data)
975 static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu,
976 unsigned int index, unsigned int start, unsigned int count,
977 uint32_t flags, void *data)
979 struct eventfd_ctx *trigger;
981 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
982 int fd = *(int *)data;
984 trigger = eventfd_ctx_fdget(fd);
985 if (IS_ERR(trigger)) {
986 gvt_vgpu_err("eventfd_ctx_fdget failed\n");
987 return PTR_ERR(trigger);
989 vgpu->vdev.msi_trigger = trigger;
995 static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, uint32_t flags,
996 unsigned int index, unsigned int start, unsigned int count,
999 int (*func)(struct intel_vgpu *vgpu, unsigned int index,
1000 unsigned int start, unsigned int count, uint32_t flags,
1004 case VFIO_PCI_INTX_IRQ_INDEX:
1005 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1006 case VFIO_IRQ_SET_ACTION_MASK:
1007 func = intel_vgpu_set_intx_mask;
1009 case VFIO_IRQ_SET_ACTION_UNMASK:
1010 func = intel_vgpu_set_intx_unmask;
1012 case VFIO_IRQ_SET_ACTION_TRIGGER:
1013 func = intel_vgpu_set_intx_trigger;
1017 case VFIO_PCI_MSI_IRQ_INDEX:
1018 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1019 case VFIO_IRQ_SET_ACTION_MASK:
1020 case VFIO_IRQ_SET_ACTION_UNMASK:
1021 /* XXX Need masking support exported */
1023 case VFIO_IRQ_SET_ACTION_TRIGGER:
1024 func = intel_vgpu_set_msi_trigger;
1033 return func(vgpu, index, start, count, flags, data);
1036 static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
1039 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
1040 unsigned long minsz;
1042 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd);
1044 if (cmd == VFIO_DEVICE_GET_INFO) {
1045 struct vfio_device_info info;
1047 minsz = offsetofend(struct vfio_device_info, num_irqs);
1049 if (copy_from_user(&info, (void __user *)arg, minsz))
1052 if (info.argsz < minsz)
1055 info.flags = VFIO_DEVICE_FLAGS_PCI;
1056 info.flags |= VFIO_DEVICE_FLAGS_RESET;
1057 info.num_regions = VFIO_PCI_NUM_REGIONS +
1058 vgpu->vdev.num_regions;
1059 info.num_irqs = VFIO_PCI_NUM_IRQS;
1061 return copy_to_user((void __user *)arg, &info, minsz) ?
1064 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
1065 struct vfio_region_info info;
1066 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
1068 struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
1073 minsz = offsetofend(struct vfio_region_info, offset);
1075 if (copy_from_user(&info, (void __user *)arg, minsz))
1078 if (info.argsz < minsz)
1081 switch (info.index) {
1082 case VFIO_PCI_CONFIG_REGION_INDEX:
1083 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1084 info.size = vgpu->gvt->device_info.cfg_space_size;
1085 info.flags = VFIO_REGION_INFO_FLAG_READ |
1086 VFIO_REGION_INFO_FLAG_WRITE;
1088 case VFIO_PCI_BAR0_REGION_INDEX:
1089 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1090 info.size = vgpu->cfg_space.bar[info.index].size;
1096 info.flags = VFIO_REGION_INFO_FLAG_READ |
1097 VFIO_REGION_INFO_FLAG_WRITE;
1099 case VFIO_PCI_BAR1_REGION_INDEX:
1100 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1104 case VFIO_PCI_BAR2_REGION_INDEX:
1105 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1106 info.flags = VFIO_REGION_INFO_FLAG_CAPS |
1107 VFIO_REGION_INFO_FLAG_MMAP |
1108 VFIO_REGION_INFO_FLAG_READ |
1109 VFIO_REGION_INFO_FLAG_WRITE;
1110 info.size = gvt_aperture_sz(vgpu->gvt);
1112 size = sizeof(*sparse) +
1113 (nr_areas * sizeof(*sparse->areas));
1114 sparse = kzalloc(size, GFP_KERNEL);
1118 sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1119 sparse->header.version = 1;
1120 sparse->nr_areas = nr_areas;
1121 cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1122 sparse->areas[0].offset =
1123 PAGE_ALIGN(vgpu_aperture_offset(vgpu));
1124 sparse->areas[0].size = vgpu_aperture_sz(vgpu);
1127 case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1128 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1132 gvt_dbg_core("get region info bar:%d\n", info.index);
1135 case VFIO_PCI_ROM_REGION_INDEX:
1136 case VFIO_PCI_VGA_REGION_INDEX:
1137 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1141 gvt_dbg_core("get region info index:%d\n", info.index);
1145 struct vfio_region_info_cap_type cap_type = {
1146 .header.id = VFIO_REGION_INFO_CAP_TYPE,
1147 .header.version = 1 };
1149 if (info.index >= VFIO_PCI_NUM_REGIONS +
1150 vgpu->vdev.num_regions)
1153 i = info.index - VFIO_PCI_NUM_REGIONS;
1156 VFIO_PCI_INDEX_TO_OFFSET(info.index);
1157 info.size = vgpu->vdev.region[i].size;
1158 info.flags = vgpu->vdev.region[i].flags;
1160 cap_type.type = vgpu->vdev.region[i].type;
1161 cap_type.subtype = vgpu->vdev.region[i].subtype;
1163 ret = vfio_info_add_capability(&caps,
1171 if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) {
1172 switch (cap_type_id) {
1173 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1174 ret = vfio_info_add_capability(&caps,
1175 &sparse->header, sizeof(*sparse) +
1177 sizeof(*sparse->areas)));
1188 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
1189 if (info.argsz < sizeof(info) + caps.size) {
1190 info.argsz = sizeof(info) + caps.size;
1191 info.cap_offset = 0;
1193 vfio_info_cap_shift(&caps, sizeof(info));
1194 if (copy_to_user((void __user *)arg +
1195 sizeof(info), caps.buf,
1200 info.cap_offset = sizeof(info);
1206 return copy_to_user((void __user *)arg, &info, minsz) ?
1208 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
1209 struct vfio_irq_info info;
1211 minsz = offsetofend(struct vfio_irq_info, count);
1213 if (copy_from_user(&info, (void __user *)arg, minsz))
1216 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
1219 switch (info.index) {
1220 case VFIO_PCI_INTX_IRQ_INDEX:
1221 case VFIO_PCI_MSI_IRQ_INDEX:
1227 info.flags = VFIO_IRQ_INFO_EVENTFD;
1229 info.count = intel_vgpu_get_irq_count(vgpu, info.index);
1231 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
1232 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
1233 VFIO_IRQ_INFO_AUTOMASKED);
1235 info.flags |= VFIO_IRQ_INFO_NORESIZE;
1237 return copy_to_user((void __user *)arg, &info, minsz) ?
1239 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
1240 struct vfio_irq_set hdr;
1243 size_t data_size = 0;
1245 minsz = offsetofend(struct vfio_irq_set, count);
1247 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1250 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
1251 int max = intel_vgpu_get_irq_count(vgpu, hdr.index);
1253 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
1254 VFIO_PCI_NUM_IRQS, &data_size);
1256 gvt_vgpu_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
1260 data = memdup_user((void __user *)(arg + minsz),
1263 return PTR_ERR(data);
1267 ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index,
1268 hdr.start, hdr.count, data);
1272 } else if (cmd == VFIO_DEVICE_RESET) {
1273 intel_gvt_ops->vgpu_reset(vgpu);
1275 } else if (cmd == VFIO_DEVICE_QUERY_GFX_PLANE) {
1276 struct vfio_device_gfx_plane_info dmabuf;
1279 minsz = offsetofend(struct vfio_device_gfx_plane_info,
1281 if (copy_from_user(&dmabuf, (void __user *)arg, minsz))
1283 if (dmabuf.argsz < minsz)
1286 ret = intel_gvt_ops->vgpu_query_plane(vgpu, &dmabuf);
1290 return copy_to_user((void __user *)arg, &dmabuf, minsz) ?
1292 } else if (cmd == VFIO_DEVICE_GET_GFX_DMABUF) {
1296 if (get_user(dmabuf_id, (__u32 __user *)arg))
1299 dmabuf_fd = intel_gvt_ops->vgpu_get_dmabuf(vgpu, dmabuf_id);
1308 vgpu_id_show(struct device *dev, struct device_attribute *attr,
1311 struct mdev_device *mdev = mdev_from_dev(dev);
1314 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1315 mdev_get_drvdata(mdev);
1316 return sprintf(buf, "%d\n", vgpu->id);
1318 return sprintf(buf, "\n");
1322 hw_id_show(struct device *dev, struct device_attribute *attr,
1325 struct mdev_device *mdev = mdev_from_dev(dev);
1328 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1329 mdev_get_drvdata(mdev);
1330 return sprintf(buf, "%u\n",
1331 vgpu->submission.shadow_ctx->hw_id);
1333 return sprintf(buf, "\n");
1336 static DEVICE_ATTR_RO(vgpu_id);
1337 static DEVICE_ATTR_RO(hw_id);
1339 static struct attribute *intel_vgpu_attrs[] = {
1340 &dev_attr_vgpu_id.attr,
1341 &dev_attr_hw_id.attr,
1345 static const struct attribute_group intel_vgpu_group = {
1346 .name = "intel_vgpu",
1347 .attrs = intel_vgpu_attrs,
1350 static const struct attribute_group *intel_vgpu_groups[] = {
1355 static struct mdev_parent_ops intel_vgpu_ops = {
1356 .mdev_attr_groups = intel_vgpu_groups,
1357 .create = intel_vgpu_create,
1358 .remove = intel_vgpu_remove,
1360 .open = intel_vgpu_open,
1361 .release = intel_vgpu_release,
1363 .read = intel_vgpu_read,
1364 .write = intel_vgpu_write,
1365 .mmap = intel_vgpu_mmap,
1366 .ioctl = intel_vgpu_ioctl,
1369 static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)
1371 struct attribute **kvm_type_attrs;
1372 struct attribute_group **kvm_vgpu_type_groups;
1374 intel_gvt_ops = ops;
1375 if (!intel_gvt_ops->get_gvt_attrs(&kvm_type_attrs,
1376 &kvm_vgpu_type_groups))
1378 intel_vgpu_ops.supported_type_groups = kvm_vgpu_type_groups;
1380 return mdev_register_device(dev, &intel_vgpu_ops);
1383 static void kvmgt_host_exit(struct device *dev, void *gvt)
1385 mdev_unregister_device(dev);
1388 static int kvmgt_page_track_add(unsigned long handle, u64 gfn)
1390 struct kvmgt_guest_info *info;
1392 struct kvm_memory_slot *slot;
1395 if (!handle_valid(handle))
1398 info = (struct kvmgt_guest_info *)handle;
1401 idx = srcu_read_lock(&kvm->srcu);
1402 slot = gfn_to_memslot(kvm, gfn);
1404 srcu_read_unlock(&kvm->srcu, idx);
1408 spin_lock(&kvm->mmu_lock);
1410 if (kvmgt_gfn_is_write_protected(info, gfn))
1413 kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1414 kvmgt_protect_table_add(info, gfn);
1417 spin_unlock(&kvm->mmu_lock);
1418 srcu_read_unlock(&kvm->srcu, idx);
1422 static int kvmgt_page_track_remove(unsigned long handle, u64 gfn)
1424 struct kvmgt_guest_info *info;
1426 struct kvm_memory_slot *slot;
1429 if (!handle_valid(handle))
1432 info = (struct kvmgt_guest_info *)handle;
1435 idx = srcu_read_lock(&kvm->srcu);
1436 slot = gfn_to_memslot(kvm, gfn);
1438 srcu_read_unlock(&kvm->srcu, idx);
1442 spin_lock(&kvm->mmu_lock);
1444 if (!kvmgt_gfn_is_write_protected(info, gfn))
1447 kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1448 kvmgt_protect_table_del(info, gfn);
1451 spin_unlock(&kvm->mmu_lock);
1452 srcu_read_unlock(&kvm->srcu, idx);
1456 static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1457 const u8 *val, int len,
1458 struct kvm_page_track_notifier_node *node)
1460 struct kvmgt_guest_info *info = container_of(node,
1461 struct kvmgt_guest_info, track_node);
1463 if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
1464 intel_gvt_ops->write_protect_handler(info->vgpu, gpa,
1468 static void kvmgt_page_track_flush_slot(struct kvm *kvm,
1469 struct kvm_memory_slot *slot,
1470 struct kvm_page_track_notifier_node *node)
1474 struct kvmgt_guest_info *info = container_of(node,
1475 struct kvmgt_guest_info, track_node);
1477 spin_lock(&kvm->mmu_lock);
1478 for (i = 0; i < slot->npages; i++) {
1479 gfn = slot->base_gfn + i;
1480 if (kvmgt_gfn_is_write_protected(info, gfn)) {
1481 kvm_slot_page_track_remove_page(kvm, slot, gfn,
1482 KVM_PAGE_TRACK_WRITE);
1483 kvmgt_protect_table_del(info, gfn);
1486 spin_unlock(&kvm->mmu_lock);
1489 static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
1491 struct intel_vgpu *itr;
1492 struct kvmgt_guest_info *info;
1496 mutex_lock(&vgpu->gvt->lock);
1497 for_each_active_vgpu(vgpu->gvt, itr, id) {
1498 if (!handle_valid(itr->handle))
1501 info = (struct kvmgt_guest_info *)itr->handle;
1502 if (kvm && kvm == info->kvm) {
1508 mutex_unlock(&vgpu->gvt->lock);
1512 static int kvmgt_guest_init(struct mdev_device *mdev)
1514 struct kvmgt_guest_info *info;
1515 struct intel_vgpu *vgpu;
1518 vgpu = mdev_get_drvdata(mdev);
1519 if (handle_valid(vgpu->handle))
1522 kvm = vgpu->vdev.kvm;
1523 if (!kvm || kvm->mm != current->mm) {
1524 gvt_vgpu_err("KVM is required to use Intel vGPU\n");
1528 if (__kvmgt_vgpu_exist(vgpu, kvm))
1531 info = vzalloc(sizeof(struct kvmgt_guest_info));
1535 vgpu->handle = (unsigned long)info;
1538 kvm_get_kvm(info->kvm);
1540 kvmgt_protect_table_init(info);
1541 gvt_cache_init(vgpu);
1543 mutex_init(&vgpu->dmabuf_lock);
1544 init_completion(&vgpu->vblank_done);
1546 info->track_node.track_write = kvmgt_page_track_write;
1547 info->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
1548 kvm_page_track_register_notifier(kvm, &info->track_node);
1550 info->debugfs_cache_entries = debugfs_create_ulong(
1551 "kvmgt_nr_cache_entries",
1552 0444, vgpu->debugfs,
1553 &vgpu->vdev.nr_cache_entries);
1554 if (!info->debugfs_cache_entries)
1555 gvt_vgpu_err("Cannot create kvmgt debugfs entry\n");
1560 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info)
1562 debugfs_remove(info->debugfs_cache_entries);
1564 kvm_page_track_unregister_notifier(info->kvm, &info->track_node);
1565 kvm_put_kvm(info->kvm);
1566 kvmgt_protect_table_destroy(info);
1567 gvt_cache_destroy(info->vgpu);
1573 static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle)
1575 /* nothing to do here */
1579 static void kvmgt_detach_vgpu(unsigned long handle)
1581 /* nothing to do here */
1584 static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data)
1586 struct kvmgt_guest_info *info;
1587 struct intel_vgpu *vgpu;
1589 if (!handle_valid(handle))
1592 info = (struct kvmgt_guest_info *)handle;
1595 if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1)
1601 static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn)
1603 struct kvmgt_guest_info *info;
1606 if (!handle_valid(handle))
1607 return INTEL_GVT_INVALID_ADDR;
1609 info = (struct kvmgt_guest_info *)handle;
1611 pfn = gfn_to_pfn(info->kvm, gfn);
1612 if (is_error_noslot_pfn(pfn))
1613 return INTEL_GVT_INVALID_ADDR;
1618 int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn,
1619 dma_addr_t *dma_addr)
1621 struct kvmgt_guest_info *info;
1622 struct intel_vgpu *vgpu;
1623 struct gvt_dma *entry;
1626 if (!handle_valid(handle))
1629 info = (struct kvmgt_guest_info *)handle;
1632 mutex_lock(&info->vgpu->vdev.cache_lock);
1634 entry = __gvt_cache_find_gfn(info->vgpu, gfn);
1636 ret = gvt_dma_map_page(vgpu, gfn, dma_addr);
1640 ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr);
1644 kref_get(&entry->ref);
1645 *dma_addr = entry->dma_addr;
1648 mutex_unlock(&info->vgpu->vdev.cache_lock);
1652 gvt_dma_unmap_page(vgpu, gfn, *dma_addr);
1654 mutex_unlock(&info->vgpu->vdev.cache_lock);
1658 static void __gvt_dma_release(struct kref *ref)
1660 struct gvt_dma *entry = container_of(ref, typeof(*entry), ref);
1662 gvt_dma_unmap_page(entry->vgpu, entry->gfn, entry->dma_addr);
1663 __gvt_cache_remove_entry(entry->vgpu, entry);
1666 void kvmgt_dma_unmap_guest_page(unsigned long handle, dma_addr_t dma_addr)
1668 struct kvmgt_guest_info *info;
1669 struct gvt_dma *entry;
1671 if (!handle_valid(handle))
1674 info = (struct kvmgt_guest_info *)handle;
1676 mutex_lock(&info->vgpu->vdev.cache_lock);
1677 entry = __gvt_cache_find_dma_addr(info->vgpu, dma_addr);
1679 kref_put(&entry->ref, __gvt_dma_release);
1680 mutex_unlock(&info->vgpu->vdev.cache_lock);
1683 static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa,
1684 void *buf, unsigned long len, bool write)
1686 struct kvmgt_guest_info *info;
1689 bool kthread = current->mm == NULL;
1691 if (!handle_valid(handle))
1694 info = (struct kvmgt_guest_info *)handle;
1700 idx = srcu_read_lock(&kvm->srcu);
1701 ret = write ? kvm_write_guest(kvm, gpa, buf, len) :
1702 kvm_read_guest(kvm, gpa, buf, len);
1703 srcu_read_unlock(&kvm->srcu, idx);
1711 static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa,
1712 void *buf, unsigned long len)
1714 return kvmgt_rw_gpa(handle, gpa, buf, len, false);
1717 static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa,
1718 void *buf, unsigned long len)
1720 return kvmgt_rw_gpa(handle, gpa, buf, len, true);
1723 static unsigned long kvmgt_virt_to_pfn(void *addr)
1725 return PFN_DOWN(__pa(addr));
1728 static bool kvmgt_is_valid_gfn(unsigned long handle, unsigned long gfn)
1730 struct kvmgt_guest_info *info;
1733 if (!handle_valid(handle))
1736 info = (struct kvmgt_guest_info *)handle;
1739 return kvm_is_visible_gfn(kvm, gfn);
1743 struct intel_gvt_mpt kvmgt_mpt = {
1744 .host_init = kvmgt_host_init,
1745 .host_exit = kvmgt_host_exit,
1746 .attach_vgpu = kvmgt_attach_vgpu,
1747 .detach_vgpu = kvmgt_detach_vgpu,
1748 .inject_msi = kvmgt_inject_msi,
1749 .from_virt_to_mfn = kvmgt_virt_to_pfn,
1750 .enable_page_track = kvmgt_page_track_add,
1751 .disable_page_track = kvmgt_page_track_remove,
1752 .read_gpa = kvmgt_read_gpa,
1753 .write_gpa = kvmgt_write_gpa,
1754 .gfn_to_mfn = kvmgt_gfn_to_pfn,
1755 .dma_map_guest_page = kvmgt_dma_map_guest_page,
1756 .dma_unmap_guest_page = kvmgt_dma_unmap_guest_page,
1757 .set_opregion = kvmgt_set_opregion,
1758 .get_vfio_device = kvmgt_get_vfio_device,
1759 .put_vfio_device = kvmgt_put_vfio_device,
1760 .is_valid_gfn = kvmgt_is_valid_gfn,
1762 EXPORT_SYMBOL_GPL(kvmgt_mpt);
1764 static int __init kvmgt_init(void)
1769 static void __exit kvmgt_exit(void)
1773 module_init(kvmgt_init);
1774 module_exit(kvmgt_exit);
1776 MODULE_LICENSE("GPL and additional rights");
1777 MODULE_AUTHOR("Intel Corporation");