From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jike Song Subject: [RFC v2 2/4] vfio: VFIO bus driver for MDEV devices Date: Fri, 2 Sep 2016 16:16:10 +0800 Message-ID: <1472804172-25542-3-git-send-email-jike.song@intel.com> References: <1472804172-25542-1-git-send-email-jike.song@intel.com> Cc: kevin.tian@intel.com, guangrong.xiao@linux.intel.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, zhenyuw@linux.intel.com, jike.song@intel.com, zhiyuan.lv@intel.com, pbonzini@redhat.com, bjsdjshi@linux.vnet.ibm.com, kraxel@redhat.com To: alex.williamson@redhat.com, kwankhede@nvidia.com, cjia@nvidia.com Return-path: In-Reply-To: <1472804172-25542-1-git-send-email-jike.song@intel.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+gceq-qemu-devel=gmane.org@nongnu.org Sender: "Qemu-devel" List-Id: kvm.vger.kernel.org This driver implements the VFIO bus support for MDEV devices. Vendor drivers are expected to register with MDEV core driver. MDEV core driver creates mediated device and calls probe routine of this driver, then the mdev is added to VFIO core module. Main aim of this module is to manage all VFIO APIs for each mediated PCI device. See Documentation/vfio-mediated-device.txt for details. Signed-off-by: Kirti Wankhede Signed-off-by: Neo Jia Signed-off-by: Xiao Guangrong Signed-off-by: Jike Song --- drivers/vfio/mdev/Kconfig | 8 ++ drivers/vfio/mdev/Makefile | 1 + drivers/vfio/mdev/vfio_mdev.c | 187 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 drivers/vfio/mdev/vfio_mdev.c diff --git a/drivers/vfio/mdev/Kconfig b/drivers/vfio/mdev/Kconfig index d25439f..b2fe0c6 100644 --- a/drivers/vfio/mdev/Kconfig +++ b/drivers/vfio/mdev/Kconfig @@ -8,3 +8,11 @@ config MDEV See Documentation/vfio-mediated-device.txt for more details. If you don't know what do here, say N. + +config VFIO_MDEV + tristate "VFIO Bus driver for Mediated devices" + depends on VFIO && MDEV + default n + help + VFIO Bus driver for mediated devices. + diff --git a/drivers/vfio/mdev/Makefile b/drivers/vfio/mdev/Makefile index 8bd78b5..ee9f89f 100644 --- a/drivers/vfio/mdev/Makefile +++ b/drivers/vfio/mdev/Makefile @@ -2,3 +2,4 @@ mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o obj-$(CONFIG_MDEV) += mdev.o +obj-$(CONFIG_VFIO_MDEV) += vfio_mdev.o diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c new file mode 100644 index 0000000..c22ebd8 --- /dev/null +++ b/drivers/vfio/mdev/vfio_mdev.c @@ -0,0 +1,187 @@ +/* + * VFIO Bus driver for Mediated device + * + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * Author: Neo Jia + * Kirti Wankhede + * + * Copyright (c) 2016 Intel Corporation. + * Author: + * Xiao Guangrong + * Jike Song + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mdev_private.h" + +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "NVIDIA Corporation" +#define DRIVER_DESC "VFIO Bus driver for Mediated device" + +struct vfio_mdev { + struct iommu_group *group; + struct mdev_device *mdev; +}; + +static int vfio_mdev_open(void *device_data) +{ + if (!try_module_get(THIS_MODULE)) + return -ENODEV; + + return 0; +} + +static void vfio_mdev_close(void *device_data) +{ + module_put(THIS_MODULE); +} + +static long vfio_mdev_unlocked_ioctl(void *device_data, + unsigned int cmd, unsigned long arg) +{ + struct vfio_mdev *vmdev = device_data; + struct mdev_device *mdev = vmdev->mdev; + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->ioctl) + return host->ops->ioctl(mdev, cmd, arg); + + return -ENODEV; +} + +static ssize_t vfio_mdev_read(void *device_data, char __user *buf, + size_t count, loff_t *ppos) +{ + struct vfio_mdev *vmdev = device_data; + struct mdev_device *mdev = vmdev->mdev; + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->read) + return host->ops->read(mdev, buf, count, ppos); + + return -ENODEV; +} + +static ssize_t vfio_mdev_write(void *device_data, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct vfio_mdev *vmdev = device_data; + struct mdev_device *mdev = vmdev->mdev; + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->write) + return host->ops->write(mdev, buf, count, ppos); + + return -ENODEV; +} + +static int vfio_mdev_mmap(void *device_data, struct vm_area_struct *vma) +{ + struct vfio_mdev *vmdev = device_data; + struct mdev_device *mdev = vmdev->mdev; + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->mmap) + return host->ops->mmap(mdev, vma); + + return -ENODEV; +} + +static const struct vfio_device_ops vfio_mdev_dev_ops = { + .name = "vfio-mdev", + .open = vfio_mdev_open, + .release = vfio_mdev_close, + .ioctl = vfio_mdev_unlocked_ioctl, + .read = vfio_mdev_read, + .write = vfio_mdev_write, + .mmap = vfio_mdev_mmap, +}; + +static int vfio_mdev_probe(struct device *dev) +{ + struct vfio_mdev *vmdev; + struct mdev_device *mdev = dev_to_mdev(dev); + int ret; + + vmdev = kzalloc(sizeof(*vmdev), GFP_KERNEL); + if (IS_ERR(vmdev)) + return PTR_ERR(vmdev); + + vmdev->mdev = mdev; + vmdev->group = mdev->group; + + ret = vfio_add_group_dev(dev, &vfio_mdev_dev_ops, vmdev); + if (ret) + kfree(vmdev); + + return ret; +} + +static void vfio_mdev_remove(struct device *dev) +{ + struct vfio_mdev *vmdev; + + vmdev = vfio_del_group_dev(dev); + kfree(vmdev); +} + +static int vfio_mdev_online(struct device *dev) +{ + struct mdev_device *mdev = dev_to_mdev(dev); + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->start) + return host->ops->start(mdev); + + return -ENOTSUPP; +} + +static int vfio_mdev_offline(struct device *dev) +{ + struct mdev_device *mdev = dev_to_mdev(dev); + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->stop) + return host->ops->stop(mdev); + + return -ENOTSUPP; +} + +static struct mdev_driver vfio_mdev_driver = { + .name = "vfio_mdev", + .probe = vfio_mdev_probe, + .remove = vfio_mdev_remove, + .online = vfio_mdev_online, + .offline = vfio_mdev_offline, +}; + +static int __init vfio_mdev_init(void) +{ + return mdev_register_driver(&vfio_mdev_driver, THIS_MODULE); +} + +static void __exit vfio_mdev_exit(void) +{ + mdev_unregister_driver(&vfio_mdev_driver); +} + +module_init(vfio_mdev_init) +module_exit(vfio_mdev_exit) + +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC); -- 1.9.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52193) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfjh8-0004Oj-Ta for qemu-devel@nongnu.org; Fri, 02 Sep 2016 04:19:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bfjh6-0007C0-E7 for qemu-devel@nongnu.org; Fri, 02 Sep 2016 04:19:25 -0400 Received: from mga05.intel.com ([192.55.52.43]:48821) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfjh6-0007Aw-3z for qemu-devel@nongnu.org; Fri, 02 Sep 2016 04:19:24 -0400 From: Jike Song Date: Fri, 2 Sep 2016 16:16:10 +0800 Message-Id: <1472804172-25542-3-git-send-email-jike.song@intel.com> In-Reply-To: <1472804172-25542-1-git-send-email-jike.song@intel.com> References: <1472804172-25542-1-git-send-email-jike.song@intel.com> Subject: [Qemu-devel] [RFC v2 2/4] vfio: VFIO bus driver for MDEV devices List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: alex.williamson@redhat.com, kwankhede@nvidia.com, cjia@nvidia.com Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org, bjsdjshi@linux.vnet.ibm.com, kevin.tian@intel.com, guangrong.xiao@linux.intel.com, zhenyuw@linux.intel.com, zhiyuan.lv@intel.com, jike.song@intel.com, pbonzini@redhat.com, kraxel@redhat.com This driver implements the VFIO bus support for MDEV devices. Vendor drivers are expected to register with MDEV core driver. MDEV core driver creates mediated device and calls probe routine of this driver, then the mdev is added to VFIO core module. Main aim of this module is to manage all VFIO APIs for each mediated PCI device. See Documentation/vfio-mediated-device.txt for details. Signed-off-by: Kirti Wankhede Signed-off-by: Neo Jia Signed-off-by: Xiao Guangrong Signed-off-by: Jike Song --- drivers/vfio/mdev/Kconfig | 8 ++ drivers/vfio/mdev/Makefile | 1 + drivers/vfio/mdev/vfio_mdev.c | 187 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 drivers/vfio/mdev/vfio_mdev.c diff --git a/drivers/vfio/mdev/Kconfig b/drivers/vfio/mdev/Kconfig index d25439f..b2fe0c6 100644 --- a/drivers/vfio/mdev/Kconfig +++ b/drivers/vfio/mdev/Kconfig @@ -8,3 +8,11 @@ config MDEV See Documentation/vfio-mediated-device.txt for more details. If you don't know what do here, say N. + +config VFIO_MDEV + tristate "VFIO Bus driver for Mediated devices" + depends on VFIO && MDEV + default n + help + VFIO Bus driver for mediated devices. + diff --git a/drivers/vfio/mdev/Makefile b/drivers/vfio/mdev/Makefile index 8bd78b5..ee9f89f 100644 --- a/drivers/vfio/mdev/Makefile +++ b/drivers/vfio/mdev/Makefile @@ -2,3 +2,4 @@ mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o obj-$(CONFIG_MDEV) += mdev.o +obj-$(CONFIG_VFIO_MDEV) += vfio_mdev.o diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c new file mode 100644 index 0000000..c22ebd8 --- /dev/null +++ b/drivers/vfio/mdev/vfio_mdev.c @@ -0,0 +1,187 @@ +/* + * VFIO Bus driver for Mediated device + * + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * Author: Neo Jia + * Kirti Wankhede + * + * Copyright (c) 2016 Intel Corporation. + * Author: + * Xiao Guangrong + * Jike Song + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mdev_private.h" + +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "NVIDIA Corporation" +#define DRIVER_DESC "VFIO Bus driver for Mediated device" + +struct vfio_mdev { + struct iommu_group *group; + struct mdev_device *mdev; +}; + +static int vfio_mdev_open(void *device_data) +{ + if (!try_module_get(THIS_MODULE)) + return -ENODEV; + + return 0; +} + +static void vfio_mdev_close(void *device_data) +{ + module_put(THIS_MODULE); +} + +static long vfio_mdev_unlocked_ioctl(void *device_data, + unsigned int cmd, unsigned long arg) +{ + struct vfio_mdev *vmdev = device_data; + struct mdev_device *mdev = vmdev->mdev; + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->ioctl) + return host->ops->ioctl(mdev, cmd, arg); + + return -ENODEV; +} + +static ssize_t vfio_mdev_read(void *device_data, char __user *buf, + size_t count, loff_t *ppos) +{ + struct vfio_mdev *vmdev = device_data; + struct mdev_device *mdev = vmdev->mdev; + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->read) + return host->ops->read(mdev, buf, count, ppos); + + return -ENODEV; +} + +static ssize_t vfio_mdev_write(void *device_data, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct vfio_mdev *vmdev = device_data; + struct mdev_device *mdev = vmdev->mdev; + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->write) + return host->ops->write(mdev, buf, count, ppos); + + return -ENODEV; +} + +static int vfio_mdev_mmap(void *device_data, struct vm_area_struct *vma) +{ + struct vfio_mdev *vmdev = device_data; + struct mdev_device *mdev = vmdev->mdev; + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->mmap) + return host->ops->mmap(mdev, vma); + + return -ENODEV; +} + +static const struct vfio_device_ops vfio_mdev_dev_ops = { + .name = "vfio-mdev", + .open = vfio_mdev_open, + .release = vfio_mdev_close, + .ioctl = vfio_mdev_unlocked_ioctl, + .read = vfio_mdev_read, + .write = vfio_mdev_write, + .mmap = vfio_mdev_mmap, +}; + +static int vfio_mdev_probe(struct device *dev) +{ + struct vfio_mdev *vmdev; + struct mdev_device *mdev = dev_to_mdev(dev); + int ret; + + vmdev = kzalloc(sizeof(*vmdev), GFP_KERNEL); + if (IS_ERR(vmdev)) + return PTR_ERR(vmdev); + + vmdev->mdev = mdev; + vmdev->group = mdev->group; + + ret = vfio_add_group_dev(dev, &vfio_mdev_dev_ops, vmdev); + if (ret) + kfree(vmdev); + + return ret; +} + +static void vfio_mdev_remove(struct device *dev) +{ + struct vfio_mdev *vmdev; + + vmdev = vfio_del_group_dev(dev); + kfree(vmdev); +} + +static int vfio_mdev_online(struct device *dev) +{ + struct mdev_device *mdev = dev_to_mdev(dev); + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->start) + return host->ops->start(mdev); + + return -ENOTSUPP; +} + +static int vfio_mdev_offline(struct device *dev) +{ + struct mdev_device *mdev = dev_to_mdev(dev); + struct mdev_host *host = dev_to_host(mdev->dev.parent); + + if (host->ops->stop) + return host->ops->stop(mdev); + + return -ENOTSUPP; +} + +static struct mdev_driver vfio_mdev_driver = { + .name = "vfio_mdev", + .probe = vfio_mdev_probe, + .remove = vfio_mdev_remove, + .online = vfio_mdev_online, + .offline = vfio_mdev_offline, +}; + +static int __init vfio_mdev_init(void) +{ + return mdev_register_driver(&vfio_mdev_driver, THIS_MODULE); +} + +static void __exit vfio_mdev_exit(void) +{ + mdev_unregister_driver(&vfio_mdev_driver); +} + +module_init(vfio_mdev_init) +module_exit(vfio_mdev_exit) + +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC); -- 1.9.1