Skip to contentSkip to conversation

xen pci platform device driver

PATCH In review Stefano Stabellini 2 participants 3 messages Apr 22, 2010 – Apr 23, 2010 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen pci platform device driver

Hi all,
this patch adds the xen pci platform device driver that is responsible for initializing the grant table and xenbus in PV on HVM mode. Few changes to xenbus and grant table are necessary to allow the delayed initialization in HVM mode.
Finally grant table needs few additional modifications to work in HVM mode.

---
 drivers/xen/Kconfig                  |   10 ++
 drivers/xen/Makefile                 |    1 +
 drivers/xen/events.c                 |    2 +-
 drivers/xen/grant-table.c            |   66 +++++++++-
 drivers/xen/platform-pci.c           |  227 ++++++++++++++++++++++++++++++++++
 drivers/xen/xenbus/xenbus_probe.c    |   20 ++-
 include/xen/grant_table.h            |    1 +
 include/xen/interface/grant_table.h  |    1 +
 include/xen/interface/platform_pci.h |   45 +++++++
 include/xen/platform_pci.h           |   40 ++++++
 include/xen/xenbus.h                 |    1 +
 11 files changed, 400 insertions(+), 14 deletions(-)
 create mode 100644 drivers/xen/platform-pci.c
 create mode 100644 include/xen/interface/platform_pci.h
 create mode 100644 include/xen/platform_pci.h
drivers/xen/Kconfig +10 -0
index d6d6f3e..5f9f7da 100644
@@ -184,3 +184,13 @@ config ACPI_PROCESSOR_XEN
 	   tristate
 	   depends on XEN_DOM0 && ACPI_PROCESSOR && CPU_FREQ
 	   default y
+
+config XEN_PLATFORM_PCI
+	tristate "xen platform pci device driver"
+	depends on XEN
+	select XEN_XENBUS_FRONTEND
+	help
+	  Driver for the Xen Pci Platform device: it is responsible for
+	  initializing xenbus and grant_table when running in a Xen HVM
+	  domain. As a consequence this driver is required to run any Xen PV
+	  frontend on Xen HVM.
drivers/xen/Makefile +1 -0
index 23bc06e..5a63c02 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_XEN_SYS_HYPERVISOR)	+= sys-hypervisor.o
 obj-$(CONFIG_XEN_S3)           += acpi.o
 obj-$(CONFIG_ACPI_PROCESSOR_XEN) += acpi_processor.o
 obj-$(CONFIG_ACPI_HOTPLUG_MEMORY)  += xen_acpi_memhotplug.o
+obj-$(CONFIG_XEN_PLATFORM_PCI)		+= platform-pci.o
 
 xen-evtchn-y				:= evtchn.o
 xen-gntdev-y				:= gntdev.o
drivers/xen/events.c +1 -1
index 50a360b..5f758c9 100644
@@ -1120,7 +1120,7 @@ static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
 	struct evtchn_bind_vcpu bind_vcpu;
 	int evtchn = evtchn_from_irq(irq);
 
-	if (!VALID_EVTCHN(evtchn))
+	if (!VALID_EVTCHN(evtchn) || xen_hvm_domain())
 		return -1;
 
 	/* Send future instances of this interrupt to other vcpu. */
drivers/xen/grant-table.c +59 -7

110 lines not on this page · raw message

drivers/xen/platform-pci.c new file +227 -0

227 lines not on this page · raw message

drivers/xen/xenbus/xenbus_probe.c +14 -6
index 7d11957..f0e6574 100644
@@ -668,17 +668,25 @@ void xenbus_probe(struct work_struct *unused)
 	blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
 }
 
-static int __init xenbus_probe_init(void)
+static int __init __xenbus_probe_init(void)
+{
+	/* Delay initialization in the PV on HVM case */
+	if (xen_hvm_domain())
+		return 0;
+
+	if (!xen_pv_domain())
+		return -ENODEV;
+
+	return xenbus_probe_init();
+}
+
+int xenbus_probe_init(void)
 {
 	int err = 0;
 	unsigned long page = 0;
 
 	DPRINTK("");
 
-	err = -ENODEV;
-	if (!xen_domain())
-		return err;
-
 	/*
 	 * Domain0 doesn't have a store_evtchn or store_mfn yet.
 	 */
@@ -752,6 +760,6 @@ static int __init xenbus_probe_init(void)
 	return err;
 }
 
-postcore_initcall(xenbus_probe_init);
+postcore_initcall(__xenbus_probe_init);
 
 MODULE_LICENSE("GPL");
