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)
56 #define OPREGION_SIGNATURE "IntelGraphicsMem"
59 struct intel_vgpu_regops
{
60 size_t (*rw
)(struct intel_vgpu
*vgpu
, char *buf
,
61 size_t count
, loff_t
*ppos
, bool iswrite
);
62 void (*release
)(struct intel_vgpu
*vgpu
,
63 struct vfio_region
*region
);
71 const struct intel_vgpu_regops
*ops
;
77 struct hlist_node hnode
;
80 struct kvmgt_guest_info
{
82 struct intel_vgpu
*vgpu
;
83 struct kvm_page_track_notifier_node track_node
;
84 #define NR_BKT (1 << 18)
85 struct hlist_head ptable
[NR_BKT
];
95 static inline bool handle_valid(unsigned long handle
)
97 return !!(handle
& ~0xff);
100 static int kvmgt_guest_init(struct mdev_device
*mdev
);
101 static void intel_vgpu_release_work(struct work_struct
*work
);
102 static bool kvmgt_guest_exit(struct kvmgt_guest_info
*info
);
104 static int gvt_dma_map_iova(struct intel_vgpu
*vgpu
, kvm_pfn_t pfn
,
108 struct device
*dev
= &vgpu
->gvt
->dev_priv
->drm
.pdev
->dev
;
111 if (unlikely(!pfn_valid(pfn
)))
114 page
= pfn_to_page(pfn
);
115 daddr
= dma_map_page(dev
, page
, 0, PAGE_SIZE
,
116 PCI_DMA_BIDIRECTIONAL
);
117 if (dma_mapping_error(dev
, daddr
))
120 *iova
= (unsigned long)(daddr
>> PAGE_SHIFT
);
124 static void gvt_dma_unmap_iova(struct intel_vgpu
*vgpu
, unsigned long iova
)
126 struct device
*dev
= &vgpu
->gvt
->dev_priv
->drm
.pdev
->dev
;
129 daddr
= (dma_addr_t
)(iova
<< PAGE_SHIFT
);
130 dma_unmap_page(dev
, daddr
, PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
133 static struct gvt_dma
*__gvt_cache_find(struct intel_vgpu
*vgpu
, gfn_t gfn
)
135 struct rb_node
*node
= vgpu
->vdev
.cache
.rb_node
;
136 struct gvt_dma
*ret
= NULL
;
139 struct gvt_dma
*itr
= rb_entry(node
, struct gvt_dma
, node
);
142 node
= node
->rb_left
;
143 else if (gfn
> itr
->gfn
)
144 node
= node
->rb_right
;
155 static unsigned long gvt_cache_find(struct intel_vgpu
*vgpu
, gfn_t gfn
)
157 struct gvt_dma
*entry
;
160 mutex_lock(&vgpu
->vdev
.cache_lock
);
162 entry
= __gvt_cache_find(vgpu
, gfn
);
163 iova
= (entry
== NULL
) ? INTEL_GVT_INVALID_ADDR
: entry
->iova
;
165 mutex_unlock(&vgpu
->vdev
.cache_lock
);
169 static void gvt_cache_add(struct intel_vgpu
*vgpu
, gfn_t gfn
,
172 struct gvt_dma
*new, *itr
;
173 struct rb_node
**link
= &vgpu
->vdev
.cache
.rb_node
, *parent
= NULL
;
175 new = kzalloc(sizeof(struct gvt_dma
), GFP_KERNEL
);
182 mutex_lock(&vgpu
->vdev
.cache_lock
);
185 itr
= rb_entry(parent
, struct gvt_dma
, node
);
189 else if (gfn
< itr
->gfn
)
190 link
= &parent
->rb_left
;
192 link
= &parent
->rb_right
;
195 rb_link_node(&new->node
, parent
, link
);
196 rb_insert_color(&new->node
, &vgpu
->vdev
.cache
);
197 mutex_unlock(&vgpu
->vdev
.cache_lock
);
201 mutex_unlock(&vgpu
->vdev
.cache_lock
);
205 static void __gvt_cache_remove_entry(struct intel_vgpu
*vgpu
,
206 struct gvt_dma
*entry
)
208 rb_erase(&entry
->node
, &vgpu
->vdev
.cache
);
212 static void gvt_cache_remove(struct intel_vgpu
*vgpu
, gfn_t gfn
)
214 struct device
*dev
= mdev_dev(vgpu
->vdev
.mdev
);
215 struct gvt_dma
*this;
219 mutex_lock(&vgpu
->vdev
.cache_lock
);
220 this = __gvt_cache_find(vgpu
, gfn
);
222 mutex_unlock(&vgpu
->vdev
.cache_lock
);
227 gvt_dma_unmap_iova(vgpu
, this->iova
);
228 rc
= vfio_unpin_pages(dev
, &g1
, 1);
230 __gvt_cache_remove_entry(vgpu
, this);
231 mutex_unlock(&vgpu
->vdev
.cache_lock
);
234 static void gvt_cache_init(struct intel_vgpu
*vgpu
)
236 vgpu
->vdev
.cache
= RB_ROOT
;
237 mutex_init(&vgpu
->vdev
.cache_lock
);
240 static void gvt_cache_destroy(struct intel_vgpu
*vgpu
)
243 struct rb_node
*node
= NULL
;
244 struct device
*dev
= mdev_dev(vgpu
->vdev
.mdev
);
248 mutex_lock(&vgpu
->vdev
.cache_lock
);
249 node
= rb_first(&vgpu
->vdev
.cache
);
251 mutex_unlock(&vgpu
->vdev
.cache_lock
);
254 dma
= rb_entry(node
, struct gvt_dma
, node
);
255 gvt_dma_unmap_iova(vgpu
, dma
->iova
);
257 __gvt_cache_remove_entry(vgpu
, dma
);
258 mutex_unlock(&vgpu
->vdev
.cache_lock
);
259 vfio_unpin_pages(dev
, &gfn
, 1);
263 static void kvmgt_protect_table_init(struct kvmgt_guest_info
*info
)
265 hash_init(info
->ptable
);
268 static void kvmgt_protect_table_destroy(struct kvmgt_guest_info
*info
)
270 struct kvmgt_pgfn
*p
;
271 struct hlist_node
*tmp
;
274 hash_for_each_safe(info
->ptable
, i
, tmp
, p
, hnode
) {
280 static struct kvmgt_pgfn
*
281 __kvmgt_protect_table_find(struct kvmgt_guest_info
*info
, gfn_t gfn
)
283 struct kvmgt_pgfn
*p
, *res
= NULL
;
285 hash_for_each_possible(info
->ptable
, p
, hnode
, gfn
) {
295 static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info
*info
,
298 struct kvmgt_pgfn
*p
;
300 p
= __kvmgt_protect_table_find(info
, gfn
);
304 static void kvmgt_protect_table_add(struct kvmgt_guest_info
*info
, gfn_t gfn
)
306 struct kvmgt_pgfn
*p
;
308 if (kvmgt_gfn_is_write_protected(info
, gfn
))
311 p
= kzalloc(sizeof(struct kvmgt_pgfn
), GFP_ATOMIC
);
312 if (WARN(!p
, "gfn: 0x%llx\n", gfn
))
316 hash_add(info
->ptable
, &p
->hnode
, gfn
);
319 static void kvmgt_protect_table_del(struct kvmgt_guest_info
*info
,
322 struct kvmgt_pgfn
*p
;
324 p
= __kvmgt_protect_table_find(info
, gfn
);
331 static size_t intel_vgpu_reg_rw_opregion(struct intel_vgpu
*vgpu
, char *buf
,
332 size_t count
, loff_t
*ppos
, bool iswrite
)
334 unsigned int i
= VFIO_PCI_OFFSET_TO_INDEX(*ppos
) -
335 VFIO_PCI_NUM_REGIONS
;
336 void *base
= vgpu
->vdev
.region
[i
].data
;
337 loff_t pos
= *ppos
& VFIO_PCI_OFFSET_MASK
;
339 if (pos
>= vgpu
->vdev
.region
[i
].size
|| iswrite
) {
340 gvt_vgpu_err("invalid op or offset for Intel vgpu OpRegion\n");
343 count
= min(count
, (size_t)(vgpu
->vdev
.region
[i
].size
- pos
));
344 memcpy(buf
, base
+ pos
, count
);
349 static void intel_vgpu_reg_release_opregion(struct intel_vgpu
*vgpu
,
350 struct vfio_region
*region
)
354 static const struct intel_vgpu_regops intel_vgpu_regops_opregion
= {
355 .rw
= intel_vgpu_reg_rw_opregion
,
356 .release
= intel_vgpu_reg_release_opregion
,
359 static int intel_vgpu_register_reg(struct intel_vgpu
*vgpu
,
360 unsigned int type
, unsigned int subtype
,
361 const struct intel_vgpu_regops
*ops
,
362 size_t size
, u32 flags
, void *data
)
364 struct vfio_region
*region
;
366 region
= krealloc(vgpu
->vdev
.region
,
367 (vgpu
->vdev
.num_regions
+ 1) * sizeof(*region
),
372 vgpu
->vdev
.region
= region
;
373 vgpu
->vdev
.region
[vgpu
->vdev
.num_regions
].type
= type
;
374 vgpu
->vdev
.region
[vgpu
->vdev
.num_regions
].subtype
= subtype
;
375 vgpu
->vdev
.region
[vgpu
->vdev
.num_regions
].ops
= ops
;
376 vgpu
->vdev
.region
[vgpu
->vdev
.num_regions
].size
= size
;
377 vgpu
->vdev
.region
[vgpu
->vdev
.num_regions
].flags
= flags
;
378 vgpu
->vdev
.region
[vgpu
->vdev
.num_regions
].data
= data
;
379 vgpu
->vdev
.num_regions
++;
383 static int kvmgt_get_vfio_device(void *p_vgpu
)
385 struct intel_vgpu
*vgpu
= (struct intel_vgpu
*)p_vgpu
;
387 vgpu
->vdev
.vfio_device
= vfio_device_get_from_dev(
388 mdev_dev(vgpu
->vdev
.mdev
));
389 if (!vgpu
->vdev
.vfio_device
) {
390 gvt_vgpu_err("failed to get vfio device\n");
397 static int kvmgt_set_opregion(void *p_vgpu
)
399 struct intel_vgpu
*vgpu
= (struct intel_vgpu
*)p_vgpu
;
403 /* Each vgpu has its own opregion, although VFIO would create another
404 * one later. This one is used to expose opregion to VFIO. And the
405 * other one created by VFIO later, is used by guest actually.
407 base
= vgpu_opregion(vgpu
)->va
;
411 if (memcmp(base
, OPREGION_SIGNATURE
, 16)) {
416 ret
= intel_vgpu_register_reg(vgpu
,
417 PCI_VENDOR_ID_INTEL
| VFIO_REGION_TYPE_PCI_VENDOR_TYPE
,
418 VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION
,
419 &intel_vgpu_regops_opregion
, OPREGION_SIZE
,
420 VFIO_REGION_INFO_FLAG_READ
, base
);
425 static void kvmgt_put_vfio_device(void *vgpu
)
427 if (WARN_ON(!((struct intel_vgpu
*)vgpu
)->vdev
.vfio_device
))
430 vfio_device_put(((struct intel_vgpu
*)vgpu
)->vdev
.vfio_device
);
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_ops
->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 inline bool intel_vgpu_in_aperture(struct intel_vgpu
*vgpu
, uint64_t off
)
656 return off
>= vgpu_aperture_offset(vgpu
) &&
657 off
< vgpu_aperture_offset(vgpu
) + vgpu_aperture_sz(vgpu
);
660 static int intel_vgpu_aperture_rw(struct intel_vgpu
*vgpu
, uint64_t off
,
661 void *buf
, unsigned long count
, bool is_write
)
665 if (!intel_vgpu_in_aperture(vgpu
, off
) ||
666 !intel_vgpu_in_aperture(vgpu
, off
+ count
)) {
667 gvt_vgpu_err("Invalid aperture offset %llu\n", off
);
671 aperture_va
= io_mapping_map_wc(&vgpu
->gvt
->dev_priv
->ggtt
.iomap
,
672 ALIGN_DOWN(off
, PAGE_SIZE
),
673 count
+ offset_in_page(off
));
678 memcpy(aperture_va
+ offset_in_page(off
), buf
, count
);
680 memcpy(buf
, aperture_va
+ offset_in_page(off
), count
);
682 io_mapping_unmap(aperture_va
);
687 static ssize_t
intel_vgpu_rw(struct mdev_device
*mdev
, char *buf
,
688 size_t count
, loff_t
*ppos
, bool is_write
)
690 struct intel_vgpu
*vgpu
= mdev_get_drvdata(mdev
);
691 unsigned int index
= VFIO_PCI_OFFSET_TO_INDEX(*ppos
);
692 uint64_t pos
= *ppos
& VFIO_PCI_OFFSET_MASK
;
696 if (index
>= VFIO_PCI_NUM_REGIONS
+ vgpu
->vdev
.num_regions
) {
697 gvt_vgpu_err("invalid index: %u\n", index
);
702 case VFIO_PCI_CONFIG_REGION_INDEX
:
704 ret
= intel_gvt_ops
->emulate_cfg_write(vgpu
, pos
,
707 ret
= intel_gvt_ops
->emulate_cfg_read(vgpu
, pos
,
710 case VFIO_PCI_BAR0_REGION_INDEX
:
711 ret
= intel_vgpu_bar_rw(vgpu
, PCI_BASE_ADDRESS_0
, pos
,
712 buf
, count
, is_write
);
714 case VFIO_PCI_BAR2_REGION_INDEX
:
715 ret
= intel_vgpu_aperture_rw(vgpu
, pos
, buf
, count
, is_write
);
717 case VFIO_PCI_BAR1_REGION_INDEX
:
718 case VFIO_PCI_BAR3_REGION_INDEX
:
719 case VFIO_PCI_BAR4_REGION_INDEX
:
720 case VFIO_PCI_BAR5_REGION_INDEX
:
721 case VFIO_PCI_VGA_REGION_INDEX
:
722 case VFIO_PCI_ROM_REGION_INDEX
:
725 if (index
>= VFIO_PCI_NUM_REGIONS
+ vgpu
->vdev
.num_regions
)
728 index
-= VFIO_PCI_NUM_REGIONS
;
729 return vgpu
->vdev
.region
[index
].ops
->rw(vgpu
, buf
, count
,
733 return ret
== 0 ? count
: ret
;
736 static bool gtt_entry(struct mdev_device
*mdev
, loff_t
*ppos
)
738 struct intel_vgpu
*vgpu
= mdev_get_drvdata(mdev
);
739 unsigned int index
= VFIO_PCI_OFFSET_TO_INDEX(*ppos
);
740 struct intel_gvt
*gvt
= vgpu
->gvt
;
743 /* Only allow MMIO GGTT entry access */
744 if (index
!= PCI_BASE_ADDRESS_0
)
747 offset
= (u64
)(*ppos
& VFIO_PCI_OFFSET_MASK
) -
748 intel_vgpu_get_bar_gpa(vgpu
, PCI_BASE_ADDRESS_0
);
750 return (offset
>= gvt
->device_info
.gtt_start_offset
&&
751 offset
< gvt
->device_info
.gtt_start_offset
+ gvt_ggtt_sz(gvt
)) ?
755 static ssize_t
intel_vgpu_read(struct mdev_device
*mdev
, char __user
*buf
,
756 size_t count
, loff_t
*ppos
)
758 unsigned int done
= 0;
764 /* Only support GGTT entry 8 bytes read */
765 if (count
>= 8 && !(*ppos
% 8) &&
766 gtt_entry(mdev
, ppos
)) {
769 ret
= intel_vgpu_rw(mdev
, (char *)&val
, sizeof(val
),
774 if (copy_to_user(buf
, &val
, sizeof(val
)))
778 } else if (count
>= 4 && !(*ppos
% 4)) {
781 ret
= intel_vgpu_rw(mdev
, (char *)&val
, sizeof(val
),
786 if (copy_to_user(buf
, &val
, sizeof(val
)))
790 } else if (count
>= 2 && !(*ppos
% 2)) {
793 ret
= intel_vgpu_rw(mdev
, (char *)&val
, sizeof(val
),
798 if (copy_to_user(buf
, &val
, sizeof(val
)))
805 ret
= intel_vgpu_rw(mdev
, &val
, sizeof(val
), ppos
,
810 if (copy_to_user(buf
, &val
, sizeof(val
)))
828 static ssize_t
intel_vgpu_write(struct mdev_device
*mdev
,
829 const char __user
*buf
,
830 size_t count
, loff_t
*ppos
)
832 unsigned int done
= 0;
838 /* Only support GGTT entry 8 bytes write */
839 if (count
>= 8 && !(*ppos
% 8) &&
840 gtt_entry(mdev
, ppos
)) {
843 if (copy_from_user(&val
, buf
, sizeof(val
)))
846 ret
= intel_vgpu_rw(mdev
, (char *)&val
, sizeof(val
),
852 } else if (count
>= 4 && !(*ppos
% 4)) {
855 if (copy_from_user(&val
, buf
, sizeof(val
)))
858 ret
= intel_vgpu_rw(mdev
, (char *)&val
, sizeof(val
),
864 } else if (count
>= 2 && !(*ppos
% 2)) {
867 if (copy_from_user(&val
, buf
, sizeof(val
)))
870 ret
= intel_vgpu_rw(mdev
, (char *)&val
,
871 sizeof(val
), ppos
, true);
879 if (copy_from_user(&val
, buf
, sizeof(val
)))
882 ret
= intel_vgpu_rw(mdev
, &val
, sizeof(val
),
901 static int intel_vgpu_mmap(struct mdev_device
*mdev
, struct vm_area_struct
*vma
)
905 unsigned long req_size
, pgoff
= 0;
907 struct intel_vgpu
*vgpu
= mdev_get_drvdata(mdev
);
909 index
= vma
->vm_pgoff
>> (VFIO_PCI_OFFSET_SHIFT
- PAGE_SHIFT
);
910 if (index
>= VFIO_PCI_ROM_REGION_INDEX
)
913 if (vma
->vm_end
< vma
->vm_start
)
915 if ((vma
->vm_flags
& VM_SHARED
) == 0)
917 if (index
!= VFIO_PCI_BAR2_REGION_INDEX
)
920 pg_prot
= vma
->vm_page_prot
;
921 virtaddr
= vma
->vm_start
;
922 req_size
= vma
->vm_end
- vma
->vm_start
;
923 pgoff
= vgpu_aperture_pa_base(vgpu
) >> PAGE_SHIFT
;
925 return remap_pfn_range(vma
, virtaddr
, pgoff
, req_size
, pg_prot
);
928 static int intel_vgpu_get_irq_count(struct intel_vgpu
*vgpu
, int type
)
930 if (type
== VFIO_PCI_INTX_IRQ_INDEX
|| type
== VFIO_PCI_MSI_IRQ_INDEX
)
936 static int intel_vgpu_set_intx_mask(struct intel_vgpu
*vgpu
,
937 unsigned int index
, unsigned int start
,
938 unsigned int count
, uint32_t flags
,
944 static int intel_vgpu_set_intx_unmask(struct intel_vgpu
*vgpu
,
945 unsigned int index
, unsigned int start
,
946 unsigned int count
, uint32_t flags
, void *data
)
951 static int intel_vgpu_set_intx_trigger(struct intel_vgpu
*vgpu
,
952 unsigned int index
, unsigned int start
, unsigned int count
,
953 uint32_t flags
, void *data
)
958 static int intel_vgpu_set_msi_trigger(struct intel_vgpu
*vgpu
,
959 unsigned int index
, unsigned int start
, unsigned int count
,
960 uint32_t flags
, void *data
)
962 struct eventfd_ctx
*trigger
;
964 if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
965 int fd
= *(int *)data
;
967 trigger
= eventfd_ctx_fdget(fd
);
968 if (IS_ERR(trigger
)) {
969 gvt_vgpu_err("eventfd_ctx_fdget failed\n");
970 return PTR_ERR(trigger
);
972 vgpu
->vdev
.msi_trigger
= trigger
;
978 static int intel_vgpu_set_irqs(struct intel_vgpu
*vgpu
, uint32_t flags
,
979 unsigned int index
, unsigned int start
, unsigned int count
,
982 int (*func
)(struct intel_vgpu
*vgpu
, unsigned int index
,
983 unsigned int start
, unsigned int count
, uint32_t flags
,
987 case VFIO_PCI_INTX_IRQ_INDEX
:
988 switch (flags
& VFIO_IRQ_SET_ACTION_TYPE_MASK
) {
989 case VFIO_IRQ_SET_ACTION_MASK
:
990 func
= intel_vgpu_set_intx_mask
;
992 case VFIO_IRQ_SET_ACTION_UNMASK
:
993 func
= intel_vgpu_set_intx_unmask
;
995 case VFIO_IRQ_SET_ACTION_TRIGGER
:
996 func
= intel_vgpu_set_intx_trigger
;
1000 case VFIO_PCI_MSI_IRQ_INDEX
:
1001 switch (flags
& VFIO_IRQ_SET_ACTION_TYPE_MASK
) {
1002 case VFIO_IRQ_SET_ACTION_MASK
:
1003 case VFIO_IRQ_SET_ACTION_UNMASK
:
1004 /* XXX Need masking support exported */
1006 case VFIO_IRQ_SET_ACTION_TRIGGER
:
1007 func
= intel_vgpu_set_msi_trigger
;
1016 return func(vgpu
, index
, start
, count
, flags
, data
);
1019 static long intel_vgpu_ioctl(struct mdev_device
*mdev
, unsigned int cmd
,
1022 struct intel_vgpu
*vgpu
= mdev_get_drvdata(mdev
);
1023 unsigned long minsz
;
1025 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu
->id
, cmd
);
1027 if (cmd
== VFIO_DEVICE_GET_INFO
) {
1028 struct vfio_device_info info
;
1030 minsz
= offsetofend(struct vfio_device_info
, num_irqs
);
1032 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
1035 if (info
.argsz
< minsz
)
1038 info
.flags
= VFIO_DEVICE_FLAGS_PCI
;
1039 info
.flags
|= VFIO_DEVICE_FLAGS_RESET
;
1040 info
.num_regions
= VFIO_PCI_NUM_REGIONS
+
1041 vgpu
->vdev
.num_regions
;
1042 info
.num_irqs
= VFIO_PCI_NUM_IRQS
;
1044 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
1047 } else if (cmd
== VFIO_DEVICE_GET_REGION_INFO
) {
1048 struct vfio_region_info info
;
1049 struct vfio_info_cap caps
= { .buf
= NULL
, .size
= 0 };
1051 struct vfio_region_info_cap_sparse_mmap
*sparse
= NULL
;
1056 minsz
= offsetofend(struct vfio_region_info
, offset
);
1058 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
1061 if (info
.argsz
< minsz
)
1064 switch (info
.index
) {
1065 case VFIO_PCI_CONFIG_REGION_INDEX
:
1066 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
1067 info
.size
= vgpu
->gvt
->device_info
.cfg_space_size
;
1068 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
1069 VFIO_REGION_INFO_FLAG_WRITE
;
1071 case VFIO_PCI_BAR0_REGION_INDEX
:
1072 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
1073 info
.size
= vgpu
->cfg_space
.bar
[info
.index
].size
;
1079 info
.flags
= VFIO_REGION_INFO_FLAG_READ
|
1080 VFIO_REGION_INFO_FLAG_WRITE
;
1082 case VFIO_PCI_BAR1_REGION_INDEX
:
1083 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
1087 case VFIO_PCI_BAR2_REGION_INDEX
:
1088 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
1089 info
.flags
= VFIO_REGION_INFO_FLAG_CAPS
|
1090 VFIO_REGION_INFO_FLAG_MMAP
|
1091 VFIO_REGION_INFO_FLAG_READ
|
1092 VFIO_REGION_INFO_FLAG_WRITE
;
1093 info
.size
= gvt_aperture_sz(vgpu
->gvt
);
1095 size
= sizeof(*sparse
) +
1096 (nr_areas
* sizeof(*sparse
->areas
));
1097 sparse
= kzalloc(size
, GFP_KERNEL
);
1101 sparse
->header
.id
= VFIO_REGION_INFO_CAP_SPARSE_MMAP
;
1102 sparse
->header
.version
= 1;
1103 sparse
->nr_areas
= nr_areas
;
1104 cap_type_id
= VFIO_REGION_INFO_CAP_SPARSE_MMAP
;
1105 sparse
->areas
[0].offset
=
1106 PAGE_ALIGN(vgpu_aperture_offset(vgpu
));
1107 sparse
->areas
[0].size
= vgpu_aperture_sz(vgpu
);
1110 case VFIO_PCI_BAR3_REGION_INDEX
... VFIO_PCI_BAR5_REGION_INDEX
:
1111 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
1115 gvt_dbg_core("get region info bar:%d\n", info
.index
);
1118 case VFIO_PCI_ROM_REGION_INDEX
:
1119 case VFIO_PCI_VGA_REGION_INDEX
:
1120 info
.offset
= VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
1124 gvt_dbg_core("get region info index:%d\n", info
.index
);
1128 struct vfio_region_info_cap_type cap_type
= {
1129 .header
.id
= VFIO_REGION_INFO_CAP_TYPE
,
1130 .header
.version
= 1 };
1132 if (info
.index
>= VFIO_PCI_NUM_REGIONS
+
1133 vgpu
->vdev
.num_regions
)
1136 i
= info
.index
- VFIO_PCI_NUM_REGIONS
;
1139 VFIO_PCI_INDEX_TO_OFFSET(info
.index
);
1140 info
.size
= vgpu
->vdev
.region
[i
].size
;
1141 info
.flags
= vgpu
->vdev
.region
[i
].flags
;
1143 cap_type
.type
= vgpu
->vdev
.region
[i
].type
;
1144 cap_type
.subtype
= vgpu
->vdev
.region
[i
].subtype
;
1146 ret
= vfio_info_add_capability(&caps
,
1154 if ((info
.flags
& VFIO_REGION_INFO_FLAG_CAPS
) && sparse
) {
1155 switch (cap_type_id
) {
1156 case VFIO_REGION_INFO_CAP_SPARSE_MMAP
:
1157 ret
= vfio_info_add_capability(&caps
,
1158 &sparse
->header
, sizeof(*sparse
) +
1160 sizeof(*sparse
->areas
)));
1171 info
.flags
|= VFIO_REGION_INFO_FLAG_CAPS
;
1172 if (info
.argsz
< sizeof(info
) + caps
.size
) {
1173 info
.argsz
= sizeof(info
) + caps
.size
;
1174 info
.cap_offset
= 0;
1176 vfio_info_cap_shift(&caps
, sizeof(info
));
1177 if (copy_to_user((void __user
*)arg
+
1178 sizeof(info
), caps
.buf
,
1183 info
.cap_offset
= sizeof(info
);
1189 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
1191 } else if (cmd
== VFIO_DEVICE_GET_IRQ_INFO
) {
1192 struct vfio_irq_info info
;
1194 minsz
= offsetofend(struct vfio_irq_info
, count
);
1196 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
1199 if (info
.argsz
< minsz
|| info
.index
>= VFIO_PCI_NUM_IRQS
)
1202 switch (info
.index
) {
1203 case VFIO_PCI_INTX_IRQ_INDEX
:
1204 case VFIO_PCI_MSI_IRQ_INDEX
:
1210 info
.flags
= VFIO_IRQ_INFO_EVENTFD
;
1212 info
.count
= intel_vgpu_get_irq_count(vgpu
, info
.index
);
1214 if (info
.index
== VFIO_PCI_INTX_IRQ_INDEX
)
1215 info
.flags
|= (VFIO_IRQ_INFO_MASKABLE
|
1216 VFIO_IRQ_INFO_AUTOMASKED
);
1218 info
.flags
|= VFIO_IRQ_INFO_NORESIZE
;
1220 return copy_to_user((void __user
*)arg
, &info
, minsz
) ?
1222 } else if (cmd
== VFIO_DEVICE_SET_IRQS
) {
1223 struct vfio_irq_set hdr
;
1226 size_t data_size
= 0;
1228 minsz
= offsetofend(struct vfio_irq_set
, count
);
1230 if (copy_from_user(&hdr
, (void __user
*)arg
, minsz
))
1233 if (!(hdr
.flags
& VFIO_IRQ_SET_DATA_NONE
)) {
1234 int max
= intel_vgpu_get_irq_count(vgpu
, hdr
.index
);
1236 ret
= vfio_set_irqs_validate_and_prepare(&hdr
, max
,
1237 VFIO_PCI_NUM_IRQS
, &data_size
);
1239 gvt_vgpu_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
1243 data
= memdup_user((void __user
*)(arg
+ minsz
),
1246 return PTR_ERR(data
);
1250 ret
= intel_vgpu_set_irqs(vgpu
, hdr
.flags
, hdr
.index
,
1251 hdr
.start
, hdr
.count
, data
);
1255 } else if (cmd
== VFIO_DEVICE_RESET
) {
1256 intel_gvt_ops
->vgpu_reset(vgpu
);
1258 } else if (cmd
== VFIO_DEVICE_QUERY_GFX_PLANE
) {
1259 struct vfio_device_gfx_plane_info dmabuf
;
1262 minsz
= offsetofend(struct vfio_device_gfx_plane_info
,
1264 if (copy_from_user(&dmabuf
, (void __user
*)arg
, minsz
))
1266 if (dmabuf
.argsz
< minsz
)
1269 ret
= intel_gvt_ops
->vgpu_query_plane(vgpu
, &dmabuf
);
1273 return copy_to_user((void __user
*)arg
, &dmabuf
, minsz
) ?
1275 } else if (cmd
== VFIO_DEVICE_GET_GFX_DMABUF
) {
1279 if (get_user(dmabuf_id
, (__u32 __user
*)arg
))
1282 dmabuf_fd
= intel_gvt_ops
->vgpu_get_dmabuf(vgpu
, dmabuf_id
);
1291 vgpu_id_show(struct device
*dev
, struct device_attribute
*attr
,
1294 struct mdev_device
*mdev
= mdev_from_dev(dev
);
1297 struct intel_vgpu
*vgpu
= (struct intel_vgpu
*)
1298 mdev_get_drvdata(mdev
);
1299 return sprintf(buf
, "%d\n", vgpu
->id
);
1301 return sprintf(buf
, "\n");
1305 hw_id_show(struct device
*dev
, struct device_attribute
*attr
,
1308 struct mdev_device
*mdev
= mdev_from_dev(dev
);
1311 struct intel_vgpu
*vgpu
= (struct intel_vgpu
*)
1312 mdev_get_drvdata(mdev
);
1313 return sprintf(buf
, "%u\n",
1314 vgpu
->submission
.shadow_ctx
->hw_id
);
1316 return sprintf(buf
, "\n");
1319 static DEVICE_ATTR_RO(vgpu_id
);
1320 static DEVICE_ATTR_RO(hw_id
);
1322 static struct attribute
*intel_vgpu_attrs
[] = {
1323 &dev_attr_vgpu_id
.attr
,
1324 &dev_attr_hw_id
.attr
,
1328 static const struct attribute_group intel_vgpu_group
= {
1329 .name
= "intel_vgpu",
1330 .attrs
= intel_vgpu_attrs
,
1333 static const struct attribute_group
*intel_vgpu_groups
[] = {
1338 static struct mdev_parent_ops intel_vgpu_ops
= {
1339 .mdev_attr_groups
= intel_vgpu_groups
,
1340 .create
= intel_vgpu_create
,
1341 .remove
= intel_vgpu_remove
,
1343 .open
= intel_vgpu_open
,
1344 .release
= intel_vgpu_release
,
1346 .read
= intel_vgpu_read
,
1347 .write
= intel_vgpu_write
,
1348 .mmap
= intel_vgpu_mmap
,
1349 .ioctl
= intel_vgpu_ioctl
,
1352 static int kvmgt_host_init(struct device
*dev
, void *gvt
, const void *ops
)
1354 struct attribute
**kvm_type_attrs
;
1355 struct attribute_group
**kvm_vgpu_type_groups
;
1357 intel_gvt_ops
= ops
;
1358 if (!intel_gvt_ops
->get_gvt_attrs(&kvm_type_attrs
,
1359 &kvm_vgpu_type_groups
))
1361 intel_vgpu_ops
.supported_type_groups
= kvm_vgpu_type_groups
;
1363 return mdev_register_device(dev
, &intel_vgpu_ops
);
1366 static void kvmgt_host_exit(struct device
*dev
, void *gvt
)
1368 mdev_unregister_device(dev
);
1371 static int kvmgt_write_protect_add(unsigned long handle
, u64 gfn
)
1373 struct kvmgt_guest_info
*info
;
1375 struct kvm_memory_slot
*slot
;
1378 if (!handle_valid(handle
))
1381 info
= (struct kvmgt_guest_info
*)handle
;
1384 idx
= srcu_read_lock(&kvm
->srcu
);
1385 slot
= gfn_to_memslot(kvm
, gfn
);
1387 srcu_read_unlock(&kvm
->srcu
, idx
);
1391 spin_lock(&kvm
->mmu_lock
);
1393 if (kvmgt_gfn_is_write_protected(info
, gfn
))
1396 kvm_slot_page_track_add_page(kvm
, slot
, gfn
, KVM_PAGE_TRACK_WRITE
);
1397 kvmgt_protect_table_add(info
, gfn
);
1400 spin_unlock(&kvm
->mmu_lock
);
1401 srcu_read_unlock(&kvm
->srcu
, idx
);
1405 static int kvmgt_write_protect_remove(unsigned long handle
, u64 gfn
)
1407 struct kvmgt_guest_info
*info
;
1409 struct kvm_memory_slot
*slot
;
1412 if (!handle_valid(handle
))
1415 info
= (struct kvmgt_guest_info
*)handle
;
1418 idx
= srcu_read_lock(&kvm
->srcu
);
1419 slot
= gfn_to_memslot(kvm
, gfn
);
1421 srcu_read_unlock(&kvm
->srcu
, idx
);
1425 spin_lock(&kvm
->mmu_lock
);
1427 if (!kvmgt_gfn_is_write_protected(info
, gfn
))
1430 kvm_slot_page_track_remove_page(kvm
, slot
, gfn
, KVM_PAGE_TRACK_WRITE
);
1431 kvmgt_protect_table_del(info
, gfn
);
1434 spin_unlock(&kvm
->mmu_lock
);
1435 srcu_read_unlock(&kvm
->srcu
, idx
);
1439 static void kvmgt_page_track_write(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1440 const u8
*val
, int len
,
1441 struct kvm_page_track_notifier_node
*node
)
1443 struct kvmgt_guest_info
*info
= container_of(node
,
1444 struct kvmgt_guest_info
, track_node
);
1446 if (kvmgt_gfn_is_write_protected(info
, gpa_to_gfn(gpa
)))
1447 intel_gvt_ops
->write_protect_handler(info
->vgpu
, gpa
,
1451 static void kvmgt_page_track_flush_slot(struct kvm
*kvm
,
1452 struct kvm_memory_slot
*slot
,
1453 struct kvm_page_track_notifier_node
*node
)
1457 struct kvmgt_guest_info
*info
= container_of(node
,
1458 struct kvmgt_guest_info
, track_node
);
1460 spin_lock(&kvm
->mmu_lock
);
1461 for (i
= 0; i
< slot
->npages
; i
++) {
1462 gfn
= slot
->base_gfn
+ i
;
1463 if (kvmgt_gfn_is_write_protected(info
, gfn
)) {
1464 kvm_slot_page_track_remove_page(kvm
, slot
, gfn
,
1465 KVM_PAGE_TRACK_WRITE
);
1466 kvmgt_protect_table_del(info
, gfn
);
1469 spin_unlock(&kvm
->mmu_lock
);
1472 static bool __kvmgt_vgpu_exist(struct intel_vgpu
*vgpu
, struct kvm
*kvm
)
1474 struct intel_vgpu
*itr
;
1475 struct kvmgt_guest_info
*info
;
1479 mutex_lock(&vgpu
->gvt
->lock
);
1480 for_each_active_vgpu(vgpu
->gvt
, itr
, id
) {
1481 if (!handle_valid(itr
->handle
))
1484 info
= (struct kvmgt_guest_info
*)itr
->handle
;
1485 if (kvm
&& kvm
== info
->kvm
) {
1491 mutex_unlock(&vgpu
->gvt
->lock
);
1495 static int kvmgt_guest_init(struct mdev_device
*mdev
)
1497 struct kvmgt_guest_info
*info
;
1498 struct intel_vgpu
*vgpu
;
1501 vgpu
= mdev_get_drvdata(mdev
);
1502 if (handle_valid(vgpu
->handle
))
1505 kvm
= vgpu
->vdev
.kvm
;
1506 if (!kvm
|| kvm
->mm
!= current
->mm
) {
1507 gvt_vgpu_err("KVM is required to use Intel vGPU\n");
1511 if (__kvmgt_vgpu_exist(vgpu
, kvm
))
1514 info
= vzalloc(sizeof(struct kvmgt_guest_info
));
1518 vgpu
->handle
= (unsigned long)info
;
1521 kvm_get_kvm(info
->kvm
);
1523 kvmgt_protect_table_init(info
);
1524 gvt_cache_init(vgpu
);
1526 mutex_init(&vgpu
->dmabuf_lock
);
1527 init_completion(&vgpu
->vblank_done
);
1529 info
->track_node
.track_write
= kvmgt_page_track_write
;
1530 info
->track_node
.track_flush_slot
= kvmgt_page_track_flush_slot
;
1531 kvm_page_track_register_notifier(kvm
, &info
->track_node
);
1536 static bool kvmgt_guest_exit(struct kvmgt_guest_info
*info
)
1538 kvm_page_track_unregister_notifier(info
->kvm
, &info
->track_node
);
1539 kvm_put_kvm(info
->kvm
);
1540 kvmgt_protect_table_destroy(info
);
1541 gvt_cache_destroy(info
->vgpu
);
1547 static int kvmgt_attach_vgpu(void *vgpu
, unsigned long *handle
)
1549 /* nothing to do here */
1553 static void kvmgt_detach_vgpu(unsigned long handle
)
1555 /* nothing to do here */
1558 static int kvmgt_inject_msi(unsigned long handle
, u32 addr
, u16 data
)
1560 struct kvmgt_guest_info
*info
;
1561 struct intel_vgpu
*vgpu
;
1563 if (!handle_valid(handle
))
1566 info
= (struct kvmgt_guest_info
*)handle
;
1569 if (eventfd_signal(vgpu
->vdev
.msi_trigger
, 1) == 1)
1575 static unsigned long kvmgt_gfn_to_pfn(unsigned long handle
, unsigned long gfn
)
1577 unsigned long iova
, pfn
;
1578 struct kvmgt_guest_info
*info
;
1580 struct intel_vgpu
*vgpu
;
1583 if (!handle_valid(handle
))
1584 return INTEL_GVT_INVALID_ADDR
;
1586 info
= (struct kvmgt_guest_info
*)handle
;
1588 iova
= gvt_cache_find(info
->vgpu
, gfn
);
1589 if (iova
!= INTEL_GVT_INVALID_ADDR
)
1592 pfn
= INTEL_GVT_INVALID_ADDR
;
1593 dev
= mdev_dev(info
->vgpu
->vdev
.mdev
);
1594 rc
= vfio_pin_pages(dev
, &gfn
, 1, IOMMU_READ
| IOMMU_WRITE
, &pfn
);
1596 gvt_vgpu_err("vfio_pin_pages failed for gfn 0x%lx: %d\n",
1598 return INTEL_GVT_INVALID_ADDR
;
1600 /* transfer to host iova for GFX to use DMA */
1601 rc
= gvt_dma_map_iova(info
->vgpu
, pfn
, &iova
);
1603 gvt_vgpu_err("gvt_dma_map_iova failed for gfn: 0x%lx\n", gfn
);
1604 vfio_unpin_pages(dev
, &gfn
, 1);
1605 return INTEL_GVT_INVALID_ADDR
;
1608 gvt_cache_add(info
->vgpu
, gfn
, iova
);
1612 static int kvmgt_rw_gpa(unsigned long handle
, unsigned long gpa
,
1613 void *buf
, unsigned long len
, bool write
)
1615 struct kvmgt_guest_info
*info
;
1618 bool kthread
= current
->mm
== NULL
;
1620 if (!handle_valid(handle
))
1623 info
= (struct kvmgt_guest_info
*)handle
;
1629 idx
= srcu_read_lock(&kvm
->srcu
);
1630 ret
= write
? kvm_write_guest(kvm
, gpa
, buf
, len
) :
1631 kvm_read_guest(kvm
, gpa
, buf
, len
);
1632 srcu_read_unlock(&kvm
->srcu
, idx
);
1640 static int kvmgt_read_gpa(unsigned long handle
, unsigned long gpa
,
1641 void *buf
, unsigned long len
)
1643 return kvmgt_rw_gpa(handle
, gpa
, buf
, len
, false);
1646 static int kvmgt_write_gpa(unsigned long handle
, unsigned long gpa
,
1647 void *buf
, unsigned long len
)
1649 return kvmgt_rw_gpa(handle
, gpa
, buf
, len
, true);
1652 static unsigned long kvmgt_virt_to_pfn(void *addr
)
1654 return PFN_DOWN(__pa(addr
));
1657 static bool kvmgt_is_valid_gfn(unsigned long handle
, unsigned long gfn
)
1659 struct kvmgt_guest_info
*info
;
1662 if (!handle_valid(handle
))
1665 info
= (struct kvmgt_guest_info
*)handle
;
1668 return kvm_is_visible_gfn(kvm
, gfn
);
1672 struct intel_gvt_mpt kvmgt_mpt
= {
1673 .host_init
= kvmgt_host_init
,
1674 .host_exit
= kvmgt_host_exit
,
1675 .attach_vgpu
= kvmgt_attach_vgpu
,
1676 .detach_vgpu
= kvmgt_detach_vgpu
,
1677 .inject_msi
= kvmgt_inject_msi
,
1678 .from_virt_to_mfn
= kvmgt_virt_to_pfn
,
1679 .set_wp_page
= kvmgt_write_protect_add
,
1680 .unset_wp_page
= kvmgt_write_protect_remove
,
1681 .read_gpa
= kvmgt_read_gpa
,
1682 .write_gpa
= kvmgt_write_gpa
,
1683 .gfn_to_mfn
= kvmgt_gfn_to_pfn
,
1684 .set_opregion
= kvmgt_set_opregion
,
1685 .get_vfio_device
= kvmgt_get_vfio_device
,
1686 .put_vfio_device
= kvmgt_put_vfio_device
,
1687 .is_valid_gfn
= kvmgt_is_valid_gfn
,
1689 EXPORT_SYMBOL_GPL(kvmgt_mpt
);
1691 static int __init
kvmgt_init(void)
1696 static void __exit
kvmgt_exit(void)
1700 module_init(kvmgt_init
);
1701 module_exit(kvmgt_exit
);
1703 MODULE_LICENSE("GPL and additional rights");
1704 MODULE_AUTHOR("Intel Corporation");