All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jike Song <jike.song@intel.com>
To: alex.williamson@redhat.com, kwankhede@nvidia.com, cjia@nvidia.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
Subject: [RFC v2 2/4] vfio: VFIO bus driver for MDEV devices
Date: Fri,  2 Sep 2016 16:16:10 +0800	[thread overview]
Message-ID: <1472804172-25542-3-git-send-email-jike.song@intel.com> (raw)
In-Reply-To: <1472804172-25542-1-git-send-email-jike.song@intel.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 <kwankhede@nvidia.com>
Signed-off-by: Neo Jia <cjia@nvidia.com>
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Jike Song <jike.song@intel.com>
---
 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 <cjia@nvidia.com>
+ *	       Kirti Wankhede <kwankhede@nvidia.com>
+ *
+ * Copyright (c) 2016 Intel Corporation.
+ * Author:
+ *	Xiao Guangrong <guangrong.xiao@linux.intel.com>
+ *	Jike Song <jike.song@intel.com>
+ *
+ * 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 <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/vfio.h>
+#include <linux/iommu.h>
+#include <linux/mdev.h>
+
+#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

WARNING: multiple messages have this Message-ID (diff)
From: Jike Song <jike.song@intel.com>
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
Subject: [Qemu-devel] [RFC v2 2/4] vfio: VFIO bus driver for MDEV devices
Date: Fri,  2 Sep 2016 16:16:10 +0800	[thread overview]
Message-ID: <1472804172-25542-3-git-send-email-jike.song@intel.com> (raw)
In-Reply-To: <1472804172-25542-1-git-send-email-jike.song@intel.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 <kwankhede@nvidia.com>
Signed-off-by: Neo Jia <cjia@nvidia.com>
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Jike Song <jike.song@intel.com>
---
 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 <cjia@nvidia.com>
+ *	       Kirti Wankhede <kwankhede@nvidia.com>
+ *
+ * Copyright (c) 2016 Intel Corporation.
+ * Author:
+ *	Xiao Guangrong <guangrong.xiao@linux.intel.com>
+ *	Jike Song <jike.song@intel.com>
+ *
+ * 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 <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/vfio.h>
+#include <linux/iommu.h>
+#include <linux/mdev.h>
+
+#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

  parent reply	other threads:[~2016-09-02  8:16 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-02  8:16 [RFC v2 0/4] adding mdev bus and vfio support Jike Song
2016-09-02  8:16 ` [Qemu-devel] " Jike Song
2016-09-02  8:16 ` [RFC v2 1/4] Mediated device Core driver Jike Song
2016-09-02  8:16   ` [Qemu-devel] " Jike Song
2016-09-02  8:16 ` Jike Song [this message]
2016-09-02  8:16   ` [Qemu-devel] [RFC v2 2/4] vfio: VFIO bus driver for MDEV devices Jike Song
2016-09-02  8:16 ` [RFC v2 3/4] vfio iommu: Add support for mediated devices Jike Song
2016-09-02  8:16   ` [Qemu-devel] " Jike Song
2016-09-02  8:16 ` [RFC v2 4/4] docs: Add Documentation for Mediated devices Jike Song
2016-09-02  8:16   ` [Qemu-devel] " Jike Song
2016-09-02 22:09   ` Eric Blake
2016-09-02 22:09     ` Eric Blake
2016-09-02 23:30     ` Neo Jia
2016-09-02 23:30       ` Neo Jia
2016-09-02 15:03 ` [RFC v2 0/4] adding mdev bus and vfio support Alex Williamson
2016-09-02 15:03   ` [Qemu-devel] " Alex Williamson
2016-09-02 20:05   ` Neo Jia
2016-09-02 20:05     ` [Qemu-devel] " Neo Jia
2016-09-07  2:22   ` Jike Song
2016-09-07  2:22     ` [Qemu-devel] " Jike Song
2016-09-07  3:38     ` Neo Jia
2016-09-07  3:38       ` [Qemu-devel] " Neo Jia
2016-09-07  6:42       ` Jike Song
2016-09-07  6:42         ` [Qemu-devel] " Jike Song
2016-09-07 16:56         ` Alex Williamson
2016-09-07 16:56           ` [Qemu-devel] " Alex Williamson
2016-09-08  8:00           ` Jike Song
2016-09-08  8:00             ` [Qemu-devel] " Jike Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1472804172-25542-3-git-send-email-jike.song@intel.com \
    --to=jike.song@intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=bjsdjshi@linux.vnet.ibm.com \
    --cc=cjia@nvidia.com \
    --cc=guangrong.xiao@linux.intel.com \
    --cc=kevin.tian@intel.com \
    --cc=kraxel@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=zhenyuw@linux.intel.com \
    --cc=zhiyuan.lv@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.