10f3e72b5SJason Gunthorpe // SPDX-License-Identifier: GPL-2.0-only 20f3e72b5SJason Gunthorpe /* 30f3e72b5SJason Gunthorpe * VFIO core 40f3e72b5SJason Gunthorpe * 50f3e72b5SJason Gunthorpe * Copyright (C) 2012 Red Hat, Inc. All rights reserved. 60f3e72b5SJason Gunthorpe * Author: Alex Williamson <alex.williamson@redhat.com> 70f3e72b5SJason Gunthorpe * 80f3e72b5SJason Gunthorpe * Derived from original vfio: 90f3e72b5SJason Gunthorpe * Copyright 2010 Cisco Systems, Inc. All rights reserved. 100f3e72b5SJason Gunthorpe * Author: Tom Lyon, pugs@cisco.com 110f3e72b5SJason Gunthorpe */ 120f3e72b5SJason Gunthorpe 130f3e72b5SJason Gunthorpe #include <linux/cdev.h> 140f3e72b5SJason Gunthorpe #include <linux/compat.h> 150f3e72b5SJason Gunthorpe #include <linux/device.h> 160f3e72b5SJason Gunthorpe #include <linux/fs.h> 170f3e72b5SJason Gunthorpe #include <linux/idr.h> 180f3e72b5SJason Gunthorpe #include <linux/iommu.h> 192b48f52fSMatthew Rosato #ifdef CONFIG_HAVE_KVM 202b48f52fSMatthew Rosato #include <linux/kvm_host.h> 212b48f52fSMatthew Rosato #endif 220f3e72b5SJason Gunthorpe #include <linux/list.h> 230f3e72b5SJason Gunthorpe #include <linux/miscdevice.h> 240f3e72b5SJason Gunthorpe #include <linux/module.h> 250f3e72b5SJason Gunthorpe #include <linux/mutex.h> 260f3e72b5SJason Gunthorpe #include <linux/pci.h> 270f3e72b5SJason Gunthorpe #include <linux/rwsem.h> 280f3e72b5SJason Gunthorpe #include <linux/sched.h> 290f3e72b5SJason Gunthorpe #include <linux/slab.h> 300f3e72b5SJason Gunthorpe #include <linux/stat.h> 310f3e72b5SJason Gunthorpe #include <linux/string.h> 320f3e72b5SJason Gunthorpe #include <linux/uaccess.h> 330f3e72b5SJason Gunthorpe #include <linux/vfio.h> 340f3e72b5SJason Gunthorpe #include <linux/wait.h> 350f3e72b5SJason Gunthorpe #include <linux/sched/signal.h> 368e5c6995SAbhishek Sahu #include <linux/pm_runtime.h> 3780c4b92aSYishai Hadas #include <linux/interval_tree.h> 3880c4b92aSYishai Hadas #include <linux/iova_bitmap.h> 392a3dab19SJason Gunthorpe #include <linux/iommufd.h> 400f3e72b5SJason Gunthorpe #include "vfio.h" 410f3e72b5SJason Gunthorpe 420f3e72b5SJason Gunthorpe #define DRIVER_VERSION "0.3" 430f3e72b5SJason Gunthorpe #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>" 440f3e72b5SJason Gunthorpe #define DRIVER_DESC "VFIO - User Level meta-driver" 450f3e72b5SJason Gunthorpe 460f3e72b5SJason Gunthorpe static struct vfio { 473c28a761SYi Liu struct class *device_class; 483c28a761SYi Liu struct ida device_ida; 490f3e72b5SJason Gunthorpe } vfio; 500f3e72b5SJason Gunthorpe 510f3e72b5SJason Gunthorpe static DEFINE_XARRAY(vfio_device_set_xa); 520f3e72b5SJason Gunthorpe 530f3e72b5SJason Gunthorpe int vfio_assign_device_set(struct vfio_device *device, void *set_id) 540f3e72b5SJason Gunthorpe { 550f3e72b5SJason Gunthorpe unsigned long idx = (unsigned long)set_id; 560f3e72b5SJason Gunthorpe struct vfio_device_set *new_dev_set; 570f3e72b5SJason Gunthorpe struct vfio_device_set *dev_set; 580f3e72b5SJason Gunthorpe 590f3e72b5SJason Gunthorpe if (WARN_ON(!set_id)) 600f3e72b5SJason Gunthorpe return -EINVAL; 610f3e72b5SJason Gunthorpe 620f3e72b5SJason Gunthorpe /* 630f3e72b5SJason Gunthorpe * Atomically acquire a singleton object in the xarray for this set_id 640f3e72b5SJason Gunthorpe */ 650f3e72b5SJason Gunthorpe xa_lock(&vfio_device_set_xa); 660f3e72b5SJason Gunthorpe dev_set = xa_load(&vfio_device_set_xa, idx); 670f3e72b5SJason Gunthorpe if (dev_set) 680f3e72b5SJason Gunthorpe goto found_get_ref; 690f3e72b5SJason Gunthorpe xa_unlock(&vfio_device_set_xa); 700f3e72b5SJason Gunthorpe 710f3e72b5SJason Gunthorpe new_dev_set = kzalloc(sizeof(*new_dev_set), GFP_KERNEL); 720f3e72b5SJason Gunthorpe if (!new_dev_set) 730f3e72b5SJason Gunthorpe return -ENOMEM; 740f3e72b5SJason Gunthorpe mutex_init(&new_dev_set->lock); 750f3e72b5SJason Gunthorpe INIT_LIST_HEAD(&new_dev_set->device_list); 760f3e72b5SJason Gunthorpe new_dev_set->set_id = set_id; 770f3e72b5SJason Gunthorpe 780f3e72b5SJason Gunthorpe xa_lock(&vfio_device_set_xa); 790f3e72b5SJason Gunthorpe dev_set = __xa_cmpxchg(&vfio_device_set_xa, idx, NULL, new_dev_set, 800f3e72b5SJason Gunthorpe GFP_KERNEL); 810f3e72b5SJason Gunthorpe if (!dev_set) { 820f3e72b5SJason Gunthorpe dev_set = new_dev_set; 830f3e72b5SJason Gunthorpe goto found_get_ref; 840f3e72b5SJason Gunthorpe } 850f3e72b5SJason Gunthorpe 860f3e72b5SJason Gunthorpe kfree(new_dev_set); 870f3e72b5SJason Gunthorpe if (xa_is_err(dev_set)) { 880f3e72b5SJason Gunthorpe xa_unlock(&vfio_device_set_xa); 890f3e72b5SJason Gunthorpe return xa_err(dev_set); 900f3e72b5SJason Gunthorpe } 910f3e72b5SJason Gunthorpe 920f3e72b5SJason Gunthorpe found_get_ref: 930f3e72b5SJason Gunthorpe dev_set->device_count++; 940f3e72b5SJason Gunthorpe xa_unlock(&vfio_device_set_xa); 950f3e72b5SJason Gunthorpe mutex_lock(&dev_set->lock); 960f3e72b5SJason Gunthorpe device->dev_set = dev_set; 970f3e72b5SJason Gunthorpe list_add_tail(&device->dev_set_list, &dev_set->device_list); 980f3e72b5SJason Gunthorpe mutex_unlock(&dev_set->lock); 990f3e72b5SJason Gunthorpe return 0; 1000f3e72b5SJason Gunthorpe } 1010f3e72b5SJason Gunthorpe EXPORT_SYMBOL_GPL(vfio_assign_device_set); 1020f3e72b5SJason Gunthorpe 1030f3e72b5SJason Gunthorpe static void vfio_release_device_set(struct vfio_device *device) 1040f3e72b5SJason Gunthorpe { 1050f3e72b5SJason Gunthorpe struct vfio_device_set *dev_set = device->dev_set; 1060f3e72b5SJason Gunthorpe 1070f3e72b5SJason Gunthorpe if (!dev_set) 1080f3e72b5SJason Gunthorpe return; 1090f3e72b5SJason Gunthorpe 1100f3e72b5SJason Gunthorpe mutex_lock(&dev_set->lock); 1110f3e72b5SJason Gunthorpe list_del(&device->dev_set_list); 1120f3e72b5SJason Gunthorpe mutex_unlock(&dev_set->lock); 1130f3e72b5SJason Gunthorpe 1140f3e72b5SJason Gunthorpe xa_lock(&vfio_device_set_xa); 1150f3e72b5SJason Gunthorpe if (!--dev_set->device_count) { 1160f3e72b5SJason Gunthorpe __xa_erase(&vfio_device_set_xa, 1170f3e72b5SJason Gunthorpe (unsigned long)dev_set->set_id); 1180f3e72b5SJason Gunthorpe mutex_destroy(&dev_set->lock); 1190f3e72b5SJason Gunthorpe kfree(dev_set); 1200f3e72b5SJason Gunthorpe } 1210f3e72b5SJason Gunthorpe xa_unlock(&vfio_device_set_xa); 1220f3e72b5SJason Gunthorpe } 1230f3e72b5SJason Gunthorpe 1245cd189e4SAnthony DeRossi unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set) 1255cd189e4SAnthony DeRossi { 1265cd189e4SAnthony DeRossi struct vfio_device *cur; 1275cd189e4SAnthony DeRossi unsigned int open_count = 0; 1285cd189e4SAnthony DeRossi 1295cd189e4SAnthony DeRossi lockdep_assert_held(&dev_set->lock); 1305cd189e4SAnthony DeRossi 1315cd189e4SAnthony DeRossi list_for_each_entry(cur, &dev_set->device_list, dev_set_list) 1325cd189e4SAnthony DeRossi open_count += cur->open_count; 1335cd189e4SAnthony DeRossi return open_count; 1345cd189e4SAnthony DeRossi } 1355cd189e4SAnthony DeRossi EXPORT_SYMBOL_GPL(vfio_device_set_open_count); 1365cd189e4SAnthony DeRossi 1370f3e72b5SJason Gunthorpe /* 1380f3e72b5SJason Gunthorpe * Device objects - create, release, get, put, search 1390f3e72b5SJason Gunthorpe */ 1400f3e72b5SJason Gunthorpe /* Device reference always implies a group reference */ 1419eefba80SYi Liu void vfio_device_put_registration(struct vfio_device *device) 1420f3e72b5SJason Gunthorpe { 1430f3e72b5SJason Gunthorpe if (refcount_dec_and_test(&device->refcount)) 1440f3e72b5SJason Gunthorpe complete(&device->comp); 1450f3e72b5SJason Gunthorpe } 1460f3e72b5SJason Gunthorpe 1479eefba80SYi Liu bool vfio_device_try_get_registration(struct vfio_device *device) 1480f3e72b5SJason Gunthorpe { 1490f3e72b5SJason Gunthorpe return refcount_inc_not_zero(&device->refcount); 1500f3e72b5SJason Gunthorpe } 1510f3e72b5SJason Gunthorpe 1520f3e72b5SJason Gunthorpe /* 1530f3e72b5SJason Gunthorpe * VFIO driver API 1540f3e72b5SJason Gunthorpe */ 155cb9ff3f3SKevin Tian /* Release helper called by vfio_put_device() */ 1563c28a761SYi Liu static void vfio_device_release(struct device *dev) 157cb9ff3f3SKevin Tian { 158cb9ff3f3SKevin Tian struct vfio_device *device = 1593c28a761SYi Liu container_of(dev, struct vfio_device, device); 160cb9ff3f3SKevin Tian 161ebb72b76SKevin Tian vfio_release_device_set(device); 1623c28a761SYi Liu ida_free(&vfio.device_ida, device->index); 163cb9ff3f3SKevin Tian 164913447d0SEric Farman if (device->ops->release) 165cb9ff3f3SKevin Tian device->ops->release(device); 166913447d0SEric Farman 167913447d0SEric Farman kvfree(device); 168cb9ff3f3SKevin Tian } 169cb9ff3f3SKevin Tian 170d1104f93SEric Farman static int vfio_init_device(struct vfio_device *device, struct device *dev, 171d1104f93SEric Farman const struct vfio_device_ops *ops); 172d1104f93SEric Farman 173cb9ff3f3SKevin Tian /* 174cb9ff3f3SKevin Tian * Allocate and initialize vfio_device so it can be registered to vfio 175cb9ff3f3SKevin Tian * core. 176cb9ff3f3SKevin Tian * 177cb9ff3f3SKevin Tian * Drivers should use the wrapper vfio_alloc_device() for allocation. 178cb9ff3f3SKevin Tian * @size is the size of the structure to be allocated, including any 179cb9ff3f3SKevin Tian * private data used by the driver. 180cb9ff3f3SKevin Tian * 181cb9ff3f3SKevin Tian * Driver may provide an @init callback to cover device private data. 182cb9ff3f3SKevin Tian * 183cb9ff3f3SKevin Tian * Use vfio_put_device() to release the structure after success return. 184cb9ff3f3SKevin Tian */ 185cb9ff3f3SKevin Tian struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev, 186cb9ff3f3SKevin Tian const struct vfio_device_ops *ops) 187cb9ff3f3SKevin Tian { 188cb9ff3f3SKevin Tian struct vfio_device *device; 189cb9ff3f3SKevin Tian int ret; 190cb9ff3f3SKevin Tian 191cb9ff3f3SKevin Tian if (WARN_ON(size < sizeof(struct vfio_device))) 192cb9ff3f3SKevin Tian return ERR_PTR(-EINVAL); 193cb9ff3f3SKevin Tian 194cb9ff3f3SKevin Tian device = kvzalloc(size, GFP_KERNEL); 195cb9ff3f3SKevin Tian if (!device) 196cb9ff3f3SKevin Tian return ERR_PTR(-ENOMEM); 197cb9ff3f3SKevin Tian 198cb9ff3f3SKevin Tian ret = vfio_init_device(device, dev, ops); 199cb9ff3f3SKevin Tian if (ret) 200cb9ff3f3SKevin Tian goto out_free; 201cb9ff3f3SKevin Tian return device; 202cb9ff3f3SKevin Tian 203cb9ff3f3SKevin Tian out_free: 204cb9ff3f3SKevin Tian kvfree(device); 205cb9ff3f3SKevin Tian return ERR_PTR(ret); 206cb9ff3f3SKevin Tian } 207cb9ff3f3SKevin Tian EXPORT_SYMBOL_GPL(_vfio_alloc_device); 208cb9ff3f3SKevin Tian 209cb9ff3f3SKevin Tian /* 210cb9ff3f3SKevin Tian * Initialize a vfio_device so it can be registered to vfio core. 211cb9ff3f3SKevin Tian */ 212d1104f93SEric Farman static int vfio_init_device(struct vfio_device *device, struct device *dev, 213cb9ff3f3SKevin Tian const struct vfio_device_ops *ops) 214cb9ff3f3SKevin Tian { 215cb9ff3f3SKevin Tian int ret; 216cb9ff3f3SKevin Tian 2173c28a761SYi Liu ret = ida_alloc_max(&vfio.device_ida, MINORMASK, GFP_KERNEL); 2183c28a761SYi Liu if (ret < 0) { 2193c28a761SYi Liu dev_dbg(dev, "Error to alloc index\n"); 2203c28a761SYi Liu return ret; 2213c28a761SYi Liu } 2223c28a761SYi Liu 2233c28a761SYi Liu device->index = ret; 224ebb72b76SKevin Tian init_completion(&device->comp); 225ebb72b76SKevin Tian device->dev = dev; 226ebb72b76SKevin Tian device->ops = ops; 227cb9ff3f3SKevin Tian 228cb9ff3f3SKevin Tian if (ops->init) { 229cb9ff3f3SKevin Tian ret = ops->init(device); 230cb9ff3f3SKevin Tian if (ret) 231cb9ff3f3SKevin Tian goto out_uninit; 232cb9ff3f3SKevin Tian } 233cb9ff3f3SKevin Tian 2343c28a761SYi Liu device_initialize(&device->device); 2353c28a761SYi Liu device->device.release = vfio_device_release; 2363c28a761SYi Liu device->device.class = vfio.device_class; 2373c28a761SYi Liu device->device.parent = device->dev; 238cb9ff3f3SKevin Tian return 0; 239cb9ff3f3SKevin Tian 240cb9ff3f3SKevin Tian out_uninit: 241ebb72b76SKevin Tian vfio_release_device_set(device); 2423c28a761SYi Liu ida_free(&vfio.device_ida, device->index); 243cb9ff3f3SKevin Tian return ret; 244cb9ff3f3SKevin Tian } 245cb9ff3f3SKevin Tian 24649ea02d3SYi Liu static int __vfio_register_dev(struct vfio_device *device, 24749ea02d3SYi Liu enum vfio_group_type type) 24849ea02d3SYi Liu { 24949ea02d3SYi Liu int ret; 25049ea02d3SYi Liu 251a4d1f91dSJason Gunthorpe if (WARN_ON(device->ops->bind_iommufd && 252a4d1f91dSJason Gunthorpe (!device->ops->unbind_iommufd || 253a4d1f91dSJason Gunthorpe !device->ops->attach_ioas))) 254a4d1f91dSJason Gunthorpe return -EINVAL; 255a4d1f91dSJason Gunthorpe 2560f3e72b5SJason Gunthorpe /* 2570f3e72b5SJason Gunthorpe * If the driver doesn't specify a set then the device is added to a 2580f3e72b5SJason Gunthorpe * singleton set just for itself. 2590f3e72b5SJason Gunthorpe */ 2600f3e72b5SJason Gunthorpe if (!device->dev_set) 2610f3e72b5SJason Gunthorpe vfio_assign_device_set(device, device); 2620f3e72b5SJason Gunthorpe 2633c28a761SYi Liu ret = dev_set_name(&device->device, "vfio%d", device->index); 2643c28a761SYi Liu if (ret) 26549ea02d3SYi Liu return ret; 26649ea02d3SYi Liu 26749ea02d3SYi Liu ret = vfio_device_set_group(device, type); 26849ea02d3SYi Liu if (ret) 26949ea02d3SYi Liu return ret; 2703c28a761SYi Liu 2713c28a761SYi Liu ret = device_add(&device->device); 2723c28a761SYi Liu if (ret) 2733c28a761SYi Liu goto err_out; 2743c28a761SYi Liu 2750f3e72b5SJason Gunthorpe /* Refcounting can't start until the driver calls register */ 2760f3e72b5SJason Gunthorpe refcount_set(&device->refcount, 1); 2770f3e72b5SJason Gunthorpe 27832e09228SYi Liu vfio_device_group_register(device); 2790f3e72b5SJason Gunthorpe 2800f3e72b5SJason Gunthorpe return 0; 2813c28a761SYi Liu err_out: 282ca5f21b2SJason Gunthorpe vfio_device_remove_group(device); 2833c28a761SYi Liu return ret; 2840f3e72b5SJason Gunthorpe } 2850f3e72b5SJason Gunthorpe 2860f3e72b5SJason Gunthorpe int vfio_register_group_dev(struct vfio_device *device) 2870f3e72b5SJason Gunthorpe { 28849ea02d3SYi Liu return __vfio_register_dev(device, VFIO_IOMMU); 2890f3e72b5SJason Gunthorpe } 2900f3e72b5SJason Gunthorpe EXPORT_SYMBOL_GPL(vfio_register_group_dev); 2910f3e72b5SJason Gunthorpe 2920f3e72b5SJason Gunthorpe /* 2930f3e72b5SJason Gunthorpe * Register a virtual device without IOMMU backing. The user of this 2940f3e72b5SJason Gunthorpe * device must not be able to directly trigger unmediated DMA. 2950f3e72b5SJason Gunthorpe */ 2960f3e72b5SJason Gunthorpe int vfio_register_emulated_iommu_dev(struct vfio_device *device) 2970f3e72b5SJason Gunthorpe { 29849ea02d3SYi Liu return __vfio_register_dev(device, VFIO_EMULATED_IOMMU); 2990f3e72b5SJason Gunthorpe } 3000f3e72b5SJason Gunthorpe EXPORT_SYMBOL_GPL(vfio_register_emulated_iommu_dev); 3010f3e72b5SJason Gunthorpe 3020f3e72b5SJason Gunthorpe /* 3030f3e72b5SJason Gunthorpe * Decrement the device reference count and wait for the device to be 3040f3e72b5SJason Gunthorpe * removed. Open file descriptors for the device... */ 3050f3e72b5SJason Gunthorpe void vfio_unregister_group_dev(struct vfio_device *device) 3060f3e72b5SJason Gunthorpe { 3070f3e72b5SJason Gunthorpe unsigned int i = 0; 3080f3e72b5SJason Gunthorpe bool interrupted = false; 3090f3e72b5SJason Gunthorpe long rc; 3100f3e72b5SJason Gunthorpe 3114a725b8dSKevin Tian vfio_device_put_registration(device); 3120f3e72b5SJason Gunthorpe rc = try_wait_for_completion(&device->comp); 3130f3e72b5SJason Gunthorpe while (rc <= 0) { 3140f3e72b5SJason Gunthorpe if (device->ops->request) 3150f3e72b5SJason Gunthorpe device->ops->request(device, i++); 3160f3e72b5SJason Gunthorpe 3170f3e72b5SJason Gunthorpe if (interrupted) { 3180f3e72b5SJason Gunthorpe rc = wait_for_completion_timeout(&device->comp, 3190f3e72b5SJason Gunthorpe HZ * 10); 3200f3e72b5SJason Gunthorpe } else { 3210f3e72b5SJason Gunthorpe rc = wait_for_completion_interruptible_timeout( 3220f3e72b5SJason Gunthorpe &device->comp, HZ * 10); 3230f3e72b5SJason Gunthorpe if (rc < 0) { 3240f3e72b5SJason Gunthorpe interrupted = true; 3250f3e72b5SJason Gunthorpe dev_warn(device->dev, 3260f3e72b5SJason Gunthorpe "Device is currently in use, task" 3270f3e72b5SJason Gunthorpe " \"%s\" (%d) " 3280f3e72b5SJason Gunthorpe "blocked until device is released", 3290f3e72b5SJason Gunthorpe current->comm, task_pid_nr(current)); 3300f3e72b5SJason Gunthorpe } 3310f3e72b5SJason Gunthorpe } 3320f3e72b5SJason Gunthorpe } 3330f3e72b5SJason Gunthorpe 33432e09228SYi Liu vfio_device_group_unregister(device); 3350f3e72b5SJason Gunthorpe 3363c28a761SYi Liu /* Balances device_add in register path */ 3373c28a761SYi Liu device_del(&device->device); 3383c28a761SYi Liu 33949ea02d3SYi Liu /* Balances vfio_device_set_group in register path */ 340ca5f21b2SJason Gunthorpe vfio_device_remove_group(device); 3410f3e72b5SJason Gunthorpe } 3420f3e72b5SJason Gunthorpe EXPORT_SYMBOL_GPL(vfio_unregister_group_dev); 3430f3e72b5SJason Gunthorpe 3442b48f52fSMatthew Rosato #ifdef CONFIG_HAVE_KVM 3452b48f52fSMatthew Rosato void _vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm) 3462b48f52fSMatthew Rosato { 3472b48f52fSMatthew Rosato void (*pfn)(struct kvm *kvm); 3482b48f52fSMatthew Rosato bool (*fn)(struct kvm *kvm); 3492b48f52fSMatthew Rosato bool ret; 3502b48f52fSMatthew Rosato 3512b48f52fSMatthew Rosato lockdep_assert_held(&device->dev_set->lock); 3522b48f52fSMatthew Rosato 3532b48f52fSMatthew Rosato pfn = symbol_get(kvm_put_kvm); 3542b48f52fSMatthew Rosato if (WARN_ON(!pfn)) 3552b48f52fSMatthew Rosato return; 3562b48f52fSMatthew Rosato 3572b48f52fSMatthew Rosato fn = symbol_get(kvm_get_kvm_safe); 3582b48f52fSMatthew Rosato if (WARN_ON(!fn)) { 3592b48f52fSMatthew Rosato symbol_put(kvm_put_kvm); 3602b48f52fSMatthew Rosato return; 3612b48f52fSMatthew Rosato } 3622b48f52fSMatthew Rosato 3632b48f52fSMatthew Rosato ret = fn(kvm); 3642b48f52fSMatthew Rosato symbol_put(kvm_get_kvm_safe); 3652b48f52fSMatthew Rosato if (!ret) { 3662b48f52fSMatthew Rosato symbol_put(kvm_put_kvm); 3672b48f52fSMatthew Rosato return; 3682b48f52fSMatthew Rosato } 3692b48f52fSMatthew Rosato 3702b48f52fSMatthew Rosato device->put_kvm = pfn; 3712b48f52fSMatthew Rosato device->kvm = kvm; 3722b48f52fSMatthew Rosato } 3732b48f52fSMatthew Rosato 3742b48f52fSMatthew Rosato void vfio_device_put_kvm(struct vfio_device *device) 3752b48f52fSMatthew Rosato { 3762b48f52fSMatthew Rosato lockdep_assert_held(&device->dev_set->lock); 3772b48f52fSMatthew Rosato 3782b48f52fSMatthew Rosato if (!device->kvm) 3792b48f52fSMatthew Rosato return; 3802b48f52fSMatthew Rosato 3812b48f52fSMatthew Rosato if (WARN_ON(!device->put_kvm)) 3822b48f52fSMatthew Rosato goto clear; 3832b48f52fSMatthew Rosato 3842b48f52fSMatthew Rosato device->put_kvm(device->kvm); 3852b48f52fSMatthew Rosato device->put_kvm = NULL; 3862b48f52fSMatthew Rosato symbol_put(kvm_put_kvm); 3872b48f52fSMatthew Rosato 3882b48f52fSMatthew Rosato clear: 3892b48f52fSMatthew Rosato device->kvm = NULL; 3902b48f52fSMatthew Rosato } 3912b48f52fSMatthew Rosato #endif 3922b48f52fSMatthew Rosato 3930f3e72b5SJason Gunthorpe /* true if the vfio_device has open_device() called but not close_device() */ 3944741f2e9SJason Gunthorpe static bool vfio_assert_device_open(struct vfio_device *device) 3950f3e72b5SJason Gunthorpe { 3960f3e72b5SJason Gunthorpe return !WARN_ON_ONCE(!READ_ONCE(device->open_count)); 3970f3e72b5SJason Gunthorpe } 3980f3e72b5SJason Gunthorpe 3995c8d3d93SYi Liu static int vfio_device_first_open(struct vfio_device *device, 400*b0d2d569SMatthew Rosato struct iommufd_ctx *iommufd) 401294aaccbSJason Gunthorpe { 402294aaccbSJason Gunthorpe int ret; 403294aaccbSJason Gunthorpe 404294aaccbSJason Gunthorpe lockdep_assert_held(&device->dev_set->lock); 405294aaccbSJason Gunthorpe 406294aaccbSJason Gunthorpe if (!try_module_get(device->dev->driver->owner)) 407294aaccbSJason Gunthorpe return -ENODEV; 408294aaccbSJason Gunthorpe 4095c8d3d93SYi Liu if (iommufd) 4105c8d3d93SYi Liu ret = vfio_iommufd_bind(device, iommufd); 4115c8d3d93SYi Liu else 4125c8d3d93SYi Liu ret = vfio_device_group_use_iommu(device); 413bab6fabcSJason Gunthorpe if (ret) 414bab6fabcSJason Gunthorpe goto err_module_put; 415bab6fabcSJason Gunthorpe 416294aaccbSJason Gunthorpe if (device->ops->open_device) { 417294aaccbSJason Gunthorpe ret = device->ops->open_device(device); 418294aaccbSJason Gunthorpe if (ret) 4195c8d3d93SYi Liu goto err_unuse_iommu; 420294aaccbSJason Gunthorpe } 421294aaccbSJason Gunthorpe return 0; 422294aaccbSJason Gunthorpe 4235c8d3d93SYi Liu err_unuse_iommu: 4245c8d3d93SYi Liu if (iommufd) 425a4d1f91dSJason Gunthorpe vfio_iommufd_unbind(device); 4265c8d3d93SYi Liu else 4275c8d3d93SYi Liu vfio_device_group_unuse_iommu(device); 428bab6fabcSJason Gunthorpe err_module_put: 429294aaccbSJason Gunthorpe module_put(device->dev->driver->owner); 430294aaccbSJason Gunthorpe return ret; 431294aaccbSJason Gunthorpe } 432294aaccbSJason Gunthorpe 4335c8d3d93SYi Liu static void vfio_device_last_close(struct vfio_device *device, 4345c8d3d93SYi Liu struct iommufd_ctx *iommufd) 435294aaccbSJason Gunthorpe { 436294aaccbSJason Gunthorpe lockdep_assert_held(&device->dev_set->lock); 437294aaccbSJason Gunthorpe 438294aaccbSJason Gunthorpe if (device->ops->close_device) 439294aaccbSJason Gunthorpe device->ops->close_device(device); 4405c8d3d93SYi Liu if (iommufd) 441a4d1f91dSJason Gunthorpe vfio_iommufd_unbind(device); 4425c8d3d93SYi Liu else 4435c8d3d93SYi Liu vfio_device_group_unuse_iommu(device); 444294aaccbSJason Gunthorpe module_put(device->dev->driver->owner); 445294aaccbSJason Gunthorpe } 446294aaccbSJason Gunthorpe 447*b0d2d569SMatthew Rosato int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd) 4480f3e72b5SJason Gunthorpe { 4495cfff077SYi Liu int ret = 0; 4500f3e72b5SJason Gunthorpe 4512b48f52fSMatthew Rosato lockdep_assert_held(&device->dev_set->lock); 4522b48f52fSMatthew Rosato 4530f3e72b5SJason Gunthorpe device->open_count++; 4540f3e72b5SJason Gunthorpe if (device->open_count == 1) { 455*b0d2d569SMatthew Rosato ret = vfio_device_first_open(device, iommufd); 4560f3e72b5SJason Gunthorpe if (ret) 4575cfff077SYi Liu device->open_count--; 4580f3e72b5SJason Gunthorpe } 4590f3e72b5SJason Gunthorpe 4605cfff077SYi Liu return ret; 4615cfff077SYi Liu } 4625cfff077SYi Liu 4639eefba80SYi Liu void vfio_device_close(struct vfio_device *device, 4645c8d3d93SYi Liu struct iommufd_ctx *iommufd) 4655cfff077SYi Liu { 4662b48f52fSMatthew Rosato lockdep_assert_held(&device->dev_set->lock); 4672b48f52fSMatthew Rosato 4685cfff077SYi Liu vfio_assert_device_open(device); 4695cfff077SYi Liu if (device->open_count == 1) 4705c8d3d93SYi Liu vfio_device_last_close(device, iommufd); 4715cfff077SYi Liu device->open_count--; 4725cfff077SYi Liu } 4735cfff077SYi Liu 4740f3e72b5SJason Gunthorpe /* 4758e5c6995SAbhishek Sahu * Wrapper around pm_runtime_resume_and_get(). 4768e5c6995SAbhishek Sahu * Return error code on failure or 0 on success. 4778e5c6995SAbhishek Sahu */ 4788e5c6995SAbhishek Sahu static inline int vfio_device_pm_runtime_get(struct vfio_device *device) 4798e5c6995SAbhishek Sahu { 4808e5c6995SAbhishek Sahu struct device *dev = device->dev; 4818e5c6995SAbhishek Sahu 4828e5c6995SAbhishek Sahu if (dev->driver && dev->driver->pm) { 4838e5c6995SAbhishek Sahu int ret; 4848e5c6995SAbhishek Sahu 4858e5c6995SAbhishek Sahu ret = pm_runtime_resume_and_get(dev); 4868e5c6995SAbhishek Sahu if (ret) { 4878e5c6995SAbhishek Sahu dev_info_ratelimited(dev, 4888e5c6995SAbhishek Sahu "vfio: runtime resume failed %d\n", ret); 4898e5c6995SAbhishek Sahu return -EIO; 4908e5c6995SAbhishek Sahu } 4918e5c6995SAbhishek Sahu } 4928e5c6995SAbhishek Sahu 4938e5c6995SAbhishek Sahu return 0; 4948e5c6995SAbhishek Sahu } 4958e5c6995SAbhishek Sahu 4968e5c6995SAbhishek Sahu /* 4978e5c6995SAbhishek Sahu * Wrapper around pm_runtime_put(). 4988e5c6995SAbhishek Sahu */ 4998e5c6995SAbhishek Sahu static inline void vfio_device_pm_runtime_put(struct vfio_device *device) 5008e5c6995SAbhishek Sahu { 5018e5c6995SAbhishek Sahu struct device *dev = device->dev; 5028e5c6995SAbhishek Sahu 5038e5c6995SAbhishek Sahu if (dev->driver && dev->driver->pm) 5048e5c6995SAbhishek Sahu pm_runtime_put(dev); 5058e5c6995SAbhishek Sahu } 5068e5c6995SAbhishek Sahu 5078e5c6995SAbhishek Sahu /* 5080f3e72b5SJason Gunthorpe * VFIO Device fd 5090f3e72b5SJason Gunthorpe */ 5100f3e72b5SJason Gunthorpe static int vfio_device_fops_release(struct inode *inode, struct file *filep) 5110f3e72b5SJason Gunthorpe { 5120f3e72b5SJason Gunthorpe struct vfio_device *device = filep->private_data; 5130f3e72b5SJason Gunthorpe 5145c8d3d93SYi Liu vfio_device_group_close(device); 5150f3e72b5SJason Gunthorpe 5164a725b8dSKevin Tian vfio_device_put_registration(device); 5170f3e72b5SJason Gunthorpe 5180f3e72b5SJason Gunthorpe return 0; 5190f3e72b5SJason Gunthorpe } 5200f3e72b5SJason Gunthorpe 5210f3e72b5SJason Gunthorpe /* 5220f3e72b5SJason Gunthorpe * vfio_mig_get_next_state - Compute the next step in the FSM 5230f3e72b5SJason Gunthorpe * @cur_fsm - The current state the device is in 5240f3e72b5SJason Gunthorpe * @new_fsm - The target state to reach 5250f3e72b5SJason Gunthorpe * @next_fsm - Pointer to the next step to get to new_fsm 5260f3e72b5SJason Gunthorpe * 5270f3e72b5SJason Gunthorpe * Return 0 upon success, otherwise -errno 5280f3e72b5SJason Gunthorpe * Upon success the next step in the state progression between cur_fsm and 5290f3e72b5SJason Gunthorpe * new_fsm will be set in next_fsm. 5300f3e72b5SJason Gunthorpe * 5310f3e72b5SJason Gunthorpe * This breaks down requests for combination transitions into smaller steps and 5320f3e72b5SJason Gunthorpe * returns the next step to get to new_fsm. The function may need to be called 5330f3e72b5SJason Gunthorpe * multiple times before reaching new_fsm. 5340f3e72b5SJason Gunthorpe * 5350f3e72b5SJason Gunthorpe */ 5360f3e72b5SJason Gunthorpe int vfio_mig_get_next_state(struct vfio_device *device, 5370f3e72b5SJason Gunthorpe enum vfio_device_mig_state cur_fsm, 5380f3e72b5SJason Gunthorpe enum vfio_device_mig_state new_fsm, 5390f3e72b5SJason Gunthorpe enum vfio_device_mig_state *next_fsm) 5400f3e72b5SJason Gunthorpe { 5414db52602SJason Gunthorpe enum { VFIO_DEVICE_NUM_STATES = VFIO_DEVICE_STATE_PRE_COPY_P2P + 1 }; 5420f3e72b5SJason Gunthorpe /* 5430f3e72b5SJason Gunthorpe * The coding in this table requires the driver to implement the 5440f3e72b5SJason Gunthorpe * following FSM arcs: 5450f3e72b5SJason Gunthorpe * RESUMING -> STOP 5460f3e72b5SJason Gunthorpe * STOP -> RESUMING 5470f3e72b5SJason Gunthorpe * STOP -> STOP_COPY 5480f3e72b5SJason Gunthorpe * STOP_COPY -> STOP 5490f3e72b5SJason Gunthorpe * 5500f3e72b5SJason Gunthorpe * If P2P is supported then the driver must also implement these FSM 5510f3e72b5SJason Gunthorpe * arcs: 5520f3e72b5SJason Gunthorpe * RUNNING -> RUNNING_P2P 5530f3e72b5SJason Gunthorpe * RUNNING_P2P -> RUNNING 5540f3e72b5SJason Gunthorpe * RUNNING_P2P -> STOP 5550f3e72b5SJason Gunthorpe * STOP -> RUNNING_P2P 5564db52602SJason Gunthorpe * 5574db52602SJason Gunthorpe * If precopy is supported then the driver must support these additional 5584db52602SJason Gunthorpe * FSM arcs: 5594db52602SJason Gunthorpe * RUNNING -> PRE_COPY 5604db52602SJason Gunthorpe * PRE_COPY -> RUNNING 5614db52602SJason Gunthorpe * PRE_COPY -> STOP_COPY 5624db52602SJason Gunthorpe * However, if precopy and P2P are supported together then the driver 5634db52602SJason Gunthorpe * must support these additional arcs beyond the P2P arcs above: 5644db52602SJason Gunthorpe * PRE_COPY -> RUNNING 5654db52602SJason Gunthorpe * PRE_COPY -> PRE_COPY_P2P 5664db52602SJason Gunthorpe * PRE_COPY_P2P -> PRE_COPY 5674db52602SJason Gunthorpe * PRE_COPY_P2P -> RUNNING_P2P 5684db52602SJason Gunthorpe * PRE_COPY_P2P -> STOP_COPY 5694db52602SJason Gunthorpe * RUNNING -> PRE_COPY 5704db52602SJason Gunthorpe * RUNNING_P2P -> PRE_COPY_P2P 5714db52602SJason Gunthorpe * 5724db52602SJason Gunthorpe * Without P2P and precopy the driver must implement: 5730f3e72b5SJason Gunthorpe * RUNNING -> STOP 5740f3e72b5SJason Gunthorpe * STOP -> RUNNING 5750f3e72b5SJason Gunthorpe * 5760f3e72b5SJason Gunthorpe * The coding will step through multiple states for some combination 5770f3e72b5SJason Gunthorpe * transitions; if all optional features are supported, this means the 5780f3e72b5SJason Gunthorpe * following ones: 5794db52602SJason Gunthorpe * PRE_COPY -> PRE_COPY_P2P -> STOP_COPY 5804db52602SJason Gunthorpe * PRE_COPY -> RUNNING -> RUNNING_P2P 5814db52602SJason Gunthorpe * PRE_COPY -> RUNNING -> RUNNING_P2P -> STOP 5824db52602SJason Gunthorpe * PRE_COPY -> RUNNING -> RUNNING_P2P -> STOP -> RESUMING 5834db52602SJason Gunthorpe * PRE_COPY_P2P -> RUNNING_P2P -> RUNNING 5844db52602SJason Gunthorpe * PRE_COPY_P2P -> RUNNING_P2P -> STOP 5854db52602SJason Gunthorpe * PRE_COPY_P2P -> RUNNING_P2P -> STOP -> RESUMING 5860f3e72b5SJason Gunthorpe * RESUMING -> STOP -> RUNNING_P2P 5874db52602SJason Gunthorpe * RESUMING -> STOP -> RUNNING_P2P -> PRE_COPY_P2P 5880f3e72b5SJason Gunthorpe * RESUMING -> STOP -> RUNNING_P2P -> RUNNING 5894db52602SJason Gunthorpe * RESUMING -> STOP -> RUNNING_P2P -> RUNNING -> PRE_COPY 5900f3e72b5SJason Gunthorpe * RESUMING -> STOP -> STOP_COPY 5914db52602SJason Gunthorpe * RUNNING -> RUNNING_P2P -> PRE_COPY_P2P 5920f3e72b5SJason Gunthorpe * RUNNING -> RUNNING_P2P -> STOP 5930f3e72b5SJason Gunthorpe * RUNNING -> RUNNING_P2P -> STOP -> RESUMING 5940f3e72b5SJason Gunthorpe * RUNNING -> RUNNING_P2P -> STOP -> STOP_COPY 5954db52602SJason Gunthorpe * RUNNING_P2P -> RUNNING -> PRE_COPY 5960f3e72b5SJason Gunthorpe * RUNNING_P2P -> STOP -> RESUMING 5970f3e72b5SJason Gunthorpe * RUNNING_P2P -> STOP -> STOP_COPY 5984db52602SJason Gunthorpe * STOP -> RUNNING_P2P -> PRE_COPY_P2P 5990f3e72b5SJason Gunthorpe * STOP -> RUNNING_P2P -> RUNNING 6004db52602SJason Gunthorpe * STOP -> RUNNING_P2P -> RUNNING -> PRE_COPY 6010f3e72b5SJason Gunthorpe * STOP_COPY -> STOP -> RESUMING 6020f3e72b5SJason Gunthorpe * STOP_COPY -> STOP -> RUNNING_P2P 6030f3e72b5SJason Gunthorpe * STOP_COPY -> STOP -> RUNNING_P2P -> RUNNING 6044db52602SJason Gunthorpe * 6054db52602SJason Gunthorpe * The following transitions are blocked: 6064db52602SJason Gunthorpe * STOP_COPY -> PRE_COPY 6074db52602SJason Gunthorpe * STOP_COPY -> PRE_COPY_P2P 6080f3e72b5SJason Gunthorpe */ 6090f3e72b5SJason Gunthorpe static const u8 vfio_from_fsm_table[VFIO_DEVICE_NUM_STATES][VFIO_DEVICE_NUM_STATES] = { 6100f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = { 6110f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_STOP, 6120f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_RUNNING_P2P, 6134db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = VFIO_DEVICE_STATE_RUNNING_P2P, 6144db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P, 6150f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP_COPY, 6160f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_RESUMING, 6170f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P, 6180f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR, 6190f3e72b5SJason Gunthorpe }, 6200f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = { 6210f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_RUNNING_P2P, 6220f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_RUNNING, 6234db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = VFIO_DEVICE_STATE_PRE_COPY, 6244db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P, 6250f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_RUNNING_P2P, 6260f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_RUNNING_P2P, 6270f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P, 6280f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR, 6290f3e72b5SJason Gunthorpe }, 6304db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = { 6314db52602SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_RUNNING, 6324db52602SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_RUNNING, 6334db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = VFIO_DEVICE_STATE_PRE_COPY, 6344db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_DEVICE_STATE_PRE_COPY_P2P, 6354db52602SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_PRE_COPY_P2P, 6364db52602SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_RUNNING, 6374db52602SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_RUNNING, 6384db52602SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR, 6394db52602SJason Gunthorpe }, 6404db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = { 6414db52602SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_RUNNING_P2P, 6424db52602SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_RUNNING_P2P, 6434db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = VFIO_DEVICE_STATE_PRE_COPY, 6444db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_DEVICE_STATE_PRE_COPY_P2P, 6454db52602SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP_COPY, 6464db52602SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_RUNNING_P2P, 6474db52602SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P, 6484db52602SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR, 6494db52602SJason Gunthorpe }, 6500f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = { 6510f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_STOP, 6520f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_STOP, 6534db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = VFIO_DEVICE_STATE_ERROR, 6544db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_DEVICE_STATE_ERROR, 6550f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP_COPY, 6560f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_STOP, 6570f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_STOP, 6580f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR, 6590f3e72b5SJason Gunthorpe }, 6600f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = { 6610f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_STOP, 6620f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_STOP, 6634db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = VFIO_DEVICE_STATE_STOP, 6644db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_DEVICE_STATE_STOP, 6650f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP, 6660f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_RESUMING, 6670f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_STOP, 6680f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR, 6690f3e72b5SJason Gunthorpe }, 6700f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = { 6710f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_STOP, 6720f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_RUNNING, 6734db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = VFIO_DEVICE_STATE_RUNNING, 6744db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_DEVICE_STATE_PRE_COPY_P2P, 6750f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP, 6760f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_STOP, 6770f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P, 6780f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR, 6790f3e72b5SJason Gunthorpe }, 6800f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = { 6810f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_ERROR, 6820f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_ERROR, 6834db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = VFIO_DEVICE_STATE_ERROR, 6844db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_DEVICE_STATE_ERROR, 6850f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_ERROR, 6860f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_ERROR, 6870f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_ERROR, 6880f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR, 6890f3e72b5SJason Gunthorpe }, 6900f3e72b5SJason Gunthorpe }; 6910f3e72b5SJason Gunthorpe 6920f3e72b5SJason Gunthorpe static const unsigned int state_flags_table[VFIO_DEVICE_NUM_STATES] = { 6930f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP] = VFIO_MIGRATION_STOP_COPY, 6940f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING] = VFIO_MIGRATION_STOP_COPY, 6954db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY] = 6964db52602SJason Gunthorpe VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_PRE_COPY, 6974db52602SJason Gunthorpe [VFIO_DEVICE_STATE_PRE_COPY_P2P] = VFIO_MIGRATION_STOP_COPY | 6984db52602SJason Gunthorpe VFIO_MIGRATION_P2P | 6994db52602SJason Gunthorpe VFIO_MIGRATION_PRE_COPY, 7000f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_MIGRATION_STOP_COPY, 7010f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RESUMING] = VFIO_MIGRATION_STOP_COPY, 7020f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_RUNNING_P2P] = 7030f3e72b5SJason Gunthorpe VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P, 7040f3e72b5SJason Gunthorpe [VFIO_DEVICE_STATE_ERROR] = ~0U, 7050f3e72b5SJason Gunthorpe }; 7060f3e72b5SJason Gunthorpe 7070f3e72b5SJason Gunthorpe if (WARN_ON(cur_fsm >= ARRAY_SIZE(vfio_from_fsm_table) || 7080f3e72b5SJason Gunthorpe (state_flags_table[cur_fsm] & device->migration_flags) != 7090f3e72b5SJason Gunthorpe state_flags_table[cur_fsm])) 7100f3e72b5SJason Gunthorpe return -EINVAL; 7110f3e72b5SJason Gunthorpe 7120f3e72b5SJason Gunthorpe if (new_fsm >= ARRAY_SIZE(vfio_from_fsm_table) || 7130f3e72b5SJason Gunthorpe (state_flags_table[new_fsm] & device->migration_flags) != 7140f3e72b5SJason Gunthorpe state_flags_table[new_fsm]) 7150f3e72b5SJason Gunthorpe return -EINVAL; 7160f3e72b5SJason Gunthorpe 7170f3e72b5SJason Gunthorpe /* 7180f3e72b5SJason Gunthorpe * Arcs touching optional and unsupported states are skipped over. The 7190f3e72b5SJason Gunthorpe * driver will instead see an arc from the original state to the next 7200f3e72b5SJason Gunthorpe * logical state, as per the above comment. 7210f3e72b5SJason Gunthorpe */ 7220f3e72b5SJason Gunthorpe *next_fsm = vfio_from_fsm_table[cur_fsm][new_fsm]; 7230f3e72b5SJason Gunthorpe while ((state_flags_table[*next_fsm] & device->migration_flags) != 7240f3e72b5SJason Gunthorpe state_flags_table[*next_fsm]) 7250f3e72b5SJason Gunthorpe *next_fsm = vfio_from_fsm_table[*next_fsm][new_fsm]; 7260f3e72b5SJason Gunthorpe 7270f3e72b5SJason Gunthorpe return (*next_fsm != VFIO_DEVICE_STATE_ERROR) ? 0 : -EINVAL; 7280f3e72b5SJason Gunthorpe } 7290f3e72b5SJason Gunthorpe EXPORT_SYMBOL_GPL(vfio_mig_get_next_state); 7300f3e72b5SJason Gunthorpe 7310f3e72b5SJason Gunthorpe /* 7320f3e72b5SJason Gunthorpe * Convert the drivers's struct file into a FD number and return it to userspace 7330f3e72b5SJason Gunthorpe */ 7340f3e72b5SJason Gunthorpe static int vfio_ioct_mig_return_fd(struct file *filp, void __user *arg, 7350f3e72b5SJason Gunthorpe struct vfio_device_feature_mig_state *mig) 7360f3e72b5SJason Gunthorpe { 7370f3e72b5SJason Gunthorpe int ret; 7380f3e72b5SJason Gunthorpe int fd; 7390f3e72b5SJason Gunthorpe 7400f3e72b5SJason Gunthorpe fd = get_unused_fd_flags(O_CLOEXEC); 7410f3e72b5SJason Gunthorpe if (fd < 0) { 7420f3e72b5SJason Gunthorpe ret = fd; 7430f3e72b5SJason Gunthorpe goto out_fput; 7440f3e72b5SJason Gunthorpe } 7450f3e72b5SJason Gunthorpe 7460f3e72b5SJason Gunthorpe mig->data_fd = fd; 7470f3e72b5SJason Gunthorpe if (copy_to_user(arg, mig, sizeof(*mig))) { 7480f3e72b5SJason Gunthorpe ret = -EFAULT; 7490f3e72b5SJason Gunthorpe goto out_put_unused; 7500f3e72b5SJason Gunthorpe } 7510f3e72b5SJason Gunthorpe fd_install(fd, filp); 7520f3e72b5SJason Gunthorpe return 0; 7530f3e72b5SJason Gunthorpe 7540f3e72b5SJason Gunthorpe out_put_unused: 7550f3e72b5SJason Gunthorpe put_unused_fd(fd); 7560f3e72b5SJason Gunthorpe out_fput: 7570f3e72b5SJason Gunthorpe fput(filp); 7580f3e72b5SJason Gunthorpe return ret; 7590f3e72b5SJason Gunthorpe } 7600f3e72b5SJason Gunthorpe 7610f3e72b5SJason Gunthorpe static int 7620f3e72b5SJason Gunthorpe vfio_ioctl_device_feature_mig_device_state(struct vfio_device *device, 7630f3e72b5SJason Gunthorpe u32 flags, void __user *arg, 7640f3e72b5SJason Gunthorpe size_t argsz) 7650f3e72b5SJason Gunthorpe { 7660f3e72b5SJason Gunthorpe size_t minsz = 7670f3e72b5SJason Gunthorpe offsetofend(struct vfio_device_feature_mig_state, data_fd); 7680f3e72b5SJason Gunthorpe struct vfio_device_feature_mig_state mig; 7690f3e72b5SJason Gunthorpe struct file *filp = NULL; 7700f3e72b5SJason Gunthorpe int ret; 7710f3e72b5SJason Gunthorpe 7720f3e72b5SJason Gunthorpe if (!device->mig_ops) 7730f3e72b5SJason Gunthorpe return -ENOTTY; 7740f3e72b5SJason Gunthorpe 7750f3e72b5SJason Gunthorpe ret = vfio_check_feature(flags, argsz, 7760f3e72b5SJason Gunthorpe VFIO_DEVICE_FEATURE_SET | 7770f3e72b5SJason Gunthorpe VFIO_DEVICE_FEATURE_GET, 7780f3e72b5SJason Gunthorpe sizeof(mig)); 7790f3e72b5SJason Gunthorpe if (ret != 1) 7800f3e72b5SJason Gunthorpe return ret; 7810f3e72b5SJason Gunthorpe 7820f3e72b5SJason Gunthorpe if (copy_from_user(&mig, arg, minsz)) 7830f3e72b5SJason Gunthorpe return -EFAULT; 7840f3e72b5SJason Gunthorpe 7850f3e72b5SJason Gunthorpe if (flags & VFIO_DEVICE_FEATURE_GET) { 7860f3e72b5SJason Gunthorpe enum vfio_device_mig_state curr_state; 7870f3e72b5SJason Gunthorpe 7880f3e72b5SJason Gunthorpe ret = device->mig_ops->migration_get_state(device, 7890f3e72b5SJason Gunthorpe &curr_state); 7900f3e72b5SJason Gunthorpe if (ret) 7910f3e72b5SJason Gunthorpe return ret; 7920f3e72b5SJason Gunthorpe mig.device_state = curr_state; 7930f3e72b5SJason Gunthorpe goto out_copy; 7940f3e72b5SJason Gunthorpe } 7950f3e72b5SJason Gunthorpe 7960f3e72b5SJason Gunthorpe /* Handle the VFIO_DEVICE_FEATURE_SET */ 7970f3e72b5SJason Gunthorpe filp = device->mig_ops->migration_set_state(device, mig.device_state); 7980f3e72b5SJason Gunthorpe if (IS_ERR(filp) || !filp) 7990f3e72b5SJason Gunthorpe goto out_copy; 8000f3e72b5SJason Gunthorpe 8010f3e72b5SJason Gunthorpe return vfio_ioct_mig_return_fd(filp, arg, &mig); 8020f3e72b5SJason Gunthorpe out_copy: 8030f3e72b5SJason Gunthorpe mig.data_fd = -1; 8040f3e72b5SJason Gunthorpe if (copy_to_user(arg, &mig, sizeof(mig))) 8050f3e72b5SJason Gunthorpe return -EFAULT; 8060f3e72b5SJason Gunthorpe if (IS_ERR(filp)) 8070f3e72b5SJason Gunthorpe return PTR_ERR(filp); 8080f3e72b5SJason Gunthorpe return 0; 8090f3e72b5SJason Gunthorpe } 8100f3e72b5SJason Gunthorpe 8114e016f96SYishai Hadas static int 8124e016f96SYishai Hadas vfio_ioctl_device_feature_migration_data_size(struct vfio_device *device, 8134e016f96SYishai Hadas u32 flags, void __user *arg, 8144e016f96SYishai Hadas size_t argsz) 8154e016f96SYishai Hadas { 8164e016f96SYishai Hadas struct vfio_device_feature_mig_data_size data_size = {}; 8174e016f96SYishai Hadas unsigned long stop_copy_length; 8184e016f96SYishai Hadas int ret; 8194e016f96SYishai Hadas 8204e016f96SYishai Hadas if (!device->mig_ops) 8214e016f96SYishai Hadas return -ENOTTY; 8224e016f96SYishai Hadas 8234e016f96SYishai Hadas ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET, 8244e016f96SYishai Hadas sizeof(data_size)); 8254e016f96SYishai Hadas if (ret != 1) 8264e016f96SYishai Hadas return ret; 8274e016f96SYishai Hadas 8284e016f96SYishai Hadas ret = device->mig_ops->migration_get_data_size(device, &stop_copy_length); 8294e016f96SYishai Hadas if (ret) 8304e016f96SYishai Hadas return ret; 8314e016f96SYishai Hadas 8324e016f96SYishai Hadas data_size.stop_copy_length = stop_copy_length; 8334e016f96SYishai Hadas if (copy_to_user(arg, &data_size, sizeof(data_size))) 8344e016f96SYishai Hadas return -EFAULT; 8354e016f96SYishai Hadas 8364e016f96SYishai Hadas return 0; 8374e016f96SYishai Hadas } 8384e016f96SYishai Hadas 8390f3e72b5SJason Gunthorpe static int vfio_ioctl_device_feature_migration(struct vfio_device *device, 8400f3e72b5SJason Gunthorpe u32 flags, void __user *arg, 8410f3e72b5SJason Gunthorpe size_t argsz) 8420f3e72b5SJason Gunthorpe { 8430f3e72b5SJason Gunthorpe struct vfio_device_feature_migration mig = { 8440f3e72b5SJason Gunthorpe .flags = device->migration_flags, 8450f3e72b5SJason Gunthorpe }; 8460f3e72b5SJason Gunthorpe int ret; 8470f3e72b5SJason Gunthorpe 8480f3e72b5SJason Gunthorpe if (!device->mig_ops) 8490f3e72b5SJason Gunthorpe return -ENOTTY; 8500f3e72b5SJason Gunthorpe 8510f3e72b5SJason Gunthorpe ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET, 8520f3e72b5SJason Gunthorpe sizeof(mig)); 8530f3e72b5SJason Gunthorpe if (ret != 1) 8540f3e72b5SJason Gunthorpe return ret; 8550f3e72b5SJason Gunthorpe if (copy_to_user(arg, &mig, sizeof(mig))) 8560f3e72b5SJason Gunthorpe return -EFAULT; 8570f3e72b5SJason Gunthorpe return 0; 8580f3e72b5SJason Gunthorpe } 8590f3e72b5SJason Gunthorpe 86080c4b92aSYishai Hadas /* Ranges should fit into a single kernel page */ 86180c4b92aSYishai Hadas #define LOG_MAX_RANGES \ 86280c4b92aSYishai Hadas (PAGE_SIZE / sizeof(struct vfio_device_feature_dma_logging_range)) 86380c4b92aSYishai Hadas 86480c4b92aSYishai Hadas static int 86580c4b92aSYishai Hadas vfio_ioctl_device_feature_logging_start(struct vfio_device *device, 86680c4b92aSYishai Hadas u32 flags, void __user *arg, 86780c4b92aSYishai Hadas size_t argsz) 86880c4b92aSYishai Hadas { 86980c4b92aSYishai Hadas size_t minsz = 87080c4b92aSYishai Hadas offsetofend(struct vfio_device_feature_dma_logging_control, 87180c4b92aSYishai Hadas ranges); 87280c4b92aSYishai Hadas struct vfio_device_feature_dma_logging_range __user *ranges; 87380c4b92aSYishai Hadas struct vfio_device_feature_dma_logging_control control; 87480c4b92aSYishai Hadas struct vfio_device_feature_dma_logging_range range; 87580c4b92aSYishai Hadas struct rb_root_cached root = RB_ROOT_CACHED; 87680c4b92aSYishai Hadas struct interval_tree_node *nodes; 87780c4b92aSYishai Hadas u64 iova_end; 87880c4b92aSYishai Hadas u32 nnodes; 87980c4b92aSYishai Hadas int i, ret; 88080c4b92aSYishai Hadas 88180c4b92aSYishai Hadas if (!device->log_ops) 88280c4b92aSYishai Hadas return -ENOTTY; 88380c4b92aSYishai Hadas 88480c4b92aSYishai Hadas ret = vfio_check_feature(flags, argsz, 88580c4b92aSYishai Hadas VFIO_DEVICE_FEATURE_SET, 88680c4b92aSYishai Hadas sizeof(control)); 88780c4b92aSYishai Hadas if (ret != 1) 88880c4b92aSYishai Hadas return ret; 88980c4b92aSYishai Hadas 89080c4b92aSYishai Hadas if (copy_from_user(&control, arg, minsz)) 89180c4b92aSYishai Hadas return -EFAULT; 89280c4b92aSYishai Hadas 89380c4b92aSYishai Hadas nnodes = control.num_ranges; 89480c4b92aSYishai Hadas if (!nnodes) 89580c4b92aSYishai Hadas return -EINVAL; 89680c4b92aSYishai Hadas 89780c4b92aSYishai Hadas if (nnodes > LOG_MAX_RANGES) 89880c4b92aSYishai Hadas return -E2BIG; 89980c4b92aSYishai Hadas 90080c4b92aSYishai Hadas ranges = u64_to_user_ptr(control.ranges); 90180c4b92aSYishai Hadas nodes = kmalloc_array(nnodes, sizeof(struct interval_tree_node), 90280c4b92aSYishai Hadas GFP_KERNEL); 90380c4b92aSYishai Hadas if (!nodes) 90480c4b92aSYishai Hadas return -ENOMEM; 90580c4b92aSYishai Hadas 90680c4b92aSYishai Hadas for (i = 0; i < nnodes; i++) { 90780c4b92aSYishai Hadas if (copy_from_user(&range, &ranges[i], sizeof(range))) { 90880c4b92aSYishai Hadas ret = -EFAULT; 90980c4b92aSYishai Hadas goto end; 91080c4b92aSYishai Hadas } 91180c4b92aSYishai Hadas if (!IS_ALIGNED(range.iova, control.page_size) || 91280c4b92aSYishai Hadas !IS_ALIGNED(range.length, control.page_size)) { 91380c4b92aSYishai Hadas ret = -EINVAL; 91480c4b92aSYishai Hadas goto end; 91580c4b92aSYishai Hadas } 91680c4b92aSYishai Hadas 91780c4b92aSYishai Hadas if (check_add_overflow(range.iova, range.length, &iova_end) || 91880c4b92aSYishai Hadas iova_end > ULONG_MAX) { 91980c4b92aSYishai Hadas ret = -EOVERFLOW; 92080c4b92aSYishai Hadas goto end; 92180c4b92aSYishai Hadas } 92280c4b92aSYishai Hadas 92380c4b92aSYishai Hadas nodes[i].start = range.iova; 92480c4b92aSYishai Hadas nodes[i].last = range.iova + range.length - 1; 92580c4b92aSYishai Hadas if (interval_tree_iter_first(&root, nodes[i].start, 92680c4b92aSYishai Hadas nodes[i].last)) { 92780c4b92aSYishai Hadas /* Range overlapping */ 92880c4b92aSYishai Hadas ret = -EINVAL; 92980c4b92aSYishai Hadas goto end; 93080c4b92aSYishai Hadas } 93180c4b92aSYishai Hadas interval_tree_insert(nodes + i, &root); 93280c4b92aSYishai Hadas } 93380c4b92aSYishai Hadas 93480c4b92aSYishai Hadas ret = device->log_ops->log_start(device, &root, nnodes, 93580c4b92aSYishai Hadas &control.page_size); 93680c4b92aSYishai Hadas if (ret) 93780c4b92aSYishai Hadas goto end; 93880c4b92aSYishai Hadas 93980c4b92aSYishai Hadas if (copy_to_user(arg, &control, sizeof(control))) { 94080c4b92aSYishai Hadas ret = -EFAULT; 94180c4b92aSYishai Hadas device->log_ops->log_stop(device); 94280c4b92aSYishai Hadas } 94380c4b92aSYishai Hadas 94480c4b92aSYishai Hadas end: 94580c4b92aSYishai Hadas kfree(nodes); 94680c4b92aSYishai Hadas return ret; 94780c4b92aSYishai Hadas } 94880c4b92aSYishai Hadas 94980c4b92aSYishai Hadas static int 95080c4b92aSYishai Hadas vfio_ioctl_device_feature_logging_stop(struct vfio_device *device, 95180c4b92aSYishai Hadas u32 flags, void __user *arg, 95280c4b92aSYishai Hadas size_t argsz) 95380c4b92aSYishai Hadas { 95480c4b92aSYishai Hadas int ret; 95580c4b92aSYishai Hadas 95680c4b92aSYishai Hadas if (!device->log_ops) 95780c4b92aSYishai Hadas return -ENOTTY; 95880c4b92aSYishai Hadas 95980c4b92aSYishai Hadas ret = vfio_check_feature(flags, argsz, 96080c4b92aSYishai Hadas VFIO_DEVICE_FEATURE_SET, 0); 96180c4b92aSYishai Hadas if (ret != 1) 96280c4b92aSYishai Hadas return ret; 96380c4b92aSYishai Hadas 96480c4b92aSYishai Hadas return device->log_ops->log_stop(device); 96580c4b92aSYishai Hadas } 96680c4b92aSYishai Hadas 96780c4b92aSYishai Hadas static int vfio_device_log_read_and_clear(struct iova_bitmap *iter, 96880c4b92aSYishai Hadas unsigned long iova, size_t length, 96980c4b92aSYishai Hadas void *opaque) 97080c4b92aSYishai Hadas { 97180c4b92aSYishai Hadas struct vfio_device *device = opaque; 97280c4b92aSYishai Hadas 97380c4b92aSYishai Hadas return device->log_ops->log_read_and_clear(device, iova, length, iter); 97480c4b92aSYishai Hadas } 97580c4b92aSYishai Hadas 97680c4b92aSYishai Hadas static int 97780c4b92aSYishai Hadas vfio_ioctl_device_feature_logging_report(struct vfio_device *device, 97880c4b92aSYishai Hadas u32 flags, void __user *arg, 97980c4b92aSYishai Hadas size_t argsz) 98080c4b92aSYishai Hadas { 98180c4b92aSYishai Hadas size_t minsz = 98280c4b92aSYishai Hadas offsetofend(struct vfio_device_feature_dma_logging_report, 98380c4b92aSYishai Hadas bitmap); 98480c4b92aSYishai Hadas struct vfio_device_feature_dma_logging_report report; 98580c4b92aSYishai Hadas struct iova_bitmap *iter; 98680c4b92aSYishai Hadas u64 iova_end; 98780c4b92aSYishai Hadas int ret; 98880c4b92aSYishai Hadas 98980c4b92aSYishai Hadas if (!device->log_ops) 99080c4b92aSYishai Hadas return -ENOTTY; 99180c4b92aSYishai Hadas 99280c4b92aSYishai Hadas ret = vfio_check_feature(flags, argsz, 99380c4b92aSYishai Hadas VFIO_DEVICE_FEATURE_GET, 99480c4b92aSYishai Hadas sizeof(report)); 99580c4b92aSYishai Hadas if (ret != 1) 99680c4b92aSYishai Hadas return ret; 99780c4b92aSYishai Hadas 99880c4b92aSYishai Hadas if (copy_from_user(&report, arg, minsz)) 99980c4b92aSYishai Hadas return -EFAULT; 100080c4b92aSYishai Hadas 100180c4b92aSYishai Hadas if (report.page_size < SZ_4K || !is_power_of_2(report.page_size)) 100280c4b92aSYishai Hadas return -EINVAL; 100380c4b92aSYishai Hadas 100480c4b92aSYishai Hadas if (check_add_overflow(report.iova, report.length, &iova_end) || 100580c4b92aSYishai Hadas iova_end > ULONG_MAX) 100680c4b92aSYishai Hadas return -EOVERFLOW; 100780c4b92aSYishai Hadas 100880c4b92aSYishai Hadas iter = iova_bitmap_alloc(report.iova, report.length, 100980c4b92aSYishai Hadas report.page_size, 101080c4b92aSYishai Hadas u64_to_user_ptr(report.bitmap)); 101180c4b92aSYishai Hadas if (IS_ERR(iter)) 101280c4b92aSYishai Hadas return PTR_ERR(iter); 101380c4b92aSYishai Hadas 101480c4b92aSYishai Hadas ret = iova_bitmap_for_each(iter, device, 101580c4b92aSYishai Hadas vfio_device_log_read_and_clear); 101680c4b92aSYishai Hadas 101780c4b92aSYishai Hadas iova_bitmap_free(iter); 101880c4b92aSYishai Hadas return ret; 101980c4b92aSYishai Hadas } 102080c4b92aSYishai Hadas 10210f3e72b5SJason Gunthorpe static int vfio_ioctl_device_feature(struct vfio_device *device, 10220f3e72b5SJason Gunthorpe struct vfio_device_feature __user *arg) 10230f3e72b5SJason Gunthorpe { 10240f3e72b5SJason Gunthorpe size_t minsz = offsetofend(struct vfio_device_feature, flags); 10250f3e72b5SJason Gunthorpe struct vfio_device_feature feature; 10260f3e72b5SJason Gunthorpe 10270f3e72b5SJason Gunthorpe if (copy_from_user(&feature, arg, minsz)) 10280f3e72b5SJason Gunthorpe return -EFAULT; 10290f3e72b5SJason Gunthorpe 10300f3e72b5SJason Gunthorpe if (feature.argsz < minsz) 10310f3e72b5SJason Gunthorpe return -EINVAL; 10320f3e72b5SJason Gunthorpe 10330f3e72b5SJason Gunthorpe /* Check unknown flags */ 10340f3e72b5SJason Gunthorpe if (feature.flags & 10350f3e72b5SJason Gunthorpe ~(VFIO_DEVICE_FEATURE_MASK | VFIO_DEVICE_FEATURE_SET | 10360f3e72b5SJason Gunthorpe VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_PROBE)) 10370f3e72b5SJason Gunthorpe return -EINVAL; 10380f3e72b5SJason Gunthorpe 10390f3e72b5SJason Gunthorpe /* GET & SET are mutually exclusive except with PROBE */ 10400f3e72b5SJason Gunthorpe if (!(feature.flags & VFIO_DEVICE_FEATURE_PROBE) && 10410f3e72b5SJason Gunthorpe (feature.flags & VFIO_DEVICE_FEATURE_SET) && 10420f3e72b5SJason Gunthorpe (feature.flags & VFIO_DEVICE_FEATURE_GET)) 10430f3e72b5SJason Gunthorpe return -EINVAL; 10440f3e72b5SJason Gunthorpe 10450f3e72b5SJason Gunthorpe switch (feature.flags & VFIO_DEVICE_FEATURE_MASK) { 10460f3e72b5SJason Gunthorpe case VFIO_DEVICE_FEATURE_MIGRATION: 10470f3e72b5SJason Gunthorpe return vfio_ioctl_device_feature_migration( 10480f3e72b5SJason Gunthorpe device, feature.flags, arg->data, 10490f3e72b5SJason Gunthorpe feature.argsz - minsz); 10500f3e72b5SJason Gunthorpe case VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE: 10510f3e72b5SJason Gunthorpe return vfio_ioctl_device_feature_mig_device_state( 10520f3e72b5SJason Gunthorpe device, feature.flags, arg->data, 10530f3e72b5SJason Gunthorpe feature.argsz - minsz); 105480c4b92aSYishai Hadas case VFIO_DEVICE_FEATURE_DMA_LOGGING_START: 105580c4b92aSYishai Hadas return vfio_ioctl_device_feature_logging_start( 105680c4b92aSYishai Hadas device, feature.flags, arg->data, 105780c4b92aSYishai Hadas feature.argsz - minsz); 105880c4b92aSYishai Hadas case VFIO_DEVICE_FEATURE_DMA_LOGGING_STOP: 105980c4b92aSYishai Hadas return vfio_ioctl_device_feature_logging_stop( 106080c4b92aSYishai Hadas device, feature.flags, arg->data, 106180c4b92aSYishai Hadas feature.argsz - minsz); 106280c4b92aSYishai Hadas case VFIO_DEVICE_FEATURE_DMA_LOGGING_REPORT: 106380c4b92aSYishai Hadas return vfio_ioctl_device_feature_logging_report( 106480c4b92aSYishai Hadas device, feature.flags, arg->data, 106580c4b92aSYishai Hadas feature.argsz - minsz); 10664e016f96SYishai Hadas case VFIO_DEVICE_FEATURE_MIG_DATA_SIZE: 10674e016f96SYishai Hadas return vfio_ioctl_device_feature_migration_data_size( 10684e016f96SYishai Hadas device, feature.flags, arg->data, 10694e016f96SYishai Hadas feature.argsz - minsz); 10700f3e72b5SJason Gunthorpe default: 10710f3e72b5SJason Gunthorpe if (unlikely(!device->ops->device_feature)) 10720f3e72b5SJason Gunthorpe return -EINVAL; 10730f3e72b5SJason Gunthorpe return device->ops->device_feature(device, feature.flags, 10740f3e72b5SJason Gunthorpe arg->data, 10750f3e72b5SJason Gunthorpe feature.argsz - minsz); 10760f3e72b5SJason Gunthorpe } 10770f3e72b5SJason Gunthorpe } 10780f3e72b5SJason Gunthorpe 10790f3e72b5SJason Gunthorpe static long vfio_device_fops_unl_ioctl(struct file *filep, 10800f3e72b5SJason Gunthorpe unsigned int cmd, unsigned long arg) 10810f3e72b5SJason Gunthorpe { 10820f3e72b5SJason Gunthorpe struct vfio_device *device = filep->private_data; 10838e5c6995SAbhishek Sahu int ret; 10848e5c6995SAbhishek Sahu 10858e5c6995SAbhishek Sahu ret = vfio_device_pm_runtime_get(device); 10868e5c6995SAbhishek Sahu if (ret) 10878e5c6995SAbhishek Sahu return ret; 10880f3e72b5SJason Gunthorpe 10890f3e72b5SJason Gunthorpe switch (cmd) { 10900f3e72b5SJason Gunthorpe case VFIO_DEVICE_FEATURE: 10918e5c6995SAbhishek Sahu ret = vfio_ioctl_device_feature(device, (void __user *)arg); 10928e5c6995SAbhishek Sahu break; 10938e5c6995SAbhishek Sahu 10940f3e72b5SJason Gunthorpe default: 10950f3e72b5SJason Gunthorpe if (unlikely(!device->ops->ioctl)) 10968e5c6995SAbhishek Sahu ret = -EINVAL; 10978e5c6995SAbhishek Sahu else 10988e5c6995SAbhishek Sahu ret = device->ops->ioctl(device, cmd, arg); 10998e5c6995SAbhishek Sahu break; 11000f3e72b5SJason Gunthorpe } 11018e5c6995SAbhishek Sahu 11028e5c6995SAbhishek Sahu vfio_device_pm_runtime_put(device); 11038e5c6995SAbhishek Sahu return ret; 11040f3e72b5SJason Gunthorpe } 11050f3e72b5SJason Gunthorpe 11060f3e72b5SJason Gunthorpe static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf, 11070f3e72b5SJason Gunthorpe size_t count, loff_t *ppos) 11080f3e72b5SJason Gunthorpe { 11090f3e72b5SJason Gunthorpe struct vfio_device *device = filep->private_data; 11100f3e72b5SJason Gunthorpe 11110f3e72b5SJason Gunthorpe if (unlikely(!device->ops->read)) 11120f3e72b5SJason Gunthorpe return -EINVAL; 11130f3e72b5SJason Gunthorpe 11140f3e72b5SJason Gunthorpe return device->ops->read(device, buf, count, ppos); 11150f3e72b5SJason Gunthorpe } 11160f3e72b5SJason Gunthorpe 11170f3e72b5SJason Gunthorpe static ssize_t vfio_device_fops_write(struct file *filep, 11180f3e72b5SJason Gunthorpe const char __user *buf, 11190f3e72b5SJason Gunthorpe size_t count, loff_t *ppos) 11200f3e72b5SJason Gunthorpe { 11210f3e72b5SJason Gunthorpe struct vfio_device *device = filep->private_data; 11220f3e72b5SJason Gunthorpe 11230f3e72b5SJason Gunthorpe if (unlikely(!device->ops->write)) 11240f3e72b5SJason Gunthorpe return -EINVAL; 11250f3e72b5SJason Gunthorpe 11260f3e72b5SJason Gunthorpe return device->ops->write(device, buf, count, ppos); 11270f3e72b5SJason Gunthorpe } 11280f3e72b5SJason Gunthorpe 11290f3e72b5SJason Gunthorpe static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) 11300f3e72b5SJason Gunthorpe { 11310f3e72b5SJason Gunthorpe struct vfio_device *device = filep->private_data; 11320f3e72b5SJason Gunthorpe 11330f3e72b5SJason Gunthorpe if (unlikely(!device->ops->mmap)) 11340f3e72b5SJason Gunthorpe return -EINVAL; 11350f3e72b5SJason Gunthorpe 11360f3e72b5SJason Gunthorpe return device->ops->mmap(device, vma); 11370f3e72b5SJason Gunthorpe } 11380f3e72b5SJason Gunthorpe 11399eefba80SYi Liu const struct file_operations vfio_device_fops = { 11400f3e72b5SJason Gunthorpe .owner = THIS_MODULE, 11410f3e72b5SJason Gunthorpe .release = vfio_device_fops_release, 11420f3e72b5SJason Gunthorpe .read = vfio_device_fops_read, 11430f3e72b5SJason Gunthorpe .write = vfio_device_fops_write, 11440f3e72b5SJason Gunthorpe .unlocked_ioctl = vfio_device_fops_unl_ioctl, 11450f3e72b5SJason Gunthorpe .compat_ioctl = compat_ptr_ioctl, 11460f3e72b5SJason Gunthorpe .mmap = vfio_device_fops_mmap, 11470f3e72b5SJason Gunthorpe }; 11480f3e72b5SJason Gunthorpe 11490f3e72b5SJason Gunthorpe /* 11500f3e72b5SJason Gunthorpe * Sub-module support 11510f3e72b5SJason Gunthorpe */ 11520f3e72b5SJason Gunthorpe /* 11530f3e72b5SJason Gunthorpe * Helper for managing a buffer of info chain capabilities, allocate or 11540f3e72b5SJason Gunthorpe * reallocate a buffer with additional @size, filling in @id and @version 11550f3e72b5SJason Gunthorpe * of the capability. A pointer to the new capability is returned. 11560f3e72b5SJason Gunthorpe * 11570f3e72b5SJason Gunthorpe * NB. The chain is based at the head of the buffer, so new entries are 11580f3e72b5SJason Gunthorpe * added to the tail, vfio_info_cap_shift() should be called to fixup the 11590f3e72b5SJason Gunthorpe * next offsets prior to copying to the user buffer. 11600f3e72b5SJason Gunthorpe */ 11610f3e72b5SJason Gunthorpe struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps, 11620f3e72b5SJason Gunthorpe size_t size, u16 id, u16 version) 11630f3e72b5SJason Gunthorpe { 11640f3e72b5SJason Gunthorpe void *buf; 11650f3e72b5SJason Gunthorpe struct vfio_info_cap_header *header, *tmp; 11660f3e72b5SJason Gunthorpe 11670f3e72b5SJason Gunthorpe buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL); 11680f3e72b5SJason Gunthorpe if (!buf) { 11690f3e72b5SJason Gunthorpe kfree(caps->buf); 11700f3e72b5SJason Gunthorpe caps->buf = NULL; 11710f3e72b5SJason Gunthorpe caps->size = 0; 11720f3e72b5SJason Gunthorpe return ERR_PTR(-ENOMEM); 11730f3e72b5SJason Gunthorpe } 11740f3e72b5SJason Gunthorpe 11750f3e72b5SJason Gunthorpe caps->buf = buf; 11760f3e72b5SJason Gunthorpe header = buf + caps->size; 11770f3e72b5SJason Gunthorpe 11780f3e72b5SJason Gunthorpe /* Eventually copied to user buffer, zero */ 11790f3e72b5SJason Gunthorpe memset(header, 0, size); 11800f3e72b5SJason Gunthorpe 11810f3e72b5SJason Gunthorpe header->id = id; 11820f3e72b5SJason Gunthorpe header->version = version; 11830f3e72b5SJason Gunthorpe 11840f3e72b5SJason Gunthorpe /* Add to the end of the capability chain */ 11850f3e72b5SJason Gunthorpe for (tmp = buf; tmp->next; tmp = buf + tmp->next) 11860f3e72b5SJason Gunthorpe ; /* nothing */ 11870f3e72b5SJason Gunthorpe 11880f3e72b5SJason Gunthorpe tmp->next = caps->size; 11890f3e72b5SJason Gunthorpe caps->size += size; 11900f3e72b5SJason Gunthorpe 11910f3e72b5SJason Gunthorpe return header; 11920f3e72b5SJason Gunthorpe } 11930f3e72b5SJason Gunthorpe EXPORT_SYMBOL_GPL(vfio_info_cap_add); 11940f3e72b5SJason Gunthorpe 11950f3e72b5SJason Gunthorpe void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) 11960f3e72b5SJason Gunthorpe { 11970f3e72b5SJason Gunthorpe struct vfio_info_cap_header *tmp; 11980f3e72b5SJason Gunthorpe void *buf = (void *)caps->buf; 11990f3e72b5SJason Gunthorpe 12000f3e72b5SJason Gunthorpe for (tmp = buf; tmp->next; tmp = buf + tmp->next - offset) 12010f3e72b5SJason Gunthorpe tmp->next += offset; 12020f3e72b5SJason Gunthorpe } 12030f3e72b5SJason Gunthorpe EXPORT_SYMBOL(vfio_info_cap_shift); 12040f3e72b5SJason Gunthorpe 12050f3e72b5SJason Gunthorpe int vfio_info_add_capability(struct vfio_info_cap *caps, 12060f3e72b5SJason Gunthorpe struct vfio_info_cap_header *cap, size_t size) 12070f3e72b5SJason Gunthorpe { 12080f3e72b5SJason Gunthorpe struct vfio_info_cap_header *header; 12090f3e72b5SJason Gunthorpe 12100f3e72b5SJason Gunthorpe header = vfio_info_cap_add(caps, size, cap->id, cap->version); 12110f3e72b5SJason Gunthorpe if (IS_ERR(header)) 12120f3e72b5SJason Gunthorpe return PTR_ERR(header); 12130f3e72b5SJason Gunthorpe 12140f3e72b5SJason Gunthorpe memcpy(header + 1, cap + 1, size - sizeof(*header)); 12150f3e72b5SJason Gunthorpe 12160f3e72b5SJason Gunthorpe return 0; 12170f3e72b5SJason Gunthorpe } 12180f3e72b5SJason Gunthorpe EXPORT_SYMBOL(vfio_info_add_capability); 12190f3e72b5SJason Gunthorpe 12200f3e72b5SJason Gunthorpe int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs, 12210f3e72b5SJason Gunthorpe int max_irq_type, size_t *data_size) 12220f3e72b5SJason Gunthorpe { 12230f3e72b5SJason Gunthorpe unsigned long minsz; 12240f3e72b5SJason Gunthorpe size_t size; 12250f3e72b5SJason Gunthorpe 12260f3e72b5SJason Gunthorpe minsz = offsetofend(struct vfio_irq_set, count); 12270f3e72b5SJason Gunthorpe 12280f3e72b5SJason Gunthorpe if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) || 12290f3e72b5SJason Gunthorpe (hdr->count >= (U32_MAX - hdr->start)) || 12300f3e72b5SJason Gunthorpe (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | 12310f3e72b5SJason Gunthorpe VFIO_IRQ_SET_ACTION_TYPE_MASK))) 12320f3e72b5SJason Gunthorpe return -EINVAL; 12330f3e72b5SJason Gunthorpe 12340f3e72b5SJason Gunthorpe if (data_size) 12350f3e72b5SJason Gunthorpe *data_size = 0; 12360f3e72b5SJason Gunthorpe 12370f3e72b5SJason Gunthorpe if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs) 12380f3e72b5SJason Gunthorpe return -EINVAL; 12390f3e72b5SJason Gunthorpe 12400f3e72b5SJason Gunthorpe switch (hdr->flags & VFIO_IRQ_SET_DATA_TYPE_MASK) { 12410f3e72b5SJason Gunthorpe case VFIO_IRQ_SET_DATA_NONE: 12420f3e72b5SJason Gunthorpe size = 0; 12430f3e72b5SJason Gunthorpe break; 12440f3e72b5SJason Gunthorpe case VFIO_IRQ_SET_DATA_BOOL: 12450f3e72b5SJason Gunthorpe size = sizeof(uint8_t); 12460f3e72b5SJason Gunthorpe break; 12470f3e72b5SJason Gunthorpe case VFIO_IRQ_SET_DATA_EVENTFD: 12480f3e72b5SJason Gunthorpe size = sizeof(int32_t); 12490f3e72b5SJason Gunthorpe break; 12500f3e72b5SJason Gunthorpe default: 12510f3e72b5SJason Gunthorpe return -EINVAL; 12520f3e72b5SJason Gunthorpe } 12530f3e72b5SJason Gunthorpe 12540f3e72b5SJason Gunthorpe if (size) { 12550f3e72b5SJason Gunthorpe if (hdr->argsz - minsz < hdr->count * size) 12560f3e72b5SJason Gunthorpe return -EINVAL; 12570f3e72b5SJason Gunthorpe 12580f3e72b5SJason Gunthorpe if (!data_size) 12590f3e72b5SJason Gunthorpe return -EINVAL; 12600f3e72b5SJason Gunthorpe 12610f3e72b5SJason Gunthorpe *data_size = hdr->count * size; 12620f3e72b5SJason Gunthorpe } 12630f3e72b5SJason Gunthorpe 12640f3e72b5SJason Gunthorpe return 0; 12650f3e72b5SJason Gunthorpe } 12660f3e72b5SJason Gunthorpe EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare); 12670f3e72b5SJason Gunthorpe 12680f3e72b5SJason Gunthorpe /* 12694741f2e9SJason Gunthorpe * Pin contiguous user pages and return their associated host pages for local 12704741f2e9SJason Gunthorpe * domain only. 12714741f2e9SJason Gunthorpe * @device [in] : device 12724741f2e9SJason Gunthorpe * @iova [in] : starting IOVA of user pages to be pinned. 12734741f2e9SJason Gunthorpe * @npage [in] : count of pages to be pinned. This count should not 12744741f2e9SJason Gunthorpe * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. 12754741f2e9SJason Gunthorpe * @prot [in] : protection flags 12764741f2e9SJason Gunthorpe * @pages[out] : array of host pages 12774741f2e9SJason Gunthorpe * Return error or number of pages pinned. 12784741f2e9SJason Gunthorpe * 12794741f2e9SJason Gunthorpe * A driver may only call this function if the vfio_device was created 12808da7a0e7SYi Liu * by vfio_register_emulated_iommu_dev() due to vfio_device_container_pin_pages(). 12814741f2e9SJason Gunthorpe */ 12824741f2e9SJason Gunthorpe int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, 12834741f2e9SJason Gunthorpe int npage, int prot, struct page **pages) 12844741f2e9SJason Gunthorpe { 12854741f2e9SJason Gunthorpe /* group->container cannot change while a vfio device is open */ 12864741f2e9SJason Gunthorpe if (!pages || !npage || WARN_ON(!vfio_assert_device_open(device))) 12874741f2e9SJason Gunthorpe return -EINVAL; 12888da7a0e7SYi Liu if (vfio_device_has_container(device)) 12898da7a0e7SYi Liu return vfio_device_container_pin_pages(device, iova, 12908da7a0e7SYi Liu npage, prot, pages); 12914741f2e9SJason Gunthorpe if (device->iommufd_access) { 12924741f2e9SJason Gunthorpe int ret; 12934741f2e9SJason Gunthorpe 12944741f2e9SJason Gunthorpe if (iova > ULONG_MAX) 12954741f2e9SJason Gunthorpe return -EINVAL; 12964741f2e9SJason Gunthorpe /* 12974741f2e9SJason Gunthorpe * VFIO ignores the sub page offset, npages is from the start of 12984741f2e9SJason Gunthorpe * a PAGE_SIZE chunk of IOVA. The caller is expected to recover 12994741f2e9SJason Gunthorpe * the sub page offset by doing: 13004741f2e9SJason Gunthorpe * pages[0] + (iova % PAGE_SIZE) 13014741f2e9SJason Gunthorpe */ 13024741f2e9SJason Gunthorpe ret = iommufd_access_pin_pages( 13034741f2e9SJason Gunthorpe device->iommufd_access, ALIGN_DOWN(iova, PAGE_SIZE), 13044741f2e9SJason Gunthorpe npage * PAGE_SIZE, pages, 13054741f2e9SJason Gunthorpe (prot & IOMMU_WRITE) ? IOMMUFD_ACCESS_RW_WRITE : 0); 13064741f2e9SJason Gunthorpe if (ret) 13074741f2e9SJason Gunthorpe return ret; 13084741f2e9SJason Gunthorpe return npage; 13094741f2e9SJason Gunthorpe } 13104741f2e9SJason Gunthorpe return -EINVAL; 13114741f2e9SJason Gunthorpe } 13124741f2e9SJason Gunthorpe EXPORT_SYMBOL(vfio_pin_pages); 13134741f2e9SJason Gunthorpe 13144741f2e9SJason Gunthorpe /* 13154741f2e9SJason Gunthorpe * Unpin contiguous host pages for local domain only. 13164741f2e9SJason Gunthorpe * @device [in] : device 13174741f2e9SJason Gunthorpe * @iova [in] : starting address of user pages to be unpinned. 13184741f2e9SJason Gunthorpe * @npage [in] : count of pages to be unpinned. This count should not 13194741f2e9SJason Gunthorpe * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. 13204741f2e9SJason Gunthorpe */ 13214741f2e9SJason Gunthorpe void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage) 13224741f2e9SJason Gunthorpe { 13234741f2e9SJason Gunthorpe if (WARN_ON(!vfio_assert_device_open(device))) 13244741f2e9SJason Gunthorpe return; 13254741f2e9SJason Gunthorpe 13268da7a0e7SYi Liu if (vfio_device_has_container(device)) { 13278da7a0e7SYi Liu vfio_device_container_unpin_pages(device, iova, npage); 13284741f2e9SJason Gunthorpe return; 13294741f2e9SJason Gunthorpe } 13304741f2e9SJason Gunthorpe if (device->iommufd_access) { 13314741f2e9SJason Gunthorpe if (WARN_ON(iova > ULONG_MAX)) 13324741f2e9SJason Gunthorpe return; 13334741f2e9SJason Gunthorpe iommufd_access_unpin_pages(device->iommufd_access, 13344741f2e9SJason Gunthorpe ALIGN_DOWN(iova, PAGE_SIZE), 13354741f2e9SJason Gunthorpe npage * PAGE_SIZE); 13364741f2e9SJason Gunthorpe return; 13374741f2e9SJason Gunthorpe } 13384741f2e9SJason Gunthorpe } 13394741f2e9SJason Gunthorpe EXPORT_SYMBOL(vfio_unpin_pages); 13404741f2e9SJason Gunthorpe 13414741f2e9SJason Gunthorpe /* 13424741f2e9SJason Gunthorpe * This interface allows the CPUs to perform some sort of virtual DMA on 13434741f2e9SJason Gunthorpe * behalf of the device. 13444741f2e9SJason Gunthorpe * 13454741f2e9SJason Gunthorpe * CPUs read/write from/into a range of IOVAs pointing to user space memory 13464741f2e9SJason Gunthorpe * into/from a kernel buffer. 13474741f2e9SJason Gunthorpe * 13484741f2e9SJason Gunthorpe * As the read/write of user space memory is conducted via the CPUs and is 13494741f2e9SJason Gunthorpe * not a real device DMA, it is not necessary to pin the user space memory. 13504741f2e9SJason Gunthorpe * 13514741f2e9SJason Gunthorpe * @device [in] : VFIO device 13524741f2e9SJason Gunthorpe * @iova [in] : base IOVA of a user space buffer 13534741f2e9SJason Gunthorpe * @data [in] : pointer to kernel buffer 13544741f2e9SJason Gunthorpe * @len [in] : kernel buffer length 13554741f2e9SJason Gunthorpe * @write : indicate read or write 13564741f2e9SJason Gunthorpe * Return error code on failure or 0 on success. 13574741f2e9SJason Gunthorpe */ 13584741f2e9SJason Gunthorpe int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data, 13594741f2e9SJason Gunthorpe size_t len, bool write) 13604741f2e9SJason Gunthorpe { 13614741f2e9SJason Gunthorpe if (!data || len <= 0 || !vfio_assert_device_open(device)) 13624741f2e9SJason Gunthorpe return -EINVAL; 13634741f2e9SJason Gunthorpe 13648da7a0e7SYi Liu if (vfio_device_has_container(device)) 13658da7a0e7SYi Liu return vfio_device_container_dma_rw(device, iova, 13664741f2e9SJason Gunthorpe data, len, write); 13674741f2e9SJason Gunthorpe 13684741f2e9SJason Gunthorpe if (device->iommufd_access) { 13694741f2e9SJason Gunthorpe unsigned int flags = 0; 13704741f2e9SJason Gunthorpe 13714741f2e9SJason Gunthorpe if (iova > ULONG_MAX) 13724741f2e9SJason Gunthorpe return -EINVAL; 13734741f2e9SJason Gunthorpe 13744741f2e9SJason Gunthorpe /* VFIO historically tries to auto-detect a kthread */ 13754741f2e9SJason Gunthorpe if (!current->mm) 13764741f2e9SJason Gunthorpe flags |= IOMMUFD_ACCESS_RW_KTHREAD; 13774741f2e9SJason Gunthorpe if (write) 13784741f2e9SJason Gunthorpe flags |= IOMMUFD_ACCESS_RW_WRITE; 13794741f2e9SJason Gunthorpe return iommufd_access_rw(device->iommufd_access, iova, data, 13804741f2e9SJason Gunthorpe len, flags); 13814741f2e9SJason Gunthorpe } 13824741f2e9SJason Gunthorpe return -EINVAL; 13834741f2e9SJason Gunthorpe } 13844741f2e9SJason Gunthorpe EXPORT_SYMBOL(vfio_dma_rw); 13854741f2e9SJason Gunthorpe 13864741f2e9SJason Gunthorpe /* 13870f3e72b5SJason Gunthorpe * Module/class support 13880f3e72b5SJason Gunthorpe */ 13891334e47eSYi Liu static int __init vfio_init(void) 13901334e47eSYi Liu { 13911334e47eSYi Liu int ret; 13921334e47eSYi Liu 13931334e47eSYi Liu ida_init(&vfio.device_ida); 13941334e47eSYi Liu 13951334e47eSYi Liu ret = vfio_group_init(); 13961334e47eSYi Liu if (ret) 13971334e47eSYi Liu return ret; 13981334e47eSYi Liu 1399e2d55709SJason Gunthorpe ret = vfio_virqfd_init(); 1400e2d55709SJason Gunthorpe if (ret) 1401e2d55709SJason Gunthorpe goto err_virqfd; 1402e2d55709SJason Gunthorpe 14031334e47eSYi Liu /* /sys/class/vfio-dev/vfioX */ 14041334e47eSYi Liu vfio.device_class = class_create(THIS_MODULE, "vfio-dev"); 14051334e47eSYi Liu if (IS_ERR(vfio.device_class)) { 14061334e47eSYi Liu ret = PTR_ERR(vfio.device_class); 14071334e47eSYi Liu goto err_dev_class; 14081334e47eSYi Liu } 14091334e47eSYi Liu 14101334e47eSYi Liu pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); 14111334e47eSYi Liu return 0; 14121334e47eSYi Liu 14131334e47eSYi Liu err_dev_class: 1414e2d55709SJason Gunthorpe vfio_virqfd_exit(); 1415e2d55709SJason Gunthorpe err_virqfd: 14161334e47eSYi Liu vfio_group_cleanup(); 14171334e47eSYi Liu return ret; 14181334e47eSYi Liu } 14191334e47eSYi Liu 14201334e47eSYi Liu static void __exit vfio_cleanup(void) 14211334e47eSYi Liu { 14221334e47eSYi Liu ida_destroy(&vfio.device_ida); 14233c28a761SYi Liu class_destroy(vfio.device_class); 14243c28a761SYi Liu vfio.device_class = NULL; 1425e2d55709SJason Gunthorpe vfio_virqfd_exit(); 14261334e47eSYi Liu vfio_group_cleanup(); 14270f3e72b5SJason Gunthorpe xa_destroy(&vfio_device_set_xa); 14280f3e72b5SJason Gunthorpe } 14290f3e72b5SJason Gunthorpe 14300f3e72b5SJason Gunthorpe module_init(vfio_init); 14310f3e72b5SJason Gunthorpe module_exit(vfio_cleanup); 14320f3e72b5SJason Gunthorpe 14330f3e72b5SJason Gunthorpe MODULE_VERSION(DRIVER_VERSION); 14340f3e72b5SJason Gunthorpe MODULE_LICENSE("GPL v2"); 14350f3e72b5SJason Gunthorpe MODULE_AUTHOR(DRIVER_AUTHOR); 14360f3e72b5SJason Gunthorpe MODULE_DESCRIPTION(DRIVER_DESC); 14370f3e72b5SJason Gunthorpe MODULE_SOFTDEP("post: vfio_iommu_type1 vfio_iommu_spapr_tce"); 1438