1 1.1 riastrad /* $NetBSD: kvmgt.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * KVMGT - the implementation of Intel mediated pass-through framework for KVM 5 1.1 riastrad * 6 1.1 riastrad * Copyright(c) 2014-2016 Intel Corporation. All rights reserved. 7 1.1 riastrad * 8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 9 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 10 1.1 riastrad * to deal in the Software without restriction, including without limitation 11 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 13 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * The above copyright notice and this permission notice (including the next 16 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 17 1.1 riastrad * Software. 18 1.1 riastrad * 19 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 1.1 riastrad * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 1.1 riastrad * SOFTWARE. 26 1.1 riastrad * 27 1.1 riastrad * Authors: 28 1.1 riastrad * Kevin Tian <kevin.tian (at) intel.com> 29 1.1 riastrad * Jike Song <jike.song (at) intel.com> 30 1.1 riastrad * Xiaoguang Chen <xiaoguang.chen (at) intel.com> 31 1.1 riastrad */ 32 1.1 riastrad 33 1.1 riastrad #include <sys/cdefs.h> 34 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: kvmgt.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $"); 35 1.1 riastrad 36 1.1 riastrad #include <linux/init.h> 37 1.1 riastrad #include <linux/device.h> 38 1.1 riastrad #include <linux/mm.h> 39 1.1 riastrad #include <linux/mmu_context.h> 40 1.1 riastrad #include <linux/sched/mm.h> 41 1.1 riastrad #include <linux/types.h> 42 1.1 riastrad #include <linux/list.h> 43 1.1 riastrad #include <linux/rbtree.h> 44 1.1 riastrad #include <linux/spinlock.h> 45 1.1 riastrad #include <linux/eventfd.h> 46 1.1 riastrad #include <linux/uuid.h> 47 1.1 riastrad #include <linux/kvm_host.h> 48 1.1 riastrad #include <linux/vfio.h> 49 1.1 riastrad #include <linux/mdev.h> 50 1.1 riastrad #include <linux/debugfs.h> 51 1.1 riastrad 52 1.1 riastrad #include <linux/nospec.h> 53 1.1 riastrad 54 1.1 riastrad #include "i915_drv.h" 55 1.1 riastrad #include "gvt.h" 56 1.1 riastrad 57 1.1 riastrad static const struct intel_gvt_ops *intel_gvt_ops; 58 1.1 riastrad 59 1.1 riastrad /* helper macros copied from vfio-pci */ 60 1.1 riastrad #define VFIO_PCI_OFFSET_SHIFT 40 61 1.1 riastrad #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT) 62 1.1 riastrad #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT) 63 1.1 riastrad #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1) 64 1.1 riastrad 65 1.1 riastrad #define EDID_BLOB_OFFSET (PAGE_SIZE/2) 66 1.1 riastrad 67 1.1 riastrad #define OPREGION_SIGNATURE "IntelGraphicsMem" 68 1.1 riastrad 69 1.1 riastrad struct vfio_region; 70 1.1 riastrad struct intel_vgpu_regops { 71 1.1 riastrad size_t (*rw)(struct intel_vgpu *vgpu, char *buf, 72 1.1 riastrad size_t count, loff_t *ppos, bool iswrite); 73 1.1 riastrad void (*release)(struct intel_vgpu *vgpu, 74 1.1 riastrad struct vfio_region *region); 75 1.1 riastrad }; 76 1.1 riastrad 77 1.1 riastrad struct vfio_region { 78 1.1 riastrad u32 type; 79 1.1 riastrad u32 subtype; 80 1.1 riastrad size_t size; 81 1.1 riastrad u32 flags; 82 1.1 riastrad const struct intel_vgpu_regops *ops; 83 1.1 riastrad void *data; 84 1.1 riastrad }; 85 1.1 riastrad 86 1.1 riastrad struct vfio_edid_region { 87 1.1 riastrad struct vfio_region_gfx_edid vfio_edid_regs; 88 1.1 riastrad void *edid_blob; 89 1.1 riastrad }; 90 1.1 riastrad 91 1.1 riastrad struct kvmgt_pgfn { 92 1.1 riastrad gfn_t gfn; 93 1.1 riastrad struct hlist_node hnode; 94 1.1 riastrad }; 95 1.1 riastrad 96 1.1 riastrad struct kvmgt_guest_info { 97 1.1 riastrad struct kvm *kvm; 98 1.1 riastrad struct intel_vgpu *vgpu; 99 1.1 riastrad struct kvm_page_track_notifier_node track_node; 100 1.1 riastrad #define NR_BKT (1 << 18) 101 1.1 riastrad struct hlist_head ptable[NR_BKT]; 102 1.1 riastrad #undef NR_BKT 103 1.1 riastrad struct dentry *debugfs_cache_entries; 104 1.1 riastrad }; 105 1.1 riastrad 106 1.1 riastrad struct gvt_dma { 107 1.1 riastrad struct intel_vgpu *vgpu; 108 1.1 riastrad struct rb_node gfn_node; 109 1.1 riastrad struct rb_node dma_addr_node; 110 1.1 riastrad gfn_t gfn; 111 1.1 riastrad dma_addr_t dma_addr; 112 1.1 riastrad unsigned long size; 113 1.1 riastrad struct kref ref; 114 1.1 riastrad }; 115 1.1 riastrad 116 1.1 riastrad static inline bool handle_valid(unsigned long handle) 117 1.1 riastrad { 118 1.1 riastrad return !!(handle & ~0xff); 119 1.1 riastrad } 120 1.1 riastrad 121 1.1 riastrad static int kvmgt_guest_init(struct mdev_device *mdev); 122 1.1 riastrad static void intel_vgpu_release_work(struct work_struct *work); 123 1.1 riastrad static bool kvmgt_guest_exit(struct kvmgt_guest_info *info); 124 1.1 riastrad 125 1.1 riastrad static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, 126 1.1 riastrad unsigned long size) 127 1.1 riastrad { 128 1.1 riastrad int total_pages; 129 1.1 riastrad int npage; 130 1.1 riastrad int ret; 131 1.1 riastrad 132 1.1 riastrad total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE; 133 1.1 riastrad 134 1.1 riastrad for (npage = 0; npage < total_pages; npage++) { 135 1.1 riastrad unsigned long cur_gfn = gfn + npage; 136 1.1 riastrad 137 1.1 riastrad ret = vfio_unpin_pages(mdev_dev(vgpu->vdev.mdev), &cur_gfn, 1); 138 1.1 riastrad WARN_ON(ret != 1); 139 1.1 riastrad } 140 1.1 riastrad } 141 1.1 riastrad 142 1.1 riastrad /* Pin a normal or compound guest page for dma. */ 143 1.1 riastrad static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, 144 1.1 riastrad unsigned long size, struct page **page) 145 1.1 riastrad { 146 1.1 riastrad unsigned long base_pfn = 0; 147 1.1 riastrad int total_pages; 148 1.1 riastrad int npage; 149 1.1 riastrad int ret; 150 1.1 riastrad 151 1.1 riastrad total_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE; 152 1.1 riastrad /* 153 1.1 riastrad * We pin the pages one-by-one to avoid allocating a big arrary 154 1.1 riastrad * on stack to hold pfns. 155 1.1 riastrad */ 156 1.1 riastrad for (npage = 0; npage < total_pages; npage++) { 157 1.1 riastrad unsigned long cur_gfn = gfn + npage; 158 1.1 riastrad unsigned long pfn; 159 1.1 riastrad 160 1.1 riastrad ret = vfio_pin_pages(mdev_dev(vgpu->vdev.mdev), &cur_gfn, 1, 161 1.1 riastrad IOMMU_READ | IOMMU_WRITE, &pfn); 162 1.1 riastrad if (ret != 1) { 163 1.1 riastrad gvt_vgpu_err("vfio_pin_pages failed for gfn 0x%lx, ret %d\n", 164 1.1 riastrad cur_gfn, ret); 165 1.1 riastrad goto err; 166 1.1 riastrad } 167 1.1 riastrad 168 1.1 riastrad if (!pfn_valid(pfn)) { 169 1.1 riastrad gvt_vgpu_err("pfn 0x%lx is not mem backed\n", pfn); 170 1.1 riastrad npage++; 171 1.1 riastrad ret = -EFAULT; 172 1.1 riastrad goto err; 173 1.1 riastrad } 174 1.1 riastrad 175 1.1 riastrad if (npage == 0) 176 1.1 riastrad base_pfn = pfn; 177 1.1 riastrad else if (base_pfn + npage != pfn) { 178 1.1 riastrad gvt_vgpu_err("The pages are not continuous\n"); 179 1.1 riastrad ret = -EINVAL; 180 1.1 riastrad npage++; 181 1.1 riastrad goto err; 182 1.1 riastrad } 183 1.1 riastrad } 184 1.1 riastrad 185 1.1 riastrad *page = pfn_to_page(base_pfn); 186 1.1 riastrad return 0; 187 1.1 riastrad err: 188 1.1 riastrad gvt_unpin_guest_page(vgpu, gfn, npage * PAGE_SIZE); 189 1.1 riastrad return ret; 190 1.1 riastrad } 191 1.1 riastrad 192 1.1 riastrad static int gvt_dma_map_page(struct intel_vgpu *vgpu, unsigned long gfn, 193 1.1 riastrad dma_addr_t *dma_addr, unsigned long size) 194 1.1 riastrad { 195 1.1 riastrad struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev; 196 1.1 riastrad struct page *page = NULL; 197 1.1 riastrad int ret; 198 1.1 riastrad 199 1.1 riastrad ret = gvt_pin_guest_page(vgpu, gfn, size, &page); 200 1.1 riastrad if (ret) 201 1.1 riastrad return ret; 202 1.1 riastrad 203 1.1 riastrad /* Setup DMA mapping. */ 204 1.1 riastrad *dma_addr = dma_map_page(dev, page, 0, size, PCI_DMA_BIDIRECTIONAL); 205 1.1 riastrad if (dma_mapping_error(dev, *dma_addr)) { 206 1.1 riastrad gvt_vgpu_err("DMA mapping failed for pfn 0x%lx, ret %d\n", 207 1.1 riastrad page_to_pfn(page), ret); 208 1.1 riastrad gvt_unpin_guest_page(vgpu, gfn, size); 209 1.1 riastrad return -ENOMEM; 210 1.1 riastrad } 211 1.1 riastrad 212 1.1 riastrad return 0; 213 1.1 riastrad } 214 1.1 riastrad 215 1.1 riastrad static void gvt_dma_unmap_page(struct intel_vgpu *vgpu, unsigned long gfn, 216 1.1 riastrad dma_addr_t dma_addr, unsigned long size) 217 1.1 riastrad { 218 1.1 riastrad struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev; 219 1.1 riastrad 220 1.1 riastrad dma_unmap_page(dev, dma_addr, size, PCI_DMA_BIDIRECTIONAL); 221 1.1 riastrad gvt_unpin_guest_page(vgpu, gfn, size); 222 1.1 riastrad } 223 1.1 riastrad 224 1.1 riastrad static struct gvt_dma *__gvt_cache_find_dma_addr(struct intel_vgpu *vgpu, 225 1.1 riastrad dma_addr_t dma_addr) 226 1.1 riastrad { 227 1.1 riastrad struct rb_node *node = vgpu->vdev.dma_addr_cache.rb_node; 228 1.1 riastrad struct gvt_dma *itr; 229 1.1 riastrad 230 1.1 riastrad while (node) { 231 1.1 riastrad itr = rb_entry(node, struct gvt_dma, dma_addr_node); 232 1.1 riastrad 233 1.1 riastrad if (dma_addr < itr->dma_addr) 234 1.1 riastrad node = node->rb_left; 235 1.1 riastrad else if (dma_addr > itr->dma_addr) 236 1.1 riastrad node = node->rb_right; 237 1.1 riastrad else 238 1.1 riastrad return itr; 239 1.1 riastrad } 240 1.1 riastrad return NULL; 241 1.1 riastrad } 242 1.1 riastrad 243 1.1 riastrad static struct gvt_dma *__gvt_cache_find_gfn(struct intel_vgpu *vgpu, gfn_t gfn) 244 1.1 riastrad { 245 1.1 riastrad struct rb_node *node = vgpu->vdev.gfn_cache.rb_node; 246 1.1 riastrad struct gvt_dma *itr; 247 1.1 riastrad 248 1.1 riastrad while (node) { 249 1.1 riastrad itr = rb_entry(node, struct gvt_dma, gfn_node); 250 1.1 riastrad 251 1.1 riastrad if (gfn < itr->gfn) 252 1.1 riastrad node = node->rb_left; 253 1.1 riastrad else if (gfn > itr->gfn) 254 1.1 riastrad node = node->rb_right; 255 1.1 riastrad else 256 1.1 riastrad return itr; 257 1.1 riastrad } 258 1.1 riastrad return NULL; 259 1.1 riastrad } 260 1.1 riastrad 261 1.1 riastrad static int __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn, 262 1.1 riastrad dma_addr_t dma_addr, unsigned long size) 263 1.1 riastrad { 264 1.1 riastrad struct gvt_dma *new, *itr; 265 1.1 riastrad struct rb_node **link, *parent = NULL; 266 1.1 riastrad 267 1.1 riastrad new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL); 268 1.1 riastrad if (!new) 269 1.1 riastrad return -ENOMEM; 270 1.1 riastrad 271 1.1 riastrad new->vgpu = vgpu; 272 1.1 riastrad new->gfn = gfn; 273 1.1 riastrad new->dma_addr = dma_addr; 274 1.1 riastrad new->size = size; 275 1.1 riastrad kref_init(&new->ref); 276 1.1 riastrad 277 1.1 riastrad /* gfn_cache maps gfn to struct gvt_dma. */ 278 1.1 riastrad link = &vgpu->vdev.gfn_cache.rb_node; 279 1.1 riastrad while (*link) { 280 1.1 riastrad parent = *link; 281 1.1 riastrad itr = rb_entry(parent, struct gvt_dma, gfn_node); 282 1.1 riastrad 283 1.1 riastrad if (gfn < itr->gfn) 284 1.1 riastrad link = &parent->rb_left; 285 1.1 riastrad else 286 1.1 riastrad link = &parent->rb_right; 287 1.1 riastrad } 288 1.1 riastrad rb_link_node(&new->gfn_node, parent, link); 289 1.1 riastrad rb_insert_color(&new->gfn_node, &vgpu->vdev.gfn_cache); 290 1.1 riastrad 291 1.1 riastrad /* dma_addr_cache maps dma addr to struct gvt_dma. */ 292 1.1 riastrad parent = NULL; 293 1.1 riastrad link = &vgpu->vdev.dma_addr_cache.rb_node; 294 1.1 riastrad while (*link) { 295 1.1 riastrad parent = *link; 296 1.1 riastrad itr = rb_entry(parent, struct gvt_dma, dma_addr_node); 297 1.1 riastrad 298 1.1 riastrad if (dma_addr < itr->dma_addr) 299 1.1 riastrad link = &parent->rb_left; 300 1.1 riastrad else 301 1.1 riastrad link = &parent->rb_right; 302 1.1 riastrad } 303 1.1 riastrad rb_link_node(&new->dma_addr_node, parent, link); 304 1.1 riastrad rb_insert_color(&new->dma_addr_node, &vgpu->vdev.dma_addr_cache); 305 1.1 riastrad 306 1.1 riastrad vgpu->vdev.nr_cache_entries++; 307 1.1 riastrad return 0; 308 1.1 riastrad } 309 1.1 riastrad 310 1.1 riastrad static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu, 311 1.1 riastrad struct gvt_dma *entry) 312 1.1 riastrad { 313 1.1 riastrad rb_erase(&entry->gfn_node, &vgpu->vdev.gfn_cache); 314 1.1 riastrad rb_erase(&entry->dma_addr_node, &vgpu->vdev.dma_addr_cache); 315 1.1 riastrad kfree(entry); 316 1.1 riastrad vgpu->vdev.nr_cache_entries--; 317 1.1 riastrad } 318 1.1 riastrad 319 1.1 riastrad static void gvt_cache_destroy(struct intel_vgpu *vgpu) 320 1.1 riastrad { 321 1.1 riastrad struct gvt_dma *dma; 322 1.1 riastrad struct rb_node *node = NULL; 323 1.1 riastrad 324 1.1 riastrad for (;;) { 325 1.1 riastrad mutex_lock(&vgpu->vdev.cache_lock); 326 1.1 riastrad node = rb_first(&vgpu->vdev.gfn_cache); 327 1.1 riastrad if (!node) { 328 1.1 riastrad mutex_unlock(&vgpu->vdev.cache_lock); 329 1.1 riastrad break; 330 1.1 riastrad } 331 1.1 riastrad dma = rb_entry(node, struct gvt_dma, gfn_node); 332 1.1 riastrad gvt_dma_unmap_page(vgpu, dma->gfn, dma->dma_addr, dma->size); 333 1.1 riastrad __gvt_cache_remove_entry(vgpu, dma); 334 1.1 riastrad mutex_unlock(&vgpu->vdev.cache_lock); 335 1.1 riastrad } 336 1.1 riastrad } 337 1.1 riastrad 338 1.1 riastrad static void gvt_cache_init(struct intel_vgpu *vgpu) 339 1.1 riastrad { 340 1.1 riastrad vgpu->vdev.gfn_cache = RB_ROOT; 341 1.1 riastrad vgpu->vdev.dma_addr_cache = RB_ROOT; 342 1.1 riastrad vgpu->vdev.nr_cache_entries = 0; 343 1.1 riastrad mutex_init(&vgpu->vdev.cache_lock); 344 1.1 riastrad } 345 1.1 riastrad 346 1.1 riastrad static void kvmgt_protect_table_init(struct kvmgt_guest_info *info) 347 1.1 riastrad { 348 1.1 riastrad hash_init(info->ptable); 349 1.1 riastrad } 350 1.1 riastrad 351 1.1 riastrad static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info) 352 1.1 riastrad { 353 1.1 riastrad struct kvmgt_pgfn *p; 354 1.1 riastrad struct hlist_node *tmp; 355 1.1 riastrad int i; 356 1.1 riastrad 357 1.1 riastrad hash_for_each_safe(info->ptable, i, tmp, p, hnode) { 358 1.1 riastrad hash_del(&p->hnode); 359 1.1 riastrad kfree(p); 360 1.1 riastrad } 361 1.1 riastrad } 362 1.1 riastrad 363 1.1 riastrad static struct kvmgt_pgfn * 364 1.1 riastrad __kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn) 365 1.1 riastrad { 366 1.1 riastrad struct kvmgt_pgfn *p, *res = NULL; 367 1.1 riastrad 368 1.1 riastrad hash_for_each_possible(info->ptable, p, hnode, gfn) { 369 1.1 riastrad if (gfn == p->gfn) { 370 1.1 riastrad res = p; 371 1.1 riastrad break; 372 1.1 riastrad } 373 1.1 riastrad } 374 1.1 riastrad 375 1.1 riastrad return res; 376 1.1 riastrad } 377 1.1 riastrad 378 1.1 riastrad static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info, 379 1.1 riastrad gfn_t gfn) 380 1.1 riastrad { 381 1.1 riastrad struct kvmgt_pgfn *p; 382 1.1 riastrad 383 1.1 riastrad p = __kvmgt_protect_table_find(info, gfn); 384 1.1 riastrad return !!p; 385 1.1 riastrad } 386 1.1 riastrad 387 1.1 riastrad static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn) 388 1.1 riastrad { 389 1.1 riastrad struct kvmgt_pgfn *p; 390 1.1 riastrad 391 1.1 riastrad if (kvmgt_gfn_is_write_protected(info, gfn)) 392 1.1 riastrad return; 393 1.1 riastrad 394 1.1 riastrad p = kzalloc(sizeof(struct kvmgt_pgfn), GFP_ATOMIC); 395 1.1 riastrad if (WARN(!p, "gfn: 0x%llx\n", gfn)) 396 1.1 riastrad return; 397 1.1 riastrad 398 1.1 riastrad p->gfn = gfn; 399 1.1 riastrad hash_add(info->ptable, &p->hnode, gfn); 400 1.1 riastrad } 401 1.1 riastrad 402 1.1 riastrad static void kvmgt_protect_table_del(struct kvmgt_guest_info *info, 403 1.1 riastrad gfn_t gfn) 404 1.1 riastrad { 405 1.1 riastrad struct kvmgt_pgfn *p; 406 1.1 riastrad 407 1.1 riastrad p = __kvmgt_protect_table_find(info, gfn); 408 1.1 riastrad if (p) { 409 1.1 riastrad hash_del(&p->hnode); 410 1.1 riastrad kfree(p); 411 1.1 riastrad } 412 1.1 riastrad } 413 1.1 riastrad 414 1.1 riastrad static size_t intel_vgpu_reg_rw_opregion(struct intel_vgpu *vgpu, char *buf, 415 1.1 riastrad size_t count, loff_t *ppos, bool iswrite) 416 1.1 riastrad { 417 1.1 riastrad unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - 418 1.1 riastrad VFIO_PCI_NUM_REGIONS; 419 1.1 riastrad void *base = vgpu->vdev.region[i].data; 420 1.1 riastrad loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK; 421 1.1 riastrad 422 1.1 riastrad if (pos >= vgpu->vdev.region[i].size || iswrite) { 423 1.1 riastrad gvt_vgpu_err("invalid op or offset for Intel vgpu OpRegion\n"); 424 1.1 riastrad return -EINVAL; 425 1.1 riastrad } 426 1.1 riastrad count = min(count, (size_t)(vgpu->vdev.region[i].size - pos)); 427 1.1 riastrad memcpy(buf, base + pos, count); 428 1.1 riastrad 429 1.1 riastrad return count; 430 1.1 riastrad } 431 1.1 riastrad 432 1.1 riastrad static void intel_vgpu_reg_release_opregion(struct intel_vgpu *vgpu, 433 1.1 riastrad struct vfio_region *region) 434 1.1 riastrad { 435 1.1 riastrad } 436 1.1 riastrad 437 1.1 riastrad static const struct intel_vgpu_regops intel_vgpu_regops_opregion = { 438 1.1 riastrad .rw = intel_vgpu_reg_rw_opregion, 439 1.1 riastrad .release = intel_vgpu_reg_release_opregion, 440 1.1 riastrad }; 441 1.1 riastrad 442 1.1 riastrad static int handle_edid_regs(struct intel_vgpu *vgpu, 443 1.1 riastrad struct vfio_edid_region *region, char *buf, 444 1.1 riastrad size_t count, u16 offset, bool is_write) 445 1.1 riastrad { 446 1.1 riastrad struct vfio_region_gfx_edid *regs = ®ion->vfio_edid_regs; 447 1.1 riastrad unsigned int data; 448 1.1 riastrad 449 1.1 riastrad if (offset + count > sizeof(*regs)) 450 1.1 riastrad return -EINVAL; 451 1.1 riastrad 452 1.1 riastrad if (count != 4) 453 1.1 riastrad return -EINVAL; 454 1.1 riastrad 455 1.1 riastrad if (is_write) { 456 1.1 riastrad data = *((unsigned int *)buf); 457 1.1 riastrad switch (offset) { 458 1.1 riastrad case offsetof(struct vfio_region_gfx_edid, link_state): 459 1.1 riastrad if (data == VFIO_DEVICE_GFX_LINK_STATE_UP) { 460 1.1 riastrad if (!drm_edid_block_valid( 461 1.1 riastrad (u8 *)region->edid_blob, 462 1.1 riastrad 0, 463 1.1 riastrad true, 464 1.1 riastrad NULL)) { 465 1.1 riastrad gvt_vgpu_err("invalid EDID blob\n"); 466 1.1 riastrad return -EINVAL; 467 1.1 riastrad } 468 1.1 riastrad intel_gvt_ops->emulate_hotplug(vgpu, true); 469 1.1 riastrad } else if (data == VFIO_DEVICE_GFX_LINK_STATE_DOWN) 470 1.1 riastrad intel_gvt_ops->emulate_hotplug(vgpu, false); 471 1.1 riastrad else { 472 1.1 riastrad gvt_vgpu_err("invalid EDID link state %d\n", 473 1.1 riastrad regs->link_state); 474 1.1 riastrad return -EINVAL; 475 1.1 riastrad } 476 1.1 riastrad regs->link_state = data; 477 1.1 riastrad break; 478 1.1 riastrad case offsetof(struct vfio_region_gfx_edid, edid_size): 479 1.1 riastrad if (data > regs->edid_max_size) { 480 1.1 riastrad gvt_vgpu_err("EDID size is bigger than %d!\n", 481 1.1 riastrad regs->edid_max_size); 482 1.1 riastrad return -EINVAL; 483 1.1 riastrad } 484 1.1 riastrad regs->edid_size = data; 485 1.1 riastrad break; 486 1.1 riastrad default: 487 1.1 riastrad /* read-only regs */ 488 1.1 riastrad gvt_vgpu_err("write read-only EDID region at offset %d\n", 489 1.1 riastrad offset); 490 1.1 riastrad return -EPERM; 491 1.1 riastrad } 492 1.1 riastrad } else { 493 1.1 riastrad memcpy(buf, (char *)regs + offset, count); 494 1.1 riastrad } 495 1.1 riastrad 496 1.1 riastrad return count; 497 1.1 riastrad } 498 1.1 riastrad 499 1.1 riastrad static int handle_edid_blob(struct vfio_edid_region *region, char *buf, 500 1.1 riastrad size_t count, u16 offset, bool is_write) 501 1.1 riastrad { 502 1.1 riastrad if (offset + count > region->vfio_edid_regs.edid_size) 503 1.1 riastrad return -EINVAL; 504 1.1 riastrad 505 1.1 riastrad if (is_write) 506 1.1 riastrad memcpy(region->edid_blob + offset, buf, count); 507 1.1 riastrad else 508 1.1 riastrad memcpy(buf, region->edid_blob + offset, count); 509 1.1 riastrad 510 1.1 riastrad return count; 511 1.1 riastrad } 512 1.1 riastrad 513 1.1 riastrad static size_t intel_vgpu_reg_rw_edid(struct intel_vgpu *vgpu, char *buf, 514 1.1 riastrad size_t count, loff_t *ppos, bool iswrite) 515 1.1 riastrad { 516 1.1 riastrad int ret; 517 1.1 riastrad unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - 518 1.1 riastrad VFIO_PCI_NUM_REGIONS; 519 1.1 riastrad struct vfio_edid_region *region = 520 1.1 riastrad (struct vfio_edid_region *)vgpu->vdev.region[i].data; 521 1.1 riastrad loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK; 522 1.1 riastrad 523 1.1 riastrad if (pos < region->vfio_edid_regs.edid_offset) { 524 1.1 riastrad ret = handle_edid_regs(vgpu, region, buf, count, pos, iswrite); 525 1.1 riastrad } else { 526 1.1 riastrad pos -= EDID_BLOB_OFFSET; 527 1.1 riastrad ret = handle_edid_blob(region, buf, count, pos, iswrite); 528 1.1 riastrad } 529 1.1 riastrad 530 1.1 riastrad if (ret < 0) 531 1.1 riastrad gvt_vgpu_err("failed to access EDID region\n"); 532 1.1 riastrad 533 1.1 riastrad return ret; 534 1.1 riastrad } 535 1.1 riastrad 536 1.1 riastrad static void intel_vgpu_reg_release_edid(struct intel_vgpu *vgpu, 537 1.1 riastrad struct vfio_region *region) 538 1.1 riastrad { 539 1.1 riastrad kfree(region->data); 540 1.1 riastrad } 541 1.1 riastrad 542 1.1 riastrad static const struct intel_vgpu_regops intel_vgpu_regops_edid = { 543 1.1 riastrad .rw = intel_vgpu_reg_rw_edid, 544 1.1 riastrad .release = intel_vgpu_reg_release_edid, 545 1.1 riastrad }; 546 1.1 riastrad 547 1.1 riastrad static int intel_vgpu_register_reg(struct intel_vgpu *vgpu, 548 1.1 riastrad unsigned int type, unsigned int subtype, 549 1.1 riastrad const struct intel_vgpu_regops *ops, 550 1.1 riastrad size_t size, u32 flags, void *data) 551 1.1 riastrad { 552 1.1 riastrad struct vfio_region *region; 553 1.1 riastrad 554 1.1 riastrad region = krealloc(vgpu->vdev.region, 555 1.1 riastrad (vgpu->vdev.num_regions + 1) * sizeof(*region), 556 1.1 riastrad GFP_KERNEL); 557 1.1 riastrad if (!region) 558 1.1 riastrad return -ENOMEM; 559 1.1 riastrad 560 1.1 riastrad vgpu->vdev.region = region; 561 1.1 riastrad vgpu->vdev.region[vgpu->vdev.num_regions].type = type; 562 1.1 riastrad vgpu->vdev.region[vgpu->vdev.num_regions].subtype = subtype; 563 1.1 riastrad vgpu->vdev.region[vgpu->vdev.num_regions].ops = ops; 564 1.1 riastrad vgpu->vdev.region[vgpu->vdev.num_regions].size = size; 565 1.1 riastrad vgpu->vdev.region[vgpu->vdev.num_regions].flags = flags; 566 1.1 riastrad vgpu->vdev.region[vgpu->vdev.num_regions].data = data; 567 1.1 riastrad vgpu->vdev.num_regions++; 568 1.1 riastrad return 0; 569 1.1 riastrad } 570 1.1 riastrad 571 1.1 riastrad static int kvmgt_get_vfio_device(void *p_vgpu) 572 1.1 riastrad { 573 1.1 riastrad struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu; 574 1.1 riastrad 575 1.1 riastrad vgpu->vdev.vfio_device = vfio_device_get_from_dev( 576 1.1 riastrad mdev_dev(vgpu->vdev.mdev)); 577 1.1 riastrad if (!vgpu->vdev.vfio_device) { 578 1.1 riastrad gvt_vgpu_err("failed to get vfio device\n"); 579 1.1 riastrad return -ENODEV; 580 1.1 riastrad } 581 1.1 riastrad return 0; 582 1.1 riastrad } 583 1.1 riastrad 584 1.1 riastrad 585 1.1 riastrad static int kvmgt_set_opregion(void *p_vgpu) 586 1.1 riastrad { 587 1.1 riastrad struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu; 588 1.1 riastrad void *base; 589 1.1 riastrad int ret; 590 1.1 riastrad 591 1.1 riastrad /* Each vgpu has its own opregion, although VFIO would create another 592 1.1 riastrad * one later. This one is used to expose opregion to VFIO. And the 593 1.1 riastrad * other one created by VFIO later, is used by guest actually. 594 1.1 riastrad */ 595 1.1 riastrad base = vgpu_opregion(vgpu)->va; 596 1.1 riastrad if (!base) 597 1.1 riastrad return -ENOMEM; 598 1.1 riastrad 599 1.1 riastrad if (memcmp(base, OPREGION_SIGNATURE, 16)) { 600 1.1 riastrad memunmap(base); 601 1.1 riastrad return -EINVAL; 602 1.1 riastrad } 603 1.1 riastrad 604 1.1 riastrad ret = intel_vgpu_register_reg(vgpu, 605 1.1 riastrad PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE, 606 1.1 riastrad VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, 607 1.1 riastrad &intel_vgpu_regops_opregion, OPREGION_SIZE, 608 1.1 riastrad VFIO_REGION_INFO_FLAG_READ, base); 609 1.1 riastrad 610 1.1 riastrad return ret; 611 1.1 riastrad } 612 1.1 riastrad 613 1.1 riastrad static int kvmgt_set_edid(void *p_vgpu, int port_num) 614 1.1 riastrad { 615 1.1 riastrad struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu; 616 1.1 riastrad struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num); 617 1.1 riastrad struct vfio_edid_region *base; 618 1.1 riastrad int ret; 619 1.1 riastrad 620 1.1 riastrad base = kzalloc(sizeof(*base), GFP_KERNEL); 621 1.1 riastrad if (!base) 622 1.1 riastrad return -ENOMEM; 623 1.1 riastrad 624 1.1 riastrad /* TODO: Add multi-port and EDID extension block support */ 625 1.1 riastrad base->vfio_edid_regs.edid_offset = EDID_BLOB_OFFSET; 626 1.1 riastrad base->vfio_edid_regs.edid_max_size = EDID_SIZE; 627 1.1 riastrad base->vfio_edid_regs.edid_size = EDID_SIZE; 628 1.1 riastrad base->vfio_edid_regs.max_xres = vgpu_edid_xres(port->id); 629 1.1 riastrad base->vfio_edid_regs.max_yres = vgpu_edid_yres(port->id); 630 1.1 riastrad base->edid_blob = port->edid->edid_block; 631 1.1 riastrad 632 1.1 riastrad ret = intel_vgpu_register_reg(vgpu, 633 1.1 riastrad VFIO_REGION_TYPE_GFX, 634 1.1 riastrad VFIO_REGION_SUBTYPE_GFX_EDID, 635 1.1 riastrad &intel_vgpu_regops_edid, EDID_SIZE, 636 1.1 riastrad VFIO_REGION_INFO_FLAG_READ | 637 1.1 riastrad VFIO_REGION_INFO_FLAG_WRITE | 638 1.1 riastrad VFIO_REGION_INFO_FLAG_CAPS, base); 639 1.1 riastrad 640 1.1 riastrad return ret; 641 1.1 riastrad } 642 1.1 riastrad 643 1.1 riastrad static void kvmgt_put_vfio_device(void *vgpu) 644 1.1 riastrad { 645 1.1 riastrad if (WARN_ON(!((struct intel_vgpu *)vgpu)->vdev.vfio_device)) 646 1.1 riastrad return; 647 1.1 riastrad 648 1.1 riastrad vfio_device_put(((struct intel_vgpu *)vgpu)->vdev.vfio_device); 649 1.1 riastrad } 650 1.1 riastrad 651 1.1 riastrad static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev) 652 1.1 riastrad { 653 1.1 riastrad struct intel_vgpu *vgpu = NULL; 654 1.1 riastrad struct intel_vgpu_type *type; 655 1.1 riastrad struct device *pdev; 656 1.1 riastrad void *gvt; 657 1.1 riastrad int ret; 658 1.1 riastrad 659 1.1 riastrad pdev = mdev_parent_dev(mdev); 660 1.1 riastrad gvt = kdev_to_i915(pdev)->gvt; 661 1.1 riastrad 662 1.1 riastrad type = intel_gvt_ops->gvt_find_vgpu_type(gvt, kobject_name(kobj)); 663 1.1 riastrad if (!type) { 664 1.1 riastrad gvt_vgpu_err("failed to find type %s to create\n", 665 1.1 riastrad kobject_name(kobj)); 666 1.1 riastrad ret = -EINVAL; 667 1.1 riastrad goto out; 668 1.1 riastrad } 669 1.1 riastrad 670 1.1 riastrad vgpu = intel_gvt_ops->vgpu_create(gvt, type); 671 1.1 riastrad if (IS_ERR_OR_NULL(vgpu)) { 672 1.1 riastrad ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu); 673 1.1 riastrad gvt_err("failed to create intel vgpu: %d\n", ret); 674 1.1 riastrad goto out; 675 1.1 riastrad } 676 1.1 riastrad 677 1.1 riastrad INIT_WORK(&vgpu->vdev.release_work, intel_vgpu_release_work); 678 1.1 riastrad 679 1.1 riastrad vgpu->vdev.mdev = mdev; 680 1.1 riastrad mdev_set_drvdata(mdev, vgpu); 681 1.1 riastrad 682 1.1 riastrad gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n", 683 1.1 riastrad dev_name(mdev_dev(mdev))); 684 1.1 riastrad ret = 0; 685 1.1 riastrad 686 1.1 riastrad out: 687 1.1 riastrad return ret; 688 1.1 riastrad } 689 1.1 riastrad 690 1.1 riastrad static int intel_vgpu_remove(struct mdev_device *mdev) 691 1.1 riastrad { 692 1.1 riastrad struct intel_vgpu *vgpu = mdev_get_drvdata(mdev); 693 1.1 riastrad 694 1.1 riastrad if (handle_valid(vgpu->handle)) 695 1.1 riastrad return -EBUSY; 696 1.1 riastrad 697 1.1 riastrad intel_gvt_ops->vgpu_destroy(vgpu); 698 1.1 riastrad return 0; 699 1.1 riastrad } 700 1.1 riastrad 701 1.1 riastrad static int intel_vgpu_iommu_notifier(struct notifier_block *nb, 702 1.1 riastrad unsigned long action, void *data) 703 1.1 riastrad { 704 1.1 riastrad struct intel_vgpu *vgpu = container_of(nb, 705 1.1 riastrad struct intel_vgpu, 706 1.1 riastrad vdev.iommu_notifier); 707 1.1 riastrad 708 1.1 riastrad if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) { 709 1.1 riastrad struct vfio_iommu_type1_dma_unmap *unmap = data; 710 1.1 riastrad struct gvt_dma *entry; 711 1.1 riastrad unsigned long iov_pfn, end_iov_pfn; 712 1.1 riastrad 713 1.1 riastrad iov_pfn = unmap->iova >> PAGE_SHIFT; 714 1.1 riastrad end_iov_pfn = iov_pfn + unmap->size / PAGE_SIZE; 715 1.1 riastrad 716 1.1 riastrad mutex_lock(&vgpu->vdev.cache_lock); 717 1.1 riastrad for (; iov_pfn < end_iov_pfn; iov_pfn++) { 718 1.1 riastrad entry = __gvt_cache_find_gfn(vgpu, iov_pfn); 719 1.1 riastrad if (!entry) 720 1.1 riastrad continue; 721 1.1 riastrad 722 1.1 riastrad gvt_dma_unmap_page(vgpu, entry->gfn, entry->dma_addr, 723 1.1 riastrad entry->size); 724 1.1 riastrad __gvt_cache_remove_entry(vgpu, entry); 725 1.1 riastrad } 726 1.1 riastrad mutex_unlock(&vgpu->vdev.cache_lock); 727 1.1 riastrad } 728 1.1 riastrad 729 1.1 riastrad return NOTIFY_OK; 730 1.1 riastrad } 731 1.1 riastrad 732 1.1 riastrad static int intel_vgpu_group_notifier(struct notifier_block *nb, 733 1.1 riastrad unsigned long action, void *data) 734 1.1 riastrad { 735 1.1 riastrad struct intel_vgpu *vgpu = container_of(nb, 736 1.1 riastrad struct intel_vgpu, 737 1.1 riastrad vdev.group_notifier); 738 1.1 riastrad 739 1.1 riastrad /* the only action we care about */ 740 1.1 riastrad if (action == VFIO_GROUP_NOTIFY_SET_KVM) { 741 1.1 riastrad vgpu->vdev.kvm = data; 742 1.1 riastrad 743 1.1 riastrad if (!data) 744 1.1 riastrad schedule_work(&vgpu->vdev.release_work); 745 1.1 riastrad } 746 1.1 riastrad 747 1.1 riastrad return NOTIFY_OK; 748 1.1 riastrad } 749 1.1 riastrad 750 1.1 riastrad static int intel_vgpu_open(struct mdev_device *mdev) 751 1.1 riastrad { 752 1.1 riastrad struct intel_vgpu *vgpu = mdev_get_drvdata(mdev); 753 1.1 riastrad unsigned long events; 754 1.1 riastrad int ret; 755 1.1 riastrad 756 1.1 riastrad vgpu->vdev.iommu_notifier.notifier_call = intel_vgpu_iommu_notifier; 757 1.1 riastrad vgpu->vdev.group_notifier.notifier_call = intel_vgpu_group_notifier; 758 1.1 riastrad 759 1.1 riastrad events = VFIO_IOMMU_NOTIFY_DMA_UNMAP; 760 1.1 riastrad ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &events, 761 1.1 riastrad &vgpu->vdev.iommu_notifier); 762 1.1 riastrad if (ret != 0) { 763 1.1 riastrad gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n", 764 1.1 riastrad ret); 765 1.1 riastrad goto out; 766 1.1 riastrad } 767 1.1 riastrad 768 1.1 riastrad events = VFIO_GROUP_NOTIFY_SET_KVM; 769 1.1 riastrad ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &events, 770 1.1 riastrad &vgpu->vdev.group_notifier); 771 1.1 riastrad if (ret != 0) { 772 1.1 riastrad gvt_vgpu_err("vfio_register_notifier for group failed: %d\n", 773 1.1 riastrad ret); 774 1.1 riastrad goto undo_iommu; 775 1.1 riastrad } 776 1.1 riastrad 777 1.1 riastrad /* Take a module reference as mdev core doesn't take 778 1.1 riastrad * a reference for vendor driver. 779 1.1 riastrad */ 780 1.1 riastrad if (!try_module_get(THIS_MODULE)) 781 1.1 riastrad goto undo_group; 782 1.1 riastrad 783 1.1 riastrad ret = kvmgt_guest_init(mdev); 784 1.1 riastrad if (ret) 785 1.1 riastrad goto undo_group; 786 1.1 riastrad 787 1.1 riastrad intel_gvt_ops->vgpu_activate(vgpu); 788 1.1 riastrad 789 1.1 riastrad atomic_set(&vgpu->vdev.released, 0); 790 1.1 riastrad return ret; 791 1.1 riastrad 792 1.1 riastrad undo_group: 793 1.1 riastrad vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, 794 1.1 riastrad &vgpu->vdev.group_notifier); 795 1.1 riastrad 796 1.1 riastrad undo_iommu: 797 1.1 riastrad vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, 798 1.1 riastrad &vgpu->vdev.iommu_notifier); 799 1.1 riastrad out: 800 1.1 riastrad return ret; 801 1.1 riastrad } 802 1.1 riastrad 803 1.1 riastrad static void intel_vgpu_release_msi_eventfd_ctx(struct intel_vgpu *vgpu) 804 1.1 riastrad { 805 1.1 riastrad struct eventfd_ctx *trigger; 806 1.1 riastrad 807 1.1 riastrad trigger = vgpu->vdev.msi_trigger; 808 1.1 riastrad if (trigger) { 809 1.1 riastrad eventfd_ctx_put(trigger); 810 1.1 riastrad vgpu->vdev.msi_trigger = NULL; 811 1.1 riastrad } 812 1.1 riastrad } 813 1.1 riastrad 814 1.1 riastrad static void __intel_vgpu_release(struct intel_vgpu *vgpu) 815 1.1 riastrad { 816 1.1 riastrad struct kvmgt_guest_info *info; 817 1.1 riastrad int ret; 818 1.1 riastrad 819 1.1 riastrad if (!handle_valid(vgpu->handle)) 820 1.1 riastrad return; 821 1.1 riastrad 822 1.1 riastrad if (atomic_cmpxchg(&vgpu->vdev.released, 0, 1)) 823 1.1 riastrad return; 824 1.1 riastrad 825 1.1 riastrad intel_gvt_ops->vgpu_release(vgpu); 826 1.1 riastrad 827 1.1 riastrad ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_IOMMU_NOTIFY, 828 1.1 riastrad &vgpu->vdev.iommu_notifier); 829 1.1 riastrad WARN(ret, "vfio_unregister_notifier for iommu failed: %d\n", ret); 830 1.1 riastrad 831 1.1 riastrad ret = vfio_unregister_notifier(mdev_dev(vgpu->vdev.mdev), VFIO_GROUP_NOTIFY, 832 1.1 riastrad &vgpu->vdev.group_notifier); 833 1.1 riastrad WARN(ret, "vfio_unregister_notifier for group failed: %d\n", ret); 834 1.1 riastrad 835 1.1 riastrad /* dereference module reference taken at open */ 836 1.1 riastrad module_put(THIS_MODULE); 837 1.1 riastrad 838 1.1 riastrad info = (struct kvmgt_guest_info *)vgpu->handle; 839 1.1 riastrad kvmgt_guest_exit(info); 840 1.1 riastrad 841 1.1 riastrad intel_vgpu_release_msi_eventfd_ctx(vgpu); 842 1.1 riastrad 843 1.1 riastrad vgpu->vdev.kvm = NULL; 844 1.1 riastrad vgpu->handle = 0; 845 1.1 riastrad } 846 1.1 riastrad 847 1.1 riastrad static void intel_vgpu_release(struct mdev_device *mdev) 848 1.1 riastrad { 849 1.1 riastrad struct intel_vgpu *vgpu = mdev_get_drvdata(mdev); 850 1.1 riastrad 851 1.1 riastrad __intel_vgpu_release(vgpu); 852 1.1 riastrad } 853 1.1 riastrad 854 1.1 riastrad static void intel_vgpu_release_work(struct work_struct *work) 855 1.1 riastrad { 856 1.1 riastrad struct intel_vgpu *vgpu = container_of(work, struct intel_vgpu, 857 1.1 riastrad vdev.release_work); 858 1.1 riastrad 859 1.1 riastrad __intel_vgpu_release(vgpu); 860 1.1 riastrad } 861 1.1 riastrad 862 1.1 riastrad static u64 intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar) 863 1.1 riastrad { 864 1.1 riastrad u32 start_lo, start_hi; 865 1.1 riastrad u32 mem_type; 866 1.1 riastrad 867 1.1 riastrad start_lo = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) & 868 1.1 riastrad PCI_BASE_ADDRESS_MEM_MASK; 869 1.1 riastrad mem_type = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space + bar)) & 870 1.1 riastrad PCI_BASE_ADDRESS_MEM_TYPE_MASK; 871 1.1 riastrad 872 1.1 riastrad switch (mem_type) { 873 1.1 riastrad case PCI_BASE_ADDRESS_MEM_TYPE_64: 874 1.1 riastrad start_hi = (*(u32 *)(vgpu->cfg_space.virtual_cfg_space 875 1.1 riastrad + bar + 4)); 876 1.1 riastrad break; 877 1.1 riastrad case PCI_BASE_ADDRESS_MEM_TYPE_32: 878 1.1 riastrad case PCI_BASE_ADDRESS_MEM_TYPE_1M: 879 1.1 riastrad /* 1M mem BAR treated as 32-bit BAR */ 880 1.1 riastrad default: 881 1.1 riastrad /* mem unknown type treated as 32-bit BAR */ 882 1.1 riastrad start_hi = 0; 883 1.1 riastrad break; 884 1.1 riastrad } 885 1.1 riastrad 886 1.1 riastrad return ((u64)start_hi << 32) | start_lo; 887 1.1 riastrad } 888 1.1 riastrad 889 1.1 riastrad static int intel_vgpu_bar_rw(struct intel_vgpu *vgpu, int bar, u64 off, 890 1.1 riastrad void *buf, unsigned int count, bool is_write) 891 1.1 riastrad { 892 1.1 riastrad u64 bar_start = intel_vgpu_get_bar_addr(vgpu, bar); 893 1.1 riastrad int ret; 894 1.1 riastrad 895 1.1 riastrad if (is_write) 896 1.1 riastrad ret = intel_gvt_ops->emulate_mmio_write(vgpu, 897 1.1 riastrad bar_start + off, buf, count); 898 1.1 riastrad else 899 1.1 riastrad ret = intel_gvt_ops->emulate_mmio_read(vgpu, 900 1.1 riastrad bar_start + off, buf, count); 901 1.1 riastrad return ret; 902 1.1 riastrad } 903 1.1 riastrad 904 1.1 riastrad static inline bool intel_vgpu_in_aperture(struct intel_vgpu *vgpu, u64 off) 905 1.1 riastrad { 906 1.1 riastrad return off >= vgpu_aperture_offset(vgpu) && 907 1.1 riastrad off < vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu); 908 1.1 riastrad } 909 1.1 riastrad 910 1.1 riastrad static int intel_vgpu_aperture_rw(struct intel_vgpu *vgpu, u64 off, 911 1.1 riastrad void *buf, unsigned long count, bool is_write) 912 1.1 riastrad { 913 1.1 riastrad void __iomem *aperture_va; 914 1.1 riastrad 915 1.1 riastrad if (!intel_vgpu_in_aperture(vgpu, off) || 916 1.1 riastrad !intel_vgpu_in_aperture(vgpu, off + count)) { 917 1.1 riastrad gvt_vgpu_err("Invalid aperture offset %llu\n", off); 918 1.1 riastrad return -EINVAL; 919 1.1 riastrad } 920 1.1 riastrad 921 1.1 riastrad aperture_va = io_mapping_map_wc(&vgpu->gvt->dev_priv->ggtt.iomap, 922 1.1 riastrad ALIGN_DOWN(off, PAGE_SIZE), 923 1.1 riastrad count + offset_in_page(off)); 924 1.1 riastrad if (!aperture_va) 925 1.1 riastrad return -EIO; 926 1.1 riastrad 927 1.1 riastrad if (is_write) 928 1.1 riastrad memcpy_toio(aperture_va + offset_in_page(off), buf, count); 929 1.1 riastrad else 930 1.1 riastrad memcpy_fromio(buf, aperture_va + offset_in_page(off), count); 931 1.1 riastrad 932 1.1 riastrad io_mapping_unmap(aperture_va); 933 1.1 riastrad 934 1.1 riastrad return 0; 935 1.1 riastrad } 936 1.1 riastrad 937 1.1 riastrad static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf, 938 1.1 riastrad size_t count, loff_t *ppos, bool is_write) 939 1.1 riastrad { 940 1.1 riastrad struct intel_vgpu *vgpu = mdev_get_drvdata(mdev); 941 1.1 riastrad unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); 942 1.1 riastrad u64 pos = *ppos & VFIO_PCI_OFFSET_MASK; 943 1.1 riastrad int ret = -EINVAL; 944 1.1 riastrad 945 1.1 riastrad 946 1.1 riastrad if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions) { 947 1.1 riastrad gvt_vgpu_err("invalid index: %u\n", index); 948 1.1 riastrad return -EINVAL; 949 1.1 riastrad } 950 1.1 riastrad 951 1.1 riastrad switch (index) { 952 1.1 riastrad case VFIO_PCI_CONFIG_REGION_INDEX: 953 1.1 riastrad if (is_write) 954 1.1 riastrad ret = intel_gvt_ops->emulate_cfg_write(vgpu, pos, 955 1.1 riastrad buf, count); 956 1.1 riastrad else 957 1.1 riastrad ret = intel_gvt_ops->emulate_cfg_read(vgpu, pos, 958 1.1 riastrad buf, count); 959 1.1 riastrad break; 960 1.1 riastrad case VFIO_PCI_BAR0_REGION_INDEX: 961 1.1 riastrad ret = intel_vgpu_bar_rw(vgpu, PCI_BASE_ADDRESS_0, pos, 962 1.1 riastrad buf, count, is_write); 963 1.1 riastrad break; 964 1.1 riastrad case VFIO_PCI_BAR2_REGION_INDEX: 965 1.1 riastrad ret = intel_vgpu_aperture_rw(vgpu, pos, buf, count, is_write); 966 1.1 riastrad break; 967 1.1 riastrad case VFIO_PCI_BAR1_REGION_INDEX: 968 1.1 riastrad case VFIO_PCI_BAR3_REGION_INDEX: 969 1.1 riastrad case VFIO_PCI_BAR4_REGION_INDEX: 970 1.1 riastrad case VFIO_PCI_BAR5_REGION_INDEX: 971 1.1 riastrad case VFIO_PCI_VGA_REGION_INDEX: 972 1.1 riastrad case VFIO_PCI_ROM_REGION_INDEX: 973 1.1 riastrad break; 974 1.1 riastrad default: 975 1.1 riastrad if (index >= VFIO_PCI_NUM_REGIONS + vgpu->vdev.num_regions) 976 1.1 riastrad return -EINVAL; 977 1.1 riastrad 978 1.1 riastrad index -= VFIO_PCI_NUM_REGIONS; 979 1.1 riastrad return vgpu->vdev.region[index].ops->rw(vgpu, buf, count, 980 1.1 riastrad ppos, is_write); 981 1.1 riastrad } 982 1.1 riastrad 983 1.1 riastrad return ret == 0 ? count : ret; 984 1.1 riastrad } 985 1.1 riastrad 986 1.1 riastrad static bool gtt_entry(struct mdev_device *mdev, loff_t *ppos) 987 1.1 riastrad { 988 1.1 riastrad struct intel_vgpu *vgpu = mdev_get_drvdata(mdev); 989 1.1 riastrad unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); 990 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 991 1.1 riastrad int offset; 992 1.1 riastrad 993 1.1 riastrad /* Only allow MMIO GGTT entry access */ 994 1.1 riastrad if (index != PCI_BASE_ADDRESS_0) 995 1.1 riastrad return false; 996 1.1 riastrad 997 1.1 riastrad offset = (u64)(*ppos & VFIO_PCI_OFFSET_MASK) - 998 1.1 riastrad intel_vgpu_get_bar_gpa(vgpu, PCI_BASE_ADDRESS_0); 999 1.1 riastrad 1000 1.1 riastrad return (offset >= gvt->device_info.gtt_start_offset && 1001 1.1 riastrad offset < gvt->device_info.gtt_start_offset + gvt_ggtt_sz(gvt)) ? 1002 1.1 riastrad true : false; 1003 1.1 riastrad } 1004 1.1 riastrad 1005 1.1 riastrad static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf, 1006 1.1 riastrad size_t count, loff_t *ppos) 1007 1.1 riastrad { 1008 1.1 riastrad unsigned int done = 0; 1009 1.1 riastrad int ret; 1010 1.1 riastrad 1011 1.1 riastrad while (count) { 1012 1.1 riastrad size_t filled; 1013 1.1 riastrad 1014 1.1 riastrad /* Only support GGTT entry 8 bytes read */ 1015 1.1 riastrad if (count >= 8 && !(*ppos % 8) && 1016 1.1 riastrad gtt_entry(mdev, ppos)) { 1017 1.1 riastrad u64 val; 1018 1.1 riastrad 1019 1.1 riastrad ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val), 1020 1.1 riastrad ppos, false); 1021 1.1 riastrad if (ret <= 0) 1022 1.1 riastrad goto read_err; 1023 1.1 riastrad 1024 1.1 riastrad if (copy_to_user(buf, &val, sizeof(val))) 1025 1.1 riastrad goto read_err; 1026 1.1 riastrad 1027 1.1 riastrad filled = 8; 1028 1.1 riastrad } else if (count >= 4 && !(*ppos % 4)) { 1029 1.1 riastrad u32 val; 1030 1.1 riastrad 1031 1.1 riastrad ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val), 1032 1.1 riastrad ppos, false); 1033 1.1 riastrad if (ret <= 0) 1034 1.1 riastrad goto read_err; 1035 1.1 riastrad 1036 1.1 riastrad if (copy_to_user(buf, &val, sizeof(val))) 1037 1.1 riastrad goto read_err; 1038 1.1 riastrad 1039 1.1 riastrad filled = 4; 1040 1.1 riastrad } else if (count >= 2 && !(*ppos % 2)) { 1041 1.1 riastrad u16 val; 1042 1.1 riastrad 1043 1.1 riastrad ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val), 1044 1.1 riastrad ppos, false); 1045 1.1 riastrad if (ret <= 0) 1046 1.1 riastrad goto read_err; 1047 1.1 riastrad 1048 1.1 riastrad if (copy_to_user(buf, &val, sizeof(val))) 1049 1.1 riastrad goto read_err; 1050 1.1 riastrad 1051 1.1 riastrad filled = 2; 1052 1.1 riastrad } else { 1053 1.1 riastrad u8 val; 1054 1.1 riastrad 1055 1.1 riastrad ret = intel_vgpu_rw(mdev, &val, sizeof(val), ppos, 1056 1.1 riastrad false); 1057 1.1 riastrad if (ret <= 0) 1058 1.1 riastrad goto read_err; 1059 1.1 riastrad 1060 1.1 riastrad if (copy_to_user(buf, &val, sizeof(val))) 1061 1.1 riastrad goto read_err; 1062 1.1 riastrad 1063 1.1 riastrad filled = 1; 1064 1.1 riastrad } 1065 1.1 riastrad 1066 1.1 riastrad count -= filled; 1067 1.1 riastrad done += filled; 1068 1.1 riastrad *ppos += filled; 1069 1.1 riastrad buf += filled; 1070 1.1 riastrad } 1071 1.1 riastrad 1072 1.1 riastrad return done; 1073 1.1 riastrad 1074 1.1 riastrad read_err: 1075 1.1 riastrad return -EFAULT; 1076 1.1 riastrad } 1077 1.1 riastrad 1078 1.1 riastrad static ssize_t intel_vgpu_write(struct mdev_device *mdev, 1079 1.1 riastrad const char __user *buf, 1080 1.1 riastrad size_t count, loff_t *ppos) 1081 1.1 riastrad { 1082 1.1 riastrad unsigned int done = 0; 1083 1.1 riastrad int ret; 1084 1.1 riastrad 1085 1.1 riastrad while (count) { 1086 1.1 riastrad size_t filled; 1087 1.1 riastrad 1088 1.1 riastrad /* Only support GGTT entry 8 bytes write */ 1089 1.1 riastrad if (count >= 8 && !(*ppos % 8) && 1090 1.1 riastrad gtt_entry(mdev, ppos)) { 1091 1.1 riastrad u64 val; 1092 1.1 riastrad 1093 1.1 riastrad if (copy_from_user(&val, buf, sizeof(val))) 1094 1.1 riastrad goto write_err; 1095 1.1 riastrad 1096 1.1 riastrad ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val), 1097 1.1 riastrad ppos, true); 1098 1.1 riastrad if (ret <= 0) 1099 1.1 riastrad goto write_err; 1100 1.1 riastrad 1101 1.1 riastrad filled = 8; 1102 1.1 riastrad } else if (count >= 4 && !(*ppos % 4)) { 1103 1.1 riastrad u32 val; 1104 1.1 riastrad 1105 1.1 riastrad if (copy_from_user(&val, buf, sizeof(val))) 1106 1.1 riastrad goto write_err; 1107 1.1 riastrad 1108 1.1 riastrad ret = intel_vgpu_rw(mdev, (char *)&val, sizeof(val), 1109 1.1 riastrad ppos, true); 1110 1.1 riastrad if (ret <= 0) 1111 1.1 riastrad goto write_err; 1112 1.1 riastrad 1113 1.1 riastrad filled = 4; 1114 1.1 riastrad } else if (count >= 2 && !(*ppos % 2)) { 1115 1.1 riastrad u16 val; 1116 1.1 riastrad 1117 1.1 riastrad if (copy_from_user(&val, buf, sizeof(val))) 1118 1.1 riastrad goto write_err; 1119 1.1 riastrad 1120 1.1 riastrad ret = intel_vgpu_rw(mdev, (char *)&val, 1121 1.1 riastrad sizeof(val), ppos, true); 1122 1.1 riastrad if (ret <= 0) 1123 1.1 riastrad goto write_err; 1124 1.1 riastrad 1125 1.1 riastrad filled = 2; 1126 1.1 riastrad } else { 1127 1.1 riastrad u8 val; 1128 1.1 riastrad 1129 1.1 riastrad if (copy_from_user(&val, buf, sizeof(val))) 1130 1.1 riastrad goto write_err; 1131 1.1 riastrad 1132 1.1 riastrad ret = intel_vgpu_rw(mdev, &val, sizeof(val), 1133 1.1 riastrad ppos, true); 1134 1.1 riastrad if (ret <= 0) 1135 1.1 riastrad goto write_err; 1136 1.1 riastrad 1137 1.1 riastrad filled = 1; 1138 1.1 riastrad } 1139 1.1 riastrad 1140 1.1 riastrad count -= filled; 1141 1.1 riastrad done += filled; 1142 1.1 riastrad *ppos += filled; 1143 1.1 riastrad buf += filled; 1144 1.1 riastrad } 1145 1.1 riastrad 1146 1.1 riastrad return done; 1147 1.1 riastrad write_err: 1148 1.1 riastrad return -EFAULT; 1149 1.1 riastrad } 1150 1.1 riastrad 1151 1.1 riastrad static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma) 1152 1.1 riastrad { 1153 1.1 riastrad unsigned int index; 1154 1.1 riastrad u64 virtaddr; 1155 1.1 riastrad unsigned long req_size, pgoff, req_start; 1156 1.1 riastrad pgprot_t pg_prot; 1157 1.1 riastrad struct intel_vgpu *vgpu = mdev_get_drvdata(mdev); 1158 1.1 riastrad 1159 1.1 riastrad index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); 1160 1.1 riastrad if (index >= VFIO_PCI_ROM_REGION_INDEX) 1161 1.1 riastrad return -EINVAL; 1162 1.1 riastrad 1163 1.1 riastrad if (vma->vm_end < vma->vm_start) 1164 1.1 riastrad return -EINVAL; 1165 1.1 riastrad if ((vma->vm_flags & VM_SHARED) == 0) 1166 1.1 riastrad return -EINVAL; 1167 1.1 riastrad if (index != VFIO_PCI_BAR2_REGION_INDEX) 1168 1.1 riastrad return -EINVAL; 1169 1.1 riastrad 1170 1.1 riastrad pg_prot = vma->vm_page_prot; 1171 1.1 riastrad virtaddr = vma->vm_start; 1172 1.1 riastrad req_size = vma->vm_end - vma->vm_start; 1173 1.1 riastrad pgoff = vma->vm_pgoff & 1174 1.1 riastrad ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); 1175 1.1 riastrad req_start = pgoff << PAGE_SHIFT; 1176 1.1 riastrad 1177 1.1 riastrad if (!intel_vgpu_in_aperture(vgpu, req_start)) 1178 1.1 riastrad return -EINVAL; 1179 1.1 riastrad if (req_start + req_size > 1180 1.1 riastrad vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu)) 1181 1.1 riastrad return -EINVAL; 1182 1.1 riastrad 1183 1.1 riastrad pgoff = (gvt_aperture_pa_base(vgpu->gvt) >> PAGE_SHIFT) + pgoff; 1184 1.1 riastrad 1185 1.1 riastrad return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot); 1186 1.1 riastrad } 1187 1.1 riastrad 1188 1.1 riastrad static int intel_vgpu_get_irq_count(struct intel_vgpu *vgpu, int type) 1189 1.1 riastrad { 1190 1.1 riastrad if (type == VFIO_PCI_INTX_IRQ_INDEX || type == VFIO_PCI_MSI_IRQ_INDEX) 1191 1.1 riastrad return 1; 1192 1.1 riastrad 1193 1.1 riastrad return 0; 1194 1.1 riastrad } 1195 1.1 riastrad 1196 1.1 riastrad static int intel_vgpu_set_intx_mask(struct intel_vgpu *vgpu, 1197 1.1 riastrad unsigned int index, unsigned int start, 1198 1.1 riastrad unsigned int count, u32 flags, 1199 1.1 riastrad void *data) 1200 1.1 riastrad { 1201 1.1 riastrad return 0; 1202 1.1 riastrad } 1203 1.1 riastrad 1204 1.1 riastrad static int intel_vgpu_set_intx_unmask(struct intel_vgpu *vgpu, 1205 1.1 riastrad unsigned int index, unsigned int start, 1206 1.1 riastrad unsigned int count, u32 flags, void *data) 1207 1.1 riastrad { 1208 1.1 riastrad return 0; 1209 1.1 riastrad } 1210 1.1 riastrad 1211 1.1 riastrad static int intel_vgpu_set_intx_trigger(struct intel_vgpu *vgpu, 1212 1.1 riastrad unsigned int index, unsigned int start, unsigned int count, 1213 1.1 riastrad u32 flags, void *data) 1214 1.1 riastrad { 1215 1.1 riastrad return 0; 1216 1.1 riastrad } 1217 1.1 riastrad 1218 1.1 riastrad static int intel_vgpu_set_msi_trigger(struct intel_vgpu *vgpu, 1219 1.1 riastrad unsigned int index, unsigned int start, unsigned int count, 1220 1.1 riastrad u32 flags, void *data) 1221 1.1 riastrad { 1222 1.1 riastrad struct eventfd_ctx *trigger; 1223 1.1 riastrad 1224 1.1 riastrad if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { 1225 1.1 riastrad int fd = *(int *)data; 1226 1.1 riastrad 1227 1.1 riastrad trigger = eventfd_ctx_fdget(fd); 1228 1.1 riastrad if (IS_ERR(trigger)) { 1229 1.1 riastrad gvt_vgpu_err("eventfd_ctx_fdget failed\n"); 1230 1.1 riastrad return PTR_ERR(trigger); 1231 1.1 riastrad } 1232 1.1 riastrad vgpu->vdev.msi_trigger = trigger; 1233 1.1 riastrad } else if ((flags & VFIO_IRQ_SET_DATA_NONE) && !count) 1234 1.1 riastrad intel_vgpu_release_msi_eventfd_ctx(vgpu); 1235 1.1 riastrad 1236 1.1 riastrad return 0; 1237 1.1 riastrad } 1238 1.1 riastrad 1239 1.1 riastrad static int intel_vgpu_set_irqs(struct intel_vgpu *vgpu, u32 flags, 1240 1.1 riastrad unsigned int index, unsigned int start, unsigned int count, 1241 1.1 riastrad void *data) 1242 1.1 riastrad { 1243 1.1 riastrad int (*func)(struct intel_vgpu *vgpu, unsigned int index, 1244 1.1 riastrad unsigned int start, unsigned int count, u32 flags, 1245 1.1 riastrad void *data) = NULL; 1246 1.1 riastrad 1247 1.1 riastrad switch (index) { 1248 1.1 riastrad case VFIO_PCI_INTX_IRQ_INDEX: 1249 1.1 riastrad switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) { 1250 1.1 riastrad case VFIO_IRQ_SET_ACTION_MASK: 1251 1.1 riastrad func = intel_vgpu_set_intx_mask; 1252 1.1 riastrad break; 1253 1.1 riastrad case VFIO_IRQ_SET_ACTION_UNMASK: 1254 1.1 riastrad func = intel_vgpu_set_intx_unmask; 1255 1.1 riastrad break; 1256 1.1 riastrad case VFIO_IRQ_SET_ACTION_TRIGGER: 1257 1.1 riastrad func = intel_vgpu_set_intx_trigger; 1258 1.1 riastrad break; 1259 1.1 riastrad } 1260 1.1 riastrad break; 1261 1.1 riastrad case VFIO_PCI_MSI_IRQ_INDEX: 1262 1.1 riastrad switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) { 1263 1.1 riastrad case VFIO_IRQ_SET_ACTION_MASK: 1264 1.1 riastrad case VFIO_IRQ_SET_ACTION_UNMASK: 1265 1.1 riastrad /* XXX Need masking support exported */ 1266 1.1 riastrad break; 1267 1.1 riastrad case VFIO_IRQ_SET_ACTION_TRIGGER: 1268 1.1 riastrad func = intel_vgpu_set_msi_trigger; 1269 1.1 riastrad break; 1270 1.1 riastrad } 1271 1.1 riastrad break; 1272 1.1 riastrad } 1273 1.1 riastrad 1274 1.1 riastrad if (!func) 1275 1.1 riastrad return -ENOTTY; 1276 1.1 riastrad 1277 1.1 riastrad return func(vgpu, index, start, count, flags, data); 1278 1.1 riastrad } 1279 1.1 riastrad 1280 1.1 riastrad static long intel_vgpu_ioctl(struct mdev_device *mdev, unsigned int cmd, 1281 1.1 riastrad unsigned long arg) 1282 1.1 riastrad { 1283 1.1 riastrad struct intel_vgpu *vgpu = mdev_get_drvdata(mdev); 1284 1.1 riastrad unsigned long minsz; 1285 1.1 riastrad 1286 1.1 riastrad gvt_dbg_core("vgpu%d ioctl, cmd: %d\n", vgpu->id, cmd); 1287 1.1 riastrad 1288 1.1 riastrad if (cmd == VFIO_DEVICE_GET_INFO) { 1289 1.1 riastrad struct vfio_device_info info; 1290 1.1 riastrad 1291 1.1 riastrad minsz = offsetofend(struct vfio_device_info, num_irqs); 1292 1.1 riastrad 1293 1.1 riastrad if (copy_from_user(&info, (void __user *)arg, minsz)) 1294 1.1 riastrad return -EFAULT; 1295 1.1 riastrad 1296 1.1 riastrad if (info.argsz < minsz) 1297 1.1 riastrad return -EINVAL; 1298 1.1 riastrad 1299 1.1 riastrad info.flags = VFIO_DEVICE_FLAGS_PCI; 1300 1.1 riastrad info.flags |= VFIO_DEVICE_FLAGS_RESET; 1301 1.1 riastrad info.num_regions = VFIO_PCI_NUM_REGIONS + 1302 1.1 riastrad vgpu->vdev.num_regions; 1303 1.1 riastrad info.num_irqs = VFIO_PCI_NUM_IRQS; 1304 1.1 riastrad 1305 1.1 riastrad return copy_to_user((void __user *)arg, &info, minsz) ? 1306 1.1 riastrad -EFAULT : 0; 1307 1.1 riastrad 1308 1.1 riastrad } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) { 1309 1.1 riastrad struct vfio_region_info info; 1310 1.1 riastrad struct vfio_info_cap caps = { .buf = NULL, .size = 0 }; 1311 1.1 riastrad unsigned int i; 1312 1.1 riastrad int ret; 1313 1.1 riastrad struct vfio_region_info_cap_sparse_mmap *sparse = NULL; 1314 1.1 riastrad int nr_areas = 1; 1315 1.1 riastrad int cap_type_id; 1316 1.1 riastrad 1317 1.1 riastrad minsz = offsetofend(struct vfio_region_info, offset); 1318 1.1 riastrad 1319 1.1 riastrad if (copy_from_user(&info, (void __user *)arg, minsz)) 1320 1.1 riastrad return -EFAULT; 1321 1.1 riastrad 1322 1.1 riastrad if (info.argsz < minsz) 1323 1.1 riastrad return -EINVAL; 1324 1.1 riastrad 1325 1.1 riastrad switch (info.index) { 1326 1.1 riastrad case VFIO_PCI_CONFIG_REGION_INDEX: 1327 1.1 riastrad info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); 1328 1.1 riastrad info.size = vgpu->gvt->device_info.cfg_space_size; 1329 1.1 riastrad info.flags = VFIO_REGION_INFO_FLAG_READ | 1330 1.1 riastrad VFIO_REGION_INFO_FLAG_WRITE; 1331 1.1 riastrad break; 1332 1.1 riastrad case VFIO_PCI_BAR0_REGION_INDEX: 1333 1.1 riastrad info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); 1334 1.1 riastrad info.size = vgpu->cfg_space.bar[info.index].size; 1335 1.1 riastrad if (!info.size) { 1336 1.1 riastrad info.flags = 0; 1337 1.1 riastrad break; 1338 1.1 riastrad } 1339 1.1 riastrad 1340 1.1 riastrad info.flags = VFIO_REGION_INFO_FLAG_READ | 1341 1.1 riastrad VFIO_REGION_INFO_FLAG_WRITE; 1342 1.1 riastrad break; 1343 1.1 riastrad case VFIO_PCI_BAR1_REGION_INDEX: 1344 1.1 riastrad info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); 1345 1.1 riastrad info.size = 0; 1346 1.1 riastrad info.flags = 0; 1347 1.1 riastrad break; 1348 1.1 riastrad case VFIO_PCI_BAR2_REGION_INDEX: 1349 1.1 riastrad info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); 1350 1.1 riastrad info.flags = VFIO_REGION_INFO_FLAG_CAPS | 1351 1.1 riastrad VFIO_REGION_INFO_FLAG_MMAP | 1352 1.1 riastrad VFIO_REGION_INFO_FLAG_READ | 1353 1.1 riastrad VFIO_REGION_INFO_FLAG_WRITE; 1354 1.1 riastrad info.size = gvt_aperture_sz(vgpu->gvt); 1355 1.1 riastrad 1356 1.1 riastrad sparse = kzalloc(struct_size(sparse, areas, nr_areas), 1357 1.1 riastrad GFP_KERNEL); 1358 1.1 riastrad if (!sparse) 1359 1.1 riastrad return -ENOMEM; 1360 1.1 riastrad 1361 1.1 riastrad sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP; 1362 1.1 riastrad sparse->header.version = 1; 1363 1.1 riastrad sparse->nr_areas = nr_areas; 1364 1.1 riastrad cap_type_id = VFIO_REGION_INFO_CAP_SPARSE_MMAP; 1365 1.1 riastrad sparse->areas[0].offset = 1366 1.1 riastrad PAGE_ALIGN(vgpu_aperture_offset(vgpu)); 1367 1.1 riastrad sparse->areas[0].size = vgpu_aperture_sz(vgpu); 1368 1.1 riastrad break; 1369 1.1 riastrad 1370 1.1 riastrad case VFIO_PCI_BAR3_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: 1371 1.1 riastrad info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); 1372 1.1 riastrad info.size = 0; 1373 1.1 riastrad info.flags = 0; 1374 1.1 riastrad 1375 1.1 riastrad gvt_dbg_core("get region info bar:%d\n", info.index); 1376 1.1 riastrad break; 1377 1.1 riastrad 1378 1.1 riastrad case VFIO_PCI_ROM_REGION_INDEX: 1379 1.1 riastrad case VFIO_PCI_VGA_REGION_INDEX: 1380 1.1 riastrad info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); 1381 1.1 riastrad info.size = 0; 1382 1.1 riastrad info.flags = 0; 1383 1.1 riastrad 1384 1.1 riastrad gvt_dbg_core("get region info index:%d\n", info.index); 1385 1.1 riastrad break; 1386 1.1 riastrad default: 1387 1.1 riastrad { 1388 1.1 riastrad struct vfio_region_info_cap_type cap_type = { 1389 1.1 riastrad .header.id = VFIO_REGION_INFO_CAP_TYPE, 1390 1.1 riastrad .header.version = 1 }; 1391 1.1 riastrad 1392 1.1 riastrad if (info.index >= VFIO_PCI_NUM_REGIONS + 1393 1.1 riastrad vgpu->vdev.num_regions) 1394 1.1 riastrad return -EINVAL; 1395 1.1 riastrad info.index = 1396 1.1 riastrad array_index_nospec(info.index, 1397 1.1 riastrad VFIO_PCI_NUM_REGIONS + 1398 1.1 riastrad vgpu->vdev.num_regions); 1399 1.1 riastrad 1400 1.1 riastrad i = info.index - VFIO_PCI_NUM_REGIONS; 1401 1.1 riastrad 1402 1.1 riastrad info.offset = 1403 1.1 riastrad VFIO_PCI_INDEX_TO_OFFSET(info.index); 1404 1.1 riastrad info.size = vgpu->vdev.region[i].size; 1405 1.1 riastrad info.flags = vgpu->vdev.region[i].flags; 1406 1.1 riastrad 1407 1.1 riastrad cap_type.type = vgpu->vdev.region[i].type; 1408 1.1 riastrad cap_type.subtype = vgpu->vdev.region[i].subtype; 1409 1.1 riastrad 1410 1.1 riastrad ret = vfio_info_add_capability(&caps, 1411 1.1 riastrad &cap_type.header, 1412 1.1 riastrad sizeof(cap_type)); 1413 1.1 riastrad if (ret) 1414 1.1 riastrad return ret; 1415 1.1 riastrad } 1416 1.1 riastrad } 1417 1.1 riastrad 1418 1.1 riastrad if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && sparse) { 1419 1.1 riastrad switch (cap_type_id) { 1420 1.1 riastrad case VFIO_REGION_INFO_CAP_SPARSE_MMAP: 1421 1.1 riastrad ret = vfio_info_add_capability(&caps, 1422 1.1 riastrad &sparse->header, 1423 1.1 riastrad struct_size(sparse, areas, 1424 1.1 riastrad sparse->nr_areas)); 1425 1.1 riastrad if (ret) { 1426 1.1 riastrad kfree(sparse); 1427 1.1 riastrad return ret; 1428 1.1 riastrad } 1429 1.1 riastrad break; 1430 1.1 riastrad default: 1431 1.1 riastrad kfree(sparse); 1432 1.1 riastrad return -EINVAL; 1433 1.1 riastrad } 1434 1.1 riastrad } 1435 1.1 riastrad 1436 1.1 riastrad if (caps.size) { 1437 1.1 riastrad info.flags |= VFIO_REGION_INFO_FLAG_CAPS; 1438 1.1 riastrad if (info.argsz < sizeof(info) + caps.size) { 1439 1.1 riastrad info.argsz = sizeof(info) + caps.size; 1440 1.1 riastrad info.cap_offset = 0; 1441 1.1 riastrad } else { 1442 1.1 riastrad vfio_info_cap_shift(&caps, sizeof(info)); 1443 1.1 riastrad if (copy_to_user((void __user *)arg + 1444 1.1 riastrad sizeof(info), caps.buf, 1445 1.1 riastrad caps.size)) { 1446 1.1 riastrad kfree(caps.buf); 1447 1.1 riastrad kfree(sparse); 1448 1.1 riastrad return -EFAULT; 1449 1.1 riastrad } 1450 1.1 riastrad info.cap_offset = sizeof(info); 1451 1.1 riastrad } 1452 1.1 riastrad 1453 1.1 riastrad kfree(caps.buf); 1454 1.1 riastrad } 1455 1.1 riastrad 1456 1.1 riastrad kfree(sparse); 1457 1.1 riastrad return copy_to_user((void __user *)arg, &info, minsz) ? 1458 1.1 riastrad -EFAULT : 0; 1459 1.1 riastrad } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) { 1460 1.1 riastrad struct vfio_irq_info info; 1461 1.1 riastrad 1462 1.1 riastrad minsz = offsetofend(struct vfio_irq_info, count); 1463 1.1 riastrad 1464 1.1 riastrad if (copy_from_user(&info, (void __user *)arg, minsz)) 1465 1.1 riastrad return -EFAULT; 1466 1.1 riastrad 1467 1.1 riastrad if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS) 1468 1.1 riastrad return -EINVAL; 1469 1.1 riastrad 1470 1.1 riastrad switch (info.index) { 1471 1.1 riastrad case VFIO_PCI_INTX_IRQ_INDEX: 1472 1.1 riastrad case VFIO_PCI_MSI_IRQ_INDEX: 1473 1.1 riastrad break; 1474 1.1 riastrad default: 1475 1.1 riastrad return -EINVAL; 1476 1.1 riastrad } 1477 1.1 riastrad 1478 1.1 riastrad info.flags = VFIO_IRQ_INFO_EVENTFD; 1479 1.1 riastrad 1480 1.1 riastrad info.count = intel_vgpu_get_irq_count(vgpu, info.index); 1481 1.1 riastrad 1482 1.1 riastrad if (info.index == VFIO_PCI_INTX_IRQ_INDEX) 1483 1.1 riastrad info.flags |= (VFIO_IRQ_INFO_MASKABLE | 1484 1.1 riastrad VFIO_IRQ_INFO_AUTOMASKED); 1485 1.1 riastrad else 1486 1.1 riastrad info.flags |= VFIO_IRQ_INFO_NORESIZE; 1487 1.1 riastrad 1488 1.1 riastrad return copy_to_user((void __user *)arg, &info, minsz) ? 1489 1.1 riastrad -EFAULT : 0; 1490 1.1 riastrad } else if (cmd == VFIO_DEVICE_SET_IRQS) { 1491 1.1 riastrad struct vfio_irq_set hdr; 1492 1.1 riastrad u8 *data = NULL; 1493 1.1 riastrad int ret = 0; 1494 1.1 riastrad size_t data_size = 0; 1495 1.1 riastrad 1496 1.1 riastrad minsz = offsetofend(struct vfio_irq_set, count); 1497 1.1 riastrad 1498 1.1 riastrad if (copy_from_user(&hdr, (void __user *)arg, minsz)) 1499 1.1 riastrad return -EFAULT; 1500 1.1 riastrad 1501 1.1 riastrad if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) { 1502 1.1 riastrad int max = intel_vgpu_get_irq_count(vgpu, hdr.index); 1503 1.1 riastrad 1504 1.1 riastrad ret = vfio_set_irqs_validate_and_prepare(&hdr, max, 1505 1.1 riastrad VFIO_PCI_NUM_IRQS, &data_size); 1506 1.1 riastrad if (ret) { 1507 1.1 riastrad gvt_vgpu_err("intel:vfio_set_irqs_validate_and_prepare failed\n"); 1508 1.1 riastrad return -EINVAL; 1509 1.1 riastrad } 1510 1.1 riastrad if (data_size) { 1511 1.1 riastrad data = memdup_user((void __user *)(arg + minsz), 1512 1.1 riastrad data_size); 1513 1.1 riastrad if (IS_ERR(data)) 1514 1.1 riastrad return PTR_ERR(data); 1515 1.1 riastrad } 1516 1.1 riastrad } 1517 1.1 riastrad 1518 1.1 riastrad ret = intel_vgpu_set_irqs(vgpu, hdr.flags, hdr.index, 1519 1.1 riastrad hdr.start, hdr.count, data); 1520 1.1 riastrad kfree(data); 1521 1.1 riastrad 1522 1.1 riastrad return ret; 1523 1.1 riastrad } else if (cmd == VFIO_DEVICE_RESET) { 1524 1.1 riastrad intel_gvt_ops->vgpu_reset(vgpu); 1525 1.1 riastrad return 0; 1526 1.1 riastrad } else if (cmd == VFIO_DEVICE_QUERY_GFX_PLANE) { 1527 1.1 riastrad struct vfio_device_gfx_plane_info dmabuf; 1528 1.1 riastrad int ret = 0; 1529 1.1 riastrad 1530 1.1 riastrad minsz = offsetofend(struct vfio_device_gfx_plane_info, 1531 1.1 riastrad dmabuf_id); 1532 1.1 riastrad if (copy_from_user(&dmabuf, (void __user *)arg, minsz)) 1533 1.1 riastrad return -EFAULT; 1534 1.1 riastrad if (dmabuf.argsz < minsz) 1535 1.1 riastrad return -EINVAL; 1536 1.1 riastrad 1537 1.1 riastrad ret = intel_gvt_ops->vgpu_query_plane(vgpu, &dmabuf); 1538 1.1 riastrad if (ret != 0) 1539 1.1 riastrad return ret; 1540 1.1 riastrad 1541 1.1 riastrad return copy_to_user((void __user *)arg, &dmabuf, minsz) ? 1542 1.1 riastrad -EFAULT : 0; 1543 1.1 riastrad } else if (cmd == VFIO_DEVICE_GET_GFX_DMABUF) { 1544 1.1 riastrad __u32 dmabuf_id; 1545 1.1 riastrad __s32 dmabuf_fd; 1546 1.1 riastrad 1547 1.1 riastrad if (get_user(dmabuf_id, (__u32 __user *)arg)) 1548 1.1 riastrad return -EFAULT; 1549 1.1 riastrad 1550 1.1 riastrad dmabuf_fd = intel_gvt_ops->vgpu_get_dmabuf(vgpu, dmabuf_id); 1551 1.1 riastrad return dmabuf_fd; 1552 1.1 riastrad 1553 1.1 riastrad } 1554 1.1 riastrad 1555 1.1 riastrad return -ENOTTY; 1556 1.1 riastrad } 1557 1.1 riastrad 1558 1.1 riastrad static ssize_t 1559 1.1 riastrad vgpu_id_show(struct device *dev, struct device_attribute *attr, 1560 1.1 riastrad char *buf) 1561 1.1 riastrad { 1562 1.1 riastrad struct mdev_device *mdev = mdev_from_dev(dev); 1563 1.1 riastrad 1564 1.1 riastrad if (mdev) { 1565 1.1 riastrad struct intel_vgpu *vgpu = (struct intel_vgpu *) 1566 1.1 riastrad mdev_get_drvdata(mdev); 1567 1.1 riastrad return sprintf(buf, "%d\n", vgpu->id); 1568 1.1 riastrad } 1569 1.1 riastrad return sprintf(buf, "\n"); 1570 1.1 riastrad } 1571 1.1 riastrad 1572 1.1 riastrad static DEVICE_ATTR_RO(vgpu_id); 1573 1.1 riastrad 1574 1.1 riastrad static struct attribute *intel_vgpu_attrs[] = { 1575 1.1 riastrad &dev_attr_vgpu_id.attr, 1576 1.1 riastrad NULL 1577 1.1 riastrad }; 1578 1.1 riastrad 1579 1.1 riastrad static const struct attribute_group intel_vgpu_group = { 1580 1.1 riastrad .name = "intel_vgpu", 1581 1.1 riastrad .attrs = intel_vgpu_attrs, 1582 1.1 riastrad }; 1583 1.1 riastrad 1584 1.1 riastrad static const struct attribute_group *intel_vgpu_groups[] = { 1585 1.1 riastrad &intel_vgpu_group, 1586 1.1 riastrad NULL, 1587 1.1 riastrad }; 1588 1.1 riastrad 1589 1.1 riastrad static struct mdev_parent_ops intel_vgpu_ops = { 1590 1.1 riastrad .mdev_attr_groups = intel_vgpu_groups, 1591 1.1 riastrad .create = intel_vgpu_create, 1592 1.1 riastrad .remove = intel_vgpu_remove, 1593 1.1 riastrad 1594 1.1 riastrad .open = intel_vgpu_open, 1595 1.1 riastrad .release = intel_vgpu_release, 1596 1.1 riastrad 1597 1.1 riastrad .read = intel_vgpu_read, 1598 1.1 riastrad .write = intel_vgpu_write, 1599 1.1 riastrad .mmap = intel_vgpu_mmap, 1600 1.1 riastrad .ioctl = intel_vgpu_ioctl, 1601 1.1 riastrad }; 1602 1.1 riastrad 1603 1.1 riastrad static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops) 1604 1.1 riastrad { 1605 1.1 riastrad struct attribute **kvm_type_attrs; 1606 1.1 riastrad struct attribute_group **kvm_vgpu_type_groups; 1607 1.1 riastrad 1608 1.1 riastrad intel_gvt_ops = ops; 1609 1.1 riastrad if (!intel_gvt_ops->get_gvt_attrs(&kvm_type_attrs, 1610 1.1 riastrad &kvm_vgpu_type_groups)) 1611 1.1 riastrad return -EFAULT; 1612 1.1 riastrad intel_vgpu_ops.supported_type_groups = kvm_vgpu_type_groups; 1613 1.1 riastrad 1614 1.1 riastrad return mdev_register_device(dev, &intel_vgpu_ops); 1615 1.1 riastrad } 1616 1.1 riastrad 1617 1.1 riastrad static void kvmgt_host_exit(struct device *dev) 1618 1.1 riastrad { 1619 1.1 riastrad mdev_unregister_device(dev); 1620 1.1 riastrad } 1621 1.1 riastrad 1622 1.1 riastrad static int kvmgt_page_track_add(unsigned long handle, u64 gfn) 1623 1.1 riastrad { 1624 1.1 riastrad struct kvmgt_guest_info *info; 1625 1.1 riastrad struct kvm *kvm; 1626 1.1 riastrad struct kvm_memory_slot *slot; 1627 1.1 riastrad int idx; 1628 1.1 riastrad 1629 1.1 riastrad if (!handle_valid(handle)) 1630 1.1 riastrad return -ESRCH; 1631 1.1 riastrad 1632 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 1633 1.1 riastrad kvm = info->kvm; 1634 1.1 riastrad 1635 1.1 riastrad idx = srcu_read_lock(&kvm->srcu); 1636 1.1 riastrad slot = gfn_to_memslot(kvm, gfn); 1637 1.1 riastrad if (!slot) { 1638 1.1 riastrad srcu_read_unlock(&kvm->srcu, idx); 1639 1.1 riastrad return -EINVAL; 1640 1.1 riastrad } 1641 1.1 riastrad 1642 1.1 riastrad spin_lock(&kvm->mmu_lock); 1643 1.1 riastrad 1644 1.1 riastrad if (kvmgt_gfn_is_write_protected(info, gfn)) 1645 1.1 riastrad goto out; 1646 1.1 riastrad 1647 1.1 riastrad kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE); 1648 1.1 riastrad kvmgt_protect_table_add(info, gfn); 1649 1.1 riastrad 1650 1.1 riastrad out: 1651 1.1 riastrad spin_unlock(&kvm->mmu_lock); 1652 1.1 riastrad srcu_read_unlock(&kvm->srcu, idx); 1653 1.1 riastrad return 0; 1654 1.1 riastrad } 1655 1.1 riastrad 1656 1.1 riastrad static int kvmgt_page_track_remove(unsigned long handle, u64 gfn) 1657 1.1 riastrad { 1658 1.1 riastrad struct kvmgt_guest_info *info; 1659 1.1 riastrad struct kvm *kvm; 1660 1.1 riastrad struct kvm_memory_slot *slot; 1661 1.1 riastrad int idx; 1662 1.1 riastrad 1663 1.1 riastrad if (!handle_valid(handle)) 1664 1.1 riastrad return 0; 1665 1.1 riastrad 1666 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 1667 1.1 riastrad kvm = info->kvm; 1668 1.1 riastrad 1669 1.1 riastrad idx = srcu_read_lock(&kvm->srcu); 1670 1.1 riastrad slot = gfn_to_memslot(kvm, gfn); 1671 1.1 riastrad if (!slot) { 1672 1.1 riastrad srcu_read_unlock(&kvm->srcu, idx); 1673 1.1 riastrad return -EINVAL; 1674 1.1 riastrad } 1675 1.1 riastrad 1676 1.1 riastrad spin_lock(&kvm->mmu_lock); 1677 1.1 riastrad 1678 1.1 riastrad if (!kvmgt_gfn_is_write_protected(info, gfn)) 1679 1.1 riastrad goto out; 1680 1.1 riastrad 1681 1.1 riastrad kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE); 1682 1.1 riastrad kvmgt_protect_table_del(info, gfn); 1683 1.1 riastrad 1684 1.1 riastrad out: 1685 1.1 riastrad spin_unlock(&kvm->mmu_lock); 1686 1.1 riastrad srcu_read_unlock(&kvm->srcu, idx); 1687 1.1 riastrad return 0; 1688 1.1 riastrad } 1689 1.1 riastrad 1690 1.1 riastrad static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, 1691 1.1 riastrad const u8 *val, int len, 1692 1.1 riastrad struct kvm_page_track_notifier_node *node) 1693 1.1 riastrad { 1694 1.1 riastrad struct kvmgt_guest_info *info = container_of(node, 1695 1.1 riastrad struct kvmgt_guest_info, track_node); 1696 1.1 riastrad 1697 1.1 riastrad if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa))) 1698 1.1 riastrad intel_gvt_ops->write_protect_handler(info->vgpu, gpa, 1699 1.1 riastrad (void *)val, len); 1700 1.1 riastrad } 1701 1.1 riastrad 1702 1.1 riastrad static void kvmgt_page_track_flush_slot(struct kvm *kvm, 1703 1.1 riastrad struct kvm_memory_slot *slot, 1704 1.1 riastrad struct kvm_page_track_notifier_node *node) 1705 1.1 riastrad { 1706 1.1 riastrad int i; 1707 1.1 riastrad gfn_t gfn; 1708 1.1 riastrad struct kvmgt_guest_info *info = container_of(node, 1709 1.1 riastrad struct kvmgt_guest_info, track_node); 1710 1.1 riastrad 1711 1.1 riastrad spin_lock(&kvm->mmu_lock); 1712 1.1 riastrad for (i = 0; i < slot->npages; i++) { 1713 1.1 riastrad gfn = slot->base_gfn + i; 1714 1.1 riastrad if (kvmgt_gfn_is_write_protected(info, gfn)) { 1715 1.1 riastrad kvm_slot_page_track_remove_page(kvm, slot, gfn, 1716 1.1 riastrad KVM_PAGE_TRACK_WRITE); 1717 1.1 riastrad kvmgt_protect_table_del(info, gfn); 1718 1.1 riastrad } 1719 1.1 riastrad } 1720 1.1 riastrad spin_unlock(&kvm->mmu_lock); 1721 1.1 riastrad } 1722 1.1 riastrad 1723 1.1 riastrad static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm) 1724 1.1 riastrad { 1725 1.1 riastrad struct intel_vgpu *itr; 1726 1.1 riastrad struct kvmgt_guest_info *info; 1727 1.1 riastrad int id; 1728 1.1 riastrad bool ret = false; 1729 1.1 riastrad 1730 1.1 riastrad mutex_lock(&vgpu->gvt->lock); 1731 1.1 riastrad for_each_active_vgpu(vgpu->gvt, itr, id) { 1732 1.1 riastrad if (!handle_valid(itr->handle)) 1733 1.1 riastrad continue; 1734 1.1 riastrad 1735 1.1 riastrad info = (struct kvmgt_guest_info *)itr->handle; 1736 1.1 riastrad if (kvm && kvm == info->kvm) { 1737 1.1 riastrad ret = true; 1738 1.1 riastrad goto out; 1739 1.1 riastrad } 1740 1.1 riastrad } 1741 1.1 riastrad out: 1742 1.1 riastrad mutex_unlock(&vgpu->gvt->lock); 1743 1.1 riastrad return ret; 1744 1.1 riastrad } 1745 1.1 riastrad 1746 1.1 riastrad static int kvmgt_guest_init(struct mdev_device *mdev) 1747 1.1 riastrad { 1748 1.1 riastrad struct kvmgt_guest_info *info; 1749 1.1 riastrad struct intel_vgpu *vgpu; 1750 1.1 riastrad struct kvm *kvm; 1751 1.1 riastrad 1752 1.1 riastrad vgpu = mdev_get_drvdata(mdev); 1753 1.1 riastrad if (handle_valid(vgpu->handle)) 1754 1.1 riastrad return -EEXIST; 1755 1.1 riastrad 1756 1.1 riastrad kvm = vgpu->vdev.kvm; 1757 1.1 riastrad if (!kvm || kvm->mm != current->mm) { 1758 1.1 riastrad gvt_vgpu_err("KVM is required to use Intel vGPU\n"); 1759 1.1 riastrad return -ESRCH; 1760 1.1 riastrad } 1761 1.1 riastrad 1762 1.1 riastrad if (__kvmgt_vgpu_exist(vgpu, kvm)) 1763 1.1 riastrad return -EEXIST; 1764 1.1 riastrad 1765 1.1 riastrad info = vzalloc(sizeof(struct kvmgt_guest_info)); 1766 1.1 riastrad if (!info) 1767 1.1 riastrad return -ENOMEM; 1768 1.1 riastrad 1769 1.1 riastrad vgpu->handle = (unsigned long)info; 1770 1.1 riastrad info->vgpu = vgpu; 1771 1.1 riastrad info->kvm = kvm; 1772 1.1 riastrad kvm_get_kvm(info->kvm); 1773 1.1 riastrad 1774 1.1 riastrad kvmgt_protect_table_init(info); 1775 1.1 riastrad gvt_cache_init(vgpu); 1776 1.1 riastrad 1777 1.1 riastrad init_completion(&vgpu->vblank_done); 1778 1.1 riastrad 1779 1.1 riastrad info->track_node.track_write = kvmgt_page_track_write; 1780 1.1 riastrad info->track_node.track_flush_slot = kvmgt_page_track_flush_slot; 1781 1.1 riastrad kvm_page_track_register_notifier(kvm, &info->track_node); 1782 1.1 riastrad 1783 1.1 riastrad info->debugfs_cache_entries = debugfs_create_ulong( 1784 1.1 riastrad "kvmgt_nr_cache_entries", 1785 1.1 riastrad 0444, vgpu->debugfs, 1786 1.1 riastrad &vgpu->vdev.nr_cache_entries); 1787 1.1 riastrad return 0; 1788 1.1 riastrad } 1789 1.1 riastrad 1790 1.1 riastrad static bool kvmgt_guest_exit(struct kvmgt_guest_info *info) 1791 1.1 riastrad { 1792 1.1 riastrad debugfs_remove(info->debugfs_cache_entries); 1793 1.1 riastrad 1794 1.1 riastrad kvm_page_track_unregister_notifier(info->kvm, &info->track_node); 1795 1.1 riastrad kvm_put_kvm(info->kvm); 1796 1.1 riastrad kvmgt_protect_table_destroy(info); 1797 1.1 riastrad gvt_cache_destroy(info->vgpu); 1798 1.1 riastrad vfree(info); 1799 1.1 riastrad 1800 1.1 riastrad return true; 1801 1.1 riastrad } 1802 1.1 riastrad 1803 1.1 riastrad static int kvmgt_attach_vgpu(void *vgpu, unsigned long *handle) 1804 1.1 riastrad { 1805 1.1 riastrad /* nothing to do here */ 1806 1.1 riastrad return 0; 1807 1.1 riastrad } 1808 1.1 riastrad 1809 1.1 riastrad static void kvmgt_detach_vgpu(void *p_vgpu) 1810 1.1 riastrad { 1811 1.1 riastrad int i; 1812 1.1 riastrad struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu; 1813 1.1 riastrad 1814 1.1 riastrad if (!vgpu->vdev.region) 1815 1.1 riastrad return; 1816 1.1 riastrad 1817 1.1 riastrad for (i = 0; i < vgpu->vdev.num_regions; i++) 1818 1.1 riastrad if (vgpu->vdev.region[i].ops->release) 1819 1.1 riastrad vgpu->vdev.region[i].ops->release(vgpu, 1820 1.1 riastrad &vgpu->vdev.region[i]); 1821 1.1 riastrad vgpu->vdev.num_regions = 0; 1822 1.1 riastrad kfree(vgpu->vdev.region); 1823 1.1 riastrad vgpu->vdev.region = NULL; 1824 1.1 riastrad } 1825 1.1 riastrad 1826 1.1 riastrad static int kvmgt_inject_msi(unsigned long handle, u32 addr, u16 data) 1827 1.1 riastrad { 1828 1.1 riastrad struct kvmgt_guest_info *info; 1829 1.1 riastrad struct intel_vgpu *vgpu; 1830 1.1 riastrad 1831 1.1 riastrad if (!handle_valid(handle)) 1832 1.1 riastrad return -ESRCH; 1833 1.1 riastrad 1834 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 1835 1.1 riastrad vgpu = info->vgpu; 1836 1.1 riastrad 1837 1.1 riastrad /* 1838 1.1 riastrad * When guest is poweroff, msi_trigger is set to NULL, but vgpu's 1839 1.1 riastrad * config and mmio register isn't restored to default during guest 1840 1.1 riastrad * poweroff. If this vgpu is still used in next vm, this vgpu's pipe 1841 1.1 riastrad * may be enabled, then once this vgpu is active, it will get inject 1842 1.1 riastrad * vblank interrupt request. But msi_trigger is null until msi is 1843 1.1 riastrad * enabled by guest. so if msi_trigger is null, success is still 1844 1.1 riastrad * returned and don't inject interrupt into guest. 1845 1.1 riastrad */ 1846 1.1 riastrad if (vgpu->vdev.msi_trigger == NULL) 1847 1.1 riastrad return 0; 1848 1.1 riastrad 1849 1.1 riastrad if (eventfd_signal(vgpu->vdev.msi_trigger, 1) == 1) 1850 1.1 riastrad return 0; 1851 1.1 riastrad 1852 1.1 riastrad return -EFAULT; 1853 1.1 riastrad } 1854 1.1 riastrad 1855 1.1 riastrad static unsigned long kvmgt_gfn_to_pfn(unsigned long handle, unsigned long gfn) 1856 1.1 riastrad { 1857 1.1 riastrad struct kvmgt_guest_info *info; 1858 1.1 riastrad kvm_pfn_t pfn; 1859 1.1 riastrad 1860 1.1 riastrad if (!handle_valid(handle)) 1861 1.1 riastrad return INTEL_GVT_INVALID_ADDR; 1862 1.1 riastrad 1863 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 1864 1.1 riastrad 1865 1.1 riastrad pfn = gfn_to_pfn(info->kvm, gfn); 1866 1.1 riastrad if (is_error_noslot_pfn(pfn)) 1867 1.1 riastrad return INTEL_GVT_INVALID_ADDR; 1868 1.1 riastrad 1869 1.1 riastrad return pfn; 1870 1.1 riastrad } 1871 1.1 riastrad 1872 1.1 riastrad static int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn, 1873 1.1 riastrad unsigned long size, dma_addr_t *dma_addr) 1874 1.1 riastrad { 1875 1.1 riastrad struct kvmgt_guest_info *info; 1876 1.1 riastrad struct intel_vgpu *vgpu; 1877 1.1 riastrad struct gvt_dma *entry; 1878 1.1 riastrad int ret; 1879 1.1 riastrad 1880 1.1 riastrad if (!handle_valid(handle)) 1881 1.1 riastrad return -EINVAL; 1882 1.1 riastrad 1883 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 1884 1.1 riastrad vgpu = info->vgpu; 1885 1.1 riastrad 1886 1.1 riastrad mutex_lock(&info->vgpu->vdev.cache_lock); 1887 1.1 riastrad 1888 1.1 riastrad entry = __gvt_cache_find_gfn(info->vgpu, gfn); 1889 1.1 riastrad if (!entry) { 1890 1.1 riastrad ret = gvt_dma_map_page(vgpu, gfn, dma_addr, size); 1891 1.1 riastrad if (ret) 1892 1.1 riastrad goto err_unlock; 1893 1.1 riastrad 1894 1.1 riastrad ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr, size); 1895 1.1 riastrad if (ret) 1896 1.1 riastrad goto err_unmap; 1897 1.1 riastrad } else if (entry->size != size) { 1898 1.1 riastrad /* the same gfn with different size: unmap and re-map */ 1899 1.1 riastrad gvt_dma_unmap_page(vgpu, gfn, entry->dma_addr, entry->size); 1900 1.1 riastrad __gvt_cache_remove_entry(vgpu, entry); 1901 1.1 riastrad 1902 1.1 riastrad ret = gvt_dma_map_page(vgpu, gfn, dma_addr, size); 1903 1.1 riastrad if (ret) 1904 1.1 riastrad goto err_unlock; 1905 1.1 riastrad 1906 1.1 riastrad ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr, size); 1907 1.1 riastrad if (ret) 1908 1.1 riastrad goto err_unmap; 1909 1.1 riastrad } else { 1910 1.1 riastrad kref_get(&entry->ref); 1911 1.1 riastrad *dma_addr = entry->dma_addr; 1912 1.1 riastrad } 1913 1.1 riastrad 1914 1.1 riastrad mutex_unlock(&info->vgpu->vdev.cache_lock); 1915 1.1 riastrad return 0; 1916 1.1 riastrad 1917 1.1 riastrad err_unmap: 1918 1.1 riastrad gvt_dma_unmap_page(vgpu, gfn, *dma_addr, size); 1919 1.1 riastrad err_unlock: 1920 1.1 riastrad mutex_unlock(&info->vgpu->vdev.cache_lock); 1921 1.1 riastrad return ret; 1922 1.1 riastrad } 1923 1.1 riastrad 1924 1.1 riastrad static int kvmgt_dma_pin_guest_page(unsigned long handle, dma_addr_t dma_addr) 1925 1.1 riastrad { 1926 1.1 riastrad struct kvmgt_guest_info *info; 1927 1.1 riastrad struct gvt_dma *entry; 1928 1.1 riastrad int ret = 0; 1929 1.1 riastrad 1930 1.1 riastrad if (!handle_valid(handle)) 1931 1.1 riastrad return -ENODEV; 1932 1.1 riastrad 1933 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 1934 1.1 riastrad 1935 1.1 riastrad mutex_lock(&info->vgpu->vdev.cache_lock); 1936 1.1 riastrad entry = __gvt_cache_find_dma_addr(info->vgpu, dma_addr); 1937 1.1 riastrad if (entry) 1938 1.1 riastrad kref_get(&entry->ref); 1939 1.1 riastrad else 1940 1.1 riastrad ret = -ENOMEM; 1941 1.1 riastrad mutex_unlock(&info->vgpu->vdev.cache_lock); 1942 1.1 riastrad 1943 1.1 riastrad return ret; 1944 1.1 riastrad } 1945 1.1 riastrad 1946 1.1 riastrad static void __gvt_dma_release(struct kref *ref) 1947 1.1 riastrad { 1948 1.1 riastrad struct gvt_dma *entry = container_of(ref, typeof(*entry), ref); 1949 1.1 riastrad 1950 1.1 riastrad gvt_dma_unmap_page(entry->vgpu, entry->gfn, entry->dma_addr, 1951 1.1 riastrad entry->size); 1952 1.1 riastrad __gvt_cache_remove_entry(entry->vgpu, entry); 1953 1.1 riastrad } 1954 1.1 riastrad 1955 1.1 riastrad static void kvmgt_dma_unmap_guest_page(unsigned long handle, dma_addr_t dma_addr) 1956 1.1 riastrad { 1957 1.1 riastrad struct kvmgt_guest_info *info; 1958 1.1 riastrad struct gvt_dma *entry; 1959 1.1 riastrad 1960 1.1 riastrad if (!handle_valid(handle)) 1961 1.1 riastrad return; 1962 1.1 riastrad 1963 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 1964 1.1 riastrad 1965 1.1 riastrad mutex_lock(&info->vgpu->vdev.cache_lock); 1966 1.1 riastrad entry = __gvt_cache_find_dma_addr(info->vgpu, dma_addr); 1967 1.1 riastrad if (entry) 1968 1.1 riastrad kref_put(&entry->ref, __gvt_dma_release); 1969 1.1 riastrad mutex_unlock(&info->vgpu->vdev.cache_lock); 1970 1.1 riastrad } 1971 1.1 riastrad 1972 1.1 riastrad static int kvmgt_rw_gpa(unsigned long handle, unsigned long gpa, 1973 1.1 riastrad void *buf, unsigned long len, bool write) 1974 1.1 riastrad { 1975 1.1 riastrad struct kvmgt_guest_info *info; 1976 1.1 riastrad struct kvm *kvm; 1977 1.1 riastrad int idx, ret; 1978 1.1 riastrad bool kthread = current->mm == NULL; 1979 1.1 riastrad 1980 1.1 riastrad if (!handle_valid(handle)) 1981 1.1 riastrad return -ESRCH; 1982 1.1 riastrad 1983 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 1984 1.1 riastrad kvm = info->kvm; 1985 1.1 riastrad 1986 1.1 riastrad if (kthread) { 1987 1.1 riastrad if (!mmget_not_zero(kvm->mm)) 1988 1.1 riastrad return -EFAULT; 1989 1.1 riastrad use_mm(kvm->mm); 1990 1.1 riastrad } 1991 1.1 riastrad 1992 1.1 riastrad idx = srcu_read_lock(&kvm->srcu); 1993 1.1 riastrad ret = write ? kvm_write_guest(kvm, gpa, buf, len) : 1994 1.1 riastrad kvm_read_guest(kvm, gpa, buf, len); 1995 1.1 riastrad srcu_read_unlock(&kvm->srcu, idx); 1996 1.1 riastrad 1997 1.1 riastrad if (kthread) { 1998 1.1 riastrad unuse_mm(kvm->mm); 1999 1.1 riastrad mmput(kvm->mm); 2000 1.1 riastrad } 2001 1.1 riastrad 2002 1.1 riastrad return ret; 2003 1.1 riastrad } 2004 1.1 riastrad 2005 1.1 riastrad static int kvmgt_read_gpa(unsigned long handle, unsigned long gpa, 2006 1.1 riastrad void *buf, unsigned long len) 2007 1.1 riastrad { 2008 1.1 riastrad return kvmgt_rw_gpa(handle, gpa, buf, len, false); 2009 1.1 riastrad } 2010 1.1 riastrad 2011 1.1 riastrad static int kvmgt_write_gpa(unsigned long handle, unsigned long gpa, 2012 1.1 riastrad void *buf, unsigned long len) 2013 1.1 riastrad { 2014 1.1 riastrad return kvmgt_rw_gpa(handle, gpa, buf, len, true); 2015 1.1 riastrad } 2016 1.1 riastrad 2017 1.1 riastrad static unsigned long kvmgt_virt_to_pfn(void *addr) 2018 1.1 riastrad { 2019 1.1 riastrad return PFN_DOWN(__pa(addr)); 2020 1.1 riastrad } 2021 1.1 riastrad 2022 1.1 riastrad static bool kvmgt_is_valid_gfn(unsigned long handle, unsigned long gfn) 2023 1.1 riastrad { 2024 1.1 riastrad struct kvmgt_guest_info *info; 2025 1.1 riastrad struct kvm *kvm; 2026 1.1 riastrad int idx; 2027 1.1 riastrad bool ret; 2028 1.1 riastrad 2029 1.1 riastrad if (!handle_valid(handle)) 2030 1.1 riastrad return false; 2031 1.1 riastrad 2032 1.1 riastrad info = (struct kvmgt_guest_info *)handle; 2033 1.1 riastrad kvm = info->kvm; 2034 1.1 riastrad 2035 1.1 riastrad idx = srcu_read_lock(&kvm->srcu); 2036 1.1 riastrad ret = kvm_is_visible_gfn(kvm, gfn); 2037 1.1 riastrad srcu_read_unlock(&kvm->srcu, idx); 2038 1.1 riastrad 2039 1.1 riastrad return ret; 2040 1.1 riastrad } 2041 1.1 riastrad 2042 1.1 riastrad static struct intel_gvt_mpt kvmgt_mpt = { 2043 1.1 riastrad .type = INTEL_GVT_HYPERVISOR_KVM, 2044 1.1 riastrad .host_init = kvmgt_host_init, 2045 1.1 riastrad .host_exit = kvmgt_host_exit, 2046 1.1 riastrad .attach_vgpu = kvmgt_attach_vgpu, 2047 1.1 riastrad .detach_vgpu = kvmgt_detach_vgpu, 2048 1.1 riastrad .inject_msi = kvmgt_inject_msi, 2049 1.1 riastrad .from_virt_to_mfn = kvmgt_virt_to_pfn, 2050 1.1 riastrad .enable_page_track = kvmgt_page_track_add, 2051 1.1 riastrad .disable_page_track = kvmgt_page_track_remove, 2052 1.1 riastrad .read_gpa = kvmgt_read_gpa, 2053 1.1 riastrad .write_gpa = kvmgt_write_gpa, 2054 1.1 riastrad .gfn_to_mfn = kvmgt_gfn_to_pfn, 2055 1.1 riastrad .dma_map_guest_page = kvmgt_dma_map_guest_page, 2056 1.1 riastrad .dma_unmap_guest_page = kvmgt_dma_unmap_guest_page, 2057 1.1 riastrad .dma_pin_guest_page = kvmgt_dma_pin_guest_page, 2058 1.1 riastrad .set_opregion = kvmgt_set_opregion, 2059 1.1 riastrad .set_edid = kvmgt_set_edid, 2060 1.1 riastrad .get_vfio_device = kvmgt_get_vfio_device, 2061 1.1 riastrad .put_vfio_device = kvmgt_put_vfio_device, 2062 1.1 riastrad .is_valid_gfn = kvmgt_is_valid_gfn, 2063 1.1 riastrad }; 2064 1.1 riastrad 2065 1.1 riastrad static int __init kvmgt_init(void) 2066 1.1 riastrad { 2067 1.1 riastrad if (intel_gvt_register_hypervisor(&kvmgt_mpt) < 0) 2068 1.1 riastrad return -ENODEV; 2069 1.1 riastrad return 0; 2070 1.1 riastrad } 2071 1.1 riastrad 2072 1.1 riastrad static void __exit kvmgt_exit(void) 2073 1.1 riastrad { 2074 1.1 riastrad intel_gvt_unregister_hypervisor(); 2075 1.1 riastrad } 2076 1.1 riastrad 2077 1.1 riastrad module_init(kvmgt_init); 2078 1.1 riastrad module_exit(kvmgt_exit); 2079 1.1 riastrad 2080 1.1 riastrad MODULE_LICENSE("GPL and additional rights"); 2081 1.1 riastrad MODULE_AUTHOR("Intel Corporation"); 2082