All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: chrisw@sous-sol.org, aik@au1.ibm.com, pmac@au1.ibm.com,
	dwg@au1.ibm.com, joerg.roedel@amd.com, agraf@suse.de,
	benve@cisco.com, aafabbri@cisco.com, B08248@freescale.com,
	B07421@freescale.com, avi@redhat.com, kvm@vger.kernel.org,
	qemu-devel@nongnu.org, iommu@lists.linux-foundation.org,
	linux-pci@vger.kernel.org
Cc: alex.williamson@redhat.com
Subject: [Qemu-devel] [RFC PATCH 4/5] VFIO: Add PCI device support
Date: Thu, 01 Sep 2011 13:50:50 -0600	[thread overview]
Message-ID: <20110901195050.2391.12028.stgit@s20.home> (raw)
In-Reply-To: <20110901194915.2391.97400.stgit@s20.home>

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---

 drivers/vfio/Kconfig        |    7 ++
 drivers/vfio/Makefile       |    1 
 drivers/vfio/vfio_main.c    |   10 +++
 drivers/vfio/vfio_pci.c     |  124 +++++++++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio_private.h |    5 ++
 5 files changed, 147 insertions(+), 0 deletions(-)
 create mode 100644 drivers/vfio/vfio_pci.c

diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig
index a150521..b17bdbd 100644
--- a/drivers/vfio/Kconfig
+++ b/drivers/vfio/Kconfig
@@ -3,3 +3,10 @@ menuconfig VFIO
 	depends on IOMMU_API
 	help
 	  If you don't know what to do here, say N.
+
+menuconfig VFIO_PCI
+	bool "VFIO support for PCI devices"
+	depends on VFIO && PCI
+	default y if X86
+	help
+	  If you don't know what to do here, say N.
diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile
index 5eaa074..90ee753 100644
--- a/drivers/vfio/Makefile
+++ b/drivers/vfio/Makefile
@@ -1,3 +1,4 @@
 obj-$(CONFIG_VFIO) := vfio.o
 
 vfio-y := vfio_main.o vfio_iommu.o vfio_device.o
+vfio-$(CONFIG_VFIO_PCI) += vfio_pci.o
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 7f05692..c6e80f7 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -834,6 +834,12 @@ static int __init vfio_init(void)
 	if (ret)
 		goto err_cdev;
 
+#ifdef CONFIG_VFIO_PCI
+	ret = vfio_pci_init(&vfio);
+	if (ret)
+		pr_debug(DRIVER_DESC "PCI init failed %d\n", ret);
+#endif
+
 	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
 
 	return 0;
@@ -864,6 +870,10 @@ static void __exit vfio_cleanup(void)
 		}
 	}
 
+#ifdef CONFIG_VFIO_PCI
+	vfio_pci_cleanup(&vfio);
+#endif
+
 	idr_destroy(&vfio.idr);
 	cdev_del(&vfio.cdev);
 	unregister_chrdev_region(vfio.devt, MINORMASK);
