xen: register xen pci notifier
authorWeidong Han <weidong.han@intel.com>
Wed, 27 Oct 2010 16:55:04 +0000 (17:55 +0100)
committerStefano Stabellini <stefano.stabellini@eu.citrix.com>
Wed, 27 Oct 2010 17:56:07 +0000 (18:56 +0100)
Register a pci notifier to add (or remove) pci devices to Xen via
hypercalls. Xen needs to know the pci devices present in the system to
handle pci passthrough and even MSI remapping in the initial domain.

Signed-off-by: Weidong Han <weidong.han@intel.com>
Signed-off-by: Qing He <qing.he@intel.com>
Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
drivers/xen/Makefile
drivers/xen/pci.c [new file with mode: 0644]
include/xen/interface/physdev.h

index b978645..eb8a78d 100644 (file)
@@ -13,3 +13,4 @@ obj-$(CONFIG_XENFS)           += xenfs/
 obj-$(CONFIG_XEN_SYS_HYPERVISOR)       += sys-hypervisor.o
 obj-$(CONFIG_XEN_PLATFORM_PCI) += platform-pci.o
 obj-$(CONFIG_SWIOTLB_XEN)      += swiotlb-xen.o
+obj-$(CONFIG_XEN_DOM0)         += pci.o
diff --git a/drivers/xen/pci.c b/drivers/xen/pci.c
new file mode 100644 (file)
index 0000000..cef4baf
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2009, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * Author: Weidong Han <weidong.han@intel.com>
+ */
+
+#include <linux/pci.h>
+#include <xen/xen.h>
+#include <xen/interface/physdev.h>
+#include <xen/interface/xen.h>
+
+#include <asm/xen/hypervisor.h>
+#include <asm/xen/hypercall.h>
+#include "../pci/pci.h"
+
+static int xen_add_device(struct device *dev)
+{
+       int r;
+       struct pci_dev *pci_dev = to_pci_dev(dev);
+
+#ifdef CONFIG_PCI_IOV
+       if (pci_dev->is_virtfn) {
+               struct physdev_manage_pci_ext manage_pci_ext = {
+                       .bus            = pci_dev->bus->number,
+                       .devfn          = pci_dev->devfn,
+                       .is_virtfn      = 1,
+                       .physfn.bus     = pci_dev->physfn->bus->number,
+                       .physfn.devfn   = pci_dev->physfn->devfn,
+               };
+
+               r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
+                       &manage_pci_ext);
+       } else
+#endif
+       if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
+               struct physdev_manage_pci_ext manage_pci_ext = {
+                       .bus            = pci_dev->bus->number,
+                       .devfn          = pci_dev->devfn,
+                       .is_extfn       = 1,
+               };
+
+               r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
+                       &manage_pci_ext);
+       } else {
+               struct physdev_manage_pci manage_pci = {
+                       .bus    = pci_dev->bus->number,
+                       .devfn  = pci_dev->devfn,
+               };
+
+               r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
+                       &manage_pci);
+       }
+
+       return r;
+}
+
+static int xen_remove_device(struct device *dev)
+{
+       int r;
+       struct pci_dev *pci_dev = to_pci_dev(dev);
+       struct physdev_manage_pci manage_pci;
+
+       manage_pci.bus = pci_dev->bus->number;
+       manage_pci.devfn = pci_dev->devfn;
+
+       r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
+               &manage_pci);
+
+       return r;
+}
+
+static int xen_pci_notifier(struct notifier_block *nb,
+                           unsigned long action, void *data)
+{
+       struct device *dev = data;
+       int r = 0;
+
+       switch (action) {
+       case BUS_NOTIFY_ADD_DEVICE:
+               r = xen_add_device(dev);
+               break;
+       case BUS_NOTIFY_DEL_DEVICE:
+               r = xen_remove_device(dev);
+               break;
+       default:
+               break;
+       }
+
+       return r;
+}
+
+struct notifier_block device_nb = {
+       .notifier_call = xen_pci_notifier,
+};
+
+static int __init register_xen_pci_notifier(void)
+{
+       if (!xen_initial_domain())
+               return 0;
+
+       return bus_register_notifier(&pci_bus_type, &device_nb);
+}
+
+arch_initcall(register_xen_pci_notifier);
index a85d76c..2b2c66c 100644 (file)
@@ -136,6 +136,27 @@ struct physdev_unmap_pirq {
     int pirq;
 };
 
+#define PHYSDEVOP_manage_pci_add       15
+#define PHYSDEVOP_manage_pci_remove    16
+struct physdev_manage_pci {
+       /* IN */
+       uint8_t bus;
+       uint8_t devfn;
+};
+
+#define PHYSDEVOP_manage_pci_add_ext   20
+struct physdev_manage_pci_ext {
+       /* IN */
+       uint8_t bus;
+       uint8_t devfn;
+       unsigned is_extfn;
+       unsigned is_virtfn;
+       struct {
+               uint8_t bus;
+               uint8_t devfn;
+       } physfn;
+};
+
 /*
  * Argument to physdev_op_compat() hypercall. Superceded by new physdev_op()
  * hypercall since 0x00030202.