blob: ae65268efce3365a49ec7a21e1a400b8aba8d788 [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>
Jike Songf30437c2016-11-09 20:30:59 +080035#include <linux/types.h>
36#include <linux/list.h>
37#include <linux/rbtree.h>
38#include <linux/spinlock.h>
39#include <linux/eventfd.h>
40#include <linux/uuid.h>
41#include <linux/kvm_host.h>
42#include <linux/vfio.h>
Jike Song659643f2016-12-08 11:00:36 +080043#include <linux/mdev.h>
Jike Songf30437c2016-11-09 20:30:59 +080044
45#include "i915_drv.h"
46#include "gvt.h"
47
Jike Songf30437c2016-11-09 20:30:59 +080048static const struct intel_gvt_ops *intel_gvt_ops;
49
Jike Songf30437c2016-11-09 20:30:59 +080050/* helper macros copied from vfio-pci */
51#define VFIO_PCI_OFFSET_SHIFT 40
52#define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
53#define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
54#define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
55
56struct vfio_region {
57 u32 type;
58 u32 subtype;
59 size_t size;
60 u32 flags;
61};
62
63struct kvmgt_pgfn {
64 gfn_t gfn;
65 struct hlist_node hnode;
66};
67
68struct kvmgt_guest_info {
69 struct kvm *kvm;
70 struct intel_vgpu *vgpu;
71 struct kvm_page_track_notifier_node track_node;
72#define NR_BKT (1 << 18)
73 struct hlist_head ptable[NR_BKT];
74#undef NR_BKT
75};
76
77struct gvt_dma {
78 struct rb_node node;
79 gfn_t gfn;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +080080 unsigned long iova;
Jike Songf30437c2016-11-09 20:30:59 +080081};
82
Jike Song659643f2016-12-08 11:00:36 +080083static inline bool handle_valid(unsigned long handle)
84{
85 return !!(handle & ~0xff);
86}
87
88static int kvmgt_guest_init(struct mdev_device *mdev);
89static void intel_vgpu_release_work(struct work_struct *work);
90static bool kvmgt_guest_exit(struct kvmgt_guest_info *info);
91
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +080092static int gvt_dma_map_iova(struct intel_vgpu *vgpu, kvm_pfn_t pfn,
93 unsigned long *iova)
94{
95 struct page *page;
96 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
97 dma_addr_t daddr;
98
Chuanxiao Dongb6b6fbc2017-03-01 14:34:52 +080099 if (unlikely(!pfn_valid(pfn)))
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800100 return -EFAULT;
101
Chuanxiao Dongb6b6fbc2017-03-01 14:34:52 +0800102 page = pfn_to_page(pfn);
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800103 daddr = dma_map_page(dev, page, 0, PAGE_SIZE,
104 PCI_DMA_BIDIRECTIONAL);
105 if (dma_mapping_error(dev, daddr))
106 return -ENOMEM;
107
108 *iova = (unsigned long)(daddr >> PAGE_SHIFT);
109 return 0;
110}
111
112static void gvt_dma_unmap_iova(struct intel_vgpu *vgpu, unsigned long iova)
113{
114 struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev;
115 dma_addr_t daddr;
116
117 daddr = (dma_addr_t)(iova << PAGE_SHIFT);
118 dma_unmap_page(dev, daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
119}
120
Jike Songf30437c2016-11-09 20:30:59 +0800121static struct gvt_dma *__gvt_cache_find(struct intel_vgpu *vgpu, gfn_t gfn)
122{
123 struct rb_node *node = vgpu->vdev.cache.rb_node;
124 struct gvt_dma *ret = NULL;
125
126 while (node) {
127 struct gvt_dma *itr = rb_entry(node, struct gvt_dma, node);
128
129 if (gfn < itr->gfn)
130 node = node->rb_left;
131 else if (gfn > itr->gfn)
132 node = node->rb_right;
133 else {
134 ret = itr;
135 goto out;
136 }
137 }
138
139out:
140 return ret;
141}
142
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800143static unsigned long gvt_cache_find(struct intel_vgpu *vgpu, gfn_t gfn)
Jike Songf30437c2016-11-09 20:30:59 +0800144{
145 struct gvt_dma *entry;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800146 unsigned long iova;
Jike Songf30437c2016-11-09 20:30:59 +0800147
148 mutex_lock(&vgpu->vdev.cache_lock);
Jike Songf30437c2016-11-09 20:30:59 +0800149
Jike Songbfeca3e2016-12-16 10:51:04 +0800150 entry = __gvt_cache_find(vgpu, gfn);
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800151 iova = (entry == NULL) ? INTEL_GVT_INVALID_ADDR : entry->iova;
Jike Songbfeca3e2016-12-16 10:51:04 +0800152
153 mutex_unlock(&vgpu->vdev.cache_lock);
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800154 return iova;
Jike Songf30437c2016-11-09 20:30:59 +0800155}
156
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800157static void gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
158 unsigned long iova)
Jike Songf30437c2016-11-09 20:30:59 +0800159{
160 struct gvt_dma *new, *itr;
161 struct rb_node **link = &vgpu->vdev.cache.rb_node, *parent = NULL;
162
163 new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
164 if (!new)
165 return;
166
167 new->gfn = gfn;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800168 new->iova = iova;
Jike Songf30437c2016-11-09 20:30:59 +0800169
170 mutex_lock(&vgpu->vdev.cache_lock);
171 while (*link) {
172 parent = *link;
173 itr = rb_entry(parent, struct gvt_dma, node);
174
175 if (gfn == itr->gfn)
176 goto out;
177 else if (gfn < itr->gfn)
178 link = &parent->rb_left;
179 else
180 link = &parent->rb_right;
181 }
182
183 rb_link_node(&new->node, parent, link);
184 rb_insert_color(&new->node, &vgpu->vdev.cache);
185 mutex_unlock(&vgpu->vdev.cache_lock);
186 return;
187
188out:
189 mutex_unlock(&vgpu->vdev.cache_lock);
190 kfree(new);
191}
192
193static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
194 struct gvt_dma *entry)
195{
196 rb_erase(&entry->node, &vgpu->vdev.cache);
197 kfree(entry);
198}
199
Chuanxiao Dong08673c32017-07-07 13:21:52 +0800200static void gvt_cache_remove(struct intel_vgpu *vgpu, gfn_t gfn)
Jike Songf30437c2016-11-09 20:30:59 +0800201{
Alex Williamson99e31232016-12-30 08:13:44 -0700202 struct device *dev = mdev_dev(vgpu->vdev.mdev);
Jike Songf30437c2016-11-09 20:30:59 +0800203 struct gvt_dma *this;
Chuanxiao Dong08673c32017-07-07 13:21:52 +0800204 unsigned long g1;
205 int rc;
Jike Songf30437c2016-11-09 20:30:59 +0800206
207 mutex_lock(&vgpu->vdev.cache_lock);
208 this = __gvt_cache_find(vgpu, gfn);
209 if (!this) {
210 mutex_unlock(&vgpu->vdev.cache_lock);
Chuanxiao Dong08673c32017-07-07 13:21:52 +0800211 return;
Jike Songf30437c2016-11-09 20:30:59 +0800212 }
Chuanxiao Dong08673c32017-07-07 13:21:52 +0800213
214 g1 = gfn;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800215 gvt_dma_unmap_iova(vgpu, this->iova);
Chuanxiao Dong08673c32017-07-07 13:21:52 +0800216 rc = vfio_unpin_pages(dev, &g1, 1);
217 WARN_ON(rc != 1);
218 __gvt_cache_remove_entry(vgpu, this);
Jike Songf30437c2016-11-09 20:30:59 +0800219 mutex_unlock(&vgpu->vdev.cache_lock);
220}
221
222static void gvt_cache_init(struct intel_vgpu *vgpu)
223{
224 vgpu->vdev.cache = RB_ROOT;
225 mutex_init(&vgpu->vdev.cache_lock);
226}
227
228static void gvt_cache_destroy(struct intel_vgpu *vgpu)
229{
230 struct gvt_dma *dma;
231 struct rb_node *node = NULL;
Alex Williamson99e31232016-12-30 08:13:44 -0700232 struct device *dev = mdev_dev(vgpu->vdev.mdev);
Jike Song659643f2016-12-08 11:00:36 +0800233 unsigned long gfn;
Jike Songf30437c2016-11-09 20:30:59 +0800234
Chuanxiao Dongf16bd3d2017-06-26 15:20:50 +0800235 for (;;) {
236 mutex_lock(&vgpu->vdev.cache_lock);
237 node = rb_first(&vgpu->vdev.cache);
238 if (!node) {
239 mutex_unlock(&vgpu->vdev.cache_lock);
240 break;
241 }
Jike Songf30437c2016-11-09 20:30:59 +0800242 dma = rb_entry(node, struct gvt_dma, node);
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +0800243 gvt_dma_unmap_iova(vgpu, dma->iova);
Jike Song659643f2016-12-08 11:00:36 +0800244 gfn = dma->gfn;
Jike Songf30437c2016-11-09 20:30:59 +0800245 __gvt_cache_remove_entry(vgpu, dma);
Chuanxiao Dongf16bd3d2017-06-26 15:20:50 +0800246 mutex_unlock(&vgpu->vdev.cache_lock);
247 vfio_unpin_pages(dev, &gfn, 1);
Jike Songf30437c2016-11-09 20:30:59 +0800248 }
Jike Songf30437c2016-11-09 20:30:59 +0800249}
250
251static struct intel_vgpu_type *intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
252 const char *name)
253{
254 int i;
255 struct intel_vgpu_type *t;
256 const char *driver_name = dev_driver_string(
257 &gvt->dev_priv->drm.pdev->dev);
258
259 for (i = 0; i < gvt->num_types; i++) {
260 t = &gvt->types[i];
261 if (!strncmp(t->name, name + strlen(driver_name) + 1,
262 sizeof(t->name)))
263 return t;
264 }
265
266 return NULL;
267}
268
Alex Williamsonbdbfd512017-01-24 12:53:45 -0700269static ssize_t available_instances_show(struct kobject *kobj,
270 struct device *dev, char *buf)
Jike Song659643f2016-12-08 11:00:36 +0800271{
272 struct intel_vgpu_type *type;
273 unsigned int num = 0;
274 void *gvt = kdev_to_i915(dev)->gvt;
275
276 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
277 if (!type)
278 num = 0;
279 else
280 num = type->avail_instance;
281
282 return sprintf(buf, "%u\n", num);
283}
284
285static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
286 char *buf)
287{
288 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
289}
290
291static ssize_t description_show(struct kobject *kobj, struct device *dev,
292 char *buf)
293{
294 struct intel_vgpu_type *type;
295 void *gvt = kdev_to_i915(dev)->gvt;
296
297 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
298 if (!type)
299 return 0;
300
301 return sprintf(buf, "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
Ping Gaobc90d092017-03-30 00:36:37 +0800302 "fence: %d\nresolution: %s\n"
303 "weight: %d\n",
Zhenyu Wangd1a513b2017-02-24 10:58:21 +0800304 BYTES_TO_MB(type->low_gm_size),
305 BYTES_TO_MB(type->high_gm_size),
Ping Gaobc90d092017-03-30 00:36:37 +0800306 type->fence, vgpu_edid_str(type->resolution),
307 type->weight);
Jike Song659643f2016-12-08 11:00:36 +0800308}
309
Alex Williamsonbdbfd512017-01-24 12:53:45 -0700310static MDEV_TYPE_ATTR_RO(available_instances);
Jike Song659643f2016-12-08 11:00:36 +0800311static MDEV_TYPE_ATTR_RO(device_api);
312static MDEV_TYPE_ATTR_RO(description);
313
Jike Songf30437c2016-11-09 20:30:59 +0800314static struct attribute *type_attrs[] = {
Alex Williamsonbdbfd512017-01-24 12:53:45 -0700315 &mdev_type_attr_available_instances.attr,
Jike Song659643f2016-12-08 11:00:36 +0800316 &mdev_type_attr_device_api.attr,
317 &mdev_type_attr_description.attr,
Jike Songf30437c2016-11-09 20:30:59 +0800318 NULL,
319};
320
321static struct attribute_group *intel_vgpu_type_groups[] = {
322 [0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
323};
324
325static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
326{
327 int i, j;
328 struct intel_vgpu_type *type;
329 struct attribute_group *group;
330
331 for (i = 0; i < gvt->num_types; i++) {
332 type = &gvt->types[i];
333
334 group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
335 if (WARN_ON(!group))
336 goto unwind;
337
338 group->name = type->name;
339 group->attrs = type_attrs;
340 intel_vgpu_type_groups[i] = group;
341 }
342
343 return true;
344
345unwind:
346 for (j = 0; j < i; j++) {
347 group = intel_vgpu_type_groups[j];
348 kfree(group);
349 }
350
351 return false;
352}
353
354static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
355{
356 int i;
357 struct attribute_group *group;
358
359 for (i = 0; i < gvt->num_types; i++) {
360 group = intel_vgpu_type_groups[i];
361 kfree(group);
362 }
363}
364
365static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
366{
367 hash_init(info->ptable);
368}
369
370static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
371{
372 struct kvmgt_pgfn *p;
373 struct hlist_node *tmp;
374 int i;
375
376 hash_for_each_safe(info->ptable, i, tmp, p, hnode) {
377 hash_del(&p->hnode);
378 kfree(p);
379 }
380}
381
382static struct kvmgt_pgfn *
383__kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
384{
385 struct kvmgt_pgfn *p, *res = NULL;
386
387 hash_for_each_possible(info->ptable, p, hnode, gfn) {
388 if (gfn == p->gfn) {
389 res = p;
390 break;
391 }
392 }
393
394 return res;
395}
396
397static bool kvmgt_gfn_is_write_protected(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 return !!p;
404}
405
406static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
407{
408 struct kvmgt_pgfn *p;
409
410 if (kvmgt_gfn_is_write_protected(info, gfn))
411 return;
412
Jike Songc55b1de2016-12-08 11:00:34 +0800413 p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC);
Jike Songf30437c2016-11-09 20:30:59 +0800414 if (WARN(!p, "gfn: 0x%llx\n", gfn))
415 return;
416
417 p->gfn = gfn;
418 hash_add(info->ptable, &p->hnode, gfn);
419}
420
421static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
422 gfn_t gfn)
423{
424 struct kvmgt_pgfn *p;
425
426 p = __kvmgt_protect_table_find(info, gfn);
427 if (p) {
428 hash_del(&p->hnode);
429 kfree(p);
430 }
431}
432
Jike Song659643f2016-12-08 11:00:36 +0800433static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
434{
Tina Zhang695fbc02017-03-10 04:26:53 -0500435 struct intel_vgpu *vgpu = NULL;
Jike Song659643f2016-12-08 11:00:36 +0800436 struct intel_vgpu_type *type;
437 struct device *pdev;
438 void *gvt;
Jike Song57533942017-01-06 15:16:20 +0800439 int ret;
Jike Song659643f2016-12-08 11:00:36 +0800440
Alex Williamson9372e6fe2016-12-30 08:13:41 -0700441 pdev = mdev_parent_dev(mdev);
Jike Song659643f2016-12-08 11:00:36 +0800442 gvt = kdev_to_i915(pdev)->gvt;
443
444 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
445 if (!type) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500446 gvt_vgpu_err("failed to find type %s to create\n",
Jike Song659643f2016-12-08 11:00:36 +0800447 kobject_name(kobj));
Jike Song57533942017-01-06 15:16:20 +0800448 ret = -EINVAL;
449 goto out;
Jike Song659643f2016-12-08 11:00:36 +0800450 }
451
452 vgpu = intel_gvt_ops->vgpu_create(gvt, type);
453 if (IS_ERR_OR_NULL(vgpu)) {
Jike Song57533942017-01-06 15:16:20 +0800454 ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu);
Tina Zhang695fbc02017-03-10 04:26:53 -0500455 gvt_vgpu_err("failed to create intel vgpu: %d\n", ret);
Jike Song57533942017-01-06 15:16:20 +0800456 goto out;
Jike Song659643f2016-12-08 11:00:36 +0800457 }
458
459 INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work);
460
461 vgpu->vdev.mdev = mdev;
462 mdev_set_drvdata(mdev, vgpu);
463
464 gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
Alex Williamson99e31232016-12-30 08:13:44 -0700465 dev_name(mdev_dev(mdev)));
Jike Song57533942017-01-06 15:16:20 +0800466 ret = 0;
467
468out:
469 return ret;
Jike Song659643f2016-12-08 11:00:36 +0800470}
471
472static int intel_vgpu_remove(struct mdev_device *mdev)
473{
474 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
475
476 if (handle_valid(vgpu->handle))
477 return -EBUSY;
478
479 intel_gvt_ops->vgpu_destroy(vgpu);
480 return 0;
481}
482
483static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
484 unsigned long action, void *data)
485{
486 struct intel_vgpu *vgpu = container_of(nb,
487 struct intel_vgpu,
488 vdev.iommu_notifier);
489
490 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
491 struct vfio_iommu_type1_dma_unmap *unmap = data;
492 unsigned long gfn, end_gfn;
493
494 gfn = unmap->iova >> PAGE_SHIFT;
495 end_gfn = gfn + unmap->size / PAGE_SIZE;
496
497 while (gfn < end_gfn)
Chuanxiao Dong08673c32017-07-07 13:21:52 +0800498 gvt_cache_remove(vgpu, gfn++);
Jike Song659643f2016-12-08 11:00:36 +0800499 }
500
501 return NOTIFY_OK;
502}
503
504static int intel_vgpu_group_notifier(struct notifier_block *nb,
505 unsigned long action, void *data)
506{
507 struct intel_vgpu *vgpu = container_of(nb,
508 struct intel_vgpu,
509 vdev.group_notifier);
510
511 /* the only action we care about */
512 if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
513 vgpu->vdev.kvm = data;
514
515 if (!data)
516 schedule_work(&vgpu->vdev.release_work);
517 }
518
519 return NOTIFY_OK;
520}
521
522static int intel_vgpu_open(struct mdev_device *mdev)
523{
524 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
525 unsigned long events;
526 int ret;
527
528 vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
529 vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier;
530
531 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
Alex Williamson99e31232016-12-30 08:13:44 -0700532 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events,
Jike Song659643f2016-12-08 11:00:36 +0800533 &vgpu->vdev.iommu_notifier);
534 if (ret != 0) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500535 gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
536 ret);
Jike Song659643f2016-12-08 11:00:36 +0800537 goto out;
538 }
539
540 events = VFIO_GROUP_NOTIFY_SET_KVM;
Alex Williamson99e31232016-12-30 08:13:44 -0700541 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events,
Jike Song659643f2016-12-08 11:00:36 +0800542 &vgpu->vdev.group_notifier);
543 if (ret != 0) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500544 gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
545 ret);
Jike Song659643f2016-12-08 11:00:36 +0800546 goto undo_iommu;
547 }
548
Jike Song364fb6b2016-12-16 10:51:06 +0800549 ret = kvmgt_guest_init(mdev);
550 if (ret)
551 goto undo_group;
552
Zhi Wangb79c52a2017-03-30 01:48:39 +0800553 intel_gvt_ops->vgpu_activate(vgpu);
554
Jike Song364fb6b2016-12-16 10:51:06 +0800555 atomic_set(&vgpu->vdev.released, 0);
556 return ret;
557
558undo_group:
Linus Torvalds5824f922017-01-06 11:19:03 -0800559 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
Jike Song364fb6b2016-12-16 10:51:06 +0800560 &vgpu->vdev.group_notifier);
Jike Song659643f2016-12-08 11:00:36 +0800561
562undo_iommu:
Alex Williamson99e31232016-12-30 08:13:44 -0700563 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Jike Song659643f2016-12-08 11:00:36 +0800564 &vgpu->vdev.iommu_notifier);
565out:
566 return ret;
567}
568
569static void __intel_vgpu_release(struct intel_vgpu *vgpu)
570{
571 struct kvmgt_guest_info *info;
Jike Song364fb6b2016-12-16 10:51:06 +0800572 int ret;
Jike Song659643f2016-12-08 11:00:36 +0800573
574 if (!handle_valid(vgpu->handle))
575 return;
576
Jike Song364fb6b2016-12-16 10:51:06 +0800577 if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1))
578 return;
579
Zhi Wangb79c52a2017-03-30 01:48:39 +0800580 intel_gvt_ops->vgpu_deactivate(vgpu);
581
Linus Torvalds5824f922017-01-06 11:19:03 -0800582 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY,
Jike Song659643f2016-12-08 11:00:36 +0800583 &vgpu->vdev.iommu_notifier);
Jike Song364fb6b2016-12-16 10:51:06 +0800584 WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret);
585
Linus Torvalds5824f922017-01-06 11:19:03 -0800586 ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY,
Jike Song659643f2016-12-08 11:00:36 +0800587 &vgpu->vdev.group_notifier);
Jike Song364fb6b2016-12-16 10:51:06 +0800588 WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret);
Jike Song659643f2016-12-08 11:00:36 +0800589
590 info = (struct kvmgt_guest_info *)vgpu->handle;
591 kvmgt_guest_exit(info);
Jike Song364fb6b2016-12-16 10:51:06 +0800592
593 vgpu->vdev.kvm = NULL;
Jike Song659643f2016-12-08 11:00:36 +0800594 vgpu->handle = 0;
595}
596
597static void intel_vgpu_release(struct mdev_device *mdev)
598{
599 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
600
601 __intel_vgpu_release(vgpu);
602}
603
604static void intel_vgpu_release_work(struct work_struct *work)
605{
606 struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu,
607 vdev.release_work);
Jike Song8ff842f2016-12-16 10:51:07 +0800608
Jike Song659643f2016-12-08 11:00:36 +0800609 __intel_vgpu_release(vgpu);
610}
611
Changbin Duf090a002017-08-15 13:14:04 +0800612static uint64_t intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)
Jike Song659643f2016-12-08 11:00:36 +0800613{
614 u32 start_lo, start_hi;
615 u32 mem_type;
Jike Song659643f2016-12-08 11:00:36 +0800616
Changbin Duf090a002017-08-15 13:14:04 +0800617 start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
Jike Song659643f2016-12-08 11:00:36 +0800618 PCI_BASE_ADDRESS_MEM_MASK;
Changbin Duf090a002017-08-15 13:14:04 +0800619 mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
Jike Song659643f2016-12-08 11:00:36 +0800620 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
621
622 switch (mem_type) {
623 case PCI_BASE_ADDRESS_MEM_TYPE_64:
624 start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space
Changbin Duf090a002017-08-15 13:14:04 +0800625 + bar + 4));
Jike Song659643f2016-12-08 11:00:36 +0800626 break;
627 case PCI_BASE_ADDRESS_MEM_TYPE_32:
628 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
629 /* 1M mem BAR treated as 32-bit BAR */
630 default:
631 /* mem unknown type treated as 32-bit BAR */
632 start_hi = 0;
633 break;
634 }
635
636 return ((u64)start_hi << 32) | start_lo;
637}
638
Changbin Duf090a002017-08-15 13:14:04 +0800639static int intel_vgpu_bar_rw(struct intel_vgpu *vgpu, int bar, uint64_t off,
640 void *buf, unsigned int count, bool is_write)
641{
642 uint64_t bar_start = intel_vgpu_get_bar_addr(vgpu, bar);
643 int ret;
644
645 if (is_write)
646 ret = intel_gvt_ops->emulate_mmio_write(vgpu,
647 bar_start + off, buf, count);
648 else
649 ret = intel_gvt_ops->emulate_mmio_read(vgpu,
650 bar_start + off, buf, count);
651 return ret;
652}
653
Jike Song659643f2016-12-08 11:00:36 +0800654static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
655 size_t count, loff_t *ppos, bool is_write)
656{
657 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
658 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
659 uint64_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
660 int ret = -EINVAL;
661
662
663 if (index >= VFIO_PCI_NUM_REGIONS) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500664 gvt_vgpu_err("invalid index: %u\n", index);
Jike Song659643f2016-12-08 11:00:36 +0800665 return -EINVAL;
666 }
667
668 switch (index) {
669 case VFIO_PCI_CONFIG_REGION_INDEX:
670 if (is_write)
671 ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos,
672 buf, count);
673 else
674 ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos,
675 buf, count);
676 break;
677 case VFIO_PCI_BAR0_REGION_INDEX:
Changbin Duf090a002017-08-15 13:14:04 +0800678 ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_0, pos,
679 buf, count, is_write);
680 break;
681 case VFIO_PCI_BAR2_REGION_INDEX:
682 ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_2, pos,
683 buf, count, is_write);
Jike Song659643f2016-12-08 11:00:36 +0800684 break;
Changbin Du5d5fe172017-08-15 13:20:51 +0800685 case VFIO_PCI_BAR1_REGION_INDEX:
Jike Song659643f2016-12-08 11:00:36 +0800686 case VFIO_PCI_BAR3_REGION_INDEX:
687 case VFIO_PCI_BAR4_REGION_INDEX:
688 case VFIO_PCI_BAR5_REGION_INDEX:
689 case VFIO_PCI_VGA_REGION_INDEX:
690 case VFIO_PCI_ROM_REGION_INDEX:
691 default:
Tina Zhang695fbc02017-03-10 04:26:53 -0500692 gvt_vgpu_err("unsupported region: %u\n", index);
Jike Song659643f2016-12-08 11:00:36 +0800693 }
694
695 return ret == 0 ? count : ret;
696}
697
698static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
699 size_t count, loff_t *ppos)
700{
701 unsigned int done = 0;
702 int ret;
703
704 while (count) {
705 size_t filled;
706
707 if (count >= 4 && !(*ppos % 4)) {
708 u32 val;
709
710 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
711 ppos, false);
712 if (ret <= 0)
713 goto read_err;
714
715 if (copy_to_user(buf, &val, sizeof(val)))
716 goto read_err;
717
718 filled = 4;
719 } else if (count >= 2 && !(*ppos % 2)) {
720 u16 val;
721
722 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
723 ppos, false);
724 if (ret <= 0)
725 goto read_err;
726
727 if (copy_to_user(buf, &val, sizeof(val)))
728 goto read_err;
729
730 filled = 2;
731 } else {
732 u8 val;
733
734 ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos,
735 false);
736 if (ret <= 0)
737 goto read_err;
738
739 if (copy_to_user(buf, &val, sizeof(val)))
740 goto read_err;
741
742 filled = 1;
743 }
744
745 count -= filled;
746 done += filled;
747 *ppos += filled;
748 buf += filled;
749 }
750
751 return done;
752
753read_err:
754 return -EFAULT;
755}
756
757static ssize_t intel_vgpu_write(struct mdev_device *mdev,
758 const char __user *buf,
759 size_t count, loff_t *ppos)
760{
761 unsigned int done = 0;
762 int ret;
763
764 while (count) {
765 size_t filled;
766
767 if (count >= 4 && !(*ppos % 4)) {
768 u32 val;
769
770 if (copy_from_user(&val, buf, sizeof(val)))
771 goto write_err;
772
773 ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val),
774 ppos, true);
775 if (ret <= 0)
776 goto write_err;
777
778 filled = 4;
779 } else if (count >= 2 && !(*ppos % 2)) {
780 u16 val;
781
782 if (copy_from_user(&val, buf, sizeof(val)))
783 goto write_err;
784
785 ret = intel_vgpu_rw(mdev, (char *)&val,
786 sizeof(val), ppos, true);
787 if (ret <= 0)
788 goto write_err;
789
790 filled = 2;
791 } else {
792 u8 val;
793
794 if (copy_from_user(&val, buf, sizeof(val)))
795 goto write_err;
796
797 ret = intel_vgpu_rw(mdev, &val, sizeof(val),
798 ppos, true);
799 if (ret <= 0)
800 goto write_err;
801
802 filled = 1;
803 }
804
805 count -= filled;
806 done += filled;
807 *ppos += filled;
808 buf += filled;
809 }
810
811 return done;
812write_err:
813 return -EFAULT;
814}
815
816static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
817{
818 unsigned int index;
819 u64 virtaddr;
820 unsigned long req_size, pgoff = 0;
821 pgprot_t pg_prot;
822 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
823
824 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
825 if (index >= VFIO_PCI_ROM_REGION_INDEX)
826 return -EINVAL;
827
828 if (vma->vm_end < vma->vm_start)
829 return -EINVAL;
830 if ((vma->vm_flags & VM_SHARED) == 0)
831 return -EINVAL;
832 if (index != VFIO_PCI_BAR2_REGION_INDEX)
833 return -EINVAL;
834
835 pg_prot = vma->vm_page_prot;
836 virtaddr = vma->vm_start;
837 req_size = vma->vm_end - vma->vm_start;
838 pgoff = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;
839
840 return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
841}
842
843static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type)
844{
845 if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX)
846 return 1;
847
848 return 0;
849}
850
851static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu,
852 unsigned int index, unsigned int start,
853 unsigned int count, uint32_t flags,
854 void *data)
855{
856 return 0;
857}
858
859static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu,
860 unsigned int index, unsigned int start,
861 unsigned int count, uint32_t flags, void *data)
862{
863 return 0;
864}
865
866static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu,
867 unsigned int index, unsigned int start, unsigned int count,
868 uint32_t flags, void *data)
869{
870 return 0;
871}
872
873static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu,
874 unsigned int index, unsigned int start, unsigned int count,
875 uint32_t flags, void *data)
876{
877 struct eventfd_ctx *trigger;
878
879 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
880 int fd = *(int *)data;
881
882 trigger = eventfd_ctx_fdget(fd);
883 if (IS_ERR(trigger)) {
Tina Zhang695fbc02017-03-10 04:26:53 -0500884 gvt_vgpu_err("eventfd_ctx_fdget failed\n");
Jike Song659643f2016-12-08 11:00:36 +0800885 return PTR_ERR(trigger);
886 }
887 vgpu->vdev.msi_trigger = trigger;
888 }
889
890 return 0;
891}
892
893static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, uint32_t flags,
894 unsigned int index, unsigned int start, unsigned int count,
895 void *data)
896{
897 int (*func)(struct intel_vgpu *vgpu, unsigned int index,
898 unsigned int start, unsigned int count, uint32_t flags,
899 void *data) = NULL;
900
901 switch (index) {
902 case VFIO_PCI_INTX_IRQ_INDEX:
903 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
904 case VFIO_IRQ_SET_ACTION_MASK:
905 func = intel_vgpu_set_intx_mask;
906 break;
907 case VFIO_IRQ_SET_ACTION_UNMASK:
908 func = intel_vgpu_set_intx_unmask;
909 break;
910 case VFIO_IRQ_SET_ACTION_TRIGGER:
911 func = intel_vgpu_set_intx_trigger;
912 break;
913 }
914 break;
915 case VFIO_PCI_MSI_IRQ_INDEX:
916 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
917 case VFIO_IRQ_SET_ACTION_MASK:
918 case VFIO_IRQ_SET_ACTION_UNMASK:
919 /* XXX Need masking support exported */
920 break;
921 case VFIO_IRQ_SET_ACTION_TRIGGER:
922 func = intel_vgpu_set_msi_trigger;
923 break;
924 }
925 break;
926 }
927
928 if (!func)
929 return -ENOTTY;
930
931 return func(vgpu, index, start, count, flags, data);
932}
933
934static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd,
935 unsigned long arg)
936{
937 struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
938 unsigned long minsz;
939
940 gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd);
941
942 if (cmd == VFIO_DEVICE_GET_INFO) {
943 struct vfio_device_info info;
944
945 minsz = offsetofend(struct vfio_device_info, num_irqs);
946
947 if (copy_from_user(&info, (void __user *)arg, minsz))
948 return -EFAULT;
949
950 if (info.argsz < minsz)
951 return -EINVAL;
952
953 info.flags = VFIO_DEVICE_FLAGS_PCI;
954 info.flags |= VFIO_DEVICE_FLAGS_RESET;
955 info.num_regions = VFIO_PCI_NUM_REGIONS;
956 info.num_irqs = VFIO_PCI_NUM_IRQS;
957
958 return copy_to_user((void __user *)arg, &info, minsz) ?
959 -EFAULT : 0;
960
961 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
962 struct vfio_region_info info;
963 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
964 int i, ret;
965 struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
966 size_t size;
967 int nr_areas = 1;
968 int cap_type_id;
969
970 minsz = offsetofend(struct vfio_region_info, offset);
971
972 if (copy_from_user(&info, (void __user *)arg, minsz))
973 return -EFAULT;
974
975 if (info.argsz < minsz)
976 return -EINVAL;
977
978 switch (info.index) {
979 case VFIO_PCI_CONFIG_REGION_INDEX:
980 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
981 info.size = INTEL_GVT_MAX_CFG_SPACE_SZ;
982 info.flags = VFIO_REGION_INFO_FLAG_READ |
983 VFIO_REGION_INFO_FLAG_WRITE;
984 break;
985 case VFIO_PCI_BAR0_REGION_INDEX:
986 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
987 info.size = vgpu->cfg_space.bar[info.index].size;
988 if (!info.size) {
989 info.flags = 0;
990 break;
991 }
992
993 info.flags = VFIO_REGION_INFO_FLAG_READ |
994 VFIO_REGION_INFO_FLAG_WRITE;
995 break;
996 case VFIO_PCI_BAR1_REGION_INDEX:
997 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
998 info.size = 0;
999 info.flags = 0;
1000 break;
1001 case VFIO_PCI_BAR2_REGION_INDEX:
1002 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1003 info.flags = VFIO_REGION_INFO_FLAG_CAPS |
1004 VFIO_REGION_INFO_FLAG_MMAP |
1005 VFIO_REGION_INFO_FLAG_READ |
1006 VFIO_REGION_INFO_FLAG_WRITE;
1007 info.size = gvt_aperture_sz(vgpu->gvt);
1008
1009 size = sizeof(*sparse) +
1010 (nr_areas * sizeof(*sparse->areas));
1011 sparse = kzalloc(size, GFP_KERNEL);
1012 if (!sparse)
1013 return -ENOMEM;
1014
1015 sparse->nr_areas = nr_areas;
1016 cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP;
1017 sparse->areas[0].offset =
1018 PAGE_ALIGN(vgpu_aperture_offset(vgpu));
1019 sparse->areas[0].size = vgpu_aperture_sz(vgpu);
Jike Song659643f2016-12-08 11:00:36 +08001020 break;
1021
1022 case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1023 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1024 info.size = 0;
1025
1026 info.flags = 0;
1027 gvt_dbg_core("get region info bar:%d\n", info.index);
1028 break;
1029
1030 case VFIO_PCI_ROM_REGION_INDEX:
1031 case VFIO_PCI_VGA_REGION_INDEX:
1032 gvt_dbg_core("get region info index:%d\n", info.index);
1033 break;
1034 default:
1035 {
1036 struct vfio_region_info_cap_type cap_type;
1037
1038 if (info.index >= VFIO_PCI_NUM_REGIONS +
1039 vgpu->vdev.num_regions)
1040 return -EINVAL;
1041
1042 i = info.index - VFIO_PCI_NUM_REGIONS;
1043
1044 info.offset =
1045 VFIO_PCI_INDEX_TO_OFFSET(info.index);
1046 info.size = vgpu->vdev.region[i].size;
1047 info.flags = vgpu->vdev.region[i].flags;
1048
1049 cap_type.type = vgpu->vdev.region[i].type;
1050 cap_type.subtype = vgpu->vdev.region[i].subtype;
1051
1052 ret = vfio_info_add_capability(&caps,
1053 VFIO_REGION_INFO_CAP_TYPE,
1054 &cap_type);
1055 if (ret)
1056 return ret;
1057 }
1058 }
1059
1060 if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) {
1061 switch (cap_type_id) {
1062 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1063 ret = vfio_info_add_capability(&caps,
1064 VFIO_REGION_INFO_CAP_SPARSE_MMAP,
1065 sparse);
1066 kfree(sparse);
1067 if (ret)
1068 return ret;
1069 break;
1070 default:
1071 return -EINVAL;
1072 }
1073 }
1074
1075 if (caps.size) {
1076 if (info.argsz < sizeof(info) + caps.size) {
1077 info.argsz = sizeof(info) + caps.size;
1078 info.cap_offset = 0;
1079 } else {
1080 vfio_info_cap_shift(&caps, sizeof(info));
1081 if (copy_to_user((void __user *)arg +
1082 sizeof(info), caps.buf,
1083 caps.size)) {
1084 kfree(caps.buf);
1085 return -EFAULT;
1086 }
1087 info.cap_offset = sizeof(info);
1088 }
1089
1090 kfree(caps.buf);
1091 }
1092
1093 return copy_to_user((void __user *)arg, &info, minsz) ?
1094 -EFAULT : 0;
1095 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
1096 struct vfio_irq_info info;
1097
1098 minsz = offsetofend(struct vfio_irq_info, count);
1099
1100 if (copy_from_user(&info, (void __user *)arg, minsz))
1101 return -EFAULT;
1102
1103 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
1104 return -EINVAL;
1105
1106 switch (info.index) {
1107 case VFIO_PCI_INTX_IRQ_INDEX:
1108 case VFIO_PCI_MSI_IRQ_INDEX:
1109 break;
1110 default:
1111 return -EINVAL;
1112 }
1113
1114 info.flags = VFIO_IRQ_INFO_EVENTFD;
1115
1116 info.count = intel_vgpu_get_irq_count(vgpu, info.index);
1117
1118 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
1119 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
1120 VFIO_IRQ_INFO_AUTOMASKED);
1121 else
1122 info.flags |= VFIO_IRQ_INFO_NORESIZE;
1123
1124 return copy_to_user((void __user *)arg, &info, minsz) ?
1125 -EFAULT : 0;
1126 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
1127 struct vfio_irq_set hdr;
1128 u8 *data = NULL;
1129 int ret = 0;
1130 size_t data_size = 0;
1131
1132 minsz = offsetofend(struct vfio_irq_set, count);
1133
1134 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1135 return -EFAULT;
1136
1137 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
1138 int max = intel_vgpu_get_irq_count(vgpu, hdr.index);
1139
1140 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
1141 VFIO_PCI_NUM_IRQS, &data_size);
1142 if (ret) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001143 gvt_vgpu_err("intel:vfio_set_irqs_validate_and_prepare failed\n");
Jike Song659643f2016-12-08 11:00:36 +08001144 return -EINVAL;
1145 }
1146 if (data_size) {
1147 data = memdup_user((void __user *)(arg + minsz),
1148 data_size);
1149 if (IS_ERR(data))
1150 return PTR_ERR(data);
1151 }
1152 }
1153
1154 ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index,
1155 hdr.start, hdr.count, data);
1156 kfree(data);
1157
1158 return ret;
1159 } else if (cmd == VFIO_DEVICE_RESET) {
1160 intel_gvt_ops->vgpu_reset(vgpu);
1161 return 0;
1162 }
1163
1164 return 0;
1165}
1166
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001167static ssize_t
1168vgpu_id_show(struct device *dev, struct device_attribute *attr,
1169 char *buf)
1170{
1171 struct mdev_device *mdev = mdev_from_dev(dev);
1172
1173 if (mdev) {
1174 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1175 mdev_get_drvdata(mdev);
1176 return sprintf(buf, "%d\n", vgpu->id);
1177 }
1178 return sprintf(buf, "\n");
1179}
1180
Zhenyu Wanga45050d2017-08-01 13:09:47 +08001181static ssize_t
1182hw_id_show(struct device *dev, struct device_attribute *attr,
1183 char *buf)
1184{
1185 struct mdev_device *mdev = mdev_from_dev(dev);
1186
1187 if (mdev) {
1188 struct intel_vgpu *vgpu = (struct intel_vgpu *)
1189 mdev_get_drvdata(mdev);
1190 return sprintf(buf, "%u\n",
1191 vgpu->shadow_ctx->hw_id);
1192 }
1193 return sprintf(buf, "\n");
1194}
1195
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001196static DEVICE_ATTR_RO(vgpu_id);
Zhenyu Wanga45050d2017-08-01 13:09:47 +08001197static DEVICE_ATTR_RO(hw_id);
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001198
1199static struct attribute *intel_vgpu_attrs[] = {
1200 &dev_attr_vgpu_id.attr,
Zhenyu Wanga45050d2017-08-01 13:09:47 +08001201 &dev_attr_hw_id.attr,
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001202 NULL
1203};
1204
1205static const struct attribute_group intel_vgpu_group = {
1206 .name = "intel_vgpu",
1207 .attrs = intel_vgpu_attrs,
1208};
1209
1210static const struct attribute_group *intel_vgpu_groups[] = {
1211 &intel_vgpu_group,
1212 NULL,
1213};
1214
Alex Williamson42930552016-12-30 08:13:38 -07001215static const struct mdev_parent_ops intel_vgpu_ops = {
Jike Song659643f2016-12-08 11:00:36 +08001216 .supported_type_groups = intel_vgpu_type_groups,
Zhenyu Wang7a7a6562017-03-16 18:06:39 +08001217 .mdev_attr_groups = intel_vgpu_groups,
Jike Song659643f2016-12-08 11:00:36 +08001218 .create = intel_vgpu_create,
1219 .remove = intel_vgpu_remove,
1220
1221 .open = intel_vgpu_open,
1222 .release = intel_vgpu_release,
1223
1224 .read = intel_vgpu_read,
1225 .write = intel_vgpu_write,
1226 .mmap = intel_vgpu_mmap,
1227 .ioctl = intel_vgpu_ioctl,
1228};
1229
Jike Songf30437c2016-11-09 20:30:59 +08001230static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)
1231{
1232 if (!intel_gvt_init_vgpu_type_groups(gvt))
1233 return -EFAULT;
1234
1235 intel_gvt_ops = ops;
1236
Jike Song659643f2016-12-08 11:00:36 +08001237 return mdev_register_device(dev, &intel_vgpu_ops);
Jike Songf30437c2016-11-09 20:30:59 +08001238}
1239
1240static void kvmgt_host_exit(struct device *dev, void *gvt)
1241{
1242 intel_gvt_cleanup_vgpu_type_groups(gvt);
Jike Song659643f2016-12-08 11:00:36 +08001243 mdev_unregister_device(dev);
Jike Songf30437c2016-11-09 20:30:59 +08001244}
1245
1246static int kvmgt_write_protect_add(unsigned long handle, u64 gfn)
1247{
Jike Song659643f2016-12-08 11:00:36 +08001248 struct kvmgt_guest_info *info;
1249 struct kvm *kvm;
Jike Songf30437c2016-11-09 20:30:59 +08001250 struct kvm_memory_slot *slot;
1251 int idx;
1252
Jike Song659643f2016-12-08 11:00:36 +08001253 if (!handle_valid(handle))
1254 return -ESRCH;
1255
1256 info = (struct kvmgt_guest_info *)handle;
1257 kvm = info->kvm;
1258
Jike Songf30437c2016-11-09 20:30:59 +08001259 idx = srcu_read_lock(&kvm->srcu);
1260 slot = gfn_to_memslot(kvm, gfn);
Jike Songfaaaa532016-12-16 10:51:05 +08001261 if (!slot) {
1262 srcu_read_unlock(&kvm->srcu, idx);
1263 return -EINVAL;
1264 }
Jike Songf30437c2016-11-09 20:30:59 +08001265
1266 spin_lock(&kvm->mmu_lock);
1267
1268 if (kvmgt_gfn_is_write_protected(info, gfn))
1269 goto out;
1270
1271 kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1272 kvmgt_protect_table_add(info, gfn);
1273
1274out:
1275 spin_unlock(&kvm->mmu_lock);
1276 srcu_read_unlock(&kvm->srcu, idx);
1277 return 0;
1278}
1279
1280static int kvmgt_write_protect_remove(unsigned long handle, u64 gfn)
1281{
Jike Song659643f2016-12-08 11:00:36 +08001282 struct kvmgt_guest_info *info;
1283 struct kvm *kvm;
Jike Songf30437c2016-11-09 20:30:59 +08001284 struct kvm_memory_slot *slot;
1285 int idx;
1286
Jike Song659643f2016-12-08 11:00:36 +08001287 if (!handle_valid(handle))
1288 return 0;
1289
1290 info = (struct kvmgt_guest_info *)handle;
1291 kvm = info->kvm;
1292
Jike Songf30437c2016-11-09 20:30:59 +08001293 idx = srcu_read_lock(&kvm->srcu);
1294 slot = gfn_to_memslot(kvm, gfn);
Jike Songfaaaa532016-12-16 10:51:05 +08001295 if (!slot) {
1296 srcu_read_unlock(&kvm->srcu, idx);
1297 return -EINVAL;
1298 }
Jike Songf30437c2016-11-09 20:30:59 +08001299
1300 spin_lock(&kvm->mmu_lock);
1301
1302 if (!kvmgt_gfn_is_write_protected(info, gfn))
1303 goto out;
1304
1305 kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
1306 kvmgt_protect_table_del(info, gfn);
1307
1308out:
1309 spin_unlock(&kvm->mmu_lock);
1310 srcu_read_unlock(&kvm->srcu, idx);
1311 return 0;
1312}
1313
1314static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1315 const u8 *val, int len,
1316 struct kvm_page_track_notifier_node *node)
1317{
1318 struct kvmgt_guest_info *info = container_of(node,
1319 struct kvmgt_guest_info, track_node);
1320
1321 if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
1322 intel_gvt_ops->emulate_mmio_write(info->vgpu, gpa,
1323 (void *)val, len);
1324}
1325
1326static void kvmgt_page_track_flush_slot(struct kvm *kvm,
1327 struct kvm_memory_slot *slot,
1328 struct kvm_page_track_notifier_node *node)
1329{
1330 int i;
1331 gfn_t gfn;
1332 struct kvmgt_guest_info *info = container_of(node,
1333 struct kvmgt_guest_info, track_node);
1334
1335 spin_lock(&kvm->mmu_lock);
1336 for (i = 0; i < slot->npages; i++) {
1337 gfn = slot->base_gfn + i;
1338 if (kvmgt_gfn_is_write_protected(info, gfn)) {
1339 kvm_slot_page_track_remove_page(kvm, slot, gfn,
1340 KVM_PAGE_TRACK_WRITE);
1341 kvmgt_protect_table_del(info, gfn);
1342 }
1343 }
1344 spin_unlock(&kvm->mmu_lock);
1345}
1346
Jike Song659643f2016-12-08 11:00:36 +08001347static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
1348{
1349 struct intel_vgpu *itr;
1350 struct kvmgt_guest_info *info;
1351 int id;
1352 bool ret = false;
1353
1354 mutex_lock(&vgpu->gvt->lock);
1355 for_each_active_vgpu(vgpu->gvt, itr, id) {
1356 if (!handle_valid(itr->handle))
1357 continue;
1358
1359 info = (struct kvmgt_guest_info *)itr->handle;
1360 if (kvm && kvm == info->kvm) {
1361 ret = true;
1362 goto out;
1363 }
1364 }
1365out:
1366 mutex_unlock(&vgpu->gvt->lock);
1367 return ret;
1368}
1369
1370static int kvmgt_guest_init(struct mdev_device *mdev)
1371{
1372 struct kvmgt_guest_info *info;
1373 struct intel_vgpu *vgpu;
1374 struct kvm *kvm;
1375
1376 vgpu = mdev_get_drvdata(mdev);
1377 if (handle_valid(vgpu->handle))
1378 return -EEXIST;
1379
1380 kvm = vgpu->vdev.kvm;
1381 if (!kvm || kvm->mm != current->mm) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001382 gvt_vgpu_err("KVM is required to use Intel vGPU\n");
Jike Song659643f2016-12-08 11:00:36 +08001383 return -ESRCH;
1384 }
1385
1386 if (__kvmgt_vgpu_exist(vgpu, kvm))
1387 return -EEXIST;
1388
1389 info = vzalloc(sizeof(struct kvmgt_guest_info));
1390 if (!info)
1391 return -ENOMEM;
1392
1393 vgpu->handle = (unsigned long)info;
1394 info->vgpu = vgpu;
1395 info->kvm = kvm;
Alex Williamson93a15b52017-03-19 20:38:40 -06001396 kvm_get_kvm(info->kvm);
Jike Song659643f2016-12-08 11:00:36 +08001397
1398 kvmgt_protect_table_init(info);
1399 gvt_cache_init(vgpu);
1400
1401 info->track_node.track_write = kvmgt_page_track_write;
1402 info->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
1403 kvm_page_track_register_notifier(kvm, &info->track_node);
1404
1405 return 0;
1406}
1407
1408static bool kvmgt_guest_exit(struct kvmgt_guest_info *info)
1409{
Jike Song659643f2016-12-08 11:00:36 +08001410 kvm_page_track_unregister_notifier(info->kvm, &info->track_node);
Alex Williamson93a15b52017-03-19 20:38:40 -06001411 kvm_put_kvm(info->kvm);
Jike Song659643f2016-12-08 11:00:36 +08001412 kvmgt_protect_table_destroy(info);
Jike Song8ff842f2016-12-16 10:51:07 +08001413 gvt_cache_destroy(info->vgpu);
Jike Song659643f2016-12-08 11:00:36 +08001414 vfree(info);
1415
1416 return true;
1417}
1418
Jike Songf30437c2016-11-09 20:30:59 +08001419static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle)
1420{
1421 /* nothing to do here */
1422 return 0;
1423}
1424
1425static void kvmgt_detach_vgpu(unsigned long handle)
1426{
1427 /* nothing to do here */
1428}
1429
1430static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data)
1431{
Jike Song659643f2016-12-08 11:00:36 +08001432 struct kvmgt_guest_info *info;
1433 struct intel_vgpu *vgpu;
Jike Songf30437c2016-11-09 20:30:59 +08001434
Jike Song659643f2016-12-08 11:00:36 +08001435 if (!handle_valid(handle))
1436 return -ESRCH;
Jike Songf30437c2016-11-09 20:30:59 +08001437
Jike Song659643f2016-12-08 11:00:36 +08001438 info = (struct kvmgt_guest_info *)handle;
1439 vgpu = info->vgpu;
1440
1441 if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1)
1442 return 0;
1443
1444 return -EFAULT;
Jike Songf30437c2016-11-09 20:30:59 +08001445}
1446
1447static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn)
1448{
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +08001449 unsigned long iova, pfn;
Jike Song659643f2016-12-08 11:00:36 +08001450 struct kvmgt_guest_info *info;
1451 struct device *dev;
Tina Zhang695fbc02017-03-10 04:26:53 -05001452 struct intel_vgpu *vgpu;
Jike Songf30437c2016-11-09 20:30:59 +08001453 int rc;
1454
Jike Song659643f2016-12-08 11:00:36 +08001455 if (!handle_valid(handle))
1456 return INTEL_GVT_INVALID_ADDR;
1457
1458 info = (struct kvmgt_guest_info *)handle;
Tina Zhang695fbc02017-03-10 04:26:53 -05001459 vgpu = info->vgpu;
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +08001460 iova = gvt_cache_find(info->vgpu, gfn);
1461 if (iova != INTEL_GVT_INVALID_ADDR)
1462 return iova;
Jike Songf30437c2016-11-09 20:30:59 +08001463
Jike Song659643f2016-12-08 11:00:36 +08001464 pfn = INTEL_GVT_INVALID_ADDR;
Alex Williamson99e31232016-12-30 08:13:44 -07001465 dev = mdev_dev(info->vgpu->vdev.mdev);
Jike Song659643f2016-12-08 11:00:36 +08001466 rc = vfio_pin_pages(dev, &gfn, 1, IOMMU_READ | IOMMU_WRITE, &pfn);
Jike Songf30437c2016-11-09 20:30:59 +08001467 if (rc != 1) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001468 gvt_vgpu_err("vfio_pin_pages failed for gfn 0x%lx: %d\n",
1469 gfn, rc);
Jike Song659643f2016-12-08 11:00:36 +08001470 return INTEL_GVT_INVALID_ADDR;
Jike Songf30437c2016-11-09 20:30:59 +08001471 }
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +08001472 /* transfer to host iova for GFX to use DMA */
1473 rc = gvt_dma_map_iova(info->vgpu, pfn, &iova);
Chuanxiao Dong4a0b3442017-02-14 17:15:54 +08001474 if (rc) {
Tina Zhang695fbc02017-03-10 04:26:53 -05001475 gvt_vgpu_err("gvt_dma_map_iova failed for gfn: 0x%lx\n", gfn);
Chuanxiao Dong4a0b3442017-02-14 17:15:54 +08001476 vfio_unpin_pages(dev, &gfn, 1);
1477 return INTEL_GVT_INVALID_ADDR;
1478 }
Jike Songf30437c2016-11-09 20:30:59 +08001479
Chuanxiao Dongb86dc6e2017-02-09 11:38:01 +08001480 gvt_cache_add(info->vgpu, gfn, iova);
1481 return iova;
Jike Songf30437c2016-11-09 20:30:59 +08001482}
1483
Jike Songf30437c2016-11-09 20:30:59 +08001484static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa,
1485 void *buf, unsigned long len, bool write)
1486{
Jike Songf440c8a2016-12-08 11:00:35 +08001487 struct kvmgt_guest_info *info;
1488 struct kvm *kvm;
Changbin Du5180edc2017-03-16 09:45:09 +08001489 int idx, ret;
Jike Songf440c8a2016-12-08 11:00:35 +08001490 bool kthread = current->mm == NULL;
Jike Songf30437c2016-11-09 20:30:59 +08001491
Jike Song659643f2016-12-08 11:00:36 +08001492 if (!handle_valid(handle))
1493 return -ESRCH;
1494
Jike Songf440c8a2016-12-08 11:00:35 +08001495 info = (struct kvmgt_guest_info *)handle;
1496 kvm = info->kvm;
Jike Songf30437c2016-11-09 20:30:59 +08001497
Jike Songf440c8a2016-12-08 11:00:35 +08001498 if (kthread)
1499 use_mm(kvm->mm);
Jike Songf30437c2016-11-09 20:30:59 +08001500
Changbin Du5180edc2017-03-16 09:45:09 +08001501 idx = srcu_read_lock(&kvm->srcu);
Jike Songf440c8a2016-12-08 11:00:35 +08001502 ret = write ? kvm_write_guest(kvm, gpa, buf, len) :
1503 kvm_read_guest(kvm, gpa, buf, len);
Changbin Du5180edc2017-03-16 09:45:09 +08001504 srcu_read_unlock(&kvm->srcu, idx);
Jike Songf440c8a2016-12-08 11:00:35 +08001505
1506 if (kthread)
1507 unuse_mm(kvm->mm);
1508
1509 return ret;
Jike Songf30437c2016-11-09 20:30:59 +08001510}
1511
1512static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa,
1513 void *buf, unsigned long len)
1514{
1515 return kvmgt_rw_gpa(handle, gpa, buf, len, false);
1516}
1517
1518static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa,
1519 void *buf, unsigned long len)
1520{
1521 return kvmgt_rw_gpa(handle, gpa, buf, len, true);
1522}
1523
1524static unsigned long kvmgt_virt_to_pfn(void *addr)
1525{
1526 return PFN_DOWN(__pa(addr));
1527}
1528
1529struct intel_gvt_mpt kvmgt_mpt = {
Jike Songf30437c2016-11-09 20:30:59 +08001530 .host_init = kvmgt_host_init,
1531 .host_exit = kvmgt_host_exit,
1532 .attach_vgpu = kvmgt_attach_vgpu,
1533 .detach_vgpu = kvmgt_detach_vgpu,
1534 .inject_msi = kvmgt_inject_msi,
1535 .from_virt_to_mfn = kvmgt_virt_to_pfn,
1536 .set_wp_page = kvmgt_write_protect_add,
1537 .unset_wp_page = kvmgt_write_protect_remove,
1538 .read_gpa = kvmgt_read_gpa,
1539 .write_gpa = kvmgt_write_gpa,
1540 .gfn_to_mfn = kvmgt_gfn_to_pfn,
1541};
1542EXPORT_SYMBOL_GPL(kvmgt_mpt);
1543
1544static int __init kvmgt_init(void)
1545{
1546 return 0;
1547}
1548
1549static void __exit kvmgt_exit(void)
1550{
1551}
1552
1553module_init(kvmgt_init);
1554module_exit(kvmgt_exit);
1555
1556MODULE_LICENSE("GPL and additional rights");
1557MODULE_AUTHOR("Intel Corporation");