diff --git a/drivers/vfio/vfio_pci.c b/drivers/vfio/vfio_pci.c
new file mode 100644
index 0000000..88325d0
--- /dev/null
+++ b/drivers/vfio/vfio_pci.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2011 Red Hat, Inc.  All rights reserved.
+ *     Author: Alex Williamson <alex.williamson@redhat.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.
+ *
+ * Derived from original vfio:
+ * Copyright 2010 Cisco Systems, Inc.  All rights reserved.
+ * Author: Tom Lyon, pugs@cisco.com
+ */
+
+#include <linux/device.h>
+#include <linux/notifier.h>
+#include <linux/pci.h>
+#include <linux/slab.h>
+#include <linux/vfio.h>
+
+#include "vfio_private.h"
+
+struct vfio_pci_device {
+	struct vfio_device	vdev;
+	struct pci_dev		*pdev;
+};
+
+static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+	return 0;
+}
+
+static void vfio_pci_remove(struct pci_dev *pdev)
+{
+}
+
+static struct pci_driver vfio_pci_driver = {
+	.name		= "vfio",
+	.id_table	= NULL, /* only dynamic id's */
+	.probe		= vfio_pci_probe,
+	.remove		= vfio_pci_remove,
+};
+
+static struct vfio_device *vfio_pci_new(struct device *dev)
+{
+	struct vfio_pci_device *pvdev;
+
+	pvdev = kzalloc(sizeof(*pvdev), GFP_KERNEL);
+	if (!pvdev)
+		return ERR_PTR(-ENOMEM);
+
+	printk("%s: alloc pvdev @%p\n", __FUNCTION__, pvdev);
+	pvdev->pdev = container_of(dev, struct pci_dev, dev);
+
+	// PCI stuff...
+
+	return &pvdev->vdev;
+}
+
+static void vfio_pci_free(struct vfio_device *vdev)
+{
+	struct vfio_pci_device *pvdev;
+
+	pvdev = container_of(vdev, struct vfio_pci_device, vdev);
+
+	// PCI stuff...
+
+	printk("%s: freeing pvdev @%p\n", __FUNCTION__, pvdev);
+	kfree(pvdev);
+}
+
+static const struct vfio_device_ops vfio_pci_ops = {
+	.new	= vfio_pci_new,
+	.free	= vfio_pci_free,
+};
+
+static int vfio_pci_device_notifier(struct notifier_block *nb,
+				    unsigned long action, void *data)
+{
+        struct device *dev = data;
+	struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
+
+        if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
+		return 0;
+
+        if (action == BUS_NOTIFY_ADD_DEVICE)
+                return vfio_group_add_dev(dev, (void *)&vfio_pci_ops);
+        else if (action == BUS_NOTIFY_DEL_DEVICE)
+                vfio_group_del_dev(dev);
+        return 0;
+}
+
+static int vfio_pci_add_dev(struct device *dev, void *unused)
+{
+	struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
+
+        if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
+		return 0;
+
+	return vfio_group_add_dev(dev, (void *)&vfio_pci_ops);
+}
+
+static struct notifier_block vfio_pci_device_nb = {
+        .notifier_call = vfio_pci_device_notifier,
+};
+
+void __exit vfio_pci_cleanup(struct vfio *vfio)
+{
+	bus_unregister_notifier(&pci_bus_type, &vfio_pci_device_nb);
+	pci_unregister_driver(&vfio_pci_driver);
+}
+
+int __init vfio_pci_init(struct vfio *vfio)
+{
+	int ret;
+
+	ret = pci_register_driver(&vfio_pci_driver);
+	if (ret)
+		return ret;
+
+	bus_register_notifier(&pci_bus_type, &vfio_pci_device_nb);
+	bus_for_each_dev(&pci_bus_type, NULL, NULL, vfio_pci_add_dev);
+
+	return 0;
+}
diff --git a/drivers/vfio/vfio_private.h b/drivers/vfio/vfio_private.h
index 2cc300c..85c88ea 100644
--- a/drivers/vfio/vfio_private.h
+++ b/drivers/vfio/vfio_private.h
@@ -79,4 +79,9 @@ struct vfio_group {
 extern int vfio_group_add_dev(struct device *dev, void *data);
 extern void vfio_group_del_dev(struct device *dev);
 
+#ifdef CONFIG_VFIO_PCI
+extern int vfio_pci_init(struct vfio *vfio);
+extern void vfio_pci_cleanup(struct vfio *vfio);
+#endif
+
 #endif /* VFIO_PRIVATE_H */

  parent reply	other threads:[~2011-09-01 19:52 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-01 19:50 [Qemu-devel] [RFC PATCH 0/5] VFIO-NG group/device/iommu framework Alex Williamson
2011-09-01 19:50 ` [RFC PATCH 1/5] iommu: Add iommu_device_group callback and iommu_group sysfs entry Alex Williamson
2011-09-01 19:50   ` [Qemu-devel] " Alex Williamson
2011-09-01 19:50 ` [RFC PATCH 2/5] intel-iommu: Implement iommu_device_group Alex Williamson
2011-09-01 19:50   ` [Qemu-devel] " Alex Williamson
2011-09-01 19:50 ` [Qemu-devel] [RFC PATCH 3/5] VFIO: Base framework for new VFIO driver Alex Williamson
2011-09-07 14:52   ` Konrad Rzeszutek Wilk
2011-09-07 14:52     ` [Qemu-devel] " Konrad Rzeszutek Wilk
2011-09-19 16:42     ` Alex Williamson
2011-09-19 16:42       ` [Qemu-devel] " Alex Williamson
2011-09-01 19:50 ` Alex Williamson
2011-09-01 19:50 ` [RFC PATCH 4/5] VFIO: Add PCI device support Alex Williamson
2011-09-01 19:50 ` Alex Williamson [this message]
2011-09-07 18:55   ` Konrad Rzeszutek Wilk
2011-09-07 18:55     ` [Qemu-devel] " Konrad Rzeszutek Wilk
2011-09-08  7:52     ` Avi Kivity
2011-09-08  7:52       ` [Qemu-devel] " Avi Kivity
2011-09-08 21:52       ` Alex Williamson
2011-09-08 21:52         ` [Qemu-devel] " Alex Williamson
2011-09-01 19:50 ` [Qemu-devel] [RFC PATCH 5/5] VFIO: Simple test tool Alex Williamson
2011-09-01 19:50 ` Alex Williamson
2011-09-07 11:58 ` [RFC PATCH 0/5] VFIO-NG group/device/iommu framework Alexander Graf
2011-09-07 11:58   ` [Qemu-devel] " Alexander Graf
2011-09-08 21:54   ` Alex Williamson
2011-09-08 21:54     ` [Qemu-devel] " Alex Williamson

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=20110901195050.2391.12028.stgit@s20.home \
    --to=alex.williamson@redhat.com \
    --cc=B07421@freescale.com \
    --cc=B08248@freescale.com \
    --cc=aafabbri@cisco.com \
    --cc=agraf@suse.de \
    --cc=aik@au1.ibm.com \
    --cc=avi@redhat.com \
    --cc=benve@cisco.com \
    --cc=chrisw@sous-sol.org \
    --cc=dwg@au1.ibm.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=joerg.roedel@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=pmac@au1.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /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.