From patchwork Fri Sep 2 08:16:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jike Song X-Patchwork-Id: 9310535 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BE84A60756 for ; Fri, 2 Sep 2016 08:19:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B032F28FB9 for ; Fri, 2 Sep 2016 08:19:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A411B29709; Fri, 2 Sep 2016 08:19:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EA73B29707 for ; Fri, 2 Sep 2016 08:19:56 +0000 (UTC) Received: from localhost ([::1]:40885 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfjhc-0004Q8-3Z for patchwork-qemu-devel@patchwork.kernel.org; Fri, 02 Sep 2016 04:19:56 -0400 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 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP; 02 Sep 2016 01:19:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.30,270,1470726000"; d="scan'208";a="874396873" Received: from kvmgt1.bj.intel.com ([10.238.154.158]) by orsmga003.jf.intel.com with ESMTP; 02 Sep 2016 01:19:21 -0700 From: Jike Song To: alex.williamson@redhat.com, kwankhede@nvidia.com, cjia@nvidia.com Date: Fri, 2 Sep 2016 16:16:10 +0800 Message-Id: <1472804172-25542-3-git-send-email-jike.song@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1472804172-25542-1-git-send-email-jike.song@intel.com> References: <1472804172-25542-1-git-send-email-jike.song@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.43 Subject: [Qemu-devel] [RFC v2 2/4] vfio: VFIO bus driver for MDEV devices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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);