blob: a68addf95c230f2edcc9b5b21860e9aee406bc27 [file] [log] [blame]
Jike Songf30437c2016-11-09 20:30:59 +08001/*
2 * KVMGT - the implementation of Intel mediated pass-through framework for KVM
3 *
4 * Copyright(c) 2014-2016 Intel Corporation. All rights reserved.
5 *
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:
12 *
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.
16 *
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.
24 *
25 * Authors:
26 * Kevin Tian <kevin.tian@intel.com>
27 * Jike Song <jike.song@intel.com>
28 * Xiaoguang Chen <xiaoguang.chen@intel.com>
29 */
30
31#include <linux/init.h>
32#include <linux/device.h>
33#include <linux/mm.h>
Jike Songf440c8a2016-12-08 11:00:35 +080034#include <linux/mmu_context.h>
Zhenyu Wang0a1b60d2018-08-31 10:58:52 +080035#include <linux/sched/mm.h>
Jike Songf30437c2016-11-09 20:30:59 +080036#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>
Jike Song659643f2016-12-08 11:00:36 +080044#include <linux/mdev.h>
Changbin Du6846dfe2018-03-05 15:30:34 +080045#include <linux/debugfs.h>
Jike Songf30437c2016-11-09 20:30:59 +080046
Gustavo A. R. Silvade5372d2018-08-02 22:40:19 -050047#include <linux/nospec.h>
48
Jike Songf30437c2016-11-09 20:30:59 +080049#include "i915_drv.h"
50#include "gvt.h"
51
Jike Songf30437c2016-11-09 20:30:59 +080052static const struct intel_gvt_ops *intel_gvt_ops;
53
Jike Songf30437c2016-11-09 20:30:59 +080054/* 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)
59
Hang Yuan39c68e82019-01-30 18:25:54 +080060#define EDID_BLOB_OFFSET (PAGE_SIZE/2)
61
Tina Zhangb851ade2017-11-20 15:31:16 +080062#define OPREGION_SIGNATURE "IntelGraphicsMem"
63
64struct vfio_region;
65struct intel_vgpu_regops {
66 size_t (*rw)(struct intel_vgpu *vgpu, char *buf,
67 size_t count, loff_t *ppos, bool iswrite);
68 void (*release)(struct intel_vgpu *vgpu,
69 struct vfio_region *region);
70};
71
Jike Songf30437c2016-11-09 20:30:59 +080072struct vfio_region {
73 u32 type;
74 u32 subtype;
75 size_t size;
76 u32 flags;
Tina Zhangb851ade2017-11-20 15:31:16 +080077 const struct intel_vgpu_regops *ops;
78 void *data;
Jike Songf30437c2016-11-09 20:30:59 +080079};
80
Hang Yuan39c68e82019-01-30 18:25:54 +080081struct vfio_edid_region {
82 struct vfio_region_gfx_edid vfio_edid_regs;
83 void *edid_blob;
84};
85
Jike Songf30437c2016-11-09 20:30:59 +080086struct kvmgt_pgfn {
87 gfn_t gfn;
88 struct hlist_node hnode;
89};
90
91struct kvmgt_guest_info {
92 struct kvm *kvm;
93 struct intel_vgpu *vgpu;
94 struct kvm_page_track_notifier_node track_node;
95#define NR_BKT (1 << 18)
96 struct hlist_head ptable[NR_BKT];
97#undef NR_BKT
Changbin Du6846dfe2018-03-05 15:30:34 +080098 struct dentry *debugfs_cache_entries;
Jike Songf30437c2016-11-09 20:30:59 +080099};
100
101struct gvt_dma {
Changbin Ducf4ee732018-03-01 15:49:59 +0800102 struct intel_vgpu *vgpu;
103 struct rb_node gfn_node;
104 struct rb_node dma_addr_node;
Jike Songf30437c2016-11-09 20:30:59 +0800105 gfn_t gfn;
Changbin Ducf4ee732018-03-01 15:49:59 +0800106 dma_addr_t dma_addr;
Changbin Du79e542f2018-05-15 10:35:42 +0800107 unsigned long size;
Changbin Ducf4ee732018-03-01 15:49:59 +0800108 struct kref ref;
Jike Songf30437c2016-11-09 20:30:59 +0800109};
110
Jike Song659643f2016-12-08 11:00:36 +0800111static inline bool handle_valid(unsigned long handle)
112{
113 return !!(handle & ~0xff);
114}
115
116static int kvmgt_guest_init(struct mdev_device *mdev);
117static void intel_vgpu_release_work(struct work_struct *work);
118static bool kvmgt_guest_exit(struct kvmgt_guest_info *info);
119
Changbin Du79e542f2018-05-15 10:35:42 +0800120static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
121 unsigned long size)
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800122{
Changbin Du79e542f2018-05-15 10:35:42 +0800123 int total_pages;
124 int npage;
Changbin Ducf4ee732018-03-01 15:49:59 +0800125 int ret;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800126
Changbin Du79e542f2018-05-15 10:35:42 +0800127 total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
128
129 for (npage = 0; npage < total_pages; npage++) {
130 unsigned long cur_gfn = gfn + npage;
131
132 ret = vfio_unpin_pages(mdev_dev(vgpu->vdev.mdev), &cur_gfn, 1);
133 WARN_ON(ret != 1);
Changbin Ducf4ee732018-03-01 15:49:59 +0800134 }
Changbin Du79e542f2018-05-15 10:35:42 +0800135}
136
137/* Pin a normal or compound guest page for dma. */
138static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
139 unsigned long size, struct page **page)
140{
141 unsigned long base_pfn = 0;
142 int total_pages;
143 int npage;
144 int ret;
145
146 total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
147 /*
148 * We pin the pages one-by-one to avoid allocating a big arrary
149 * on stack to hold pfns.
150 */
151 for (npage = 0; npage < total_pages; npage++) {
152 unsigned long cur_gfn = gfn + npage;
153 unsigned long pfn;
154
155 ret = vfio_pin_pages(mdev_dev(vgpu->vdev.mdev), &cur_gfn, 1,
156 IOMMU_READ | IOMMU_WRITE, &pfn);
157 if (ret != 1) {
158 gvt_vgpu_err("vfio_pin_pages failed for gfn 0x%lx, ret %d\n",
159 cur_gfn, ret);
160 goto err;
161 }
162
163 if (!pfn_valid(pfn)) {
164 gvt_vgpu_err("pfn 0x%lx is not mem backed\n", pfn);
165 npage++;
166 ret = -EFAULT;
167 goto err;
168 }
169
170 if (npage == 0)
171 base_pfn = pfn;
172 else if (base_pfn + npage != pfn) {
173 gvt_vgpu_err("The pages are not continuous\n");
174 ret = -EINVAL;
175 npage++;
176 goto err;
177 }
178 }
179
180 *page = pfn_to_page(base_pfn);
181 return 0;
182err:
183 gvt_unpin_guest_page(vgpu, gfn, npage * PAGE_SIZE);
184 return ret;
185}
186
187static int gvt_dma_map_page(struct intel_vgpu *vgpu, unsigned long gfn,
188 dma_addr_t *dma_addr, unsigned long size)
189{
190 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
191 struct page *page = NULL;
192 int ret;
193
194 ret = gvt_pin_guest_page(vgpu, gfn, size, &page);
195 if (ret)
196 return ret;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800197
Changbin Ducf4ee732018-03-01 15:49:59 +0800198 /* Setup DMA mapping. */
Changbin Du79e542f2018-05-15 10:35:42 +0800199 *dma_addr = dma_map_page(dev, page, 0, size, PCI_DMA_BIDIRECTIONAL);
Dan Carpenter13bdff32018-07-19 11:19:21 +0300200 if (dma_mapping_error(dev, *dma_addr)) {
Changbin Du79e542f2018-05-15 10:35:42 +0800201 gvt_vgpu_err("DMA mapping failed for pfn 0x%lx, ret %d\n",
202 page_to_pfn(page), ret);
203 gvt_unpin_guest_page(vgpu, gfn, size);
Dan Carpenter13bdff32018-07-19 11:19:21 +0300204 return -ENOMEM;
Changbin Ducf4ee732018-03-01 15:49:59 +0800205 }
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800206
Dan Carpenter13bdff32018-07-19 11:19:21 +0300207 return 0;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800208}
209
Changbin Ducf4ee732018-03-01 15:49:59 +0800210static void gvt_dma_unmap_page(struct intel_vgpu *vgpu, unsigned long gfn,
Changbin Du79e542f2018-05-15 10:35:42 +0800211 dma_addr_t dma_addr, unsigned long size)
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800212{
213 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800214
Changbin Du79e542f2018-05-15 10:35:42 +0800215 dma_unmap_page(dev, dma_addr, size, PCI_DMA_BIDIRECTIONAL);
216 gvt_unpin_guest_page(vgpu, gfn, size);
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800217}
218
Changbin Ducf4ee732018-03-01 15:49:59 +0800219static struct gvt_dma *__gvt_cache_find_dma_addr(struct intel_vgpu *vgpu,
220 dma_addr_t dma_addr)
Jike Songf30437c2016-11-09 20:30:59 +0800221{
Changbin Ducf4ee732018-03-01 15:49:59 +0800222 struct rb_node *node = vgpu->vdev.dma_addr_cache.rb_node;
223 struct gvt_dma *itr;
Jike Songf30437c2016-11-09 20:30:59 +0800224
225 while (node) {
Changbin Ducf4ee732018-03-01 15:49:59 +0800226 itr = rb_entry(node, struct gvt_dma, dma_addr_node);
227
228 if (dma_addr < itr->dma_addr)
229 node = node->rb_left;
230 else if (dma_addr > itr->dma_addr)
231 node = node->rb_right;
232 else
233 return itr;
234 }
235 return NULL;
236}
237
238static struct gvt_dma *__gvt_cache_find_gfn(struct intel_vgpu *vgpu, gfn_t gfn)
239{
240 struct rb_node *node = vgpu->vdev.gfn_cache.rb_node;
241 struct gvt_dma *itr;
242
243 while (node) {
244 itr = rb_entry(node, struct gvt_dma, gfn_node);
Jike Songf30437c2016-11-09 20:30:59 +0800245
246 if (gfn < itr->gfn)
247 node = node->rb_left;
248 else if (gfn > itr->gfn)
249 node = node->rb_right;
Changbin Ducf4ee732018-03-01 15:49:59 +0800250 else
251 return itr;
Jike Songf30437c2016-11-09 20:30:59 +0800252 }
Changbin Ducf4ee732018-03-01 15:49:59 +0800253 return NULL;
Jike Songf30437c2016-11-09 20:30:59 +0800254}
255
Changbin Du5cd42232018-03-12 15:12:34 +0800256static int __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
Changbin Du79e542f2018-05-15 10:35:42 +0800257 dma_addr_t dma_addr, unsigned long size)
Jike Songf30437c2016-11-09 20:30:59 +0800258{
259 struct gvt_dma *new, *itr;
Changbin Ducf4ee732018-03-01 15:49:59 +0800260 struct rb_node **link, *parent = NULL;
Jike Songf30437c2016-11-09 20:30:59 +0800261
262 new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
263 if (!new)
Changbin Du5cd42232018-03-12 15:12:34 +0800264 return -ENOMEM;
Jike Songf30437c2016-11-09 20:30:59 +0800265
Changbin Ducf4ee732018-03-01 15:49:59 +0800266 new->vgpu = vgpu;
Jike Songf30437c2016-11-09 20:30:59 +0800267 new->gfn = gfn;
Changbin Ducf4ee732018-03-01 15:49:59 +0800268 new->dma_addr = dma_addr;
Changbin Du79e542f2018-05-15 10:35:42 +0800269 new->size = size;
Changbin Ducf4ee732018-03-01 15:49:59 +0800270 kref_init(&new->ref);
Jike Songf30437c2016-11-09 20:30:59 +0800271
Changbin Ducf4ee732018-03-01 15:49:59 +0800272 /* gfn_cache maps gfn to struct gvt_dma. */
273 link = &vgpu->vdev.gfn_cache.rb_node;
Jike Songf30437c2016-11-09 20:30:59 +0800274 while (*link) {
275 parent = *link;
Changbin Ducf4ee732018-03-01 15:49:59 +0800276 itr = rb_entry(parent, struct gvt_dma, gfn_node);
Jike Songf30437c2016-11-09 20:30:59 +0800277
Changbin Ducf4ee732018-03-01 15:49:59 +0800278 if (gfn < itr->gfn)
Jike Songf30437c2016-11-09 20:30:59 +0800279 link = &parent->rb_left;
280 else
281 link = &parent->rb_right;
282 }
Changbin Ducf4ee732018-03-01 15:49:59 +0800283 rb_link_node(&new->gfn_node, parent, link);
284 rb_insert_color(&new->gfn_node, &vgpu->vdev.gfn_cache);
Jike Songf30437c2016-11-09 20:30:59 +0800285
Changbin Ducf4ee732018-03-01 15:49:59 +0800286 /* dma_addr_cache maps dma addr to struct gvt_dma. */
287 parent = NULL;
288 link = &vgpu->vdev.dma_addr_cache.rb_node;
289 while (*link) {
290 parent = *link;
291 itr = rb_entry(parent, struct gvt_dma, dma_addr_node);
Jike Songf30437c2016-11-09 20:30:59 +0800292
Changbin Ducf4ee732018-03-01 15:49:59 +0800293 if (dma_addr < itr->dma_addr)
294 link = &parent->rb_left;
295 else
296 link = &parent->rb_right;
297 }
298 rb_link_node(&new->dma_addr_node, parent, link);
299 rb_insert_color(&new->dma_addr_node, &vgpu->vdev.dma_addr_cache);
Changbin Du6846dfe2018-03-05 15:30:34 +0800300
301 vgpu->vdev.nr_cache_entries++;
Changbin Du5cd42232018-03-12 15:12:34 +0800302 return 0;
Jike Songf30437c2016-11-09 20:30:59 +0800303}
304
305static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
306 struct gvt_dma *entry)
307{
Changbin Ducf4ee732018-03-01 15:49:59 +0800308 rb_erase(&entry->gfn_node, &vgpu->vdev.gfn_cache);
309 rb_erase(&entry->dma_addr_node, &vgpu->vdev.dma_addr_cache);
Jike Songf30437c2016-11-09 20:30:59 +0800310 kfree(entry);
Changbin Du6846dfe2018-03-05 15:30:34 +0800311 vgpu->vdev.nr_cache_entries--;
Jike Songf30437c2016-11-09 20:30:59 +0800312}
313
Jike Songf30437c2016-11-09 20:30:59 +0800314static void gvt_cache_destroy(struct intel_vgpu *vgpu)
315{
316 struct gvt_dma *dma;
317 struct rb_node *node = NULL;
Jike Songf30437c2016-11-09 20:30:59 +0800318
Chuanxiao Dongf16bd3d2017-06-26 15:20:50 +0800319 for (;;) {
320 mutex_lock(&vgpu->vdev.cache_lock);
Changbin Ducf4ee732018-03-01 15:49:59 +0800321 node = rb_first(&vgpu->vdev.gfn_cache);
Chuanxiao Dongf16bd3d2017-06-26 15:20:50 +0800322 if (!node) {
323 mutex_unlock(&vgpu->vdev.cache_lock);
324 break;
325 }
Changbin Ducf4ee732018-03-01 15:49:59 +0800326 dma = rb_entry(node, struct gvt_dma, gfn_node);
Changbin Du79e542f2018-05-15 10:35:42 +0800327 gvt_dma_unmap_page(vgpu, dma->gfn, dma->dma_addr, dma->size);
Jike Songf30437c2016-11-09 20:30:59 +0800328 __gvt_cache_remove_entry(vgpu, dma);
Chuanxiao Dongf16bd3d2017-06-26 15:20:50 +0800329 mutex_unlock(&vgpu->vdev.cache_lock);
Jike Songf30437c2016-11-09 20:30:59 +0800330 }
Jike Songf30437c2016-11-09 20:30:59 +0800331}
332
Changbin Ducf4ee732018-03-01 15:49:59 +0800333static void gvt_cache_init(struct intel_vgpu *vgpu)
334{
335 vgpu->vdev.gfn_cache = RB_ROOT;
336 vgpu->vdev.dma_addr_cache = RB_ROOT;
Changbin Du6846dfe2018-03-05 15:30:34 +0800337 vgpu->vdev.nr_cache_entries = 0;
Changbin Ducf4ee732018-03-01 15:49:59 +0800338 mutex_init(&vgpu->vdev.cache_lock);
339}
340
Jike Songf30437c2016-11-09 20:30:59 +0800341static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
342{
343 hash_init(info->ptable);
344}
345
346static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
347{
348 struct kvmgt_pgfn *p;
349 struct hlist_node *tmp;
350 int i;
351
352 hash_for_each_safe(info->ptable, i, tmp, p, hnode) {
353 hash_del(&p->hnode);
354 kfree(p);
355 }
356}
357
358static struct kvmgt_pgfn *
359__kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
360{
361 struct kvmgt_pgfn *p, *res = NULL;
362
363 hash_for_each_possible(info->ptable, p, hnode, gfn) {
364 if (gfn == p->gfn) {
365 res = p;
366 break;
367 }
368 }
369
370 return res;
371}
372
373static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info,
374 gfn_t gfn)
375{
376 struct kvmgt_pgfn *p;
377
378 p = __kvmgt_protect_table_find(info, gfn);
379 return !!p;
380}
381
382static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
383{
384 struct kvmgt_pgfn *p;
385
386 if (kvmgt_gfn_is_write_protected(info, gfn))
387 return;
388
Jike Songc55b1de2016-12-08 11:00:34 +0800389 p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC);
Jike Songf30437c2016-11-09 20:30:59 +0800390 if (WARN(!p, "gfn: 0x%llx\n", gfn))
391 return;
392
393 p->gfn = gfn;
394 hash_add(info->ptable, &p->hnode, gfn);
395}
396
397static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
398 gfn_t gfn)
399{
400 struct kvmgt_pgfn *p;
401
402 p = __kvmgt_protect_table_find(info, gfn);
403 if (p) {
404 hash_del(&p->hnode);
405 kfree(p);
406 }
407}
408
Tina Zhangb851ade2017-11-20 15:31:16 +0800409static size_t intel_vgpu_reg_rw_opregion(struct intel_vgpu *vgpu, char *buf,
410 size_t count, loff_t *ppos, bool iswrite)
411{
412 unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) -
413 VFIO_PCI_NUM_REGIONS;
414 void *base = vgpu->vdev.region[i].data;
415 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
416
417 if (pos >= vgpu->vdev.region[i].size || iswrite) {
418 gvt_vgpu_err("invalid op or offset for Intel vgpu OpRegion\n");
419 return -EINVAL;
420 }
421 count = min(count, (size_t)(vgpu->vdev.region[i].size - pos));
422 memcpy(buf, base + pos, count);
423
424 return count;
425}
426
427static void intel_vgpu_reg_release_opregion(struct intel_vgpu *vgpu,
428 struct vfio_region *region)
429{
430}
431
432static const struct intel_vgpu_regops intel_vgpu_regops_opregion = {
433 .rw = intel_vgpu_reg_rw_opregion,
434 .release = intel_vgpu_reg_release_opregion,
435};
436
Hang Yuan39c68e82019-01-30 18:25:54 +0800437static int handle_edid_regs(struct intel_vgpu *vgpu,
438 struct vfio_edid_region *region, char *buf,
439 size_t count, u16 offset, bool is_write)
440{
441 struct vfio_region_gfx_edid *regs = &region->vfio_edid_regs;
442 unsigned int data;
443
444 if (offset + count > sizeof(*regs))
445 return -EINVAL;
446
447 if (count != 4)
448 return -EINVAL;
449
450 if (is_write) {
451 data = *((unsigned int *)buf);
452 switch (offset) {
453 case offsetof(struct vfio_region_gfx_edid, link_state):
454 if (data == VFIO_DEVICE_GFX_LINK_STATE_UP) {
455 if (!drm_edid_block_valid(
456 (u8 *)region->edid_blob,
457 0,
458 true,
459 NULL)) {
460 gvt_vgpu_err("invalid EDID blob\n");
461 return -EINVAL;
462 }
463 intel_gvt_ops->emulate_hotplug(vgpu, true);
464 } else if (data == VFIO_DEVICE_GFX_LINK_STATE_DOWN)
465 intel_gvt_ops->emulate_hotplug(vgpu, false);
466 else {
467 gvt_vgpu_err("invalid EDID link state %d\n",
468 regs->link_state);
469 return -EINVAL;
470 }
471 regs->link_state = data;
472 break;
473 case offsetof(struct vfio_region_gfx_edid, edid_size):
474 if (data > regs->edid_max_size) {
475 gvt_vgpu_err("EDID size is bigger than %d!\n",
476 regs->edid_max_size);
477 return -EINVAL;
478 }
479 regs->edid_size = data;
480 break;
481 default:
482 /* read-only regs */
483 gvt_vgpu_err("write read-only EDID region at offset %d\n",
484 offset);
485 return -EPERM;
486 }
487 } else {
488 memcpy(buf, (char *)regs + offset, count);
489 }
490
491 return count;
492}
493
494static int handle_edid_blob(struct vfio_edid_region *region, char *buf,
495 size_t count, u16 offset, bool is_write)
496{
497 if (offset + count > region->vfio_edid_regs.edid_size)
498 return -EINVAL;
499
500 if (is_write)
501 memcpy(region->edid_blob + offset, buf, count);
502 else
503 memcpy(buf, region->edid_blob + offset, count);
504
505 return count;
506}
507
508static size_t intel_vgpu_reg_rw_edid(struct intel_vgpu *vgpu, char *buf,
509 size_t count, loff_t *ppos, bool iswrite)
510{
511 int ret;
512 unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) -
513 VFIO_PCI_NUM_REGIONS;
514 struct vfio_edid_region *region =
515 (struct vfio_edid_region *)vgpu->vdev.region[i].data;
516 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
517
518 if (pos < region->vfio_edid_regs.edid_offset) {
519 ret = handle_edid_regs(vgpu, region, buf, count, pos, iswrite);
520 } else {
521 pos -= EDID_BLOB_OFFSET;
522 ret = handle_edid_blob(region, buf, count, pos, iswrite);
523 }
524
525 if (ret < 0)
526 gvt_vgpu_err("failed to access EDID region\n");
527
528 return ret;
529}
530
531static void intel_vgpu_reg_release_edid(struct intel_vgpu *vgpu,
532 struct vfio_region *region)
533{
534 kfree(region->data);
535}
536
537static const struct intel_vgpu_regops intel_vgpu_regops_edid = {
538 .rw = intel_vgpu_reg_rw_edid,
539 .release = intel_vgpu_reg_release_edid,
540};
541
Tina Zhangb851ade2017-11-20 15:31:16 +0800542static int intel_vgpu_register_reg(struct intel_vgpu *vgpu,
543 unsigned int type, unsigned int subtype,
544 const struct intel_vgpu_regops *ops,
545 size_t size, u32 flags, void *data)
546{
547 struct vfio_region *region;
548
549 region = krealloc(vgpu->vdev.region,
550 (vgpu->vdev.num_regions + 1) * sizeof(*region),
551 GFP_KERNEL);
552 if (!region)
553 return -ENOMEM;
554
555 vgpu->vdev.region = region;
556 vgpu->vdev.region[vgpu->vdev.num_regions].type = type;
557 vgpu->vdev.region[vgpu->vdev.num_regions].subtype = subtype;
558 vgpu->vdev.region[vgpu->vdev.num_regions].ops = ops;
559 vgpu->vdev.region[vgpu->vdev.num_regions].size = size;
560 vgpu->vdev.region[vgpu->vdev.num_regions].flags = flags;
561 vgpu->vdev.region[vgpu->vdev.num_regions].data = data;
562 vgpu->vdev.num_regions++;
Tina Zhangb851ade2017-11-20 15:31:16 +0800563 return 0;
564}
565
Tina Zhange546e282017-11-23 16:26:36 +0800566static int kvmgt_get_vfio_device(void *p_vgpu)
567{
568 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
569
570 vgpu->vdev.vfio_device = vfio_device_get_from_dev(
571 mdev_dev(vgpu->vdev.mdev));
572 if (!vgpu->vdev.vfio_device) {
573 gvt_vgpu_err("failed to get vfio device\n");
574 return -ENODEV;
575 }
576 return 0;
577}
578
579
Tina Zhangb851ade2017-11-20 15:31:16 +0800580static int kvmgt_set_opregion(void *p_vgpu)
581{
582 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
583 void *base;
584 int ret;
585
586 /* Each vgpu has its own opregion, although VFIO would create another
587 * one later. This one is used to expose opregion to VFIO. And the
588 * other one created by VFIO later, is used by guest actually.
589 */
590 base = vgpu_opregion(vgpu)->va;
591 if (!base)
592 return -ENOMEM;
593
594 if (memcmp(base, OPREGION_SIGNATURE, 16)) {
595 memunmap(base);
596 return -EINVAL;
597 }
598
599 ret = intel_vgpu_register_reg(vgpu,
600 PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
601 VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION,
602 &intel_vgpu_regops_opregion, OPREGION_SIZE,
603 VFIO_REGION_INFO_FLAG_READ, base);
604
605 return ret;
606}
607
Hang Yuan39c68e82019-01-30 18:25:54 +0800608static int kvmgt_set_edid(void *p_vgpu, int port_num)
609{
610 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
611 struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num);
612 struct vfio_edid_region *base;
613 int ret;
614
615 base = kzalloc(sizeof(*base), GFP_KERNEL);
616 if (!base)
617 return -ENOMEM;
618
619 /* TODO: Add multi-port and EDID extension block support */
620 base->vfio_edid_regs.edid_offset = EDID_BLOB_OFFSET;
621 base->vfio_edid_regs.edid_max_size = EDID_SIZE;
622 base->vfio_edid_regs.edid_size = EDID_SIZE;
623 base->vfio_edid_regs.max_xres = vgpu_edid_xres(port->id);
624 base->vfio_edid_regs.max_yres = vgpu_edid_yres(port->id);
625 base->edid_blob = port->edid->edid_block;
626
627 ret = intel_vgpu_register_reg(vgpu,
628 VFIO_REGION_TYPE_GFX,
629 VFIO_REGION_SUBTYPE_GFX_EDID,
630 &intel_vgpu_regops_edid, EDID_SIZE,
631 VFIO_REGION_INFO_FLAG_READ |
632 VFIO_REGION_INFO_FLAG_WRITE |
633 VFIO_REGION_INFO_FLAG_CAPS, base);
634
635 return ret;
636}
637
Tina Zhange546e282017-11-23 16:26:36 +0800638static void kvmgt_put_vfio_device(void *vgpu)
639{
640 if (WARN_ON(!((struct intel_vgpu *)vgpu)->vdev.vfio_device))
641 return;
642
643 vfio_device_put(((struct intel_vgpu *)vgpu)->vdev.vfio_device);
644}
645
Jike Song659643f2016-12-08 11:00:36 +0800646static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
647{
Tina Zhang695fbc02017-03-10 04:26:53 -0500648 struct intel_vgpu *vgpu = NULL;
Jike Song659643f2016-12-08 11:00:36 +0800649 struct intel_vgpu_type *type;
650 struct device *pdev;
651 void *gvt;
Jike Song57533942017-01-06 15:16:20 +0800652 int ret;
Jike Song659643f2016-12-08 11:00:36 +0800653
Alex Williamson9372e6fe2016-12-30 08:13:41 -0700654 pdev = mdev_parent_dev(mdev);
Jike Song659643f2016-12-08 11:00:36 +0800655 gvt = kdev_to_i915(pdev)->gvt;
656
fred gao6aa23ce2017-09-28 11:03:03 +0800657 type = intel_gvt_ops->gvt_find_vgpu_type(gvt, kobject_name(kobj));
Jike Song659643f2016-12-08 11:00:36 +0800658 if (!type) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500659 gvt_vgpu_err("failed to find type %s to create\n",
Jike Song659643f2016-12-08 11:00:36 +0800660 kobject_name(kobj));
Jike Song57533942017-01-06 15:16:20 +0800661 ret = -EINVAL;
662 goto out;
Jike Song659643f2016-12-08 11:00:36 +0800663 }
664
665 vgpu = intel_gvt_ops->vgpu_create(gvt, type);
666 if (IS_ERR_OR_NULL(vgpu)) {
Jike Song57533942017-01-06 15:16:20 +0800667 ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu);
Zhenyu Wang64c066a2018-02-22 15:16:14 +0800668 gvt_err("failed to create intel vgpu: %d\n", ret);
Jike Song57533942017-01-06 15:16:20 +0800669 goto out;
Jike Song659643f2016-12-08 11:00:36 +0800670 }
671
672 INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work);
673
674 vgpu->vdev.mdev = mdev;
675 mdev_set_drvdata(mdev, vgpu);
676
677 gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
Alex Williamson99e31232016-12-30 08:13:44 -0700678 dev_name(mdev_dev(mdev)));
Jike Song57533942017-01-06 15:16:20 +0800679 ret = 0;
680
681out:
682 return ret;
Jike Song659643f2016-12-08 11:00:36 +0800683}
684
685static int intel_vgpu_remove(struct mdev_device *mdev)
686{
687 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
688
689 if (handle_valid(vgpu->handle))
690 return -EBUSY;
691
692 intel_gvt_ops->vgpu_destroy(vgpu);
693 return 0;
694}
695
696static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
697 unsigned long action, void *data)
698{
699 struct intel_vgpu *vgpu = container_of(nb,
700 struct intel_vgpu,
701 vdev.iommu_notifier);
702
703 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
704 struct vfio_iommu_type1_dma_unmap *unmap = data;
Changbin Ducf4ee732018-03-01 15:49:59 +0800705 struct gvt_dma *entry;
706 unsigned long iov_pfn, end_iov_pfn;
Jike Song659643f2016-12-08 11:00:36 +0800707
Changbin Ducf4ee732018-03-01 15:49:59 +0800708 iov_pfn = unmap->iova >> PAGE_SHIFT;
709 end_iov_pfn = iov_pfn + unmap->size / PAGE_SIZE;
Jike Song659643f2016-12-08 11:00:36 +0800710
Changbin Ducf4ee732018-03-01 15:49:59 +0800711 mutex_lock(&vgpu->vdev.cache_lock);
712 for (; iov_pfn < end_iov_pfn; iov_pfn++) {
713 entry = __gvt_cache_find_gfn(vgpu, iov_pfn);
714 if (!entry)
715 continue;
716
Changbin Du79e542f2018-05-15 10:35:42 +0800717 gvt_dma_unmap_page(vgpu, entry->gfn, entry->dma_addr,
718 entry->size);
Changbin Ducf4ee732018-03-01 15:49:59 +0800719 __gvt_cache_remove_entry(vgpu, entry);
720 }
721 mutex_unlock(&vgpu->vdev.cache_lock);
Jike Song659643f2016-12-08 11:00:36 +0800722 }
723
724 return NOTIFY_OK;
725}
726
727static int intel_vgpu_group_notifier(struct notifier_block *nb,
728 unsigned long action, void *data)
729{
730 struct intel_vgpu *vgpu = container_of(nb,
731 struct intel_vgpu,
732 vdev.group_notifier);
733
734 /* the only action we care about */
735 if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
736 vgpu->vdev.kvm = data;
737
738 if (!data)
739 schedule_work(&vgpu->vdev.release_work);
740 }
741
742 return NOTIFY_OK;
743}
744
745static int intel_vgpu_open(struct mdev_device *mdev)
746{
747 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
748 unsigned long events;
749 int ret;
750
751 vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
752 vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier;
753
754 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
Alex Williamson99e31232016-12-30 08:13:44 -0700755 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events,
Jike Song659643f2016-12-08 11:00:36 +0800756 &vgpu->vdev.iommu_notifier);
757 if (ret != 0) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500758 gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
759 ret);
Jike Song659643f2016-12-08 11:00:36 +0800760 goto out;
761 }
762
763 events = VFIO_GROUP_NOTIFY_SET_KVM;
Alex Williamson99e31232016-12-30 08:13:44 -0700764 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events,
Jike Song659643f2016-12-08 11:00:36 +0800765 &vgpu->vdev.group_notifier);
766 if (ret != 0) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500767 gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
768 ret);
Jike Song659643f2016-12-08 11:00:36 +0800769 goto undo_iommu;
770 }
771
Zhenyu Wang9bdb0732018-12-07 16:16:53 +0800772 /* Take a module reference as mdev core doesn't take
773 * a reference for vendor driver.
774 */
775 if (!try_module_get(THIS_MODULE))
776 goto undo_group;
777
Jike Song364fb6b2016-12-16 10:51:06 +0800778 ret = kvmgt_guest_init(mdev);
779 if (ret)
780 goto undo_group;
781
Zhi Wangb79c52a2017-03-30 01:48:39 +0800782 intel_gvt_ops->vgpu_activate(vgpu);
783
Jike Song364fb6b2016-12-16 10:51:06 +0800784 atomic_set(&vgpu->vdev.released, 0);
785 return ret;
786
787undo_group:
Linus Torvalds5824f922017-01-06 11:19:03 -0800788 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
Jike Song364fb6b2016-12-16 10:51:06 +0800789 &vgpu->vdev.group_notifier);
Jike Song659643f2016-12-08 11:00:36 +0800790
791undo_iommu:
Alex Williamson99e31232016-12-30 08:13:44 -0700792 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Jike Song659643f2016-12-08 11:00:36 +0800793 &vgpu->vdev.iommu_notifier);
794out:
795 return ret;
796}
797
Xiong Zhangd54e7932018-04-13 10:26:16 +0800798static void intel_vgpu_release_msi_eventfd_ctx(struct intel_vgpu *vgpu)
799{
800 struct eventfd_ctx *trigger;
801
802 trigger = vgpu->vdev.msi_trigger;
803 if (trigger) {
804 eventfd_ctx_put(trigger);
805 vgpu->vdev.msi_trigger = NULL;
806 }
807}
808
Jike Song659643f2016-12-08 11:00:36 +0800809static void __intel_vgpu_release(struct intel_vgpu *vgpu)
810{
811 struct kvmgt_guest_info *info;
Jike Song364fb6b2016-12-16 10:51:06 +0800812 int ret;
Jike Song659643f2016-12-08 11:00:36 +0800813
814 if (!handle_valid(vgpu->handle))
815 return;
816
Jike Song364fb6b2016-12-16 10:51:06 +0800817 if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1))
818 return;
819
Hang Yuanf9090d42018-08-07 18:29:21 +0800820 intel_gvt_ops->vgpu_release(vgpu);
Zhi Wangb79c52a2017-03-30 01:48:39 +0800821
Linus Torvalds5824f922017-01-06 11:19:03 -0800822 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY,
Jike Song659643f2016-12-08 11:00:36 +0800823 &vgpu->vdev.iommu_notifier);
Jike Song364fb6b2016-12-16 10:51:06 +0800824 WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret);
825
Linus Torvalds5824f922017-01-06 11:19:03 -0800826 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY,
Jike Song659643f2016-12-08 11:00:36 +0800827 &vgpu->vdev.group_notifier);
Jike Song364fb6b2016-12-16 10:51:06 +0800828 WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret);
Jike Song659643f2016-12-08 11:00:36 +0800829
Zhenyu Wang9bdb0732018-12-07 16:16:53 +0800830 /* dereference module reference taken at open */
831 module_put(THIS_MODULE);
832
Jike Song659643f2016-12-08 11:00:36 +0800833 info = (struct kvmgt_guest_info *)vgpu->handle;
834 kvmgt_guest_exit(info);
Jike Song364fb6b2016-12-16 10:51:06 +0800835
Xiong Zhangd54e7932018-04-13 10:26:16 +0800836 intel_vgpu_release_msi_eventfd_ctx(vgpu);
837
Jike Song364fb6b2016-12-16 10:51:06 +0800838 vgpu->vdev.kvm = NULL;
Jike Song659643f2016-12-08 11:00:36 +0800839 vgpu->handle = 0;
840}
841
842static void intel_vgpu_release(struct mdev_device *mdev)
843{
844 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
845
846 __intel_vgpu_release(vgpu);
847}
848
849static void intel_vgpu_release_work(struct work_struct *work)
850{
851 struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu,
852 vdev.release_work);
Jike Song8ff842f2016-12-16 10:51:07 +0800853
Jike Song659643f2016-12-08 11:00:36 +0800854 __intel_vgpu_release(vgpu);
855}
856
Jani Nikula2e679d42019-01-21 11:51:41 +0200857static u64 intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)
Jike Song659643f2016-12-08 11:00:36 +0800858{
859 u32 start_lo, start_hi;
860 u32 mem_type;
Jike Song659643f2016-12-08 11:00:36 +0800861
Changbin Duf090a002017-08-15 13:14:04 +0800862 start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
Jike Song659643f2016-12-08 11:00:36 +0800863 PCI_BASE_ADDRESS_MEM_MASK;
Changbin Duf090a002017-08-15 13:14:04 +0800864 mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
Jike Song659643f2016-12-08 11:00:36 +0800865 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
866
867 switch (mem_type) {
868 case PCI_BASE_ADDRESS_MEM_TYPE_64:
869 start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space
Changbin Duf090a002017-08-15 13:14:04 +0800870 + bar + 4));
Jike Song659643f2016-12-08 11:00:36 +0800871 break;
872 case PCI_BASE_ADDRESS_MEM_TYPE_32:
873 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
874 /* 1M mem BAR treated as 32-bit BAR */
875 default:
876 /* mem unknown type treated as 32-bit BAR */
877 start_hi = 0;
878 break;
879 }
880
881 return ((u64)start_hi << 32) | start_lo;
882}
883
Jani Nikula2e679d42019-01-21 11:51:41 +0200884static int intel_vgpu_bar_rw(struct intel_vgpu *vgpu, int bar, u64 off,
Changbin Duf090a002017-08-15 13:14:04 +0800885 void *buf, unsigned int count, bool is_write)
886{
Jani Nikula2e679d42019-01-21 11:51:41 +0200887 u64 bar_start = intel_vgpu_get_bar_addr(vgpu, bar);
Changbin Duf090a002017-08-15 13:14:04 +0800888 int ret;
889
890 if (is_write)
891 ret = intel_gvt_ops->emulate_mmio_write(vgpu,
892 bar_start + off, buf, count);
893 else
894 ret = intel_gvt_ops->emulate_mmio_read(vgpu,
895 bar_start + off, buf, count);
896 return ret;
897}
898
Jani Nikula2e679d42019-01-21 11:51:41 +0200899static inline bool intel_vgpu_in_aperture(struct intel_vgpu *vgpu, u64 off)
Changbin Dud480b282018-01-30 13:51:31 +0800900{
901 return off >= vgpu_aperture_offset(vgpu) &&
902 off < vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu);
903}
904
Jani Nikula2e679d42019-01-21 11:51:41 +0200905static int intel_vgpu_aperture_rw(struct intel_vgpu *vgpu, u64 off,
Changbin Dud480b282018-01-30 13:51:31 +0800906 void *buf, unsigned long count, bool is_write)
907{
Chris Wilson196a6622019-04-04 08:14:25 +0100908 void __iomem *aperture_va;
Changbin Dud480b282018-01-30 13:51:31 +0800909
910 if (!intel_vgpu_in_aperture(vgpu, off) ||
911 !intel_vgpu_in_aperture(vgpu, off + count)) {
912 gvt_vgpu_err("Invalid aperture offset %llu\n", off);
913 return -EINVAL;
914 }
915
916 aperture_va = io_mapping_map_wc(&vgpu->gvt->dev_priv->ggtt.iomap,
917 ALIGN_DOWN(off, PAGE_SIZE),
918 count + offset_in_page(off));
919 if (!aperture_va)
920 return -EIO;
921
922 if (is_write)
Chris Wilson196a6622019-04-04 08:14:25 +0100923 memcpy_toio(aperture_va + offset_in_page(off), buf, count);
Changbin Dud480b282018-01-30 13:51:31 +0800924 else
Chris Wilson196a6622019-04-04 08:14:25 +0100925 memcpy_fromio(buf, aperture_va + offset_in_page(off), count);
Changbin Dud480b282018-01-30 13:51:31 +0800926
927 io_mapping_unmap(aperture_va);
928
929 return 0;
930}
931
Jike Song659643f2016-12-08 11:00:36 +0800932static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
933 size_t count, loff_t *ppos, bool is_write)
934{
935 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
936 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
Jani Nikula2e679d42019-01-21 11:51:41 +0200937 u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
Jike Song659643f2016-12-08 11:00:36 +0800938 int ret = -EINVAL;
939
940
Tina Zhangb851ade2017-11-20 15:31:16 +0800941 if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500942 gvt_vgpu_err("invalid index: %u\n", index);
Jike Song659643f2016-12-08 11:00:36 +0800943 return -EINVAL;
944 }
945
946 switch (index) {
947 case VFIO_PCI_CONFIG_REGION_INDEX:
948 if (is_write)
949 ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos,
950 buf, count);
951 else
952 ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos,
953 buf, count);
954 break;
955 case VFIO_PCI_BAR0_REGION_INDEX:
Changbin Duf090a002017-08-15 13:14:04 +0800956 ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_0, pos,
957 buf, count, is_write);
958 break;
959 case VFIO_PCI_BAR2_REGION_INDEX:
Changbin Dud480b282018-01-30 13:51:31 +0800960 ret = intel_vgpu_aperture_rw(vgpu, pos, buf, count, is_write);
Jike Song659643f2016-12-08 11:00:36 +0800961 break;
Changbin Du5d5fe172017-08-15 13:20:51 +0800962 case VFIO_PCI_BAR1_REGION_INDEX:
Jike Song659643f2016-12-08 11:00:36 +0800963 case VFIO_PCI_BAR3_REGION_INDEX:
964 case VFIO_PCI_BAR4_REGION_INDEX:
965 case VFIO_PCI_BAR5_REGION_INDEX:
966 case VFIO_PCI_VGA_REGION_INDEX:
967 case VFIO_PCI_ROM_REGION_INDEX:
Tina Zhangb851ade2017-11-20 15:31:16 +0800968 break;
Jike Song659643f2016-12-08 11:00:36 +0800969 default:
Tina Zhangb851ade2017-11-20 15:31:16 +0800970 if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions)
971 return -EINVAL;
972
973 index -= VFIO_PCI_NUM_REGIONS;
974 return vgpu->vdev.region[index].ops->rw(vgpu, buf, count,
975 ppos, is_write);
Jike Song659643f2016-12-08 11:00:36 +0800976 }
977
978 return ret == 0 ? count : ret;
979}
980
Tina Zhanga26ca6a2018-02-11 14:59:19 +0800981static bool gtt_entry(struct mdev_device *mdev, loff_t *ppos)
982{
983 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
984 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
985 struct intel_gvt *gvt = vgpu->gvt;
986 int offset;
987
988 /* Only allow MMIO GGTT entry access */
989 if (index != PCI_BASE_ADDRESS_0)
990 return false;
991
992 offset = (u64)(*ppos & VFIO_PCI_OFFSET_MASK) -
993 intel_vgpu_get_bar_gpa(vgpu, PCI_BASE_ADDRESS_0);
994
995 return (offset >= gvt->device_info.gtt_start_offset &&
996 offset < gvt->device_info.gtt_start_offset + gvt_ggtt_sz(gvt)) ?
997 true : false;
998}
999
Jike Song659643f2016-12-08 11:00:36 +08001000static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
1001 size_t count, loff_t *ppos)
1002{
1003 unsigned int done = 0;
1004 int ret;
1005
1006 while (count) {
1007 size_t filled;
1008
Tina Zhanga26ca6a2018-02-11 14:59:19 +08001009 /* Only support GGTT entry 8 bytes read */
1010 if (count >= 8 && !(*ppos % 8) &&
1011 gtt_entry(mdev, ppos)) {
1012 u64 val;
1013
1014 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
1015 ppos, false);
1016 if (ret <= 0)
1017 goto read_err;
1018
1019 if (copy_to_user(buf, &val, sizeof(val)))
1020 goto read_err;
1021
1022 filled = 8;
1023 } else if (count >= 4 && !(*ppos % 4)) {
Jike Song659643f2016-12-08 11:00:36 +08001024 u32 val;
1025
1026 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
1027 ppos, false);
1028 if (ret <= 0)
1029 goto read_err;
1030
1031 if (copy_to_user(buf, &val, sizeof(val)))
1032 goto read_err;
1033
1034 filled = 4;
1035 } else if (count >= 2 && !(*ppos % 2)) {
1036 u16 val;
1037
1038 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
1039 ppos, false);
1040 if (ret <= 0)
1041 goto read_err;
1042
1043 if (copy_to_user(buf, &val, sizeof(val)))
1044 goto read_err;
1045
1046 filled = 2;
1047 } else {
1048 u8 val;
1049
1050 ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos,
1051 false);
1052 if (ret <= 0)
1053 goto read_err;
1054
1055 if (copy_to_user(buf, &val, sizeof(val)))
1056 goto read_err;
1057
1058 filled = 1;
1059 }
1060
1061 count -= filled;
1062 done += filled;
1063 *ppos += filled;
1064 buf += filled;
1065 }
1066
1067 return done;
1068
1069read_err:
1070 return -EFAULT;
1071}
1072
1073static ssize_t intel_vgpu_write(struct mdev_device *mdev,
1074 const char __user *buf,
1075 size_t count, loff_t *ppos)
1076{
1077 unsigned int done = 0;
1078 int ret;
1079
1080 while (count) {
1081 size_t filled;
1082
Tina Zhanga26ca6a2018-02-11 14:59:19 +08001083 /* Only support GGTT entry 8 bytes write */
1084 if (count >= 8 && !(*ppos % 8) &&
1085 gtt_entry(mdev, ppos)) {
1086 u64 val;
1087
1088 if (copy_from_user(&val, buf, sizeof(val)))
1089 goto write_err;
1090
1091 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
1092 ppos, true);
1093 if (ret <= 0)
1094 goto write_err;
1095
1096 filled = 8;
1097 } else if (count >= 4 && !(*ppos % 4)) {
Jike Song659643f2016-12-08 11:00:36 +08001098 u32 val;
1099
1100 if (copy_from_user(&val, buf, sizeof(val)))
1101 goto write_err;
1102
1103 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
1104 ppos, true);
1105 if (ret <= 0)
1106 goto write_err;
1107
1108 filled = 4;
1109 } else if (count >= 2 && !(*ppos % 2)) {
1110 u16 val;
1111
1112 if (copy_from_user(&val, buf, sizeof(val)))
1113 goto write_err;
1114
1115 ret = intel_vgpu_rw(mdev, (char *)&val,
1116 sizeof(val), ppos, true);
1117 if (ret <= 0)
1118 goto write_err;
1119
1120 filled = 2;
1121 } else {
1122 u8 val;
1123
1124 if (copy_from_user(&val, buf, sizeof(val)))
1125 goto write_err;
1126
1127 ret = intel_vgpu_rw(mdev, &val, sizeof(val),
1128 ppos, true);
1129 if (ret <= 0)
1130 goto write_err;
1131
1132 filled = 1;
1133 }
1134
1135 count -= filled;
1136 done += filled;
1137 *ppos += filled;
1138 buf += filled;
1139 }
1140
1141 return done;
1142write_err:
1143 return -EFAULT;
1144}
1145
1146static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
1147{
1148 unsigned int index;
1149 u64 virtaddr;
Zhenyu Wang51b00d82019-01-11 13:58:53 +08001150 unsigned long req_size, pgoff, req_start;
Jike Song659643f2016-12-08 11:00:36 +08001151 pgprot_t pg_prot;
1152 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
1153
1154 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1155 if (index >= VFIO_PCI_ROM_REGION_INDEX)
1156 return -EINVAL;
1157
1158 if (vma->vm_end < vma->vm_start)
1159 return -EINVAL;
1160 if ((vma->vm_flags & VM_SHARED) == 0)
1161 return -EINVAL;
1162 if (index != VFIO_PCI_BAR2_REGION_INDEX)
1163 return -EINVAL;
1164
1165 pg_prot = vma->vm_page_prot;
1166 virtaddr = vma->vm_start;
1167 req_size = vma->vm_end - vma->vm_start;
Zhenyu Wang51b00d82019-01-11 13:58:53 +08001168 pgoff = vma->vm_pgoff &
1169 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1170 req_start = pgoff << PAGE_SHIFT;
1171
1172 if (!intel_vgpu_in_aperture(vgpu, req_start))
1173 return -EINVAL;
1174 if (req_start + req_size >
1175 vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu))
1176 return -EINVAL;
1177
1178 pgoff = (gvt_aperture_pa_base(vgpu->gvt) >> PAGE_SHIFT) + pgoff;
Jike Song659643f2016-12-08 11:00:36 +08001179
1180 return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
1181}
1182
1183static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type)
1184{
1185 if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX)
1186 return 1;
1187
1188 return 0;
1189}
1190
1191static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu,
1192 unsigned int index, unsigned int start,
Jani Nikula2e679d42019-01-21 11:51:41 +02001193 unsigned int count, u32 flags,
Jike Song659643f2016-12-08 11:00:36 +08001194 void *data)
1195{
1196 return 0;
1197}
1198
1199static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu,
1200 unsigned int index, unsigned int start,
Jani Nikula2e679d42019-01-21 11:51:41 +02001201 unsigned int count, u32 flags, void *data)
Jike Song659643f2016-12-08 11:00:36 +08001202{
1203 return 0;
1204}
1205
1206static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu,
1207 unsigned int index, unsigned int start, unsigned int count,
Jani Nikula2e679d42019-01-21 11:51:41 +02001208 u32 flags, void *data)
Jike Song659643f2016-12-08 11:00:36 +08001209{
1210 return 0;
1211}
1212
1213static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu,
1214 unsigned int index, unsigned int start, unsigned int count,
Jani Nikula2e679d42019-01-21 11:51:41 +02001215 u32 flags, void *data)
Jike Song659643f2016-12-08 11:00:36 +08001216{
1217 struct eventfd_ctx *trigger;
1218
1219 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
1220 int fd = *(int *)data;
1221
1222 trigger = eventfd_ctx_fdget(fd);
1223 if (IS_ERR(trigger)) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001224 gvt_vgpu_err("eventfd_ctx_fdget failed\n");
Jike Song659643f2016-12-08 11:00:36 +08001225 return PTR_ERR(trigger);
1226 }
1227 vgpu->vdev.msi_trigger = trigger;
Xiong Zhangd54e7932018-04-13 10:26:16 +08001228 } else if ((flags & VFIO_IRQ_SET_DATA_NONE) && !count)
1229 intel_vgpu_release_msi_eventfd_ctx(vgpu);
Jike Song659643f2016-12-08 11:00:36 +08001230
1231 return 0;
1232}
1233
Jani Nikula2e679d42019-01-21 11:51:41 +02001234static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, u32 flags,
Jike Song659643f2016-12-08 11:00:36 +08001235 unsigned int index, unsigned int start, unsigned int count,
1236 void *data)
1237{
1238 int (*func)(struct intel_vgpu *vgpu, unsigned int index,
Jani Nikula2e679d42019-01-21 11:51:41 +02001239 unsigned int start, unsigned int count, u32 flags,
Jike Song659643f2016-12-08 11:00:36 +08001240 void *data) = NULL;
1241
1242 switch (index) {
1243 case VFIO_PCI_INTX_IRQ_INDEX:
1244 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1245 case VFIO_IRQ_SET_ACTION_MASK:
1246 func = intel_vgpu_set_intx_mask;
1247 break;
1248 case VFIO_IRQ_SET_ACTION_UNMASK:
1249 func = intel_vgpu_set_intx_unmask;
1250 break;
1251 case VFIO_IRQ_SET_ACTION_TRIGGER:
1252 func = intel_vgpu_set_intx_trigger;
1253 break;
1254 }
1255 break;
1256 case VFIO_PCI_MSI_IRQ_INDEX:
1257 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
1258 case VFIO_IRQ_SET_ACTION_MASK:
1259 case VFIO_IRQ_SET_ACTION_UNMASK:
1260 /* XXX Need masking support exported */
1261 break;
1262 case VFIO_IRQ_SET_ACTION_TRIGGER:
1263 func = intel_vgpu_set_msi_trigger;
1264 break;
1265 }
1266 break;
1267 }
1268
1269 if (!func)
1270 return -ENOTTY;
1271
1272 return func(vgpu, index, start, count, flags, data);
1273}
1274
1275static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
1276 unsigned long arg)
1277{
1278 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
1279 unsigned long minsz;
1280
1281 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd);
1282
1283 if (cmd == VFIO_DEVICE_GET_INFO) {
1284 struct vfio_device_info info;
1285
1286 minsz = offsetofend(struct vfio_device_info, num_irqs);
1287
1288 if (copy_from_user(&info, (void __user *)arg, minsz))
1289 return -EFAULT;
1290
1291 if (info.argsz < minsz)
1292 return -EINVAL;
1293
1294 info.flags = VFIO_DEVICE_FLAGS_PCI;
1295 info.flags |= VFIO_DEVICE_FLAGS_RESET;
Tina Zhangb851ade2017-11-20 15:31:16 +08001296 info.num_regions = VFIO_PCI_NUM_REGIONS +
1297 vgpu->vdev.num_regions;
Jike Song659643f2016-12-08 11:00:36 +08001298 info.num_irqs = VFIO_PCI_NUM_IRQS;
1299
1300 return copy_to_user((void __user *)arg, &info, minsz) ?
1301 -EFAULT : 0;
1302
1303 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
1304 struct vfio_region_info info;
1305 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
Gustavo A. R. Silvade5372d2018-08-02 22:40:19 -05001306 unsigned int i;
1307 int ret;
Jike Song659643f2016-12-08 11:00:36 +08001308 struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
1309 size_t size;
1310 int nr_areas = 1;
1311 int cap_type_id;
1312
1313 minsz = offsetofend(struct vfio_region_info, offset);
1314
1315 if (copy_from_user(&info, (void __user *)arg, minsz))
1316 return -EFAULT;
1317
1318 if (info.argsz < minsz)
1319 return -EINVAL;
1320
1321 switch (info.index) {
1322 case VFIO_PCI_CONFIG_REGION_INDEX:
1323 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
Changbin Du02d578e2017-08-23 14:08:10 +08001324 info.size = vgpu->gvt->device_info.cfg_space_size;
Jike Song659643f2016-12-08 11:00:36 +08001325 info.flags = VFIO_REGION_INFO_FLAG_READ |
1326 VFIO_REGION_INFO_FLAG_WRITE;
1327 break;
1328 case VFIO_PCI_BAR0_REGION_INDEX:
1329 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1330 info.size = vgpu->cfg_space.bar[info.index].size;
1331 if (!info.size) {
1332 info.flags = 0;
1333 break;
1334 }
1335
1336 info.flags = VFIO_REGION_INFO_FLAG_READ |
1337 VFIO_REGION_INFO_FLAG_WRITE;
1338 break;
1339 case VFIO_PCI_BAR1_REGION_INDEX:
1340 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1341 info.size = 0;
1342 info.flags = 0;
1343 break;
1344 case VFIO_PCI_BAR2_REGION_INDEX:
1345 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1346 info.flags = VFIO_REGION_INFO_FLAG_CAPS |
1347 VFIO_REGION_INFO_FLAG_MMAP |
1348 VFIO_REGION_INFO_FLAG_READ |
1349 VFIO_REGION_INFO_FLAG_WRITE;
1350 info.size = gvt_aperture_sz(vgpu->gvt);
1351
1352 size = sizeof(*sparse) +
1353 (nr_areas * sizeof(*sparse->areas));
1354 sparse = kzalloc(size, GFP_KERNEL);
1355 if (!sparse)
1356 return -ENOMEM;
1357
Alex Williamsondda01f72017-12-12 12:59:39 -07001358 sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1359 sparse->header.version = 1;
Jike Song659643f2016-12-08 11:00:36 +08001360 sparse->nr_areas = nr_areas;
1361 cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1362 sparse->areas[0].offset =
1363 PAGE_ALIGN(vgpu_aperture_offset(vgpu));
1364 sparse->areas[0].size = vgpu_aperture_sz(vgpu);
Jike Song659643f2016-12-08 11:00:36 +08001365 break;
1366
1367 case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1368 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1369 info.size = 0;
Jike Song659643f2016-12-08 11:00:36 +08001370 info.flags = 0;
Pei Zhang072ec932017-12-08 15:31:12 +08001371
Jike Song659643f2016-12-08 11:00:36 +08001372 gvt_dbg_core("get region info bar:%d\n", info.index);
1373 break;
1374
1375 case VFIO_PCI_ROM_REGION_INDEX:
1376 case VFIO_PCI_VGA_REGION_INDEX:
Pei Zhang072ec932017-12-08 15:31:12 +08001377 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1378 info.size = 0;
1379 info.flags = 0;
1380
Jike Song659643f2016-12-08 11:00:36 +08001381 gvt_dbg_core("get region info index:%d\n", info.index);
1382 break;
1383 default:
1384 {
Alex Williamsondda01f72017-12-12 12:59:39 -07001385 struct vfio_region_info_cap_type cap_type = {
1386 .header.id = VFIO_REGION_INFO_CAP_TYPE,
1387 .header.version = 1 };
Jike Song659643f2016-12-08 11:00:36 +08001388
1389 if (info.index >= VFIO_PCI_NUM_REGIONS +
1390 vgpu->vdev.num_regions)
1391 return -EINVAL;
Gustavo A. R. Silvade5372d2018-08-02 22:40:19 -05001392 info.index =
1393 array_index_nospec(info.index,
1394 VFIO_PCI_NUM_REGIONS +
1395 vgpu->vdev.num_regions);
Jike Song659643f2016-12-08 11:00:36 +08001396
1397 i = info.index - VFIO_PCI_NUM_REGIONS;
1398
1399 info.offset =
1400 VFIO_PCI_INDEX_TO_OFFSET(info.index);
1401 info.size = vgpu->vdev.region[i].size;
1402 info.flags = vgpu->vdev.region[i].flags;
1403
1404 cap_type.type = vgpu->vdev.region[i].type;
1405 cap_type.subtype = vgpu->vdev.region[i].subtype;
1406
1407 ret = vfio_info_add_capability(&caps,
Alex Williamsondda01f72017-12-12 12:59:39 -07001408 &cap_type.header,
1409 sizeof(cap_type));
Jike Song659643f2016-12-08 11:00:36 +08001410 if (ret)
1411 return ret;
1412 }
1413 }
1414
1415 if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) {
1416 switch (cap_type_id) {
1417 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1418 ret = vfio_info_add_capability(&caps,
Alex Williamsondda01f72017-12-12 12:59:39 -07001419 &sparse->header, sizeof(*sparse) +
1420 (sparse->nr_areas *
1421 sizeof(*sparse->areas)));
Yi Wang7590ebb2018-08-08 23:10:57 +08001422 if (ret) {
1423 kfree(sparse);
Jike Song659643f2016-12-08 11:00:36 +08001424 return ret;
Yi Wang7590ebb2018-08-08 23:10:57 +08001425 }
Jike Song659643f2016-12-08 11:00:36 +08001426 break;
1427 default:
Yi Wang7590ebb2018-08-08 23:10:57 +08001428 kfree(sparse);
Jike Song659643f2016-12-08 11:00:36 +08001429 return -EINVAL;
1430 }
1431 }
1432
1433 if (caps.size) {
Tina Zhangb851ade2017-11-20 15:31:16 +08001434 info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
Jike Song659643f2016-12-08 11:00:36 +08001435 if (info.argsz < sizeof(info) + caps.size) {
1436 info.argsz = sizeof(info) + caps.size;
1437 info.cap_offset = 0;
1438 } else {
1439 vfio_info_cap_shift(&caps, sizeof(info));
1440 if (copy_to_user((void __user *)arg +
1441 sizeof(info), caps.buf,
1442 caps.size)) {
1443 kfree(caps.buf);
Yi Wang7590ebb2018-08-08 23:10:57 +08001444 kfree(sparse);
Jike Song659643f2016-12-08 11:00:36 +08001445 return -EFAULT;
1446 }
1447 info.cap_offset = sizeof(info);
1448 }
1449
1450 kfree(caps.buf);
1451 }
1452
Yi Wang7590ebb2018-08-08 23:10:57 +08001453 kfree(sparse);
Jike Song659643f2016-12-08 11:00:36 +08001454 return copy_to_user((void __user *)arg, &info, minsz) ?
1455 -EFAULT : 0;
1456 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
1457 struct vfio_irq_info info;
1458
1459 minsz = offsetofend(struct vfio_irq_info, count);
1460
1461 if (copy_from_user(&info, (void __user *)arg, minsz))
1462 return -EFAULT;
1463
1464 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
1465 return -EINVAL;
1466
1467 switch (info.index) {
1468 case VFIO_PCI_INTX_IRQ_INDEX:
1469 case VFIO_PCI_MSI_IRQ_INDEX:
1470 break;
1471 default:
1472 return -EINVAL;
1473 }
1474
1475 info.flags = VFIO_IRQ_INFO_EVENTFD;
1476
1477 info.count = intel_vgpu_get_irq_count(vgpu, info.index);
1478
1479 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
1480 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
1481 VFIO_IRQ_INFO_AUTOMASKED);
1482 else
1483 info.flags |= VFIO_IRQ_INFO_NORESIZE;
1484
1485 return copy_to_user((void __user *)arg, &info, minsz) ?
1486 -EFAULT : 0;
1487 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
1488 struct vfio_irq_set hdr;
1489 u8 *data = NULL;
1490 int ret = 0;
1491 size_t data_size = 0;
1492
1493 minsz = offsetofend(struct vfio_irq_set, count);
1494
1495 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1496 return -EFAULT;
1497
1498 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
1499 int max = intel_vgpu_get_irq_count(vgpu, hdr.index);
1500
1501 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
1502 VFIO_PCI_NUM_IRQS, &data_size);
1503 if (ret) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001504 gvt_vgpu_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
Jike Song659643f2016-12-08 11:00:36 +08001505 return -EINVAL;
1506 }
1507 if (data_size) {
1508 data = memdup_user((void __user *)(arg + minsz),
1509 data_size);
1510 if (IS_ERR(data))
1511 return PTR_ERR(data);
1512 }
1513 }
1514
1515 ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index,
1516 hdr.start, hdr.count, data);
1517 kfree(data);
1518
1519 return ret;
1520 } else if (cmd == VFIO_DEVICE_RESET) {
1521 intel_gvt_ops->vgpu_reset(vgpu);
1522 return 0;
Tina Zhange546e282017-11-23 16:26:36 +08001523 } else if (cmd == VFIO_DEVICE_QUERY_GFX_PLANE) {
1524 struct vfio_device_gfx_plane_info dmabuf;
1525 int ret = 0;
1526
1527 minsz = offsetofend(struct vfio_device_gfx_plane_info,
1528 dmabuf_id);
1529 if (copy_from_user(&dmabuf, (void __user *)arg, minsz))
1530 return -EFAULT;
1531 if (dmabuf.argsz < minsz)
1532 return -EINVAL;
1533
1534 ret = intel_gvt_ops->vgpu_query_plane(vgpu, &dmabuf);
1535 if (ret != 0)
1536 return ret;
1537
1538 return copy_to_user((void __user *)arg, &dmabuf, minsz) ?
1539 -EFAULT : 0;
1540 } else if (cmd == VFIO_DEVICE_GET_GFX_DMABUF) {
1541 __u32 dmabuf_id;
1542 __s32 dmabuf_fd;
1543
1544 if (get_user(dmabuf_id, (__u32 __user *)arg))
1545 return -EFAULT;
1546
1547 dmabuf_fd = intel_gvt_ops->vgpu_get_dmabuf(vgpu, dmabuf_id);
1548 return dmabuf_fd;
1549
Jike Song659643f2016-12-08 11:00:36 +08001550 }
1551
Gerd Hoffmann9f591ae2018-03-21 15:08:47 +01001552 return -ENOTTY;
Jike Song659643f2016-12-08 11:00:36 +08001553}
1554
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001555static ssize_t
1556vgpu_id_show(struct device *dev, struct device_attribute *attr,
1557 char *buf)
1558{
1559 struct mdev_device *mdev = mdev_from_dev(dev);
1560
1561 if (mdev) {
1562 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1563 mdev_get_drvdata(mdev);
1564 return sprintf(buf, "%d\n", vgpu->id);
1565 }
1566 return sprintf(buf, "\n");
1567}
1568
Zhenyu Wanga45050d2017-08-01 13:09:47 +08001569static ssize_t
1570hw_id_show(struct device *dev, struct device_attribute *attr,
1571 char *buf)
1572{
1573 struct mdev_device *mdev = mdev_from_dev(dev);
1574
1575 if (mdev) {
1576 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1577 mdev_get_drvdata(mdev);
1578 return sprintf(buf, "%u\n",
Zhi Wang1406a142017-09-10 21:15:18 +08001579 vgpu->submission.shadow_ctx->hw_id);
Zhenyu Wanga45050d2017-08-01 13:09:47 +08001580 }
1581 return sprintf(buf, "\n");
1582}
1583
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001584static DEVICE_ATTR_RO(vgpu_id);
Zhenyu Wanga45050d2017-08-01 13:09:47 +08001585static DEVICE_ATTR_RO(hw_id);
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001586
1587static struct attribute *intel_vgpu_attrs[] = {
1588 &dev_attr_vgpu_id.attr,
Zhenyu Wanga45050d2017-08-01 13:09:47 +08001589 &dev_attr_hw_id.attr,
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001590 NULL
1591};
1592
1593static const struct attribute_group intel_vgpu_group = {
1594 .name = "intel_vgpu",
1595 .attrs = intel_vgpu_attrs,
1596};
1597
1598static const struct attribute_group *intel_vgpu_groups[] = {
1599 &intel_vgpu_group,
1600 NULL,
1601};
1602
fred gao6aa23ce2017-09-28 11:03:03 +08001603static struct mdev_parent_ops intel_vgpu_ops = {
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001604 .mdev_attr_groups = intel_vgpu_groups,
Jike Song659643f2016-12-08 11:00:36 +08001605 .create = intel_vgpu_create,
1606 .remove = intel_vgpu_remove,
1607
1608 .open = intel_vgpu_open,
1609 .release = intel_vgpu_release,
1610
1611 .read = intel_vgpu_read,
1612 .write = intel_vgpu_write,
1613 .mmap = intel_vgpu_mmap,
1614 .ioctl = intel_vgpu_ioctl,
1615};
1616
Jike Songf30437c2016-11-09 20:30:59 +08001617static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)
1618{
fred gao6aa23ce2017-09-28 11:03:03 +08001619 struct attribute **kvm_type_attrs;
1620 struct attribute_group **kvm_vgpu_type_groups;
Jike Songf30437c2016-11-09 20:30:59 +08001621
1622 intel_gvt_ops = ops;
fred gao6aa23ce2017-09-28 11:03:03 +08001623 if (!intel_gvt_ops->get_gvt_attrs(&kvm_type_attrs,
1624 &kvm_vgpu_type_groups))
1625 return -EFAULT;
1626 intel_vgpu_ops.supported_type_groups = kvm_vgpu_type_groups;
Jike Songf30437c2016-11-09 20:30:59 +08001627
Jike Song659643f2016-12-08 11:00:36 +08001628 return mdev_register_device(dev, &intel_vgpu_ops);
Jike Songf30437c2016-11-09 20:30:59 +08001629}
1630
Zhenyu Wanga2b84192018-12-07 16:16:52 +08001631static void kvmgt_host_exit(struct device *dev)
Jike Songf30437c2016-11-09 20:30:59 +08001632{
Jike Song659643f2016-12-08 11:00:36 +08001633 mdev_unregister_device(dev);
Jike Songf30437c2016-11-09 20:30:59 +08001634}
1635
Changbin Duf66e5ff72018-01-30 19:19:51 +08001636static int kvmgt_page_track_add(unsigned long handle, u64 gfn)
Jike Songf30437c2016-11-09 20:30:59 +08001637{
Jike Song659643f2016-12-08 11:00:36 +08001638 struct kvmgt_guest_info *info;
1639 struct kvm *kvm;
Jike Songf30437c2016-11-09 20:30:59 +08001640 struct kvm_memory_slot *slot;
1641 int idx;
1642
Jike Song659643f2016-12-08 11:00:36 +08001643 if (!handle_valid(handle))
1644 return -ESRCH;
1645
1646 info = (struct kvmgt_guest_info *)handle;
1647 kvm = info->kvm;
1648
Jike Songf30437c2016-11-09 20:30:59 +08001649 idx = srcu_read_lock(&kvm->srcu);
1650 slot = gfn_to_memslot(kvm, gfn);
Jike Songfaaaa532016-12-16 10:51:05 +08001651 if (!slot) {
1652 srcu_read_unlock(&kvm->srcu, idx);
1653 return -EINVAL;
1654 }
Jike Songf30437c2016-11-09 20:30:59 +08001655
1656 spin_lock(&kvm->mmu_lock);
1657
1658 if (kvmgt_gfn_is_write_protected(info, gfn))
1659 goto out;
1660
1661 kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1662 kvmgt_protect_table_add(info, gfn);
1663
1664out:
1665 spin_unlock(&kvm->mmu_lock);
1666 srcu_read_unlock(&kvm->srcu, idx);
1667 return 0;
1668}
1669
Changbin Duf66e5ff72018-01-30 19:19:51 +08001670static int kvmgt_page_track_remove(unsigned long handle, u64 gfn)
Jike Songf30437c2016-11-09 20:30:59 +08001671{
Jike Song659643f2016-12-08 11:00:36 +08001672 struct kvmgt_guest_info *info;
1673 struct kvm *kvm;
Jike Songf30437c2016-11-09 20:30:59 +08001674 struct kvm_memory_slot *slot;
1675 int idx;
1676
Jike Song659643f2016-12-08 11:00:36 +08001677 if (!handle_valid(handle))
1678 return 0;
1679
1680 info = (struct kvmgt_guest_info *)handle;
1681 kvm = info->kvm;
1682
Jike Songf30437c2016-11-09 20:30:59 +08001683 idx = srcu_read_lock(&kvm->srcu);
1684 slot = gfn_to_memslot(kvm, gfn);
Jike Songfaaaa532016-12-16 10:51:05 +08001685 if (!slot) {
1686 srcu_read_unlock(&kvm->srcu, idx);
1687 return -EINVAL;
1688 }
Jike Songf30437c2016-11-09 20:30:59 +08001689
1690 spin_lock(&kvm->mmu_lock);
1691
1692 if (!kvmgt_gfn_is_write_protected(info, gfn))
1693 goto out;
1694
1695 kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1696 kvmgt_protect_table_del(info, gfn);
1697
1698out:
1699 spin_unlock(&kvm->mmu_lock);
1700 srcu_read_unlock(&kvm->srcu, idx);
1701 return 0;
1702}
1703
1704static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1705 const u8 *val, int len,
1706 struct kvm_page_track_notifier_node *node)
1707{
1708 struct kvmgt_guest_info *info = container_of(node,
1709 struct kvmgt_guest_info, track_node);
1710
1711 if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
Zhenyu Wang4fafba22017-12-18 11:58:46 +08001712 intel_gvt_ops->write_protect_handler(info->vgpu, gpa,
1713 (void *)val, len);
Jike Songf30437c2016-11-09 20:30:59 +08001714}
1715
1716static void kvmgt_page_track_flush_slot(struct kvm *kvm,
1717 struct kvm_memory_slot *slot,
1718 struct kvm_page_track_notifier_node *node)
1719{
1720 int i;
1721 gfn_t gfn;
1722 struct kvmgt_guest_info *info = container_of(node,
1723 struct kvmgt_guest_info, track_node);
1724
1725 spin_lock(&kvm->mmu_lock);
1726 for (i = 0; i < slot->npages; i++) {
1727 gfn = slot->base_gfn + i;
1728 if (kvmgt_gfn_is_write_protected(info, gfn)) {
1729 kvm_slot_page_track_remove_page(kvm, slot, gfn,
1730 KVM_PAGE_TRACK_WRITE);
1731 kvmgt_protect_table_del(info, gfn);
1732 }
1733 }
1734 spin_unlock(&kvm->mmu_lock);
1735}
1736
Jike Song659643f2016-12-08 11:00:36 +08001737static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
1738{
1739 struct intel_vgpu *itr;
1740 struct kvmgt_guest_info *info;
1741 int id;
1742 bool ret = false;
1743
1744 mutex_lock(&vgpu->gvt->lock);
1745 for_each_active_vgpu(vgpu->gvt, itr, id) {
1746 if (!handle_valid(itr->handle))
1747 continue;
1748
1749 info = (struct kvmgt_guest_info *)itr->handle;
1750 if (kvm && kvm == info->kvm) {
1751 ret = true;
1752 goto out;
1753 }
1754 }
1755out:
1756 mutex_unlock(&vgpu->gvt->lock);
1757 return ret;
1758}
1759
1760static int kvmgt_guest_init(struct mdev_device *mdev)
1761{
1762 struct kvmgt_guest_info *info;
1763 struct intel_vgpu *vgpu;
1764 struct kvm *kvm;
1765
1766 vgpu = mdev_get_drvdata(mdev);
1767 if (handle_valid(vgpu->handle))
1768 return -EEXIST;
1769
1770 kvm = vgpu->vdev.kvm;
1771 if (!kvm || kvm->mm != current->mm) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001772 gvt_vgpu_err("KVM is required to use Intel vGPU\n");
Jike Song659643f2016-12-08 11:00:36 +08001773 return -ESRCH;
1774 }
1775
1776 if (__kvmgt_vgpu_exist(vgpu, kvm))
1777 return -EEXIST;
1778
1779 info = vzalloc(sizeof(struct kvmgt_guest_info));
1780 if (!info)
1781 return -ENOMEM;
1782
1783 vgpu->handle = (unsigned long)info;
1784 info->vgpu = vgpu;
1785 info->kvm = kvm;
Alex Williamson93a15b52017-03-19 20:38:40 -06001786 kvm_get_kvm(info->kvm);
Jike Song659643f2016-12-08 11:00:36 +08001787
1788 kvmgt_protect_table_init(info);
1789 gvt_cache_init(vgpu);
1790
Tina Zhange546e282017-11-23 16:26:36 +08001791 init_completion(&vgpu->vblank_done);
1792
Jike Song659643f2016-12-08 11:00:36 +08001793 info->track_node.track_write = kvmgt_page_track_write;
1794 info->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
1795 kvm_page_track_register_notifier(kvm, &info->track_node);
1796
Changbin Du6846dfe2018-03-05 15:30:34 +08001797 info->debugfs_cache_entries = debugfs_create_ulong(
1798 "kvmgt_nr_cache_entries",
1799 0444, vgpu->debugfs,
1800 &vgpu->vdev.nr_cache_entries);
1801 if (!info->debugfs_cache_entries)
1802 gvt_vgpu_err("Cannot create kvmgt debugfs entry\n");
1803
Jike Song659643f2016-12-08 11:00:36 +08001804 return 0;
1805}
1806
1807static bool kvmgt_guest_exit(struct kvmgt_guest_info *info)
1808{
Changbin Du6846dfe2018-03-05 15:30:34 +08001809 debugfs_remove(info->debugfs_cache_entries);
1810
Jike Song659643f2016-12-08 11:00:36 +08001811 kvm_page_track_unregister_notifier(info->kvm, &info->track_node);
Alex Williamson93a15b52017-03-19 20:38:40 -06001812 kvm_put_kvm(info->kvm);
Jike Song659643f2016-12-08 11:00:36 +08001813 kvmgt_protect_table_destroy(info);
Jike Song8ff842f2016-12-16 10:51:07 +08001814 gvt_cache_destroy(info->vgpu);
Jike Song659643f2016-12-08 11:00:36 +08001815 vfree(info);
1816
1817 return true;
1818}
1819
Jike Songf30437c2016-11-09 20:30:59 +08001820static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle)
1821{
1822 /* nothing to do here */
1823 return 0;
1824}
1825
Hang Yuan6c2d0f92019-01-14 18:43:39 +08001826static void kvmgt_detach_vgpu(void *p_vgpu)
Jike Songf30437c2016-11-09 20:30:59 +08001827{
Hang Yuan6c2d0f92019-01-14 18:43:39 +08001828 int i;
1829 struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
1830
1831 if (!vgpu->vdev.region)
1832 return;
1833
1834 for (i = 0; i < vgpu->vdev.num_regions; i++)
1835 if (vgpu->vdev.region[i].ops->release)
1836 vgpu->vdev.region[i].ops->release(vgpu,
1837 &vgpu->vdev.region[i]);
1838 vgpu->vdev.num_regions = 0;
1839 kfree(vgpu->vdev.region);
1840 vgpu->vdev.region = NULL;
Jike Songf30437c2016-11-09 20:30:59 +08001841}
1842
1843static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data)
1844{
Jike Song659643f2016-12-08 11:00:36 +08001845 struct kvmgt_guest_info *info;
1846 struct intel_vgpu *vgpu;
Jike Songf30437c2016-11-09 20:30:59 +08001847
Jike Song659643f2016-12-08 11:00:36 +08001848 if (!handle_valid(handle))
1849 return -ESRCH;
Jike Songf30437c2016-11-09 20:30:59 +08001850
Jike Song659643f2016-12-08 11:00:36 +08001851 info = (struct kvmgt_guest_info *)handle;
1852 vgpu = info->vgpu;
1853
Xiong Zhangd54e7932018-04-13 10:26:16 +08001854 /*
1855 * When guest is poweroff, msi_trigger is set to NULL, but vgpu's
1856 * config and mmio register isn't restored to default during guest
1857 * poweroff. If this vgpu is still used in next vm, this vgpu's pipe
1858 * may be enabled, then once this vgpu is active, it will get inject
1859 * vblank interrupt request. But msi_trigger is null until msi is
1860 * enabled by guest. so if msi_trigger is null, success is still
1861 * returned and don't inject interrupt into guest.
1862 */
1863 if (vgpu->vdev.msi_trigger == NULL)
1864 return 0;
1865
Jike Song659643f2016-12-08 11:00:36 +08001866 if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1)
1867 return 0;
1868
1869 return -EFAULT;
Jike Songf30437c2016-11-09 20:30:59 +08001870}
1871
1872static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn)
1873{
Jike Song659643f2016-12-08 11:00:36 +08001874 struct kvmgt_guest_info *info;
Changbin Ducf4ee732018-03-01 15:49:59 +08001875 kvm_pfn_t pfn;
Jike Songf30437c2016-11-09 20:30:59 +08001876
Jike Song659643f2016-12-08 11:00:36 +08001877 if (!handle_valid(handle))
1878 return INTEL_GVT_INVALID_ADDR;
1879
1880 info = (struct kvmgt_guest_info *)handle;
Changbin Ducf4ee732018-03-01 15:49:59 +08001881
1882 pfn = gfn_to_pfn(info->kvm, gfn);
1883 if (is_error_noslot_pfn(pfn))
1884 return INTEL_GVT_INVALID_ADDR;
1885
1886 return pfn;
1887}
1888
Zhenyu Wang63ef2622018-07-31 11:02:11 +08001889static int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn,
Changbin Du79e542f2018-05-15 10:35:42 +08001890 unsigned long size, dma_addr_t *dma_addr)
Changbin Ducf4ee732018-03-01 15:49:59 +08001891{
1892 struct kvmgt_guest_info *info;
1893 struct intel_vgpu *vgpu;
1894 struct gvt_dma *entry;
1895 int ret;
1896
1897 if (!handle_valid(handle))
1898 return -EINVAL;
1899
1900 info = (struct kvmgt_guest_info *)handle;
Tina Zhang695fbc02017-03-10 04:26:53 -05001901 vgpu = info->vgpu;
Jike Songf30437c2016-11-09 20:30:59 +08001902
Changbin Ducf4ee732018-03-01 15:49:59 +08001903 mutex_lock(&info->vgpu->vdev.cache_lock);
1904
1905 entry = __gvt_cache_find_gfn(info->vgpu, gfn);
1906 if (!entry) {
Changbin Du79e542f2018-05-15 10:35:42 +08001907 ret = gvt_dma_map_page(vgpu, gfn, dma_addr, size);
Changbin Du5cd42232018-03-12 15:12:34 +08001908 if (ret)
1909 goto err_unlock;
1910
Changbin Du79e542f2018-05-15 10:35:42 +08001911 ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr, size);
Changbin Du5cd42232018-03-12 15:12:34 +08001912 if (ret)
1913 goto err_unmap;
Changbin Ducf4ee732018-03-01 15:49:59 +08001914 } else {
1915 kref_get(&entry->ref);
1916 *dma_addr = entry->dma_addr;
Chuanxiao Dong4a0b3442017-02-14 17:15:54 +08001917 }
Jike Songf30437c2016-11-09 20:30:59 +08001918
Changbin Ducf4ee732018-03-01 15:49:59 +08001919 mutex_unlock(&info->vgpu->vdev.cache_lock);
1920 return 0;
Changbin Du5cd42232018-03-12 15:12:34 +08001921
1922err_unmap:
Changbin Du79e542f2018-05-15 10:35:42 +08001923 gvt_dma_unmap_page(vgpu, gfn, *dma_addr, size);
Changbin Du5cd42232018-03-12 15:12:34 +08001924err_unlock:
1925 mutex_unlock(&info->vgpu->vdev.cache_lock);
1926 return ret;
Changbin Ducf4ee732018-03-01 15:49:59 +08001927}
1928
1929static void __gvt_dma_release(struct kref *ref)
1930{
1931 struct gvt_dma *entry = container_of(ref, typeof(*entry), ref);
1932
Changbin Du79e542f2018-05-15 10:35:42 +08001933 gvt_dma_unmap_page(entry->vgpu, entry->gfn, entry->dma_addr,
1934 entry->size);
Changbin Ducf4ee732018-03-01 15:49:59 +08001935 __gvt_cache_remove_entry(entry->vgpu, entry);
1936}
1937
Zhenyu Wang63ef2622018-07-31 11:02:11 +08001938static void kvmgt_dma_unmap_guest_page(unsigned long handle, dma_addr_t dma_addr)
Changbin Ducf4ee732018-03-01 15:49:59 +08001939{
1940 struct kvmgt_guest_info *info;
1941 struct gvt_dma *entry;
1942
1943 if (!handle_valid(handle))
1944 return;
1945
1946 info = (struct kvmgt_guest_info *)handle;
1947
1948 mutex_lock(&info->vgpu->vdev.cache_lock);
1949 entry = __gvt_cache_find_dma_addr(info->vgpu, dma_addr);
1950 if (entry)
1951 kref_put(&entry->ref, __gvt_dma_release);
1952 mutex_unlock(&info->vgpu->vdev.cache_lock);
Jike Songf30437c2016-11-09 20:30:59 +08001953}
1954
Jike Songf30437c2016-11-09 20:30:59 +08001955static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa,
1956 void *buf, unsigned long len, bool write)
1957{
Jike Songf440c8a2016-12-08 11:00:35 +08001958 struct kvmgt_guest_info *info;
1959 struct kvm *kvm;
Changbin Du5180edc2017-03-16 09:45:09 +08001960 int idx, ret;
Jike Songf440c8a2016-12-08 11:00:35 +08001961 bool kthread = current->mm == NULL;
Jike Songf30437c2016-11-09 20:30:59 +08001962
Jike Song659643f2016-12-08 11:00:36 +08001963 if (!handle_valid(handle))
1964 return -ESRCH;
1965
Jike Songf440c8a2016-12-08 11:00:35 +08001966 info = (struct kvmgt_guest_info *)handle;
1967 kvm = info->kvm;
Jike Songf30437c2016-11-09 20:30:59 +08001968
Zhenyu Wang0a1b60d2018-08-31 10:58:52 +08001969 if (kthread) {
1970 if (!mmget_not_zero(kvm->mm))
1971 return -EFAULT;
Jike Songf440c8a2016-12-08 11:00:35 +08001972 use_mm(kvm->mm);
Zhenyu Wang0a1b60d2018-08-31 10:58:52 +08001973 }
Jike Songf30437c2016-11-09 20:30:59 +08001974
Changbin Du5180edc2017-03-16 09:45:09 +08001975 idx = srcu_read_lock(&kvm->srcu);
Jike Songf440c8a2016-12-08 11:00:35 +08001976 ret = write ? kvm_write_guest(kvm, gpa, buf, len) :
1977 kvm_read_guest(kvm, gpa, buf, len);
Changbin Du5180edc2017-03-16 09:45:09 +08001978 srcu_read_unlock(&kvm->srcu, idx);
Jike Songf440c8a2016-12-08 11:00:35 +08001979
Zhenyu Wang0a1b60d2018-08-31 10:58:52 +08001980 if (kthread) {
Jike Songf440c8a2016-12-08 11:00:35 +08001981 unuse_mm(kvm->mm);
Zhenyu Wang0a1b60d2018-08-31 10:58:52 +08001982 mmput(kvm->mm);
1983 }
Jike Songf440c8a2016-12-08 11:00:35 +08001984
1985 return ret;
Jike Songf30437c2016-11-09 20:30:59 +08001986}
1987
1988static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa,
1989 void *buf, unsigned long len)
1990{
1991 return kvmgt_rw_gpa(handle, gpa, buf, len, false);
1992}
1993
1994static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa,
1995 void *buf, unsigned long len)
1996{
1997 return kvmgt_rw_gpa(handle, gpa, buf, len, true);
1998}
1999
2000static unsigned long kvmgt_virt_to_pfn(void *addr)
2001{
2002 return PFN_DOWN(__pa(addr));
2003}
2004
Hang Yuancc753fb2017-12-22 18:06:31 +08002005static bool kvmgt_is_valid_gfn(unsigned long handle, unsigned long gfn)
2006{
2007 struct kvmgt_guest_info *info;
2008 struct kvm *kvm;
Weinan Lia1ac5f02018-09-17 09:46:14 +08002009 int idx;
2010 bool ret;
Hang Yuancc753fb2017-12-22 18:06:31 +08002011
2012 if (!handle_valid(handle))
2013 return false;
2014
2015 info = (struct kvmgt_guest_info *)handle;
2016 kvm = info->kvm;
2017
Weinan Lia1ac5f02018-09-17 09:46:14 +08002018 idx = srcu_read_lock(&kvm->srcu);
2019 ret = kvm_is_visible_gfn(kvm, gfn);
2020 srcu_read_unlock(&kvm->srcu, idx);
Hang Yuancc753fb2017-12-22 18:06:31 +08002021
Weinan Lia1ac5f02018-09-17 09:46:14 +08002022 return ret;
Hang Yuancc753fb2017-12-22 18:06:31 +08002023}
2024
Zhenyu Wang9bdb0732018-12-07 16:16:53 +08002025static struct intel_gvt_mpt kvmgt_mpt = {
2026 .type = INTEL_GVT_HYPERVISOR_KVM,
Jike Songf30437c2016-11-09 20:30:59 +08002027 .host_init = kvmgt_host_init,
2028 .host_exit = kvmgt_host_exit,
2029 .attach_vgpu = kvmgt_attach_vgpu,
2030 .detach_vgpu = kvmgt_detach_vgpu,
2031 .inject_msi = kvmgt_inject_msi,
2032 .from_virt_to_mfn = kvmgt_virt_to_pfn,
Changbin Duf66e5ff72018-01-30 19:19:51 +08002033 .enable_page_track = kvmgt_page_track_add,
2034 .disable_page_track = kvmgt_page_track_remove,
Jike Songf30437c2016-11-09 20:30:59 +08002035 .read_gpa = kvmgt_read_gpa,
2036 .write_gpa = kvmgt_write_gpa,
2037 .gfn_to_mfn = kvmgt_gfn_to_pfn,
Changbin Ducf4ee732018-03-01 15:49:59 +08002038 .dma_map_guest_page = kvmgt_dma_map_guest_page,
2039 .dma_unmap_guest_page = kvmgt_dma_unmap_guest_page,
Tina Zhangb851ade2017-11-20 15:31:16 +08002040 .set_opregion = kvmgt_set_opregion,
Hang Yuan39c68e82019-01-30 18:25:54 +08002041 .set_edid = kvmgt_set_edid,
Tina Zhange546e282017-11-23 16:26:36 +08002042 .get_vfio_device = kvmgt_get_vfio_device,
2043 .put_vfio_device = kvmgt_put_vfio_device,
Hang Yuancc753fb2017-12-22 18:06:31 +08002044 .is_valid_gfn = kvmgt_is_valid_gfn,
Jike Songf30437c2016-11-09 20:30:59 +08002045};
Jike Songf30437c2016-11-09 20:30:59 +08002046
2047static int __init kvmgt_init(void)
2048{
Zhenyu Wang9bdb0732018-12-07 16:16:53 +08002049 if (intel_gvt_register_hypervisor(&kvmgt_mpt) < 0)
2050 return -ENODEV;
Jike Songf30437c2016-11-09 20:30:59 +08002051 return 0;
2052}
2053
2054static void __exit kvmgt_exit(void)
2055{
Zhenyu Wang9bdb0732018-12-07 16:16:53 +08002056 intel_gvt_unregister_hypervisor();
Jike Songf30437c2016-11-09 20:30:59 +08002057}
2058
2059module_init(kvmgt_init);
2060module_exit(kvmgt_exit);
2061
2062MODULE_LICENSE("GPL and additional rights");
2063MODULE_AUTHOR("Intel Corporation");