include/xen/grant_table.h +1 -0
index 9e54167..7f48be7 100644
@@ -59,6 +59,7 @@ struct gnttab_free_callback {
 
 void gnttab_reset_grant_page(struct page *page);
 
+int gnttab_init(void);
 int gnttab_suspend(void);
 int gnttab_resume(void);
 
include/xen/interface/grant_table.h +1 -0
index 8211af8..c704fe5 100644
@@ -28,6 +28,7 @@
 #ifndef __XEN_PUBLIC_GRANT_TABLE_H__
 #define __XEN_PUBLIC_GRANT_TABLE_H__
 
+#include <xen/interface/xen.h>
 
 /***********************************
  * GRANT TABLE REPRESENTATION
include/xen/interface/platform_pci.h new file +45 -0

45 lines not on this page · raw message

include/xen/platform_pci.h new file +40 -0
new file mode 100644
index 0000000..870e7a4
@@ -0,0 +1,40 @@
+/******************************************************************************
+ * platform-pci.h
+ * 
+ * Xen platform PCI device driver
+ * Copyright (c) 2004, Intel Corporation. <[email protected]>
+ * Copyright (c) 2007, XenSource Inc.
+ *
+ * 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.
+ */
+
+#ifndef _XEN_PLATFORM_PCI_H
+#define _XEN_PLATFORM_PCI_H
+
+#include <linux/version.h>
+
+#define XEN_IOPORT_MAGIC_VAL 0x49d2
+#define XEN_IOPORT_LINUX_PRODNUM 0xffff
+#define XEN_IOPORT_LINUX_DRVVER  ((LINUX_VERSION_CODE << 8) + 0x0)
+
+#ifdef CONFIG_XEN_PLATFORM_PCI
+unsigned long alloc_xen_mmio(unsigned long len);
+#else
+static inline unsigned long alloc_xen_mmio(unsigned long len)
+{
+	return ~0UL;
+}
+#endif
+
+#endif /* _XEN_PLATFORM_PCI_H */
include/xen/xenbus.h +1 -0
index 542ca7c..a7d13ff 100644
@@ -173,6 +173,7 @@ void unregister_xenbus_watch(struct xenbus_watch *watch);
 void xs_suspend(void);
 void xs_resume(void);
 void xs_suspend_cancel(void);
+int xenbus_probe_init(void);
 
 /* Used by xenbus_dev to borrow kernel's store connection. */
 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
Signature · 1 line
1.5.4.3
#1 lore raw
Quoted 34 lines · Stefano Stabellini

Hi all,
this patch adds the xen pci platform device driver that is responsible for initializing the grant table and xenbus in PV on HVM mode. Few changes to xenbus and grant table are necessary to allow the delayed initialization in HVM mode.
Finally grant table needs few additional modifications to work in HVM mode.

---
 drivers/xen/Kconfig                  |   10 ++
 drivers/xen/Makefile                 |    1 +
 drivers/xen/events.c                 |    2 +-
 drivers/xen/grant-table.c            |   66 +++++++++-
 drivers/xen/platform-pci.c           |  227 ++++++++++++++++++++++++++++++++++
 drivers/xen/xenbus/xenbus_probe.c    |   20 ++-
 include/xen/grant_table.h            |    1 +
 include/xen/interface/grant_table.h  |    1 +
 include/xen/interface/platform_pci.h |   45 +++++++
 include/xen/platform_pci.h           |   40 ++++++
 include/xen/xenbus.h                 |    1 +
 11 files changed, 400 insertions(+), 14 deletions(-)
 create mode 100644 drivers/xen/platform-pci.c
 create mode 100644 include/xen/interface/platform_pci.h
 create mode 100644 include/xen/platform_pci.h
drivers/xen/Kconfig +0 -0
index d6d6f3e..5f9f7da 100644

drivers/xen/Kconfig config ACPI_PROCESSOR_XEN · line 193

Quoted 10 lines
drivers/xen/Kconfig +7 -0
@@ -184,3 +184,13 @@ config ACPI_PROCESSOR_XEN
 	   tristate
 	   depends on XEN_DOM0 && ACPI_PROCESSOR && CPU_FREQ
 	   default y
+
+config XEN_PLATFORM_PCI
+	tristate "xen platform pci device driver"
+	depends on XEN
+	select XEN_XENBUS_FRONTEND
+	help
+	  Driver for the Xen Pci Platform device: it is responsible for

Pci -> PCI

.. snip ..

Quoted 14 lines
drivers/xen/Kconfig +10 -0
+	  initializing xenbus and grant_table when running in a Xen HVM
+	  domain. As a consequence this driver is required to run any Xen PV
+	  frontend on Xen HVM.
@@ -448,6 +452,26 @@ static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
 	unsigned int nr_gframes = end_idx + 1;
 	int rc;
 
+	if (xen_hvm_domain()) {
+		struct xen_add_to_physmap xatp;
+		unsigned int i = end_idx;
+		rc = 0;
+		/*
+		 * Loop backwards, so that the first hypercall has the largest
+		 * index, ensuring that the table will grow only once.

drivers/xen/Kconfig static int gnttab_map(unsigned int start_idx, unsigned int end_idx) · line 471

Quoted 10 lines
drivers/xen/Kconfig +10 -0
+		 */
+		do {
+			xatp.domid = DOMID_SELF;
+			xatp.idx = i;
+			xatp.space = XENMAPSPACE_grant_table;
+			xatp.gpfn = (hvm_pv_resume_frames >> PAGE_SHIFT) + i;
+			if ((rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) != 0)
+				break;
+		} while (i-- > start_idx);
+

So if the hypercall fails, should we print out a message ? Or is it OK to return the rc?

Quoted 6 lines
drivers/xen/Kconfig +3 -0
+		return rc;
+	}
+
 	frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
 	if (!frames)
 		return -ENOMEM;

... snip ..

drivers/xen/Kconfig line 6

Quoted 8 lines

+++ b/drivers/xen/platform-pci.c

drivers/xen/Kconfig +6 -0
@@ -0,0 +1,227 @@
+/******************************************************************************
+ * platform-pci.c
+ * 
+ * Xen platform PCI device driver
+ * Copyright (c) 2005, Intel Corporation.
+ * Copyright (c) 2007, XenSource Inc.

No 'Copyright (c) 2010, Citrix'?

Quoted 11 lines
drivers/xen/Kconfig +11 -0
+ *
+ * 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

drivers/xen/Kconfig line 27

Quoted 10 lines
drivers/xen/Kconfig +10 -0
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/interrupt.h>
+
+#include <asm/io.h>

I think scripts/checkpatch.pl complains about this. You did run script/checkpath.pl, right?

You might want also to put all of those includes in an alphabetical order.

Quoted 29 lines
drivers/xen/Kconfig +29 -0
+
+#include <xen/grant_table.h>
+#include <xen/platform_pci.h>
+#include <xen/interface/platform_pci.h>
+#include <xen/xenbus.h>
+#include <xen/events.h>
+#include <xen/hvm.h>
+
+#define DRV_NAME    "xen-platform-pci"
+
+MODULE_AUTHOR("[email protected] and [email protected]");
+MODULE_DESCRIPTION("Xen platform PCI device");
+MODULE_LICENSE("GPL");
+
+static unsigned long platform_mmio;
+static unsigned long platform_mmio_alloc;
+static unsigned long platform_mmiolen;
+
+unsigned long alloc_xen_mmio(unsigned long len)
+{
+	unsigned long addr;
+
+	addr = platform_mmio + platform_mmio_alloc;
+	platform_mmio_alloc += len;
+	BUG_ON(platform_mmio_alloc > platform_mmiolen);
+
+	return addr;
+}
+

drivers/xen/Kconfig line 66

Quoted 10 lines
drivers/xen/Kconfig +10 -0
+static uint64_t get_callback_via(struct pci_dev *pdev)
+{
+	u8 pin;
+	int irq;
+
+#ifdef __ia64__
+	for (irq = 0; irq < 16; irq++) {
+		if (isa_irq_to_vector(irq) == pdev->irq)
+			return irq; /* ISA IRQ */
+	}

