Linux 4.19.133
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / gvt / kvmgt.c
blob66abe061f07b09397c228d76a177bea184ac2329
1 /*
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
15 * Software.
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
23 * SOFTWARE.
25 * Authors:
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>
33 #include <linux/mm.h>
34 #include <linux/mmu_context.h>
35 #include <linux/sched/mm.h>
36 #include <linux/types.h>
37 #include <linux/list.h>
38 #include <linux/rbtree.h>
39 #include <linux/spinlock.h>
40 #include <linux/eventfd.h>
41 #include <linux/uuid.h>
42 #include <linux/kvm_host.h>
43 #include <linux/vfio.h>
44 #include <linux/mdev.h>
45 #include <linux/debugfs.h>
47 #include <linux/nospec.h>
49 #include "i915_drv.h"
50 #include "gvt.h"
52 static const struct intel_gvt_ops *intel_gvt_ops;
54 /* helper macros copied from vfio-pci */
55 #define VFIO_PCI_OFFSET_SHIFT 40
56 #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
57 #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
58 #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
60 #define OPREGION_SIGNATURE "IntelGraphicsMem"
62 struct vfio_region;
63 struct intel_vgpu_regops {
64 size_t (*rw)(struct intel_vgpu *vgpu, char *buf,
65 size_t count, loff_t *ppos, bool iswrite);
66 void (*release)(struct intel_vgpu *vgpu,
67 struct vfio_region *region);
70 struct vfio_region {
71 u32 type;
72 u32 subtype;
73 size_t size;
74 u32 flags;
75 const struct intel_vgpu_regops *ops;
76 void *data;
79 struct kvmgt_pgfn {
80 gfn_t gfn;
81 struct hlist_node hnode;
84 struct kvmgt_guest_info {
85 struct kvm *kvm;
86 struct intel_vgpu *vgpu;
87 struct kvm_page_track_notifier_node track_node;
88 #define NR_BKT (1 << 18)
89 struct hlist_head ptable[NR_BKT];
90 #undef NR_BKT
91 struct dentry *debugfs_cache_entries;
94 struct gvt_dma {
95 struct intel_vgpu *vgpu;
96 struct rb_node gfn_node;
97 struct rb_node dma_addr_node;
98 gfn_t gfn;
99 dma_addr_t dma_addr;
100 unsigned long size;
101 struct kref ref;
104 static inline bool handle_valid(unsigned long handle)
106 return !!(handle & ~0xff);
109 static int kvmgt_guest_init(struct mdev_device *mdev);
110 static void intel_vgpu_release_work(struct work_struct *work);
111 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info);
113 static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
114 unsigned long size)
116 int total_pages;
117 int npage;
118 int ret;
120 total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
122 for (npage = 0; npage < total_pages; npage++) {
123 unsigned long cur_gfn = gfn + npage;
125 ret = vfio_unpin_pages(mdev_dev(vgpu->vdev.mdev), &cur_gfn, 1);
126 WARN_ON(ret != 1);
130 /* Pin a normal or compound guest page for dma. */
131 static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
132 unsigned long size, struct page **page)
134 unsigned long base_pfn = 0;
135 int total_pages;
136 int npage;
137 int ret;
139 total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
141 * We pin the pages one-by-one to avoid allocating a big arrary
142 * on stack to hold pfns.
144 for (npage = 0; npage < total_pages; npage++) {
145 unsigned long cur_gfn = gfn + npage;
146 unsigned long pfn;
148 ret = vfio_pin_pages(mdev_dev(vgpu->vdev.mdev), &cur_gfn, 1,
149 IOMMU_READ | IOMMU_WRITE, &pfn);
150 if (ret != 1) {
151 gvt_vgpu_err("vfio_pin_pages failed for gfn 0x%lx, ret %d\n",
152 cur_gfn, ret);
153 goto err;
156 if (!pfn_valid(pfn)) {
157 gvt_vgpu_err("pfn 0x%lx is not mem backed\n", pfn);
158 npage++;
159 ret = -EFAULT;
160 goto err;
163 if (npage == 0)
164 base_pfn = pfn;
165 else if (base_pfn + npage != pfn) {
166 gvt_vgpu_err("The pages are not continuous\n");
167 ret = -EINVAL;
168 npage++;
169 goto err;
173 *page = pfn_to_page(base_pfn);
174 return 0;
175 err:
176 gvt_unpin_guest_page(vgpu, gfn, npage * PAGE_SIZE);
177 return ret;
180 static int gvt_dma_map_page(struct intel_vgpu *vgpu, unsigned long gfn,
181 dma_addr_t *dma_addr, unsigned long size)
183 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
184 struct page *page = NULL;
185 int ret;
187 ret = gvt_pin_guest_page(vgpu, gfn, size, &page);
188 if (ret)
189 return ret;
191 /* Setup DMA mapping. */
192 *dma_addr = dma_map_page(dev, page, 0, size, PCI_DMA_BIDIRECTIONAL);
193 if (dma_mapping_error(dev, *dma_addr)) {
194 gvt_vgpu_err("DMA mapping failed for pfn 0x%lx, ret %d\n",
195 page_to_pfn(page), ret);
196 gvt_unpin_guest_page(vgpu, gfn, size);
197 return -ENOMEM;
200 return 0;
203 static void gvt_dma_unmap_page(struct intel_vgpu *vgpu, unsigned long gfn,
204 dma_addr_t dma_addr, unsigned long size)
206 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
208 dma_unmap_page(dev, dma_addr, size, PCI_DMA_BIDIRECTIONAL);
209 gvt_unpin_guest_page(vgpu, gfn, size);
212 static struct gvt_dma *__gvt_cache_find_dma_addr(struct intel_vgpu *vgpu,
213 dma_addr_t dma_addr)
215 struct rb_node *node = vgpu->vdev.dma_addr_cache.rb_node;
216 struct gvt_dma *itr;
218 while (node) {
219 itr = rb_entry(node, struct gvt_dma, dma_addr_node);
221 if (dma_addr < itr->dma_addr)
222 node = node->rb_left;
223 else if (dma_addr > itr->dma_addr)
224 node = node->rb_right;
225 else
226 return itr;
228 return NULL;
231 static struct gvt_dma *__gvt_cache_find_gfn(struct intel_vgpu *vgpu, gfn_t gfn)
233 struct rb_node *node = vgpu->vdev.gfn_cache.rb_node;
234 struct gvt_dma *itr;
236 while (node) {
237 itr = rb_entry(node, struct gvt_dma, gfn_node);
239 if (gfn < itr->gfn)
240 node = node->rb_left;
241 else if (gfn > itr->gfn)
242 node = node->rb_right;
243 else
244 return itr;
246 return NULL;
249 static int __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
250 dma_addr_t dma_addr, unsigned long size)
252 struct gvt_dma *new, *itr;
253 struct rb_node **link, *parent = NULL;
255 new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
256 if (!new)
257 return -ENOMEM;
259 new->vgpu = vgpu;
260 new->gfn = gfn;
261 new->dma_addr = dma_addr;
262 new->size = size;
263 kref_init(&new->ref);
265 /* gfn_cache maps gfn to struct gvt_dma. */
266 link = &vgpu->vdev.gfn_cache.rb_node;
267 while (*link) {
268 parent = *link;
269 itr = rb_entry(parent, struct gvt_dma, gfn_node);
271 if (gfn < itr->gfn)
272 link = &parent->rb_left;
273 else
274 link = &parent->rb_right;
276 rb_link_node(&new->gfn_node, parent, link);
277 rb_insert_color(&new->gfn_node, &vgpu->vdev.gfn_cache);
279 /* dma_addr_cache maps dma addr to struct gvt_dma. */
280 parent = NULL;
281 link = &vgpu->vdev.dma_addr_cache.rb_node;
282 while (*link) {
283 parent = *link;
284 itr = rb_entry(parent, struct gvt_dma, dma_addr_node);
286 if (dma_addr < itr->dma_addr)
287 link = &parent->rb_left;
288 else
289 link = &parent->rb_right;
291 rb_link_node(&new->dma_addr_node, parent, link);
292 rb_insert_color(&new->dma_addr_node, &vgpu->vdev.dma_addr_cache);
294 vgpu->vdev.nr_cache_entries++;
295 return 0;
298 static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
299 struct gvt_dma *entry)
301 rb_erase(&entry->gfn_node, &vgpu->vdev.gfn_cache);
302 rb_erase(&entry->dma_addr_node, &vgpu->vdev.dma_addr_cache);
303 kfree(entry);
304 vgpu->vdev.nr_cache_entries--;
307 static void gvt_cache_destroy(struct intel_vgpu *vgpu)
309 struct gvt_dma *dma;
310 struct rb_node *node = NULL;
312 for (;;) {
313 mutex_lock(&vgpu->vdev.cache_lock);
314 node = rb_first(&vgpu->vdev.gfn_cache);
315 if (!node) {
316 mutex_unlock(&vgpu->vdev.cache_lock);
317 break;
319 dma = rb_entry(node, struct gvt_dma, gfn_node);
320 gvt_dma_unmap_page(vgpu, dma->gfn, dma->dma_addr, dma->size);
321 __gvt_cache_remove_entry(vgpu, dma);
322 mutex_unlock(&vgpu->vdev.cache_lock);
326 static void gvt_cache_init(struct intel_vgpu *vgpu)
328 vgpu->vdev.gfn_cache = RB_ROOT;
329 vgpu->vdev.dma_addr_cache = RB_ROOT;
330 vgpu->vdev.nr_cache_entries = 0;
331 mutex_init(&vgpu->vdev.cache_lock);
334 static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
336 hash_init(info->ptable);
339 static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
341 struct kvmgt_pgfn *p;
342 struct hlist_node *tmp;
343 int i;
345 hash_for_each_safe(info->ptable, i, tmp, p, hnode) {
346 hash_del(&p->hnode);
347 kfree(p);
351 static struct kvmgt_pgfn *
352 __kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
354 struct kvmgt_pgfn *p, *res = NULL;
356 hash_for_each_possible(info->ptable, p, hnode, gfn) {
357 if (gfn == p->gfn) {
358 res = p;
359 break;
363 return res;
366 static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info,
367 gfn_t gfn)
369 struct kvmgt_pgfn *p;
371 p = __kvmgt_protect_table_find(info, gfn);
372 return !!p;
375 static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
377 struct kvmgt_pgfn *p;
379 if (kvmgt_gfn_is_write_protected(info, gfn))
380 return;
382 p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC);
383 if (WARN(!p, "gfn: 0x%llx\n", gfn))
384 return;
386 p->gfn = gfn;
387 hash_add(info->ptable, &p->hnode, gfn);
390 static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
391 gfn_t gfn)
393 struct kvmgt_pgfn *p;
395 p = __kvmgt_protect_table_find(info, gfn);
396 if (p) {
397 hash_del(&p->hnode);
398 kfree(p);
402 static size_t intel_vgpu_reg_rw_opregion(struct intel_vgpu *vgpu, char *buf,
403 size_t count, loff_t *ppos, bool iswrite)
405 unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) -
406 VFIO_PCI_NUM_REGIONS;
407 void *base = vgpu->vdev.region[i].data;
408 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
410 if (pos >= vgpu->vdev.region[i].size || iswrite) {
411 gvt_vgpu_err("invalid op or offset for Intel vgpu OpRegion\n");
412 return -EINVAL;
414 count = min(count, (size_t)(vgpu->vdev.region[i].size - pos));
415 memcpy(buf, base + pos, count);
417 return count;
420 static void intel_vgpu_reg_release_opregion(struct intel_vgpu *vgpu,
421 struct vfio_region *region)
425 static const struct intel_vgpu_regops intel_vgpu_regops_opregion = {
426 .rw = intel_vgpu_reg_rw_opregion,
427 .release = intel_vgpu_reg_release_opregion,
430 static int intel_vgpu_register_reg(struct intel_vgpu *vgpu,
431 unsigned int type, unsigned int subtype,
432 const struct intel_vgpu_regops *ops,
433 size_t size, u32 flags, void *data)
435 struct vfio_region *region;
437 region = krealloc(vgpu->vdev.region,
438 (vgpu->vdev.num_regions + 1) * sizeof(*region),
439 GFP_KERNEL);
440 if (!region)
441 return -ENOMEM;
443 vgpu->vdev.region = region;
444 vgpu->vdev.region[vgpu->vdev.num_regions].type = type;
445 vgpu->vdev.region[vgpu->vdev.num_regions].subtype = subtype;
446 vgpu->vdev.region[vgpu->vdev.num_regions].ops = ops;
447 vgpu->vdev.region[vgpu->vdev.num_regions].size = size;
448 vgpu->vdev.region[vgpu->vdev.num_regions].flags = flags;
449 vgpu->vdev.region[vgpu->vdev.num_regions].data = data;
450 vgpu->vdev.num_regions++;
451 return 0;
454 static int kvmgt_get_vfio_device(void *p_vgpu)
456 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
458 vgpu->vdev.vfio_device = vfio_device_get_from_dev(
459 mdev_dev(vgpu->vdev.mdev));
460 if (!vgpu->vdev.vfio_device) {
461 gvt_vgpu_err("failed to get vfio device\n");
462 return -ENODEV;
464 return 0;
468 static int kvmgt_set_opregion(void *p_vgpu)
470 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
471 void *base;
472 int ret;
474 /* Each vgpu has its own opregion, although VFIO would create another
475 * one later. This one is used to expose opregion to VFIO. And the
476 * other one created by VFIO later, is used by guest actually.
478 base = vgpu_opregion(vgpu)->va;
479 if (!base)
480 return -ENOMEM;
482 if (memcmp(base, OPREGION_SIGNATURE, 16)) {
483 memunmap(base);
484 return -EINVAL;
487 ret = intel_vgpu_register_reg(vgpu,
488 PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
489 VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION,
490 &intel_vgpu_regops_opregion, OPREGION_SIZE,
491 VFIO_REGION_INFO_FLAG_READ, base);
493 return ret;
496 static void kvmgt_put_vfio_device(void *vgpu)
498 if (WARN_ON(!((struct intel_vgpu *)vgpu)->vdev.vfio_device))
499 return;
501 vfio_device_put(((struct intel_vgpu *)vgpu)->vdev.vfio_device);
504 static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
506 struct intel_vgpu *vgpu = NULL;
507 struct intel_vgpu_type *type;
508 struct device *pdev;
509 void *gvt;
510 int ret;
512 pdev = mdev_parent_dev(mdev);
513 gvt = kdev_to_i915(pdev)->gvt;
515 type = intel_gvt_ops->gvt_find_vgpu_type(gvt, kobject_name(kobj));
516 if (!type) {
517 gvt_vgpu_err("failed to find type %s to create\n",
518 kobject_name(kobj));
519 ret = -EINVAL;
520 goto out;
523 vgpu = intel_gvt_ops->vgpu_create(gvt, type);
524 if (IS_ERR_OR_NULL(vgpu)) {
525 ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu);
526 gvt_err("failed to create intel vgpu: %d\n", ret);
527 goto out;
530 INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work);
532 vgpu->vdev.mdev = mdev;
533 mdev_set_drvdata(mdev, vgpu);
535 gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
536 dev_name(mdev_dev(mdev)));
537 ret = 0;
539 out:
540 return ret;
543 static int intel_vgpu_remove(struct mdev_device *mdev)
545 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
547 if (handle_valid(vgpu->handle))
548 return -EBUSY;
550 intel_gvt_ops->vgpu_destroy(vgpu);
551 return 0;
554 static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
555 unsigned long action, void *data)
557 struct intel_vgpu *vgpu = container_of(nb,
558 struct intel_vgpu,
559 vdev.iommu_notifier);
561 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
562 struct vfio_iommu_type1_dma_unmap *unmap = data;
563 struct gvt_dma *entry;
564 unsigned long iov_pfn, end_iov_pfn;
566 iov_pfn = unmap->iova >> PAGE_SHIFT;
567 end_iov_pfn = iov_pfn + unmap->size / PAGE_SIZE;
569 mutex_lock(&vgpu->vdev.cache_lock);
570 for (; iov_pfn < end_iov_pfn; iov_pfn++) {
571 entry = __gvt_cache_find_gfn(vgpu, iov_pfn);
572 if (!entry)
573 continue;
575 gvt_dma_unmap_page(vgpu, entry->gfn, entry->dma_addr,
576 entry->size);
577 __gvt_cache_remove_entry(vgpu, entry);
579 mutex_unlock(&vgpu->vdev.cache_lock);
582 return NOTIFY_OK;
585 static int intel_vgpu_group_notifier(struct notifier_block *nb,
586 unsigned long action, void *data)
588 struct intel_vgpu *vgpu = container_of(nb,
589 struct intel_vgpu,
590 vdev.group_notifier);
592 /* the only action we care about */
593 if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
594 vgpu->vdev.kvm = data;
596 if (!data)
597 schedule_work(&vgpu->vdev.release_work);
600 return NOTIFY_OK;
603 static int intel_vgpu_open(struct mdev_device *mdev)
605 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
606 unsigned long events;
607 int ret;
609 vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
610 vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier;
612 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
613 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events,
614 &vgpu->vdev.iommu_notifier);
615 if (ret != 0) {
616 gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
617 ret);
618 goto out;
621 events = VFIO_GROUP_NOTIFY_SET_KVM;
622 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events,
623 &vgpu->vdev.group_notifier);
624 if (ret != 0) {
625 gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
626 ret);
627 goto undo_iommu;
630 ret = kvmgt_guest_init(mdev);
631 if (ret)
632 goto undo_group;
634 intel_gvt_ops->vgpu_activate(vgpu);
636 atomic_set(&vgpu->vdev.released, 0);
637 return ret;
639 undo_group:
640 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
641 &vgpu->vdev.group_notifier);
643 undo_iommu:
644 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
645 &vgpu->vdev.iommu_notifier);
646 out:
647 return ret;
650 static void intel_vgpu_release_msi_eventfd_ctx(struct intel_vgpu *vgpu)
652 struct eventfd_ctx *trigger;
654 trigger = vgpu->vdev.msi_trigger;
655 if (trigger) {
656 eventfd_ctx_put(trigger);
657 vgpu->vdev.msi_trigger = NULL;
661 static void __intel_vgpu_release(struct intel_vgpu *vgpu)
663 struct kvmgt_guest_info *info;
664 int ret;
666 if (!handle_valid(vgpu->handle))
667 return;
669 if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1))
670 return;
672 intel_gvt_ops->vgpu_release(vgpu);
674 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY,
675 &vgpu->vdev.iommu_notifier);
676 WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret);
678 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY,
679 &vgpu->vdev.group_notifier);
680 WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret);
682 info = (struct kvmgt_guest_info *)vgpu->handle;
683 kvmgt_guest_exit(info);
685 intel_vgpu_release_msi_eventfd_ctx(vgpu);
687 vgpu->vdev.kvm = NULL;
688 vgpu->handle = 0;
691 static void intel_vgpu_release(struct mdev_device *mdev)
693 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
695 __intel_vgpu_release(vgpu);
698 static void intel_vgpu_release_work(struct work_struct *work)
700 struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu,
701 vdev.release_work);
703 __intel_vgpu_release(vgpu);
706 static uint64_t intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)
708 u32 start_lo, start_hi;
709 u32 mem_type;
711 start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
712 PCI_BASE_ADDRESS_MEM_MASK;
713 mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
714 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
716 switch (mem_type) {
717 case PCI_BASE_ADDRESS_MEM_TYPE_64:
718 start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space
719 + bar + 4));
720 break;
721 case PCI_BASE_ADDRESS_MEM_TYPE_32:
722 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
723 /* 1M mem BAR treated as 32-bit BAR */
724 default:
725 /* mem unknown type treated as 32-bit BAR */
726 start_hi = 0;
727 break;
730 return ((u64)start_hi << 32) | start_lo;
733 static int intel_vgpu_bar_rw(struct intel_vgpu *vgpu, int bar, uint64_t off,
734 void *buf, unsigned int count, bool is_write)
736 uint64_t bar_start = intel_vgpu_get_bar_addr(vgpu, bar);
737 int ret;
739 if (is_write)
740 ret = intel_gvt_ops->emulate_mmio_write(vgpu,
741 bar_start + off, buf, count);
742 else
743 ret = intel_gvt_ops->emulate_mmio_read(vgpu,
744 bar_start + off, buf, count);
745 return ret;
748 static inline bool intel_vgpu_in_aperture(struct intel_vgpu *vgpu, uint64_t off)
750 return off >= vgpu_aperture_offset(vgpu) &&
751 off < vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu);
754 static int intel_vgpu_aperture_rw(struct intel_vgpu *vgpu, uint64_t off,
755 void *buf, unsigned long count, bool is_write)
757 void *aperture_va;
759 if (!intel_vgpu_in_aperture(vgpu, off) ||
760 !intel_vgpu_in_aperture(vgpu, off + count)) {
761 gvt_vgpu_err("Invalid aperture offset %llu\n", off);
762 return -EINVAL;
765 aperture_va = io_mapping_map_wc(&vgpu->gvt->dev_priv->ggtt.iomap,
766 ALIGN_DOWN(off, PAGE_SIZE),
767 count + offset_in_page(off));
768 if (!aperture_va)
769 return -EIO;
771 if (is_write)
772 memcpy(aperture_va + offset_in_page(off), buf, count);
773 else
774 memcpy(buf, aperture_va + offset_in_page(off), count);
776 io_mapping_unmap(aperture_va);
778 return 0;
781 static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
782 size_t count, loff_t *ppos, bool is_write)
784 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
785 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
786 uint64_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
787 int ret = -EINVAL;
790 if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions) {
791 gvt_vgpu_err("invalid index: %u\n", index);
792 return -EINVAL;
795 switch (index) {
796 case VFIO_PCI_CONFIG_REGION_INDEX:
797 if (is_write)
798 ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos,
799 buf, count);
800 else
801 ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos,
802 buf, count);
803 break;
804 case VFIO_PCI_BAR0_REGION_INDEX:
805 ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_0, pos,
806 buf, count, is_write);
807 break;
808 case VFIO_PCI_BAR2_REGION_INDEX:
809 ret = intel_vgpu_aperture_rw(vgpu, pos, buf, count, is_write);
810 break;
811 case VFIO_PCI_BAR1_REGION_INDEX:
812 case VFIO_PCI_BAR3_REGION_INDEX:
813 case VFIO_PCI_BAR4_REGION_INDEX:
814 case VFIO_PCI_BAR5_REGION_INDEX:
815 case VFIO_PCI_VGA_REGION_INDEX:
816 case VFIO_PCI_ROM_REGION_INDEX:
817 break;
818 default:
819 if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions)
820 return -EINVAL;
822 index -= VFIO_PCI_NUM_REGIONS;
823 return vgpu->vdev.region[index].ops->rw(vgpu, buf, count,
824 ppos, is_write);
827 return ret == 0 ? count : ret;
830 static bool gtt_entry(struct mdev_device *mdev, loff_t *ppos)
832 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
833 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
834 struct intel_gvt *gvt = vgpu->gvt;
835 int offset;
837 /* Only allow MMIO GGTT entry access */
838 if (index != PCI_BASE_ADDRESS_0)
839 return false;
841 offset = (u64)(*ppos & VFIO_PCI_OFFSET_MASK) -
842 intel_vgpu_get_bar_gpa(vgpu, PCI_BASE_ADDRESS_0);
844 return (offset >= gvt->device_info.gtt_start_offset &&
845 offset < gvt->device_info.gtt_start_offset + gvt_ggtt_sz(gvt)) ?
846 true : false;
849 static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
850 size_t count, loff_t *ppos)
852 unsigned int done = 0;
853 int ret;
855 while (count) {
856 size_t filled;
858 /* Only support GGTT entry 8 bytes read */
859 if (count >= 8 && !(*ppos % 8) &&
860 gtt_entry(mdev, ppos)) {
861 u64 val;
863 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
864 ppos, false);
865 if (ret <= 0)
866 goto read_err;
868 if (copy_to_user(buf, &val, sizeof(val)))
869 goto read_err;
871 filled = 8;
872 } else if (count >= 4 && !(*ppos % 4)) {
873 u32 val;
875 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
876 ppos, false);
877 if (ret <= 0)
878 goto read_err;
880 if (copy_to_user(buf, &val, sizeof(val)))
881 goto read_err;
883 filled = 4;
884 } else if (count >= 2 && !(*ppos % 2)) {
885 u16 val;
887 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
888 ppos, false);
889 if (ret <= 0)
890 goto read_err;
892 if (copy_to_user(buf, &val, sizeof(val)))
893 goto read_err;
895 filled = 2;
896 } else {
897 u8 val;
899 ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos,
900 false);
901 if (ret <= 0)
902 goto read_err;
904 if (copy_to_user(buf, &val, sizeof(val)))
905 goto read_err;
907 filled = 1;
910 count -= filled;
911 done += filled;
912 *ppos += filled;
913 buf += filled;
916 return done;
918 read_err:
919 return -EFAULT;
922 static ssize_t intel_vgpu_write(struct mdev_device *mdev,
923 const char __user *buf,
924 size_t count, loff_t *ppos)
926 unsigned int done = 0;
927 int ret;
929 while (count) {
930 size_t filled;
932 /* Only support GGTT entry 8 bytes write */
933 if (count >= 8 && !(*ppos % 8) &&
934 gtt_entry(mdev, ppos)) {
935 u64 val;
937 if (copy_from_user(&val, buf, sizeof(val)))
938 goto write_err;
940 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
941 ppos, true);
942 if (ret <= 0)
943 goto write_err;
945 filled = 8;
946 } else if (count >= 4 && !(*ppos % 4)) {
947 u32 val;
949 if (copy_from_user(&val, buf, sizeof(val)))
950 goto write_err;
952 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
953 ppos, true);
954 if (ret <= 0)
955 goto write_err;
957 filled = 4;
958 } else if (count >= 2 && !(*ppos % 2)) {
959 u16 val;
961 if (copy_from_user(&val, buf, sizeof(val)))
962 goto write_err;
964 ret = intel_vgpu_rw(mdev, (char *)&val,
965 sizeof(val), ppos, true);
966 if (ret <= 0)
967 goto write_err;
969 filled = 2;
970 } else {
971 u8 val;
973 if (copy_from_user(&val, buf, sizeof(val)))
974 goto write_err;
976 ret = intel_vgpu_rw(mdev, &val, sizeof(val),
977 ppos, true);
978 if (ret <= 0)
979 goto write_err;
981 filled = 1;
984 count -= filled;
985 done += filled;
986 *ppos += filled;
987 buf += filled;
990 return done;
991 write_err:
992 return -EFAULT;
995 static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
997 unsigned int index;
998 u64 virtaddr;
999 unsigned long req_size, pgoff, req_start;
1000 pgprot_t pg_prot;
1001 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
1003 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1004 if (index >= VFIO_PCI_ROM_REGION_INDEX)
1005 return -EINVAL;
1007 if (vma->vm_end < vma->vm_start)
1008 return -EINVAL;
1009 if ((vma->vm_flags & VM_SHARED) == 0)
1010 return -EINVAL;
1011 if (index != VFIO_PCI_BAR2_REGION_INDEX)
1012 return -EINVAL;
1014 pg_prot = vma->vm_page_prot;
1015 virtaddr = vma->vm_start;
1016 req_size = vma->vm_end - vma->vm_start;
1017 pgoff = vma->vm_pgoff &
1018 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1019 req_start = pgoff << PAGE_SHIFT;
1021 if (!intel_vgpu_in_aperture(vgpu, req_start))
1022 return -EINVAL;
1023 if (req_start + req_size >
1024 vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu))
1025 return -EINVAL;
1027 pgoff = (gvt_aperture_pa_base(vgpu->gvt) >> PAGE_SHIFT) + pgoff;
1029 return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
1032 static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type)
1034 if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX)
1035 return 1;
1037 return 0;
1040 static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu,
1041 unsigned int index, unsigned int start,
1042 unsigned int count, uint32_t flags,
1043 void *data)
1045 return 0;
1048 static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu,
1049 unsigned int index, unsigned int start,
1050 unsigned int count, uint32_t flags, void *data)
1052 return 0;
1055 static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu,
1056 unsigned int index, unsigned int start, unsigned int count,
1057 uint32_t flags, void *data)
1059 return 0;
1062 static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu,
1063 unsigned int index, unsigned int start, unsigned int count,
1064 uint32_t flags, void *data)
1066 struct eventfd_ctx *trigger;
1068 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
1069 int fd = *(int *)data;
1071 trigger = eventfd_ctx_fdget(fd);
1072 if (IS_ERR(trigger)) {
1073 gvt_vgpu_err("eventfd_ctx_fdget failed\n");
1074 return PTR_ERR(trigger);
1076 vgpu->vdev.msi_trigger = trigger;
1077 } else if ((flags & VFIO_IRQ_SET_DATA_NONE) && !count)
1078 intel_vgpu_release_msi_eventfd_ctx(vgpu);
1080 return 0;
1083 static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, uint32_t flags,
1084 unsigned int index, unsigned int start, unsigned int count,
1085 void *data)
1087 int (*func)(struct intel_vgpu *vgpu, unsigned int index,
1088 unsigned int start, unsigned int count, uint32_t flags,
1089 void *data) = NULL;
1091 switch (index) {
1092 case VFIO_PCI_INTX_IRQ_INDEX:
1093 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1094 case VFIO_IRQ_SET_ACTION_MASK:
1095 func = intel_vgpu_set_intx_mask;
1096 break;
1097 case VFIO_IRQ_SET_ACTION_UNMASK:
1098 func = intel_vgpu_set_intx_unmask;
1099 break;
1100 case VFIO_IRQ_SET_ACTION_TRIGGER:
1101 func = intel_vgpu_set_intx_trigger;
1102 break;
1104 break;
1105 case VFIO_PCI_MSI_IRQ_INDEX:
1106 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1107 case VFIO_IRQ_SET_ACTION_MASK:
1108 case VFIO_IRQ_SET_ACTION_UNMASK:
1109 /* XXX Need masking support exported */
1110 break;
1111 case VFIO_IRQ_SET_ACTION_TRIGGER:
1112 func = intel_vgpu_set_msi_trigger;
1113 break;
1115 break;
1118 if (!func)
1119 return -ENOTTY;
1121 return func(vgpu, index, start, count, flags, data);
1124 static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
1125 unsigned long arg)
1127 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
1128 unsigned long minsz;
1130 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd);
1132 if (cmd == VFIO_DEVICE_GET_INFO) {
1133 struct vfio_device_info info;
1135 minsz = offsetofend(struct vfio_device_info, num_irqs);
1137 if (copy_from_user(&info, (void __user *)arg, minsz))
1138 return -EFAULT;
1140 if (info.argsz < minsz)
1141 return -EINVAL;
1143 info.flags = VFIO_DEVICE_FLAGS_PCI;
1144 info.flags |= VFIO_DEVICE_FLAGS_RESET;
1145 info.num_regions = VFIO_PCI_NUM_REGIONS +
1146 vgpu->vdev.num_regions;
1147 info.num_irqs = VFIO_PCI_NUM_IRQS;
1149 return copy_to_user((void __user *)arg, &info, minsz) ?
1150 -EFAULT : 0;
1152 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
1153 struct vfio_region_info info;
1154 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
1155 unsigned int i;
1156 int ret;
1157 struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
1158 size_t size;
1159 int nr_areas = 1;
1160 int cap_type_id;
1162 minsz = offsetofend(struct vfio_region_info, offset);
1164 if (copy_from_user(&info, (void __user *)arg, minsz))
1165 return -EFAULT;
1167 if (info.argsz < minsz)
1168 return -EINVAL;
1170 switch (info.index) {
1171 case VFIO_PCI_CONFIG_REGION_INDEX:
1172 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1173 info.size = vgpu->gvt->device_info.cfg_space_size;
1174 info.flags = VFIO_REGION_INFO_FLAG_READ |
1175 VFIO_REGION_INFO_FLAG_WRITE;
1176 break;
1177 case VFIO_PCI_BAR0_REGION_INDEX:
1178 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1179 info.size = vgpu->cfg_space.bar[info.index].size;
1180 if (!info.size) {
1181 info.flags = 0;
1182 break;
1185 info.flags = VFIO_REGION_INFO_FLAG_READ |
1186 VFIO_REGION_INFO_FLAG_WRITE;
1187 break;
1188 case VFIO_PCI_BAR1_REGION_INDEX:
1189 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1190 info.size = 0;
1191 info.flags = 0;
1192 break;
1193 case VFIO_PCI_BAR2_REGION_INDEX:
1194 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1195 info.flags = VFIO_REGION_INFO_FLAG_CAPS |
1196 VFIO_REGION_INFO_FLAG_MMAP |
1197 VFIO_REGION_INFO_FLAG_READ |
1198 VFIO_REGION_INFO_FLAG_WRITE;
1199 info.size = gvt_aperture_sz(vgpu->gvt);
1201 size = sizeof(*sparse) +
1202 (nr_areas * sizeof(*sparse->areas));
1203 sparse = kzalloc(size, GFP_KERNEL);
1204 if (!sparse)
1205 return -ENOMEM;
1207 sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1208 sparse->header.version = 1;
1209 sparse->nr_areas = nr_areas;
1210 cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1211 sparse->areas[0].offset =
1212 PAGE_ALIGN(vgpu_aperture_offset(vgpu));
1213 sparse->areas[0].size = vgpu_aperture_sz(vgpu);
1214 break;
1216 case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1217 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1218 info.size = 0;
1219 info.flags = 0;
1221 gvt_dbg_core("get region info bar:%d\n", info.index);
1222 break;
1224 case VFIO_PCI_ROM_REGION_INDEX:
1225 case VFIO_PCI_VGA_REGION_INDEX:
1226 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1227 info.size = 0;
1228 info.flags = 0;
1230 gvt_dbg_core("get region info index:%d\n", info.index);
1231 break;
1232 default:
1234 struct vfio_region_info_cap_type cap_type = {
1235 .header.id = VFIO_REGION_INFO_CAP_TYPE,
1236 .header.version = 1 };
1238 if (info.index >= VFIO_PCI_NUM_REGIONS +
1239 vgpu->vdev.num_regions)
1240 return -EINVAL;
1241 info.index =
1242 array_index_nospec(info.index,
1243 VFIO_PCI_NUM_REGIONS +
1244 vgpu->vdev.num_regions);
1246 i = info.index - VFIO_PCI_NUM_REGIONS;
1248 info.offset =
1249 VFIO_PCI_INDEX_TO_OFFSET(info.index);
1250 info.size = vgpu->vdev.region[i].size;
1251 info.flags = vgpu->vdev.region[i].flags;
1253 cap_type.type = vgpu->vdev.region[i].type;
1254 cap_type.subtype = vgpu->vdev.region[i].subtype;
1256 ret = vfio_info_add_capability(&caps,
1257 &cap_type.header,
1258 sizeof(cap_type));
1259 if (ret)
1260 return ret;
1264 if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) {
1265 switch (cap_type_id) {
1266 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1267 ret = vfio_info_add_capability(&caps,
1268 &sparse->header, sizeof(*sparse) +
1269 (sparse->nr_areas *
1270 sizeof(*sparse->areas)));
1271 if (ret) {
1272 kfree(sparse);
1273 return ret;
1275 break;
1276 default:
1277 kfree(sparse);
1278 return -EINVAL;
1282 if (caps.size) {
1283 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
1284 if (info.argsz < sizeof(info) + caps.size) {
1285 info.argsz = sizeof(info) + caps.size;
1286 info.cap_offset = 0;
1287 } else {
1288 vfio_info_cap_shift(&caps, sizeof(info));
1289 if (copy_to_user((void __user *)arg +
1290 sizeof(info), caps.buf,
1291 caps.size)) {
1292 kfree(caps.buf);
1293 kfree(sparse);
1294 return -EFAULT;
1296 info.cap_offset = sizeof(info);
1299 kfree(caps.buf);
1302 kfree(sparse);
1303 return copy_to_user((void __user *)arg, &info, minsz) ?
1304 -EFAULT : 0;
1305 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
1306 struct vfio_irq_info info;
1308 minsz = offsetofend(struct vfio_irq_info, count);
1310 if (copy_from_user(&info, (void __user *)arg, minsz))
1311 return -EFAULT;
1313 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
1314 return -EINVAL;
1316 switch (info.index) {
1317 case VFIO_PCI_INTX_IRQ_INDEX:
1318 case VFIO_PCI_MSI_IRQ_INDEX:
1319 break;
1320 default:
1321 return -EINVAL;
1324 info.flags = VFIO_IRQ_INFO_EVENTFD;
1326 info.count = intel_vgpu_get_irq_count(vgpu, info.index);
1328 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
1329 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
1330 VFIO_IRQ_INFO_AUTOMASKED);
1331 else
1332 info.flags |= VFIO_IRQ_INFO_NORESIZE;
1334 return copy_to_user((void __user *)arg, &info, minsz) ?
1335 -EFAULT : 0;
1336 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
1337 struct vfio_irq_set hdr;
1338 u8 *data = NULL;
1339 int ret = 0;
1340 size_t data_size = 0;
1342 minsz = offsetofend(struct vfio_irq_set, count);
1344 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1345 return -EFAULT;
1347 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
1348 int max = intel_vgpu_get_irq_count(vgpu, hdr.index);
1350 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
1351 VFIO_PCI_NUM_IRQS, &data_size);
1352 if (ret) {
1353 gvt_vgpu_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
1354 return -EINVAL;
1356 if (data_size) {
1357 data = memdup_user((void __user *)(arg + minsz),
1358 data_size);
1359 if (IS_ERR(data))
1360 return PTR_ERR(data);
1364 ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index,
1365 hdr.start, hdr.count, data);
1366 kfree(data);
1368 return ret;
1369 } else if (cmd == VFIO_DEVICE_RESET) {
1370 intel_gvt_ops->vgpu_reset(vgpu);
1371 return 0;
1372 } else if (cmd == VFIO_DEVICE_QUERY_GFX_PLANE) {
1373 struct vfio_device_gfx_plane_info dmabuf;
1374 int ret = 0;
1376 minsz = offsetofend(struct vfio_device_gfx_plane_info,
1377 dmabuf_id);
1378 if (copy_from_user(&dmabuf, (void __user *)arg, minsz))
1379 return -EFAULT;
1380 if (dmabuf.argsz < minsz)
1381 return -EINVAL;
1383 ret = intel_gvt_ops->vgpu_query_plane(vgpu, &dmabuf);
1384 if (ret != 0)
1385 return ret;
1387 return copy_to_user((void __user *)arg, &dmabuf, minsz) ?
1388 -EFAULT : 0;
1389 } else if (cmd == VFIO_DEVICE_GET_GFX_DMABUF) {
1390 __u32 dmabuf_id;
1391 __s32 dmabuf_fd;
1393 if (get_user(dmabuf_id, (__u32 __user *)arg))
1394 return -EFAULT;
1396 dmabuf_fd = intel_gvt_ops->vgpu_get_dmabuf(vgpu, dmabuf_id);
1397 return dmabuf_fd;
1401 return -ENOTTY;
1404 static ssize_t
1405 vgpu_id_show(struct device *dev, struct device_attribute *attr,
1406 char *buf)
1408 struct mdev_device *mdev = mdev_from_dev(dev);
1410 if (mdev) {
1411 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1412 mdev_get_drvdata(mdev);
1413 return sprintf(buf, "%d\n", vgpu->id);
1415 return sprintf(buf, "\n");
1418 static ssize_t
1419 hw_id_show(struct device *dev, struct device_attribute *attr,
1420 char *buf)
1422 struct mdev_device *mdev = mdev_from_dev(dev);
1424 if (mdev) {
1425 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1426 mdev_get_drvdata(mdev);
1427 return sprintf(buf, "%u\n",
1428 vgpu->submission.shadow_ctx->hw_id);
1430 return sprintf(buf, "\n");
1433 static DEVICE_ATTR_RO(vgpu_id);
1434 static DEVICE_ATTR_RO(hw_id);
1436 static struct attribute *intel_vgpu_attrs[] = {
1437 &dev_attr_vgpu_id.attr,
1438 &dev_attr_hw_id.attr,
1439 NULL
1442 static const struct attribute_group intel_vgpu_group = {
1443 .name = "intel_vgpu",
1444 .attrs = intel_vgpu_attrs,
1447 static const struct attribute_group *intel_vgpu_groups[] = {
1448 &intel_vgpu_group,
1449 NULL,
1452 static struct mdev_parent_ops intel_vgpu_ops = {
1453 .mdev_attr_groups = intel_vgpu_groups,
1454 .create = intel_vgpu_create,
1455 .remove = intel_vgpu_remove,
1457 .open = intel_vgpu_open,
1458 .release = intel_vgpu_release,
1460 .read = intel_vgpu_read,
1461 .write = intel_vgpu_write,
1462 .mmap = intel_vgpu_mmap,
1463 .ioctl = intel_vgpu_ioctl,
1466 static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)
1468 struct attribute **kvm_type_attrs;
1469 struct attribute_group **kvm_vgpu_type_groups;
1471 intel_gvt_ops = ops;
1472 if (!intel_gvt_ops->get_gvt_attrs(&kvm_type_attrs,
1473 &kvm_vgpu_type_groups))
1474 return -EFAULT;
1475 intel_vgpu_ops.supported_type_groups = kvm_vgpu_type_groups;
1477 return mdev_register_device(dev, &intel_vgpu_ops);
1480 static void kvmgt_host_exit(struct device *dev, void *gvt)
1482 mdev_unregister_device(dev);
1485 static int kvmgt_page_track_add(unsigned long handle, u64 gfn)
1487 struct kvmgt_guest_info *info;
1488 struct kvm *kvm;
1489 struct kvm_memory_slot *slot;
1490 int idx;
1492 if (!handle_valid(handle))
1493 return -ESRCH;
1495 info = (struct kvmgt_guest_info *)handle;
1496 kvm = info->kvm;
1498 idx = srcu_read_lock(&kvm->srcu);
1499 slot = gfn_to_memslot(kvm, gfn);
1500 if (!slot) {
1501 srcu_read_unlock(&kvm->srcu, idx);
1502 return -EINVAL;
1505 spin_lock(&kvm->mmu_lock);
1507 if (kvmgt_gfn_is_write_protected(info, gfn))
1508 goto out;
1510 kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1511 kvmgt_protect_table_add(info, gfn);
1513 out:
1514 spin_unlock(&kvm->mmu_lock);
1515 srcu_read_unlock(&kvm->srcu, idx);
1516 return 0;
1519 static int kvmgt_page_track_remove(unsigned long handle, u64 gfn)
1521 struct kvmgt_guest_info *info;
1522 struct kvm *kvm;
1523 struct kvm_memory_slot *slot;
1524 int idx;
1526 if (!handle_valid(handle))
1527 return 0;
1529 info = (struct kvmgt_guest_info *)handle;
1530 kvm = info->kvm;
1532 idx = srcu_read_lock(&kvm->srcu);
1533 slot = gfn_to_memslot(kvm, gfn);
1534 if (!slot) {
1535 srcu_read_unlock(&kvm->srcu, idx);
1536 return -EINVAL;
1539 spin_lock(&kvm->mmu_lock);
1541 if (!kvmgt_gfn_is_write_protected(info, gfn))
1542 goto out;
1544 kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1545 kvmgt_protect_table_del(info, gfn);
1547 out:
1548 spin_unlock(&kvm->mmu_lock);
1549 srcu_read_unlock(&kvm->srcu, idx);
1550 return 0;
1553 static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1554 const u8 *val, int len,
1555 struct kvm_page_track_notifier_node *node)
1557 struct kvmgt_guest_info *info = container_of(node,
1558 struct kvmgt_guest_info, track_node);
1560 if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
1561 intel_gvt_ops->write_protect_handler(info->vgpu, gpa,
1562 (void *)val, len);
1565 static void kvmgt_page_track_flush_slot(struct kvm *kvm,
1566 struct kvm_memory_slot *slot,
1567 struct kvm_page_track_notifier_node *node)
1569 int i;
1570 gfn_t gfn;
1571 struct kvmgt_guest_info *info = container_of(node,
1572 struct kvmgt_guest_info, track_node);
1574 spin_lock(&kvm->mmu_lock);
1575 for (i = 0; i < slot->npages; i++) {
1576 gfn = slot->base_gfn + i;
1577 if (kvmgt_gfn_is_write_protected(info, gfn)) {
1578 kvm_slot_page_track_remove_page(kvm, slot, gfn,
1579 KVM_PAGE_TRACK_WRITE);
1580 kvmgt_protect_table_del(info, gfn);
1583 spin_unlock(&kvm->mmu_lock);
1586 static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
1588 struct intel_vgpu *itr;
1589 struct kvmgt_guest_info *info;
1590 int id;
1591 bool ret = false;
1593 mutex_lock(&vgpu->gvt->lock);
1594 for_each_active_vgpu(vgpu->gvt, itr, id) {
1595 if (!handle_valid(itr->handle))
1596 continue;
1598 info = (struct kvmgt_guest_info *)itr->handle;
1599 if (kvm && kvm == info->kvm) {
1600 ret = true;
1601 goto out;
1604 out:
1605 mutex_unlock(&vgpu->gvt->lock);
1606 return ret;
1609 static int kvmgt_guest_init(struct mdev_device *mdev)
1611 struct kvmgt_guest_info *info;
1612 struct intel_vgpu *vgpu;
1613 struct kvm *kvm;
1615 vgpu = mdev_get_drvdata(mdev);
1616 if (handle_valid(vgpu->handle))
1617 return -EEXIST;
1619 kvm = vgpu->vdev.kvm;
1620 if (!kvm || kvm->mm != current->mm) {
1621 gvt_vgpu_err("KVM is required to use Intel vGPU\n");
1622 return -ESRCH;
1625 if (__kvmgt_vgpu_exist(vgpu, kvm))
1626 return -EEXIST;
1628 info = vzalloc(sizeof(struct kvmgt_guest_info));
1629 if (!info)
1630 return -ENOMEM;
1632 vgpu->handle = (unsigned long)info;
1633 info->vgpu = vgpu;
1634 info->kvm = kvm;
1635 kvm_get_kvm(info->kvm);
1637 kvmgt_protect_table_init(info);
1638 gvt_cache_init(vgpu);
1640 init_completion(&vgpu->vblank_done);
1642 info->track_node.track_write = kvmgt_page_track_write;
1643 info->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
1644 kvm_page_track_register_notifier(kvm, &info->track_node);
1646 info->debugfs_cache_entries = debugfs_create_ulong(
1647 "kvmgt_nr_cache_entries",
1648 0444, vgpu->debugfs,
1649 &vgpu->vdev.nr_cache_entries);
1650 if (!info->debugfs_cache_entries)
1651 gvt_vgpu_err("Cannot create kvmgt debugfs entry\n");
1653 return 0;
1656 static bool kvmgt_guest_exit(struct kvmgt_guest_info *info)
1658 debugfs_remove(info->debugfs_cache_entries);
1660 kvm_page_track_unregister_notifier(info->kvm, &info->track_node);
1661 kvm_put_kvm(info->kvm);
1662 kvmgt_protect_table_destroy(info);
1663 gvt_cache_destroy(info->vgpu);
1664 vfree(info);
1666 return true;
1669 static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle)
1671 /* nothing to do here */
1672 return 0;
1675 static void kvmgt_detach_vgpu(unsigned long handle)
1677 /* nothing to do here */
1680 static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data)
1682 struct kvmgt_guest_info *info;
1683 struct intel_vgpu *vgpu;
1685 if (!handle_valid(handle))
1686 return -ESRCH;
1688 info = (struct kvmgt_guest_info *)handle;
1689 vgpu = info->vgpu;
1692 * When guest is poweroff, msi_trigger is set to NULL, but vgpu's
1693 * config and mmio register isn't restored to default during guest
1694 * poweroff. If this vgpu is still used in next vm, this vgpu's pipe
1695 * may be enabled, then once this vgpu is active, it will get inject
1696 * vblank interrupt request. But msi_trigger is null until msi is
1697 * enabled by guest. so if msi_trigger is null, success is still
1698 * returned and don't inject interrupt into guest.
1700 if (vgpu->vdev.msi_trigger == NULL)
1701 return 0;
1703 if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1)
1704 return 0;
1706 return -EFAULT;
1709 static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn)
1711 struct kvmgt_guest_info *info;
1712 kvm_pfn_t pfn;
1714 if (!handle_valid(handle))
1715 return INTEL_GVT_INVALID_ADDR;
1717 info = (struct kvmgt_guest_info *)handle;
1719 pfn = gfn_to_pfn(info->kvm, gfn);
1720 if (is_error_noslot_pfn(pfn))
1721 return INTEL_GVT_INVALID_ADDR;
1723 return pfn;
1726 int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn,
1727 unsigned long size, dma_addr_t *dma_addr)
1729 struct kvmgt_guest_info *info;
1730 struct intel_vgpu *vgpu;
1731 struct gvt_dma *entry;
1732 int ret;
1734 if (!handle_valid(handle))
1735 return -EINVAL;
1737 info = (struct kvmgt_guest_info *)handle;
1738 vgpu = info->vgpu;
1740 mutex_lock(&info->vgpu->vdev.cache_lock);
1742 entry = __gvt_cache_find_gfn(info->vgpu, gfn);
1743 if (!entry) {
1744 ret = gvt_dma_map_page(vgpu, gfn, dma_addr, size);
1745 if (ret)
1746 goto err_unlock;
1748 ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr, size);
1749 if (ret)
1750 goto err_unmap;
1751 } else if (entry->size != size) {
1752 /* the same gfn with different size: unmap and re-map */
1753 gvt_dma_unmap_page(vgpu, gfn, entry->dma_addr, entry->size);
1754 __gvt_cache_remove_entry(vgpu, entry);
1756 ret = gvt_dma_map_page(vgpu, gfn, dma_addr, size);
1757 if (ret)
1758 goto err_unlock;
1760 ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr, size);
1761 if (ret)
1762 goto err_unmap;
1763 } else {
1764 kref_get(&entry->ref);
1765 *dma_addr = entry->dma_addr;
1768 mutex_unlock(&info->vgpu->vdev.cache_lock);
1769 return 0;
1771 err_unmap:
1772 gvt_dma_unmap_page(vgpu, gfn, *dma_addr, size);
1773 err_unlock:
1774 mutex_unlock(&info->vgpu->vdev.cache_lock);
1775 return ret;
1778 static void __gvt_dma_release(struct kref *ref)
1780 struct gvt_dma *entry = container_of(ref, typeof(*entry), ref);
1782 gvt_dma_unmap_page(entry->vgpu, entry->gfn, entry->dma_addr,
1783 entry->size);
1784 __gvt_cache_remove_entry(entry->vgpu, entry);
1787 void kvmgt_dma_unmap_guest_page(unsigned long handle, dma_addr_t dma_addr)
1789 struct kvmgt_guest_info *info;
1790 struct gvt_dma *entry;
1792 if (!handle_valid(handle))
1793 return;
1795 info = (struct kvmgt_guest_info *)handle;
1797 mutex_lock(&info->vgpu->vdev.cache_lock);
1798 entry = __gvt_cache_find_dma_addr(info->vgpu, dma_addr);
1799 if (entry)
1800 kref_put(&entry->ref, __gvt_dma_release);
1801 mutex_unlock(&info->vgpu->vdev.cache_lock);
1804 static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa,
1805 void *buf, unsigned long len, bool write)
1807 struct kvmgt_guest_info *info;
1808 struct kvm *kvm;
1809 int idx, ret;
1810 bool kthread = current->mm == NULL;
1812 if (!handle_valid(handle))
1813 return -ESRCH;
1815 info = (struct kvmgt_guest_info *)handle;
1816 kvm = info->kvm;
1818 if (kthread) {
1819 if (!mmget_not_zero(kvm->mm))
1820 return -EFAULT;
1821 use_mm(kvm->mm);
1824 idx = srcu_read_lock(&kvm->srcu);
1825 ret = write ? kvm_write_guest(kvm, gpa, buf, len) :
1826 kvm_read_guest(kvm, gpa, buf, len);
1827 srcu_read_unlock(&kvm->srcu, idx);
1829 if (kthread) {
1830 unuse_mm(kvm->mm);
1831 mmput(kvm->mm);
1834 return ret;
1837 static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa,
1838 void *buf, unsigned long len)
1840 return kvmgt_rw_gpa(handle, gpa, buf, len, false);
1843 static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa,
1844 void *buf, unsigned long len)
1846 return kvmgt_rw_gpa(handle, gpa, buf, len, true);
1849 static unsigned long kvmgt_virt_to_pfn(void *addr)
1851 return PFN_DOWN(__pa(addr));
1854 static bool kvmgt_is_valid_gfn(unsigned long handle, unsigned long gfn)
1856 struct kvmgt_guest_info *info;
1857 struct kvm *kvm;
1858 int idx;
1859 bool ret;
1861 if (!handle_valid(handle))
1862 return false;
1864 info = (struct kvmgt_guest_info *)handle;
1865 kvm = info->kvm;
1867 idx = srcu_read_lock(&kvm->srcu);
1868 ret = kvm_is_visible_gfn(kvm, gfn);
1869 srcu_read_unlock(&kvm->srcu, idx);
1871 return ret;
1874 struct intel_gvt_mpt kvmgt_mpt = {
1875 .host_init = kvmgt_host_init,
1876 .host_exit = kvmgt_host_exit,
1877 .attach_vgpu = kvmgt_attach_vgpu,
1878 .detach_vgpu = kvmgt_detach_vgpu,
1879 .inject_msi = kvmgt_inject_msi,
1880 .from_virt_to_mfn = kvmgt_virt_to_pfn,
1881 .enable_page_track = kvmgt_page_track_add,
1882 .disable_page_track = kvmgt_page_track_remove,
1883 .read_gpa = kvmgt_read_gpa,
1884 .write_gpa = kvmgt_write_gpa,
1885 .gfn_to_mfn = kvmgt_gfn_to_pfn,
1886 .dma_map_guest_page = kvmgt_dma_map_guest_page,
1887 .dma_unmap_guest_page = kvmgt_dma_unmap_guest_page,
1888 .set_opregion = kvmgt_set_opregion,
1889 .get_vfio_device = kvmgt_get_vfio_device,
1890 .put_vfio_device = kvmgt_put_vfio_device,
1891 .is_valid_gfn = kvmgt_is_valid_gfn,
1893 EXPORT_SYMBOL_GPL(kvmgt_mpt);
1895 static int __init kvmgt_init(void)
1897 return 0;
1900 static void __exit kvmgt_exit(void)
1904 module_init(kvmgt_init);
1905 module_exit(kvmgt_exit);
1907 MODULE_LICENSE("GPL and additional rights");
1908 MODULE_AUTHOR("Intel Corporation");