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 if (!pfn_valid(pfn)) {
127 gvt_vgpu_err("pfn 0x%lx is not mem backed\n", pfn);
128 vfio_unpin_pages(mdev_dev(vgpu->vdev.mdev), &gfn, 1);
132 /* Setup DMA mapping. */
133 page = pfn_to_page(pfn);
134 *dma_addr = dma_map_page(dev, page, 0, PAGE_SIZE,
135 PCI_DMA_BIDIRECTIONAL);
136 if (dma_mapping_error(dev, *dma_addr)) {
137 gvt_vgpu_err("DMA mapping failed for gfn 0x%lx\n", gfn);
138 vfio_unpin_pages(mdev_dev(vgpu->vdev.mdev), &gfn, 1);
145 static void gvt_dma_unmap_page(struct intel_vgpu *vgpu, unsigned long gfn,
148 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
151 dma_unmap_page(dev, dma_addr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
152 ret = vfio_unpin_pages(mdev_dev(vgpu->vdev.mdev), &gfn, 1);
156 static struct gvt_dma *__gvt_cache_find_dma_addr(struct intel_vgpu *vgpu,
159 struct rb_node *node = vgpu->vdev.dma_addr_cache.rb_node;
163 itr = rb_entry(node, struct gvt_dma, dma_addr_node);
165 if (dma_addr < itr->dma_addr)
166 node = node->rb_left;
167 else if (dma_addr > itr->dma_addr)
168 node = node->rb_right;
175 static struct gvt_dma *__gvt_cache_find_gfn(struct intel_vgpu *vgpu, gfn_t gfn)
177 struct rb_node *node = vgpu->vdev.gfn_cache.rb_node;
181 itr = rb_entry(node, struct gvt_dma, gfn_node);
184 node = node->rb_left;
185 else if (gfn > itr->gfn)
186 node = node->rb_right;
193 static int __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
196 struct gvt_dma *new, *itr;
197 struct rb_node **link, *parent = NULL;
199 new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
205 new->dma_addr = dma_addr;
206 kref_init(&new->ref);
208 /* gfn_cache maps gfn to struct gvt_dma. */
209 link = &vgpu->vdev.gfn_cache.rb_node;
212 itr = rb_entry(parent, struct gvt_dma, gfn_node);
215 link = &parent->rb_left;
217 link = &parent->rb_right;
219 rb_link_node(&new->gfn_node, parent, link);
220 rb_insert_color(&new->gfn_node, &vgpu->vdev.gfn_cache);
222 /* dma_addr_cache maps dma addr to struct gvt_dma. */
224 link = &vgpu->vdev.dma_addr_cache.rb_node;
227 itr = rb_entry(parent, struct gvt_dma, dma_addr_node);
229 if (dma_addr < itr->dma_addr)
230 link = &parent->rb_left;
232 link = &parent->rb_right;
234 rb_link_node(&new->dma_addr_node, parent, link);
235 rb_insert_color(&new->dma_addr_node, &vgpu->vdev.dma_addr_cache);
237 vgpu->vdev.nr_cache_entries++;
241 static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
242 struct gvt_dma *entry)
244 rb_erase(&entry->gfn_node, &vgpu->vdev.gfn_cache);
245 rb_erase(&entry->dma_addr_node, &vgpu->vdev.dma_addr_cache);
247 vgpu->vdev.nr_cache_entries--;
250 static void gvt_cache_destroy(struct intel_vgpu *vgpu)
253 struct rb_node *node = NULL;
256 mutex_lock(&vgpu->vdev.cache_lock);
257 node = rb_first(&vgpu->vdev.gfn_cache);
259 mutex_unlock(&vgpu->vdev.cache_lock);
262 dma = rb_entry(node, struct gvt_dma, gfn_node);
263 gvt_dma_unmap_page(vgpu, dma->gfn, dma->dma_addr);
264 __gvt_cache_remove_entry(vgpu, dma);
265 mutex_unlock(&vgpu->vdev.cache_lock);
269 static void gvt_cache_init(struct intel_vgpu *vgpu)
271 vgpu->vdev.gfn_cache = RB_ROOT;
272 vgpu->vdev.dma_addr_cache = RB_ROOT;
273 vgpu->vdev.nr_cache_entries = 0;
274 mutex_init(&vgpu->vdev.cache_lock);
277 static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
279 hash_init(info->ptable);
282 static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
284 struct kvmgt_pgfn *p;
285 struct hlist_node *tmp;
288 hash_for_each_safe(info->ptable, i, tmp, p, hnode) {
294 static struct kvmgt_pgfn *
295 __kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
297 struct kvmgt_pgfn *p, *res = NULL;
299 hash_for_each_possible(info->ptable, p, hnode, gfn) {
309 static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info,
312 struct kvmgt_pgfn *p;
314 p = __kvmgt_protect_table_find(info, gfn);
318 static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
320 struct kvmgt_pgfn *p;
322 if (kvmgt_gfn_is_write_protected(info, gfn))
325 p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC);
326 if (WARN(!p, "gfn: 0x%llx\n", gfn))
330 hash_add(info->ptable, &p->hnode, gfn);
333 static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
336 struct kvmgt_pgfn *p;
338 p = __kvmgt_protect_table_find(info, gfn);
345 static size_t intel_vgpu_reg_rw_opregion(struct intel_vgpu *vgpu, char *buf,
346 size_t count, loff_t *ppos, bool iswrite)
348 unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) -
349 VFIO_PCI_NUM_REGIONS;
350 void *base = vgpu->vdev.region[i].data;
351 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
353 if (pos >= vgpu->vdev.region[i].size || iswrite) {
354 gvt_vgpu_err("invalid op or offset for Intel vgpu OpRegion\n");
357 count = min(count, (size_t)(vgpu->vdev.region[i].size - pos));
358 memcpy(buf, base + pos, count);
363 static void intel_vgpu_reg_release_opregion(struct intel_vgpu *vgpu,
364 struct vfio_region *region)
368 static const struct intel_vgpu_regops intel_vgpu_regops_opregion = {
369 .rw = intel_vgpu_reg_rw_opregion,
370 .release = intel_vgpu_reg_release_opregion,
373 static int intel_vgpu_register_reg(struct intel_vgpu *vgpu,
374 unsigned int type, unsigned int subtype,
375 const struct intel_vgpu_regops *ops,
376 size_t size, u32 flags, void *data)
378 struct vfio_region *region;
380 region = krealloc(vgpu->vdev.region,
381 (vgpu->vdev.num_regions + 1) * sizeof(*region),
386 vgpu->vdev.region = region;
387 vgpu->vdev.region[vgpu->vdev.num_regions].type = type;
388 vgpu->vdev.region[vgpu->vdev.num_regions].subtype = subtype;
389 vgpu->vdev.region[vgpu->vdev.num_regions].ops = ops;
390 vgpu->vdev.region[vgpu->vdev.num_regions].size = size;
391 vgpu->vdev.region[vgpu->vdev.num_regions].flags = flags;
392 vgpu->vdev.region[vgpu->vdev.num_regions].data = data;
393 vgpu->vdev.num_regions++;
397 static int kvmgt_get_vfio_device(void *p_vgpu)
399 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
401 vgpu->vdev.vfio_device = vfio_device_get_from_dev(
402 mdev_dev(vgpu->vdev.mdev));
403 if (!vgpu->vdev.vfio_device) {
404 gvt_vgpu_err("failed to get vfio device\n");
411 static int kvmgt_set_opregion(void *p_vgpu)
413 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
417 /* Each vgpu has its own opregion, although VFIO would create another
418 * one later. This one is used to expose opregion to VFIO. And the
419 * other one created by VFIO later, is used by guest actually.
421 base = vgpu_opregion(vgpu)->va;
425 if (memcmp(base, OPREGION_SIGNATURE, 16)) {
430 ret = intel_vgpu_register_reg(vgpu,
431 PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
432 VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION,
433 &intel_vgpu_regops_opregion, OPREGION_SIZE,
434 VFIO_REGION_INFO_FLAG_READ, base);
439 static void kvmgt_put_vfio_device(void *vgpu)
441 if (WARN_ON(!((struct intel_vgpu *)vgpu)->vdev.vfio_device))
444 vfio_device_put(((struct intel_vgpu *)vgpu)->vdev.vfio_device);
447 static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
449 struct intel_vgpu *vgpu = NULL;
450 struct intel_vgpu_type *type;
455 pdev = mdev_parent_dev(mdev);
456 gvt = kdev_to_i915(pdev)->gvt;
458 type = intel_gvt_ops->gvt_find_vgpu_type(gvt, kobject_name(kobj));
460 gvt_vgpu_err("failed to find type %s to create\n",
466 vgpu = intel_gvt_ops->vgpu_create(gvt, type);
467 if (IS_ERR_OR_NULL(vgpu)) {
468 ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu);
469 gvt_err("failed to create intel vgpu: %d\n", ret);
473 INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work);
475 vgpu->vdev.mdev = mdev;
476 mdev_set_drvdata(mdev, vgpu);
478 gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
479 dev_name(mdev_dev(mdev)));
486 static int intel_vgpu_remove(struct mdev_device *mdev)
488 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
490 if (handle_valid(vgpu->handle))
493 intel_gvt_ops->vgpu_destroy(vgpu);
497 static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
498 unsigned long action, void *data)
500 struct intel_vgpu *vgpu = container_of(nb,
502 vdev.iommu_notifier);
504 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
505 struct vfio_iommu_type1_dma_unmap *unmap = data;
506 struct gvt_dma *entry;
507 unsigned long iov_pfn, end_iov_pfn;
509 iov_pfn = unmap->iova >> PAGE_SHIFT;
510 end_iov_pfn = iov_pfn + unmap->size / PAGE_SIZE;
512 mutex_lock(&vgpu->vdev.cache_lock);
513 for (; iov_pfn < end_iov_pfn; iov_pfn++) {
514 entry = __gvt_cache_find_gfn(vgpu, iov_pfn);
518 gvt_dma_unmap_page(vgpu, entry->gfn, entry->dma_addr);
519 __gvt_cache_remove_entry(vgpu, entry);
521 mutex_unlock(&vgpu->vdev.cache_lock);
527 static int intel_vgpu_group_notifier(struct notifier_block *nb,
528 unsigned long action, void *data)
530 struct intel_vgpu *vgpu = container_of(nb,
532 vdev.group_notifier);
534 /* the only action we care about */
535 if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
536 vgpu->vdev.kvm = data;
539 schedule_work(&vgpu->vdev.release_work);
545 static int intel_vgpu_open(struct mdev_device *mdev)
547 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
548 unsigned long events;
551 vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
552 vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier;
554 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
555 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events,
556 &vgpu->vdev.iommu_notifier);
558 gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
563 events = VFIO_GROUP_NOTIFY_SET_KVM;
564 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events,
565 &vgpu->vdev.group_notifier);
567 gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
572 ret = kvmgt_guest_init(mdev);
576 intel_gvt_ops->vgpu_activate(vgpu);
578 atomic_set(&vgpu->vdev.released, 0);
582 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
583 &vgpu->vdev.group_notifier);
586 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
587 &vgpu->vdev.iommu_notifier);
592 static void intel_vgpu_release_msi_eventfd_ctx(struct intel_vgpu *vgpu)
594 struct eventfd_ctx *trigger;
596 trigger = vgpu->vdev.msi_trigger;
598 eventfd_ctx_put(trigger);
599 vgpu->vdev.msi_trigger = NULL;
603 static void __intel_vgpu_release(struct intel_vgpu *vgpu)
605 struct kvmgt_guest_info *info;
608 if (!handle_valid(vgpu->handle))
611 if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1))
614 intel_gvt_ops->vgpu_deactivate(vgpu);
616 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY,
617 &vgpu->vdev.iommu_notifier);
618 WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret);
620 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY,
621 &vgpu->vdev.group_notifier);
622 WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret);
624 info = (struct kvmgt_guest_info *)vgpu->handle;
625 kvmgt_guest_exit(info);
627 intel_vgpu_release_msi_eventfd_ctx(vgpu);
629 vgpu->vdev.kvm = NULL;
633 static void intel_vgpu_release(struct mdev_device *mdev)
635 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
637 __intel_vgpu_release(vgpu);
640 static void intel_vgpu_release_work(struct work_struct *work)
642 struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu,
645 __intel_vgpu_release(vgpu);
648 static uint64_t intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)
650 u32 start_lo, start_hi;
653 start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
654 PCI_BASE_ADDRESS_MEM_MASK;
655 mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
656 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
659 case PCI_BASE_ADDRESS_MEM_TYPE_64:
660 start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space
663 case PCI_BASE_ADDRESS_MEM_TYPE_32:
664 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
665 /* 1M mem BAR treated as 32-bit BAR */
667 /* mem unknown type treated as 32-bit BAR */
672 return ((u64)start_hi << 32) | start_lo;
675 static int intel_vgpu_bar_rw(struct intel_vgpu *vgpu, int bar, uint64_t off,
676 void *buf, unsigned int count, bool is_write)
678 uint64_t bar_start = intel_vgpu_get_bar_addr(vgpu, bar);
682 ret = intel_gvt_ops->emulate_mmio_write(vgpu,
683 bar_start + off, buf, count);
685 ret = intel_gvt_ops->emulate_mmio_read(vgpu,
686 bar_start + off, buf, count);
690 static inline bool intel_vgpu_in_aperture(struct intel_vgpu *vgpu, uint64_t off)
692 return off >= vgpu_aperture_offset(vgpu) &&
693 off < vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu);
696 static int intel_vgpu_aperture_rw(struct intel_vgpu *vgpu, uint64_t off,
697 void *buf, unsigned long count, bool is_write)
701 if (!intel_vgpu_in_aperture(vgpu, off) ||
702 !intel_vgpu_in_aperture(vgpu, off + count)) {
703 gvt_vgpu_err("Invalid aperture offset %llu\n", off);
707 aperture_va = io_mapping_map_wc(&vgpu->gvt->dev_priv->ggtt.iomap,
708 ALIGN_DOWN(off, PAGE_SIZE),
709 count + offset_in_page(off));
714 memcpy(aperture_va + offset_in_page(off), buf, count);
716 memcpy(buf, aperture_va + offset_in_page(off), count);
718 io_mapping_unmap(aperture_va);
723 static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
724 size_t count, loff_t *ppos, bool is_write)
726 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
727 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
728 uint64_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
732 if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions) {
733 gvt_vgpu_err("invalid index: %u\n", index);
738 case VFIO_PCI_CONFIG_REGION_INDEX:
740 ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos,
743 ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos,
746 case VFIO_PCI_BAR0_REGION_INDEX:
747 ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_0, pos,
748 buf, count, is_write);
750 case VFIO_PCI_BAR2_REGION_INDEX:
751 ret = intel_vgpu_aperture_rw(vgpu, pos, buf, count, is_write);
753 case VFIO_PCI_BAR1_REGION_INDEX:
754 case VFIO_PCI_BAR3_REGION_INDEX:
755 case VFIO_PCI_BAR4_REGION_INDEX:
756 case VFIO_PCI_BAR5_REGION_INDEX:
757 case VFIO_PCI_VGA_REGION_INDEX:
758 case VFIO_PCI_ROM_REGION_INDEX:
761 if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions)
764 index -= VFIO_PCI_NUM_REGIONS;
765 return vgpu->vdev.region[index].ops->rw(vgpu, buf, count,
769 return ret == 0 ? count : ret;
772 static bool gtt_entry(struct mdev_device *mdev, loff_t *ppos)
774 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
775 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
776 struct intel_gvt *gvt = vgpu->gvt;
779 /* Only allow MMIO GGTT entry access */
780 if (index != PCI_BASE_ADDRESS_0)
783 offset = (u64)(*ppos & VFIO_PCI_OFFSET_MASK) -
784 intel_vgpu_get_bar_gpa(vgpu, PCI_BASE_ADDRESS_0);
786 return (offset >= gvt->device_info.gtt_start_offset &&
787 offset < gvt->device_info.gtt_start_offset + gvt_ggtt_sz(gvt)) ?
791 static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
792 size_t count, loff_t *ppos)
794 unsigned int done = 0;
800 /* Only support GGTT entry 8 bytes read */
801 if (count >= 8 && !(*ppos % 8) &&
802 gtt_entry(mdev, ppos)) {
805 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
810 if (copy_to_user(buf, &val, sizeof(val)))
814 } else if (count >= 4 && !(*ppos % 4)) {
817 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
822 if (copy_to_user(buf, &val, sizeof(val)))
826 } else if (count >= 2 && !(*ppos % 2)) {
829 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
834 if (copy_to_user(buf, &val, sizeof(val)))
841 ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos,
846 if (copy_to_user(buf, &val, sizeof(val)))
864 static ssize_t intel_vgpu_write(struct mdev_device *mdev,
865 const char __user *buf,
866 size_t count, loff_t *ppos)
868 unsigned int done = 0;
874 /* Only support GGTT entry 8 bytes write */
875 if (count >= 8 && !(*ppos % 8) &&
876 gtt_entry(mdev, ppos)) {
879 if (copy_from_user(&val, buf, sizeof(val)))
882 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
888 } else if (count >= 4 && !(*ppos % 4)) {
891 if (copy_from_user(&val, buf, sizeof(val)))
894 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
900 } else if (count >= 2 && !(*ppos % 2)) {
903 if (copy_from_user(&val, buf, sizeof(val)))
906 ret = intel_vgpu_rw(mdev, (char *)&val,
907 sizeof(val), ppos, true);
915 if (copy_from_user(&val, buf, sizeof(val)))
918 ret = intel_vgpu_rw(mdev, &val, sizeof(val),
937 static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
941 unsigned long req_size, pgoff = 0;
943 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
945 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
946 if (index >= VFIO_PCI_ROM_REGION_INDEX)
949 if (vma->vm_end < vma->vm_start)
951 if ((vma->vm_flags & VM_SHARED) == 0)
953 if (index != VFIO_PCI_BAR2_REGION_INDEX)
956 pg_prot = vma->vm_page_prot;
957 virtaddr = vma->vm_start;
958 req_size = vma->vm_end - vma->vm_start;
959 pgoff = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;
961 return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
964 static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type)
966 if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX)
972 static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu,
973 unsigned int index, unsigned int start,
974 unsigned int count, uint32_t flags,
980 static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu,
981 unsigned int index, unsigned int start,
982 unsigned int count, uint32_t flags, void *data)
987 static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu,
988 unsigned int index, unsigned int start, unsigned int count,
989 uint32_t flags, void *data)
994 static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu,
995 unsigned int index, unsigned int start, unsigned int count,
996 uint32_t flags, void *data)
998 struct eventfd_ctx *trigger;
1000 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
1001 int fd = *(int *)data;
1003 trigger = eventfd_ctx_fdget(fd);
1004 if (IS_ERR(trigger)) {
1005 gvt_vgpu_err("eventfd_ctx_fdget failed\n");
1006 return PTR_ERR(trigger);
1008 vgpu->vdev.msi_trigger = trigger;
1009 } else if ((flags & VFIO_IRQ_SET_DATA_NONE) && !count)
1010 intel_vgpu_release_msi_eventfd_ctx(vgpu);
1015 static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, uint32_t flags,
1016 unsigned int index, unsigned int start, unsigned int count,
1019 int (*func)(struct intel_vgpu *vgpu, unsigned int index,
1020 unsigned int start, unsigned int count, uint32_t flags,
1024 case VFIO_PCI_INTX_IRQ_INDEX:
1025 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1026 case VFIO_IRQ_SET_ACTION_MASK:
1027 func = intel_vgpu_set_intx_mask;
1029 case VFIO_IRQ_SET_ACTION_UNMASK:
1030 func = intel_vgpu_set_intx_unmask;
1032 case VFIO_IRQ_SET_ACTION_TRIGGER:
1033 func = intel_vgpu_set_intx_trigger;
1037 case VFIO_PCI_MSI_IRQ_INDEX:
1038 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1039 case VFIO_IRQ_SET_ACTION_MASK:
1040 case VFIO_IRQ_SET_ACTION_UNMASK:
1041 /* XXX Need masking support exported */
1043 case VFIO_IRQ_SET_ACTION_TRIGGER:
1044 func = intel_vgpu_set_msi_trigger;
1053 return func(vgpu, index, start, count, flags, data);
1056 static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
1059 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
1060 unsigned long minsz;
1062 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd);
1064 if (cmd == VFIO_DEVICE_GET_INFO) {
1065 struct vfio_device_info info;
1067 minsz = offsetofend(struct vfio_device_info, num_irqs);
1069 if (copy_from_user(&info, (void __user *)arg, minsz))
1072 if (info.argsz < minsz)
1075 info.flags = VFIO_DEVICE_FLAGS_PCI;
1076 info.flags |= VFIO_DEVICE_FLAGS_RESET;
1077 info.num_regions = VFIO_PCI_NUM_REGIONS +
1078 vgpu->vdev.num_regions;
1079 info.num_irqs = VFIO_PCI_NUM_IRQS;
1081 return copy_to_user((void __user *)arg, &info, minsz) ?
1084 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
1085 struct vfio_region_info info;
1086 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
1088 struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
1093 minsz = offsetofend(struct vfio_region_info, offset);
1095 if (copy_from_user(&info, (void __user *)arg, minsz))
1098 if (info.argsz < minsz)
1101 switch (info.index) {
1102 case VFIO_PCI_CONFIG_REGION_INDEX:
1103 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1104 info.size = vgpu->gvt->device_info.cfg_space_size;
1105 info.flags = VFIO_REGION_INFO_FLAG_READ |
1106 VFIO_REGION_INFO_FLAG_WRITE;
1108 case VFIO_PCI_BAR0_REGION_INDEX:
1109 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1110 info.size = vgpu->cfg_space.bar[info.index].size;
1116 info.flags = VFIO_REGION_INFO_FLAG_READ |
1117 VFIO_REGION_INFO_FLAG_WRITE;
1119 case VFIO_PCI_BAR1_REGION_INDEX:
1120 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1124 case VFIO_PCI_BAR2_REGION_INDEX:
1125 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1126 info.flags = VFIO_REGION_INFO_FLAG_CAPS |
1127 VFIO_REGION_INFO_FLAG_MMAP |
1128 VFIO_REGION_INFO_FLAG_READ |
1129 VFIO_REGION_INFO_FLAG_WRITE;
1130 info.size = gvt_aperture_sz(vgpu->gvt);
1132 size = sizeof(*sparse) +
1133 (nr_areas * sizeof(*sparse->areas));
1134 sparse = kzalloc(size, GFP_KERNEL);
1138 sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1139 sparse->header.version = 1;
1140 sparse->nr_areas = nr_areas;
1141 cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1142 sparse->areas[0].offset =
1143 PAGE_ALIGN(vgpu_aperture_offset(vgpu));
1144 sparse->areas[0].size = vgpu_aperture_sz(vgpu);
1147 case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1148 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1152 gvt_dbg_core("get region info bar:%d\n", info.index);
1155 case VFIO_PCI_ROM_REGION_INDEX:
1156 case VFIO_PCI_VGA_REGION_INDEX:
1157 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1161 gvt_dbg_core("get region info index:%d\n", info.index);
1165 struct vfio_region_info_cap_type cap_type = {
1166 .header.id = VFIO_REGION_INFO_CAP_TYPE,
1167 .header.version = 1 };
1169 if (info.index >= VFIO_PCI_NUM_REGIONS +
1170 vgpu->vdev.num_regions)
1173 i = info.index - VFIO_PCI_NUM_REGIONS;
1176 VFIO_PCI_INDEX_TO_OFFSET(info.index);
1177 info.size = vgpu->vdev.region[i].size;
1178 info.flags = vgpu->vdev.region[i].flags;
1180 cap_type.type = vgpu->vdev.region[i].type;
1181 cap_type.subtype = vgpu->vdev.region[i].subtype;
1183 ret = vfio_info_add_capability(&caps,
1191 if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) {
1192 switch (cap_type_id) {
1193 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1194 ret = vfio_info_add_capability(&caps,
1195 &sparse->header, sizeof(*sparse) +
1197 sizeof(*sparse->areas)));
1208 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
1209 if (info.argsz < sizeof(info) + caps.size) {
1210 info.argsz = sizeof(info) + caps.size;
1211 info.cap_offset = 0;
1213 vfio_info_cap_shift(&caps, sizeof(info));
1214 if (copy_to_user((void __user *)arg +
1215 sizeof(info), caps.buf,
1220 info.cap_offset = sizeof(info);
1226 return copy_to_user((void __user *)arg, &info, minsz) ?
1228 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
1229 struct vfio_irq_info info;
1231 minsz = offsetofend(struct vfio_irq_info, count);
1233 if (copy_from_user(&info, (void __user *)arg, minsz))
1236 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
1239 switch (info.index) {
1240 case VFIO_PCI_INTX_IRQ_INDEX:
1241 case VFIO_PCI_MSI_IRQ_INDEX:
1247 info.flags = VFIO_IRQ_INFO_EVENTFD;
1249 info.count = intel_vgpu_get_irq_count(vgpu, info.index);
1251 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
1252 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
1253 VFIO_IRQ_INFO_AUTOMASKED);
1255 info.flags |= VFIO_IRQ_INFO_NORESIZE;
1257 return copy_to_user((void __user *)arg, &info, minsz) ?
1259 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
1260 struct vfio_irq_set hdr;
1263 size_t data_size = 0;
1265 minsz = offsetofend(struct vfio_irq_set, count);
1267 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1270 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
1271 int max = intel_vgpu_get_irq_count(vgpu, hdr.index);
1273 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
1274 VFIO_PCI_NUM_IRQS, &data_size);
1276 gvt_vgpu_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
1280 data = memdup_user((void __user *)(arg + minsz),
1283 return PTR_ERR(data);
1287 ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index,
1288 hdr.start, hdr.count, data);
1292 } else if (cmd == VFIO_DEVICE_RESET) {
1293 intel_gvt_ops->vgpu_reset(vgpu);
1295 } else if (cmd == VFIO_DEVICE_QUERY_GFX_PLANE) {
1296 struct vfio_device_gfx_plane_info dmabuf;
1299 minsz = offsetofend(struct vfio_device_gfx_plane_info,
1301 if (copy_from_user(&dmabuf, (void __user *)arg, minsz))
1303 if (dmabuf.argsz < minsz)
1306 ret = intel_gvt_ops->vgpu_query_plane(vgpu, &dmabuf);
1310 return copy_to_user((void __user *)arg, &dmabuf, minsz) ?
1312 } else if (cmd == VFIO_DEVICE_GET_GFX_DMABUF) {
1316 if (get_user(dmabuf_id, (__u32 __user *)arg))
1319 dmabuf_fd = intel_gvt_ops->vgpu_get_dmabuf(vgpu, dmabuf_id);
1328 vgpu_id_show(struct device *dev, struct device_attribute *attr,
1331 struct mdev_device *mdev = mdev_from_dev(dev);
1334 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1335 mdev_get_drvdata(mdev);
1336 return sprintf(buf, "%d\n", vgpu->id);
1338 return sprintf(buf, "\n");
1342 hw_id_show(struct device *dev, struct device_attribute *attr,
1345 struct mdev_device *mdev = mdev_from_dev(dev);
1348 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1349 mdev_get_drvdata(mdev);
1350 return sprintf(buf, "%u\n",
1351 vgpu->submission.shadow_ctx->hw_id);
1353 return sprintf(buf, "\n");
1356 static DEVICE_ATTR_RO(vgpu_id);
1357 static DEVICE_ATTR_RO(hw_id);
1359 static struct attribute *intel_vgpu_attrs[] = {
1360 &dev_attr_vgpu_id.attr,
1361 &dev_attr_hw_id.attr,
1365 static const struct attribute_group intel_vgpu_group = {
1366 .name = "intel_vgpu",
1367 .attrs = intel_vgpu_attrs,
1370 static const struct attribute_group *intel_vgpu_groups[] = {
1375 static struct mdev_parent_ops intel_vgpu_ops = {
1376 .mdev_attr_groups = intel_vgpu_groups,
1377 .create = intel_vgpu_create,
1378 .remove = intel_vgpu_remove,
1380 .open = intel_vgpu_open,
1381 .release = intel_vgpu_release,
1383 .read = intel_vgpu_read,
1384 .write = intel_vgpu_write,
1385 .mmap = intel_vgpu_mmap,
1386 .ioctl = intel_vgpu_ioctl,
1389 static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)
1391 struct attribute **kvm_type_attrs;
1392 struct attribute_group **kvm_vgpu_type_groups;
1394 intel_gvt_ops = ops;
1395 if (!intel_gvt_ops->get_gvt_attrs(&kvm_type_attrs,
1396 &kvm_vgpu_type_groups))
1398 intel_vgpu_ops.supported_type_groups = kvm_vgpu_type_groups;
1400 return mdev_register_device(dev, &intel_vgpu_ops);
1403 static void kvmgt_host_exit(struct device *dev, void *gvt)
1405 mdev_unregister_device(dev);
1408 static int kvmgt_page_track_add(unsigned long handle, u64 gfn)
1410 struct kvmgt_guest_info *info;
1412 struct kvm_memory_slot *slot;
1415 if (!handle_valid(handle))
1418 info = (struct kvmgt_guest_info *)handle;
1421 idx = srcu_read_lock(&kvm->srcu);
1422 slot = gfn_to_memslot(kvm, gfn);
1424 srcu_read_unlock(&kvm->srcu, idx);
1428 spin_lock(&kvm->mmu_lock);
1430 if (kvmgt_gfn_is_write_protected(info, gfn))
1433 kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1434 kvmgt_protect_table_add(info, gfn);
1437 spin_unlock(&kvm->mmu_lock);
1438 srcu_read_unlock(&kvm->srcu, idx);
1442 static int kvmgt_page_track_remove(unsigned long handle, u64 gfn)
1444 struct kvmgt_guest_info *info;
1446 struct kvm_memory_slot *slot;
1449 if (!handle_valid(handle))
1452 info = (struct kvmgt_guest_info *)handle;
1455 idx = srcu_read_lock(&kvm->srcu);
1456 slot = gfn_to_memslot(kvm, gfn);
1458 srcu_read_unlock(&kvm->srcu, idx);
1462 spin_lock(&kvm->mmu_lock);
1464 if (!kvmgt_gfn_is_write_protected(info, gfn))
1467 kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1468 kvmgt_protect_table_del(info, gfn);
1471 spin_unlock(&kvm->mmu_lock);
1472 srcu_read_unlock(&kvm->srcu, idx);
1476 static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1477 const u8 *val, int len,
1478 struct kvm_page_track_notifier_node *node)
1480 struct kvmgt_guest_info *info = container_of(node,
1481 struct kvmgt_guest_info, track_node);
1483 if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
1484 intel_gvt_ops->write_protect_handler(info->vgpu, gpa,
1488 static void kvmgt_page_track_flush_slot(struct kvm *kvm,
1489 struct kvm_memory_slot *slot,
1490 struct kvm_page_track_notifier_node *node)
1494 struct kvmgt_guest_info *info = container_of(node,
1495 struct kvmgt_guest_info, track_node);
1497 spin_lock(&kvm->mmu_lock);
1498 for (i = 0; i < slot->npages; i++) {
1499 gfn = slot->base_gfn + i;
1500 if (kvmgt_gfn_is_write_protected(info, gfn)) {
1501 kvm_slot_page_track_remove_page(kvm, slot, gfn,
1502 KVM_PAGE_TRACK_WRITE);
1503 kvmgt_protect_table_del(info, gfn);
1506 spin_unlock(&kvm->mmu_lock);
1509 static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
1511 struct intel_vgpu *itr;
1512 struct kvmgt_guest_info *info;
1516 mutex_lock(&vgpu->gvt->lock);
1517 for_each_active_vgpu(vgpu->gvt, itr, id) {
1518 if (!handle_valid(itr->handle))
1521 info = (struct kvmgt_guest_info *)itr->handle;
1522 if (kvm && kvm == info->kvm) {
1528 mutex_unlock(&vgpu->gvt->lock);
1532 static int kvmgt_guest_init(struct mdev_device *mdev)
1534 struct kvmgt_guest_info *info;
1535 struct intel_vgpu *vgpu;
1538 vgpu = mdev_get_drvdata(mdev);
1539 if (handle_valid(vgpu->handle))
1542 kvm = vgpu->vdev.kvm;
1543 if (!kvm || kvm->mm != current->mm) {
1544 gvt_vgpu_err("KVM is required to use Intel vGPU\n");
1548 if (__kvmgt_vgpu_exist(vgpu, kvm))
1551 info = vzalloc(sizeof(struct kvmgt_guest_info));
1555 vgpu->handle = (unsigned long)info;
1558 kvm_get_kvm(info->kvm);
1560 kvmgt_protect_table_init(info);
1561 gvt_cache_init(vgpu);
1563 mutex_init(&vgpu->dmabuf_lock);
1564 init_completion(&vgpu->vblank_done);
1566 info->track_node.track_write = kvmgt_page_track_write;
1567 info->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
1568 kvm_page_track_register_notifier(kvm, &info->track_node);
1570 info->debugfs_cache_entries = debugfs_create_ulong(
1571 "kvmgt_nr_cache_entries",
1572 0444, vgpu->debugfs,
1573 &vgpu->vdev.nr_cache_entries);
1574 if (!info->debugfs_cache_entries)
1575 gvt_vgpu_err("Cannot create kvmgt debugfs entry\n");
1580 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info)
1582 debugfs_remove(info->debugfs_cache_entries);
1584 kvm_page_track_unregister_notifier(info->kvm, &info->track_node);
1585 kvm_put_kvm(info->kvm);
1586 kvmgt_protect_table_destroy(info);
1587 gvt_cache_destroy(info->vgpu);
1593 static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle)
1595 /* nothing to do here */
1599 static void kvmgt_detach_vgpu(unsigned long handle)
1601 /* nothing to do here */
1604 static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data)
1606 struct kvmgt_guest_info *info;
1607 struct intel_vgpu *vgpu;
1609 if (!handle_valid(handle))
1612 info = (struct kvmgt_guest_info *)handle;
1616 * When guest is poweroff, msi_trigger is set to NULL, but vgpu's
1617 * config and mmio register isn't restored to default during guest
1618 * poweroff. If this vgpu is still used in next vm, this vgpu's pipe
1619 * may be enabled, then once this vgpu is active, it will get inject
1620 * vblank interrupt request. But msi_trigger is null until msi is
1621 * enabled by guest. so if msi_trigger is null, success is still
1622 * returned and don't inject interrupt into guest.
1624 if (vgpu->vdev.msi_trigger == NULL)
1627 if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1)
1633 static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn)
1635 struct kvmgt_guest_info *info;
1638 if (!handle_valid(handle))
1639 return INTEL_GVT_INVALID_ADDR;
1641 info = (struct kvmgt_guest_info *)handle;
1643 pfn = gfn_to_pfn(info->kvm, gfn);
1644 if (is_error_noslot_pfn(pfn))
1645 return INTEL_GVT_INVALID_ADDR;
1650 int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn,
1651 dma_addr_t *dma_addr)
1653 struct kvmgt_guest_info *info;
1654 struct intel_vgpu *vgpu;
1655 struct gvt_dma *entry;
1658 if (!handle_valid(handle))
1661 info = (struct kvmgt_guest_info *)handle;
1664 mutex_lock(&info->vgpu->vdev.cache_lock);
1666 entry = __gvt_cache_find_gfn(info->vgpu, gfn);
1668 ret = gvt_dma_map_page(vgpu, gfn, dma_addr);
1672 ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr);
1676 kref_get(&entry->ref);
1677 *dma_addr = entry->dma_addr;
1680 mutex_unlock(&info->vgpu->vdev.cache_lock);
1684 gvt_dma_unmap_page(vgpu, gfn, *dma_addr);
1686 mutex_unlock(&info->vgpu->vdev.cache_lock);
1690 static void __gvt_dma_release(struct kref *ref)
1692 struct gvt_dma *entry = container_of(ref, typeof(*entry), ref);
1694 gvt_dma_unmap_page(entry->vgpu, entry->gfn, entry->dma_addr);
1695 __gvt_cache_remove_entry(entry->vgpu, entry);
1698 void kvmgt_dma_unmap_guest_page(unsigned long handle, dma_addr_t dma_addr)
1700 struct kvmgt_guest_info *info;
1701 struct gvt_dma *entry;
1703 if (!handle_valid(handle))
1706 info = (struct kvmgt_guest_info *)handle;
1708 mutex_lock(&info->vgpu->vdev.cache_lock);
1709 entry = __gvt_cache_find_dma_addr(info->vgpu, dma_addr);
1711 kref_put(&entry->ref, __gvt_dma_release);
1712 mutex_unlock(&info->vgpu->vdev.cache_lock);
1715 static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa,
1716 void *buf, unsigned long len, bool write)
1718 struct kvmgt_guest_info *info;
1721 bool kthread = current->mm == NULL;
1723 if (!handle_valid(handle))
1726 info = (struct kvmgt_guest_info *)handle;
1732 idx = srcu_read_lock(&kvm->srcu);
1733 ret = write ? kvm_write_guest(kvm, gpa, buf, len) :
1734 kvm_read_guest(kvm, gpa, buf, len);
1735 srcu_read_unlock(&kvm->srcu, idx);
1743 static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa,
1744 void *buf, unsigned long len)
1746 return kvmgt_rw_gpa(handle, gpa, buf, len, false);
1749 static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa,
1750 void *buf, unsigned long len)
1752 return kvmgt_rw_gpa(handle, gpa, buf, len, true);
1755 static unsigned long kvmgt_virt_to_pfn(void *addr)
1757 return PFN_DOWN(__pa(addr));
1760 static bool kvmgt_is_valid_gfn(unsigned long handle, unsigned long gfn)
1762 struct kvmgt_guest_info *info;
1765 if (!handle_valid(handle))
1768 info = (struct kvmgt_guest_info *)handle;
1771 return kvm_is_visible_gfn(kvm, gfn);
1775 struct intel_gvt_mpt kvmgt_mpt = {
1776 .host_init = kvmgt_host_init,
1777 .host_exit = kvmgt_host_exit,
1778 .attach_vgpu = kvmgt_attach_vgpu,
1779 .detach_vgpu = kvmgt_detach_vgpu,
1780 .inject_msi = kvmgt_inject_msi,
1781 .from_virt_to_mfn = kvmgt_virt_to_pfn,
1782 .enable_page_track = kvmgt_page_track_add,
1783 .disable_page_track = kvmgt_page_track_remove,
1784 .read_gpa = kvmgt_read_gpa,
1785 .write_gpa = kvmgt_write_gpa,
1786 .gfn_to_mfn = kvmgt_gfn_to_pfn,
1787 .dma_map_guest_page = kvmgt_dma_map_guest_page,
1788 .dma_unmap_guest_page = kvmgt_dma_unmap_guest_page,
1789 .set_opregion = kvmgt_set_opregion,
1790 .get_vfio_device = kvmgt_get_vfio_device,
1791 .put_vfio_device = kvmgt_put_vfio_device,
1792 .is_valid_gfn = kvmgt_is_valid_gfn,
1794 EXPORT_SYMBOL_GPL(kvmgt_mpt);
1796 static int __init kvmgt_init(void)
1801 static void __exit kvmgt_exit(void)
1805 module_init(kvmgt_init);
1806 module_exit(kvmgt_exit);
1808 MODULE_LICENSE("GPL and additional rights");
1809 MODULE_AUTHOR("Intel Corporation");