Um, is this still neccessary? Have you tested this driver on IA64 with this kernel?

Quoted 12 lines
drivers/xen/Kconfig +12 -0
+#else /* !__ia64__ */
+	irq = pdev->irq;
+	if (irq < 16)
+		return irq; /* ISA IRQ */
+#endif
+	pin = pdev->pin;
+
+	/* We don't know the GSI. Specify the PCI INTx line instead. */
+	return (((uint64_t)0x01 << 56) | /* PCI INTx identifier */
+		((uint64_t)pci_domain_nr(pdev->bus) << 32) |
+		((uint64_t)pdev->bus->number << 16) |
+		((uint64_t)(pdev->devfn & 0xff) << 8) |

drivers/xen/Kconfig line 88

Quoted 10 lines
drivers/xen/Kconfig +10 -0
+		((uint64_t)(pin - 1) & 3));
+}
+
+static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id)
+{
+	xen_hvm_evtchn_do_upcall(get_irq_regs());
+	return IRQ_HANDLED;
+}
+
+int xen_irq_init(struct pci_dev *pdev)

The name sounds awkward. How about 'get_irq' or 'allocate_irq'?

drivers/xen/Kconfig line 90

Quoted 2 lines
drivers/xen/Kconfig +2 -0
+{
+	__set_irq_handler(pdev->irq, handle_edge_irq, 0, NULL);

Is that kosher? I am not that familiar with how QEMU sets up the ACPI DSDT to figure out what the device pin (which looking at the qemu source is set to '1') corresponds to what IRQ.

Is it possible that with AMD-Vi or Intel VT-d you pass in a PCI device that would have an interrupt line that would coincide with this device? Or is the hvmloader + QEMU smart enought to take that under account and stick this device on an edge interrupt line?

Perhaps doing something like:

	int flags =  IRQF_SAMPLE_RANDOM | IRQF_NOBALANCING;

	if (pdev->irq > 16)
		flags |= IRQF_SHARED;
	else {
		flags |= IRQF_TRIGGER_RISING | IRQF_DISABLED;
		/* Whack! */
		__set_irq_handler(pdev->irq, handle_edge_irq, 0, NULL);
	}
	return request_irq(pdev->irq, do_.., flags,"xen-pla..", pdev);
Quoted 15 lines
drivers/xen/Kconfig +15 -0
+	return request_irq(pdev->irq, do_hvm_evtchn_intr,
+			IRQF_SAMPLE_RANDOM | IRQF_DISABLED | IRQF_NOBALANCING |
+			IRQF_TRIGGER_RISING, "xen-platform-pci", pdev);
+}
+
+static int __devinit platform_pci_init(struct pci_dev *pdev,
+				       const struct pci_device_id *ent)
+{
+	int i, ret;
+	long ioaddr, iolen;
+	long mmio_addr, mmio_len;
+	uint64_t callback_via;
+
+	i = pci_enable_device(pdev);
+	if (i)

drivers/xen/Kconfig line 115

Quoted 10 lines
drivers/xen/Kconfig +10 -0
+		return i;
+
+	ioaddr = pci_resource_start(pdev, 0);
+	iolen = pci_resource_len(pdev, 0);
+
+	mmio_addr = pci_resource_start(pdev, 1);
+	mmio_len = pci_resource_len(pdev, 1);
+
+	if (mmio_addr == 0 || ioaddr == 0) {
+		printk(KERN_WARNING DRV_NAME ":no resources found\n");

Uhh, you can also use 'dev_warn' - much nicer than thse printks.

Shouldn't you do pci_disable_device(..) ?

drivers/xen/Kconfig line 121

Quoted 6 lines
drivers/xen/Kconfig +6 -0
+		return -ENOENT;
+	}
+
+	if (request_mem_region(mmio_addr, mmio_len, DRV_NAME) == NULL) {
+		printk(KERN_ERR ":MEM I/O resource 0x%lx @ 0x%lx busy\n",
+		       mmio_addr, mmio_len);

Ditto

drivers/xen/Kconfig line 129

Quoted 8 lines
drivers/xen/Kconfig +8 -0
+		return -EBUSY;
+	}
+
+	if (request_region(ioaddr, iolen, DRV_NAME) == NULL) {
+		printk(KERN_ERR DRV_NAME ":I/O resource 0x%lx @ 0x%lx busy\n",
+		       iolen, ioaddr);
+		release_mem_region(mmio_addr, mmio_len);
+		return -EBUSY;

Ditto.

drivers/xen/Kconfig line 136

Quoted 7 lines
drivers/xen/Kconfig +7 -0
+	}
+
+	platform_mmio = mmio_addr;
+	platform_mmiolen = mmio_len;
+
+	if (!xen_have_vector_callback) {
+		xen_irq_init(pdev);

Not checking the return value? What if it is -1?

drivers/xen/Kconfig line 138

Quoted 2 lines
drivers/xen/Kconfig +2 -0
+		callback_via = get_callback_via(pdev);
+		if ((ret = xen_set_callback_via(callback_via)))

What happens with the xen_have_vector_callback and x86_platform_ipi_callback which were set in the previous patch? Will you receive a poke on do_hvm_pv_evtchn_intr and do_hvm_evtchn_intr?

drivers/xen/Kconfig line 150

Quoted 12 lines
drivers/xen/Kconfig +12 -0
+			goto out;
+	}
+	if ((ret = gnttab_init()))
+		goto out;
+
+	if ((ret = xenbus_probe_init()))
+		goto out;
+
+ out:
+	if (ret) {
+		release_mem_region(mmio_addr, mmio_len);
+		release_region(ioaddr, iolen);

No pci_disable_device() ?

Quoted 26 lines
drivers/xen/Kconfig +26 -0
+	}
+
+	return ret;
+}
+
+#define XEN_PLATFORM_VENDOR_ID 0x5853
+#define XEN_PLATFORM_DEVICE_ID 0x0001
+static struct pci_device_id platform_pci_tbl[] __devinitdata = {
+	{XEN_PLATFORM_VENDOR_ID, XEN_PLATFORM_DEVICE_ID,
+	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
+	{0,}
+};
+
+MODULE_DEVICE_TABLE(pci, platform_pci_tbl);
+
+static struct pci_driver platform_driver = {
+	name:     DRV_NAME,
+	probe:    platform_pci_init,
+	id_table: platform_pci_tbl,
+};
+
+static int check_platform_magic(void)
+{
+	short magic;
+	char protocol, *err;
+

drivers/xen/Kconfig line 186

Quoted 10 lines
drivers/xen/Kconfig +10 -0
+	magic = inw(XEN_IOPORT_MAGIC);
+
+	if (magic != XEN_IOPORT_MAGIC_VAL) {
+		err = "unrecognised magic value";
+		goto no_dev;
+	}
+
+	protocol = inb(XEN_IOPORT_PROTOVER);
+
+	printk(KERN_DEBUG DRV_NAME "I/O protocol version %d\n", protocol);

dev_dbg

drivers/xen/Kconfig line 193

Quoted 7 lines
drivers/xen/Kconfig +7 -0
+
+	switch (protocol) {
+	case 1:
+		outw(XEN_IOPORT_LINUX_PRODNUM, XEN_IOPORT_PRODNUM);
+		outl(XEN_IOPORT_LINUX_DRVVER, XEN_IOPORT_DRVVER);
+		if (inw(XEN_IOPORT_MAGIC) != XEN_IOPORT_MAGIC_VAL) {
+			printk(KERN_ERR DRV_NAME "blacklisted by host\n");

dev_err

drivers/xen/Kconfig line 205

Quoted 12 lines
drivers/xen/Kconfig +12 -0
+			return -ENODEV;
+		}
+		break;
+	default:
+		err = "unknown I/O protocol version";
+		goto no_dev;
+	}
+
+	return 0;
+
+ no_dev:
+	printk(KERN_WARNING DRV_NAME  "failed backend handshake: %s\n", err);

dev_warn. Shouldn't the rc be -ENODEV?

Quoted 146 lines
drivers/xen/Kconfig +22 -0
+	return 0;
+}
+
+static int __init platform_pci_module_init(void)
+{
+	int rc;
+
+	rc = check_platform_magic();
+	if (rc < 0)
+		return rc;
+
+	rc = pci_register_driver(&platform_driver);
+	if (rc) {
+		printk(KERN_INFO DRV_NAME
+		       ": No platform pci device model found\n");
+		return rc;
+	}
+
+	return 0;
+}
+
+module_init(platform_pci_module_init);
drivers/xen/xenbus/xenbus_probe.c +14 -6
index 7d11957..f0e6574 100644
@@ -668,17 +668,25 @@ void xenbus_probe(struct work_struct *unused)
 	blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
 }
 
-static int __init xenbus_probe_init(void)
+static int __init __xenbus_probe_init(void)
+{
+	/* Delay initialization in the PV on HVM case */
+	if (xen_hvm_domain())
+		return 0;
+
+	if (!xen_pv_domain())
+		return -ENODEV;
+
+	return xenbus_probe_init();
+}
+
+int xenbus_probe_init(void)
 {
 	int err = 0;
 	unsigned long page = 0;
 
 	DPRINTK("");
 
-	err = -ENODEV;
-	if (!xen_domain())
-		return err;
-
 	/*
 	 * Domain0 doesn't have a store_evtchn or store_mfn yet.
 	 */
@@ -752,6 +760,6 @@ static int __init xenbus_probe_init(void)
 	return err;
 }
 
-postcore_initcall(xenbus_probe_init);
+postcore_initcall(__xenbus_probe_init);
 
 MODULE_LICENSE("GPL");
include/xen/grant_table.h +1 -0
index 9e54167..7f48be7 100644
@@ -59,6 +59,7 @@ struct gnttab_free_callback {
 
 void gnttab_reset_grant_page(struct page *page);
 
+int gnttab_init(void);
 int gnttab_suspend(void);
 int gnttab_resume(void);
 
include/xen/interface/grant_table.h +1 -0
index 8211af8..c704fe5 100644
@@ -28,6 +28,7 @@
 #ifndef __XEN_PUBLIC_GRANT_TABLE_H__
 #define __XEN_PUBLIC_GRANT_TABLE_H__
 
+#include <xen/interface/xen.h>
 
 /***********************************
  * GRANT TABLE REPRESENTATION
include/xen/interface/platform_pci.h new file +45 -0
new file mode 100644
index 0000000..bc230cd
@@ -0,0 +1,45 @@
+/******************************************************************************
+ * platform_pci.h
+ *
+ * Interface for granting foreign access to page frames, and receiving
+ * page-ownership transfers.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __XEN_PUBLIC_PLATFORM_PCI_H__
+#define __XEN_PUBLIC_PLATFORM_PCI_H__
+
+#define XEN_IOPORT_BASE 0x10
+
+#define XEN_IOPORT_PLATFLAGS	(XEN_IOPORT_BASE + 0) /* 1 byte access (R/W) */
+#define XEN_IOPORT_MAGIC	(XEN_IOPORT_BASE + 0) /* 2 byte access (R) */
+#define XEN_IOPORT_UNPLUG	(XEN_IOPORT_BASE + 0) /* 2 byte access (W) */
+#define XEN_IOPORT_DRVVER	(XEN_IOPORT_BASE + 0) /* 4 byte access (W) */
+
+#define XEN_IOPORT_SYSLOG	(XEN_IOPORT_BASE + 2) /* 1 byte access (W) */
+#define XEN_IOPORT_PROTOVER	(XEN_IOPORT_BASE + 2) /* 1 byte access (R) */
+#define XEN_IOPORT_PRODNUM	(XEN_IOPORT_BASE + 2) /* 2 byte access (W) */
+
+#define UNPLUG_ALL_IDE_DISKS 1
+#define UNPLUG_ALL_NICS 2
+#define UNPLUG_AUX_IDE_DISKS 4
+#define UNPLUG_ALL 7
+
+#endif /* __XEN_PUBLIC_PLATFORM_PCI_H__ */
include/xen/platform_pci.h new file +0 -0
new file mode 100644
index 0000000..870e7a4

include/xen/platform_pci.h line 6

Quoted 6 lines
include/xen/platform_pci.h new file +6 -0
@@ -0,0 +1,40 @@
+/******************************************************************************
+ * platform-pci.h
+ * 
+ * Xen platform PCI device driver
+ * Copyright (c) 2004, Intel Corporation. <[email protected]>
+ * Copyright (c) 2007, XenSource Inc.

Copyring 2010 Citrix?

#2 lore raw

Thanks for the very detailed and careful review!

Quoted 3 lines · Konrad Rzeszutek Wilk

Pci -> PCI

Ok

Quoted 19 lines
Quoted 1 line

+ initializing xenbus and grant_table when running in a Xen HVM

.. snip ..

Quoted 17 lines
drivers/xen/Kconfig +13 -0
+       domain. As a consequence this driver is required to run any Xen PV
+       frontend on Xen HVM.
@@ -448,6 +452,26 @@ static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
      unsigned int nr_gframes = end_idx + 1;
      int rc;
 
+     if (xen_hvm_domain()) {
+             struct xen_add_to_physmap xatp;
+             unsigned int i = end_idx;
+             rc = 0;
+             /*
+              * Loop backwards, so that the first hypercall has the largest
+              * index, ensuring that the table will grow only once.
+              */
+             do {
+                     xatp.domid = DOMID_SELF;
+                     xatp.idx = i;
Quoted 8 lines

drivers/xen/grant-table.c static int gnttab_map(unsigned int start_idx, unsigned int end_idx) · line 471

Quoted 6 lines
drivers/xen/grant-table.c +6 -0
+                     xatp.space = XENMAPSPACE_grant_table;
+                     xatp.gpfn = (hvm_pv_resume_frames >> PAGE_SHIFT) + i;
+                     if ((rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) != 0)
+                             break;
+             } while (i-- > start_idx);
+

So if the hypercall fails, should we print out a message ? Or is it OK to return the rc?

good catch, I actually added a printk there for debugging at some point but I didn't commit it.

Quoted 8 lines
Quoted 6 lines
drivers/xen/grant-table.c +3 -0
+             return rc;
+     }
+
      frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
      if (!frames)
              return -ENOMEM;

... snip ..

Quoted 11 lines

drivers/xen/platform-pci.c line 6

Quoted 8 lines

+++ b/drivers/xen/platform-pci.c

drivers/xen/platform-pci.c new file +6 -0
@@ -0,0 +1,227 @@
+/******************************************************************************
+ * platform-pci.c
+ *
+ * Xen platform PCI device driver
+ * Copyright (c) 2005, Intel Corporation.
+ * Copyright (c) 2007, XenSource Inc.

No 'Copyright (c) 2010, Citrix'?

I'll add one

Quoted 25 lines
Quoted 11 lines
drivers/xen/platform-pci.c new file +11 -0
+ *
+ * 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

drivers/xen/platform-pci.c line 27

Quoted 10 lines
drivers/xen/platform-pci.c new file +10 -0
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/interrupt.h>
+
+#include <asm/io.h>

I think scripts/checkpatch.pl complains about this. You did run script/checkpath.pl, right?

Quoted 3 lines

You might want also to put all of those includes in an alphabetical order.

script/checkpatch.pl complains about a lot of things :) I'll fix.

Quoted 34 lines
Quoted 34 lines
drivers/xen/platform-pci.c new file +34 -0
+
+#include <xen/grant_table.h>
+#include <xen/platform_pci.h>
+#include <xen/interface/platform_pci.h>
+#include <xen/xenbus.h>
+#include <xen/events.h>
+#include <xen/hvm.h>
+
+#define DRV_NAME    "xen-platform-pci"
+
+MODULE_AUTHOR("[email protected] and [email protected]");
+MODULE_DESCRIPTION("Xen platform PCI device");
+MODULE_LICENSE("GPL");
+
+static unsigned long platform_mmio;
+static unsigned long platform_mmio_alloc;
+static unsigned long platform_mmiolen;
+
+unsigned long alloc_xen_mmio(unsigned long len)
+{
+     unsigned long addr;
+
+     addr = platform_mmio + platform_mmio_alloc;
+     platform_mmio_alloc += len;
+     BUG_ON(platform_mmio_alloc > platform_mmiolen);
+
+     return addr;
+}
+
+static uint64_t get_callback_via(struct pci_dev *pdev)
+{
+     u8 pin;
+     int irq;
+
Quoted 8 lines

drivers/xen/platform-pci.c line 66

Quoted 5 lines
drivers/xen/platform-pci.c new file +5 -0
+#ifdef __ia64__
+     for (irq = 0; irq < 16; irq++) {
+             if (isa_irq_to_vector(irq) == pdev->irq)
+                     return irq; /* ISA IRQ */
+     }

Um, is this still neccessary? Have you tested this driver on IA64 with this kernel?

No I didn't and it would be difficult for me to. However reading the code again it seems to me that the ifdef is unnecessary here, so I'll remove it.

Quoted 16 lines
Quoted 16 lines
drivers/xen/platform-pci.c new file +16 -0
+#else /* !__ia64__ */
+     irq = pdev->irq;
+     if (irq < 16)
+             return irq; /* ISA IRQ */
+#endif
+     pin = pdev->pin;
+
+     /* We don't know the GSI. Specify the PCI INTx line instead. */
+     return (((uint64_t)0x01 << 56) | /* PCI INTx identifier */
+             ((uint64_t)pci_domain_nr(pdev->bus) << 32) |
+             ((uint64_t)pdev->bus->number << 16) |
+             ((uint64_t)(pdev->devfn & 0xff) << 8) |
+             ((uint64_t)(pin - 1) & 3));
+}
+
+static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id)
Quoted 8 lines

drivers/xen/platform-pci.c line 88

Quoted 6 lines
drivers/xen/platform-pci.c new file +6 -0
+{
+     xen_hvm_evtchn_do_upcall(get_irq_regs());
+     return IRQ_HANDLED;
+}
+
+int xen_irq_init(struct pci_dev *pdev)

The name sounds awkward. How about 'get_irq' or 'allocate_irq'?

Ok, I'll go for xen_allocate_irq.

Quoted 13 lines

Review line 90

Quoted 2 lines
(quoted diff) +2 -0
+{
+     __set_irq_handler(pdev->irq, handle_edge_irq, 0, NULL);

Is that kosher? I am not that familiar with how QEMU sets up the ACPI DSDT to figure out what the device pin (which looking at the qemu source is set to '1') corresponds to what IRQ.

Is it possible that with AMD-Vi or Intel VT-d you pass in a PCI device that would have an interrupt line that would coincide with this device? Or is the hvmloader + QEMU smart enought to take that under account and stick this device on an edge interrupt line?

Perhaps doing something like:

Quoted 11 lines
        int flags =  IRQF_SAMPLE_RANDOM | IRQF_NOBALANCING;

        if (pdev->irq > 16)
                flags |= IRQF_SHARED;
        else {
                flags |= IRQF_TRIGGER_RISING | IRQF_DISABLED;
                /* Whack! */
                __set_irq_handler(pdev->irq, handle_edge_irq, 0, NULL);
        }
        return request_irq(pdev->irq, do_.., flags,"xen-pla..", pdev);

Yes I did that on purpose because it seems that it is the only thing able to fix the multivcpu problem I was having: when receiving evtchns from the pci platform device interrupt line (vector callback not available), the guest boots fine with a single vcpu but it hangs at boot with 2 or more vcpus. In particular it seems to deadlock in the xenbus initialization code because of some missing interrupts/evtchns.

Switching to handle_edge_irq (even though the interrupt is supposed to be level triggered, but it is emulated, so I guess is not important anyway) fixes the issue for me. It is also interesting that using vector callback the problem doesn't occur.

In the default scenario linux uses handle_fasteoi_irq that acks the interrupt only at the end, after the call to handle_IRQ_event, that in the evtchn case can loop several times on the pending evtchns so can take long to complete.
On the other hand handle_edge_irq acks the interrupt right away before calling handle_IRQ_event, that is the same behaviour of the ipi handler. Also evtchn handlers can re-enable interrupts and that would cause handle_edge_irq to be called again if we receive another interrupt/evtchn, and handle_edge_irq would again ack the interrupt right away even if it is marked as IRQ_INPROGRESS while handle_fasteoi_irq would just exit.

I guess I could add a comment before __set_irq_handler.

Quoted 18 lines
Quoted 18 lines
drivers/xen/platform-pci.c new file +18 -0
+     return request_irq(pdev->irq, do_hvm_evtchn_intr,
+                     IRQF_SAMPLE_RANDOM | IRQF_DISABLED | IRQF_NOBALANCING |
+                     IRQF_TRIGGER_RISING, "xen-platform-pci", pdev);
+}
+
+static int __devinit platform_pci_init(struct pci_dev *pdev,
+                                    const struct pci_device_id *ent)
+{
+     int i, ret;
+     long ioaddr, iolen;
+     long mmio_addr, mmio_len;
+     uint64_t callback_via;
+
+     i = pci_enable_device(pdev);
+     if (i)
+             return i;
+
+     ioaddr = pci_resource_start(pdev, 0);
Quoted 9 lines

drivers/xen/platform-pci.c line 115

Quoted 7 lines
drivers/xen/platform-pci.c new file +7 -0
+     iolen = pci_resource_len(pdev, 0);
+
+     mmio_addr = pci_resource_start(pdev, 1);
+     mmio_len = pci_resource_len(pdev, 1);
+
+     if (mmio_addr == 0 || ioaddr == 0) {
+             printk(KERN_WARNING DRV_NAME ":no resources found\n");

Uhh, you can also use 'dev_warn' - much nicer than thse printks.

Ok

Quoted 2 lines

Shouldn't you do pci_disable_device(..) ?

Yes I should

Quoted 9 lines

drivers/xen/platform-pci.c line 136

Quoted 7 lines
drivers/xen/platform-pci.c new file +7 -0
+     }
+
+     platform_mmio = mmio_addr;
+     platform_mmiolen = mmio_len;
+
+     if (!xen_have_vector_callback) {
+             xen_irq_init(pdev);

Not checking the return value? What if it is -1?

I'll fix

Quoted 8 lines

drivers/xen/platform-pci.c line 138

Quoted 3 lines
drivers/xen/platform-pci.c new file +2 -0
+             callback_via = get_callback_via(pdev);
+             if ((ret = xen_set_callback_via(callback_via)))

What happens with the xen_have_vector_callback and x86_platform_ipi_callback which were set in the previous patch? Will you receive a poke on do_hvm_pv_evtchn_intr and do_hvm_evtchn_intr?

Yes, exactly.
If xen_have_vector_callback == 1 then I receive evtchns from do_hvm_pv_evtchn_intr, otherwise from do_hvm_evtchn_intr.

Quoted 5 lines

drivers/xen/platform-pci.c line 150

Quoted 4 lines
drivers/xen/platform-pci.c new file +4 -0
+ out:
+     if (ret) {
+             release_mem_region(mmio_addr, mmio_len);
+             release_region(ioaddr, iolen);

No pci_disable_device() ?

I'll fix

Quoted 12 lines

drivers/xen/platform-pci.c line 186

Quoted 1 line
drivers/xen/platform-pci.c new file +1 -0
+     printk(KERN_DEBUG DRV_NAME "I/O protocol version %d\n", protocol);

dev_dbg

drivers/xen/platform-pci.c line 193

Quoted 7 lines
drivers/xen/platform-pci.c new file +7 -0
+
+     switch (protocol) {
+     case 1:
+             outw(XEN_IOPORT_LINUX_PRODNUM, XEN_IOPORT_PRODNUM);
+             outl(XEN_IOPORT_LINUX_DRVVER, XEN_IOPORT_DRVVER);
+             if (inw(XEN_IOPORT_MAGIC) != XEN_IOPORT_MAGIC_VAL) {
+                     printk(KERN_ERR DRV_NAME "blacklisted by host\n");
Quoted 5 lines

dev_err

drivers/xen/platform-pci.c line 205

Quoted 2 lines
drivers/xen/platform-pci.c new file +2 -0
+ no_dev:
+     printk(KERN_WARNING DRV_NAME  "failed backend handshake: %s\n", err);

dev_warn.

the problem here is that we are called by module_init, we don't have a struct pci_dev to use with dev_*

Quoted 1 line

Shouldn't the rc be -ENODEV?

Yes, I'll fix.

Quoted 6 lines
Quoted 6 lines
include/xen/platform_pci.h new file +0 -0
new file mode 100644
index 0000000..870e7a4
@@ -0,0 +1,40 @@
Quoted 9 lines

include/xen/platform_pci.h line 6

Quoted 6 lines
include/xen/platform_pci.h new file +6 -0
+/******************************************************************************
+ * platform-pci.h
+ *
+ * Xen platform PCI device driver
+ * Copyright (c) 2004, Intel Corporation. <[email protected]>
+ * Copyright (c) 2007, XenSource Inc.

Copyring 2010 Citrix?

I'll add one