PCI: hv: New paravirtual PCI front-end for Hyper-V VMs
Subsystems (6)
v6 cover PCI: hv: New paravirtual PCI front-end for Hyper-V VMs
From: Jake Oshins <[email protected]>
This version of this patch series incorporates feedback from Andy Shevchenko.
First, export functions that allow correlating Hyper-V virtual processors and Linux cpus, along with the means for invoking a hypercall that targets interrupts at chosen vectors on specfic cpus.
Second, mark various parts of IRQ domain related code as exported, so that this PCI front-end can implement an IRQ domain as part of a module. (The alternative would be to pull all this into the kernel, which would pull in a lot of other Hyper-V related code, as this IRQ domain depends on hv_vmbus.ko.)
Third, modify PCI so that new root PCI buses can be marked with an associated fwnode_handle, and so that root PCI buses can look up their associated IRQ domain by that handle.
Fourth, introduce a new driver, hv_pcifront, which eposes root PCI buses in a Hyper-V VM. These root PCI buses expose real PCIe devices, or PCI Virtual Functions.
Jake Oshins (7):
drivers:hv: Export a function that maps Linux CPU num onto Hyper-V
proc num
drivers:hv: Export hv_do_hypercall()
PCI: Make it possible to implement a PCI MSI IRQ Domain in a module.
PCI: Add fwnode_handle to pci_sysdata
PCI: irqdomain: Look up IRQ domain by fwnode_handle drivers:hv: Define the channel type of Hyper-V PCI Express
pass-through
PCI: hv: New paravirtual PCI front-end for Hyper-V VMs
MAINTAINERS | 1 + arch/x86/include/asm/msi.h | 4 + arch/x86/include/asm/pci.h | 13 + arch/x86/kernel/apic/msi.c | 5 +- arch/x86/kernel/apic/vector.c | 2 + drivers/hv/hv.c | 20 +- drivers/hv/hyperv_vmbus.h | 2 +- drivers/hv/vmbus_drv.c | 17 + drivers/pci/Kconfig | 7 + drivers/pci/host/Makefile | 1 + drivers/pci/host/hv_pcifront.c | 2267 ++++++++++++++++++++++++++++++++++++++++ drivers/pci/msi.c | 4 + drivers/pci/probe.c | 14 + include/asm-generic/pci.h | 4 + include/linux/hyperv.h | 14 + kernel/irq/chip.c | 1 + kernel/irq/irqdomain.c | 2 + 17 files changed, 2365 insertions(+), 13 deletions(-) create mode 100644 drivers/pci/host/hv_pcifront.c
Signature · 1 line
1.9.1
PATCH v6 1/7 drivers:hv: Export a function that maps Linux CPU num onto Hyper-V proc num
From: Jake Oshins <[email protected]>
This patch exposes the mapping between Linux CPU number and Hyper-V virtual processor number. This is necessary because the hypervisor needs to know which virtual processor to target when making a mapping in the Interrupt Redirection Table in the I/O MMU.
--- drivers/hv/vmbus_drv.c | 17 +++++++++++++++++ include/linux/hyperv.h | 2 ++ 2 files changed, 19 insertions(+)
drivers/hv/vmbus_drv.c +17 -0
@@ -1191,6 +1191,23 @@ int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj, }EXPORT_SYMBOL_GPL (vmbus_allocate_mmio); +/** +* vmbus_cpu_number_to_vp_number() - Map CPU to VP. +* @cpu_number: CPU number in Linux terms +* +* This function returns the mapping between the Linux processor +* number and the hypervisor's virtual processor number, useful +* in making hypercalls and such that talk about specific +* processors. +* +* Return: Virtual processor number in Hyper-V terms +*/ +int vmbus_cpu_number_to_vp_number (int cpu_number) +{ +return hv_context.vp_index[cpu_number]; +} +EXPORT_SYMBOL_GPL (vmbus_cpu_number_to_vp_number); +static int vmbus_acpi_add (struct acpi_device *device) {acpi_status result;
include/linux/hyperv.h +2 -0
@@ -982,6 +982,8 @@ int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj, resource_size_t size, resource_size_t align, bool fb_overlap_ok); +int vmbus_cpu_number_to_vp_number (int cpu_number); +/** * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device *
Signature · 1 line
1.9.1
PATCH v6 2/7 drivers:hv: Export hv_do_hypercall()
In reply to [email protected]From: Jake Oshins <[email protected]>
This patch exposes the function that hv_vmbus.ko uses to make hypercalls. This is necessary for retargeting an interrupt when it is given a new affinity and vector.
--- drivers/hv/hv.c | 20 ++++++++++---------- drivers/hv/hyperv_vmbus.h | 2 +- include/linux/hyperv.h | 1 + 3 files changed, 12 insertions(+), 11 deletions(-)
drivers/hv/hv.c +10 -10
@@ -89,9 +89,9 @@ static int query_hypervisor_info(void) }/* -* do_hypercall- Invoke the specified hypercall +* hv_do_hypercall- Invoke the specified hypercall */ -static u64 do_hypercall (u64 control,void *input,void *output) +u64 hv_do_hypercall (u64 control,void *input,void *output) {u64 input_address = (input) ?virt_to_phys (input) :0 ;u64 output_address = (output) ?virt_to_phys (output) :0 ;
@@ -132,6 +132,7 @@ static u64 do_hypercall(u64 control, void *input, void *output)return hv_status_lo | ((u64 )hv_status_hi <<32 );#endif /* !x86_64 */ } +EXPORT_SYMBOL_GPL (hv_do_hypercall);#ifdef CONFIG_X86_64 static cycle_t read_hv_clock_tsc (struct clocksource *arg)
@@ -315,7 +316,7 @@ int hv_post_message(union hv_connection_id connection_id, {struct hv_input_post_message *aligned_msg; -u16 status; +u64 status;if (payload_size >HV_MESSAGE_PAYLOAD_BYTE_COUNT )return -EMSGSIZE ;
@@ -329,11 +330,10 @@ int hv_post_message(union hv_connection_id connection_id, aligned_msg->payload_size = payload_size;memcpy ((void *)aligned_msg->payload, payload, payload_size); - status =do_hypercall (HVCALL_POST_MESSAGE , aligned_msg,NULL ) - &0xFFFF ; + status =hv_do_hypercall (HVCALL_POST_MESSAGE , aligned_msg,NULL );put_cpu (); -return status; +return status &0xFFFF ; }
@@ -343,13 +343,13 @@ int hv_post_message(union hv_connection_id connection_id,* * This involves a hypercall. */ -u16 hv_signal_event (void *con_id) +int hv_signal_event (void *con_id) { -u16 status; +u64 status; - status = (do_hypercall (HVCALL_SIGNAL_EVENT , con_id,NULL ) &0xFFFF ); + status =hv_do_hypercall (HVCALL_SIGNAL_EVENT , con_id,NULL ); -return status; +return status &0xFFFF ; } static int hv_ce_set_next_event(unsigned long delta,
drivers/hv/hyperv_vmbus.h +1 -1
@@ -587,7 +587,7 @@ extern int hv_post_message(union hv_connection_id connection_id,enum hv_message_type message_type, void *payload, size_t payload_size); -extern u16 hv_signal_event (void *con_id); +extern int hv_signal_event (void *con_id);extern int hv_synic_alloc (void );
include/linux/hyperv.h +1 -0
@@ -983,6 +983,7 @@ int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj, bool fb_overlap_ok);int vmbus_cpu_number_to_vp_number (int cpu_number); +u64 hv_do_hypercall (u64 control,void *input,void *output);/** * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
Signature · 1 line
1.9.1
PATCH v6 3/7 PCI: Make it possible to implement a PCI MSI IRQ Domain in a module.
In reply to [email protected]From: Jake Oshins <[email protected]>
The Linux kernel already has the concpet of IRQ domain, whereing a component can expose a set of IRQs which are managed by a particular interrupt controller chip or other subsystem. The PCI driver exposes the notion of an IRQ domain for Message-Signaled Interrupts (MSI) from PCI Express devices. This patch exposes the functions which are necessary for making an MSI IRQ domain within a module.
--- arch/x86/include/asm/msi.h | 4 ++++ arch/x86/kernel/apic/msi.c | 5 +++-- arch/x86/kernel/apic/vector.c | 2 ++ drivers/pci/msi.c | 4 ++++ kernel/irq/chip.c | 1 + kernel/irq/irqdomain.c | 2 ++ 6 files changed, 16 insertions(+), 2 deletions(-)
arch/x86/include/asm/msi.h +4 -0
@@ -1,7 +1,11 @@#ifndef _ASM_X86_MSI_H#define _ASM_X86_MSI_H#include <asm/hw_irq.h> +#include <asm/irqdomain.h> typedef struct irq_alloc_info msi_alloc_info_t ; +int pci_msi_prepare (struct irq_domain *domain,struct device *dev,int nvec, +msi_alloc_info_t *arg); +#endif /* _ASM_X86_MSI_H */
arch/x86/kernel/apic/msi.c +3 -2
@@ -96,8 +96,8 @@ static irq_hw_number_t pci_msi_get_hwirq(struct msi_domain_info *info,return arg->msi_hwirq; } -static int pci_msi_prepare (struct irq_domain *domain,struct device *dev, -int nvec,msi_alloc_info_t *arg) +int pci_msi_prepare (struct irq_domain *domain,struct device *dev,int nvec, +msi_alloc_info_t *arg) {struct pci_dev *pdev =to_pci_dev (dev);struct msi_desc *desc =first_pci_msi_entry (pdev);
@@ -113,6 +113,7 @@ static int pci_msi_prepare(struct irq_domain *domain, struct device *dev,return 0 ; } +EXPORT_SYMBOL_GPL (pci_msi_prepare);static void pci_msi_set_desc (msi_alloc_info_t *arg,struct msi_desc *desc) {
arch/x86/kernel/apic/vector.c +2 -0
@@ -29,6 +29,7 @@ struct apic_chip_data { };struct irq_domain *x86_vector_domain; +EXPORT_SYMBOL_GPL (x86_vector_domain);static DEFINE_RAW_SPINLOCK (vector_lock);static cpumask_var_t vector_cpumask;static struct irq_chip lapic_controller;
@@ -66,6 +67,7 @@ struct irq_cfg *irqd_cfg(struct irq_data *irq_data)return data ? &data->cfg :NULL ; } +EXPORT_SYMBOL_GPL (irqd_cfg);struct irq_cfg *irq_cfg (unsigned int irq) {
drivers/pci/msi.c +4 -0
@@ -257,6 +257,7 @@ void pci_msi_mask_irq(struct irq_data *data) {msi_set_mask_bit (data,1 ); } +EXPORT_SYMBOL_GPL (pci_msi_mask_irq);/** * pci_msi_unmask_irq - Generic irq chip callback to unmask PCI/MSI interrupts
@@ -266,6 +267,7 @@ void pci_msi_unmask_irq(struct irq_data *data) {msi_set_mask_bit (data,0 ); } +EXPORT_SYMBOL_GPL (pci_msi_unmask_irq);void default_restore_msi_irqs (struct pci_dev *dev) {
@@ -1126,6 +1128,7 @@ struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc) {return to_pci_dev (desc->dev); } +EXPORT_SYMBOL (msi_desc_to_pci_dev);void *msi_desc_to_pci_sysdata (struct msi_desc *desc) {
@@ -1285,6 +1288,7 @@ struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode, domain->bus_token =DOMAIN_BUS_PCI_MSI ;return domain; } +EXPORT_SYMBOL_GPL (pci_msi_create_irq_domain);/** * pci_msi_domain_alloc_irqs - Allocate interrupts for @dev in @domain
kernel/irq/chip.c +1 -0
@@ -950,6 +950,7 @@ void irq_chip_ack_parent(struct irq_data *data) data = data->parent_data; data->chip->irq_ack (data); } +EXPORT_SYMBOL_GPL (irq_chip_ack_parent);/** * irq_chip_mask_parent - Mask the parent interrupt
kernel/irq/irqdomain.c +2 -0
@@ -60,6 +60,7 @@ struct fwnode_handle *irq_domain_alloc_fwnode(void *data) fwid->fwnode.type =FWNODE_IRQCHIP ;return &fwid->fwnode; } +EXPORT_SYMBOL_GPL (irq_domain_alloc_fwnode);/** * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
@@ -77,6 +78,7 @@ void irq_domain_free_fwnode(struct fwnode_handle *fwnode)kfree (fwid->name);kfree (fwid); } +EXPORT_SYMBOL_GPL (irq_domain_free_fwnode);/** * __irq_domain_add() - Allocate a new irq_domain data structure
Signature · 1 line
1.9.1
PATCH v6 4/7 PCI: Add fwnode_handle to pci_sysdata
In reply to [email protected]From: Jake Oshins <[email protected]>
This patch adds an fwnode_handle to struct pci_sysdata, which is used by the next patch in the series when trying to locate an IRQ domain associated with a root PCI bus.
--- arch/x86/include/asm/pci.h | 13 +++++++++++++ include/asm-generic/pci.h | 4 ++++ 2 files changed, 17 insertions(+)
arch/x86/include/asm/pci.h +13 -0
@@ -20,6 +20,9 @@ struct pci_sysdata {#ifdef CONFIG_X86_64 void *iommu;/* IOMMU private data */ #endif +#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN +void *fwnode;/* IRQ domain for MSI assignment */ +#endif };extern int pci_routeirq;
@@ -41,6 +44,16 @@ static inline int pci_proc_domain(struct pci_bus *bus) }#endif +#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN +static inline void *_pci_root_bus_fwnode (struct pci_bus *bus) +{ +struct pci_sysdata *sd = bus->sysdata; +return sd->fwnode; +} + +#define pci_root_bus_fwnode _pci_root_bus_fwnode +#endif +/* Can be used to override the logic in pci_scan_bus for skipping already-configured bus numbers - to be used for buggy BIOSes or architectures with incomplete PCI setup by the loader */
include/asm-generic/pci.h +4 -0
@@ -21,4 +21,8 @@ static inline int pci_get_legacy_ide_irq(struct pci_dev *dev, int channel)#define PCI_DMA_BUS_IS_PHYS (1)#endif +#ifndef pci_root_bus_fwnode +#define pci_root_bus_fwnode (bus) ((void)(bus),NULL) +#endif +#endif /* _ASM_GENERIC_PCI_H */
Signature · 1 line
1.9.1
Re: [PATCH v6 4/7] PCI: Add fwnode_handle to pci_sysdata
Quoted 48 lines · On 2015/11/3 5:33, [email protected] wrote:
From: Jake Oshins <[email protected]>
This patch adds an
fwnode_handleto structpci_sysdata, which is used by the next patch in the series when trying to locate an IRQ domain associated with a root PCI bus.Signed-off-by Jake Oshins <[email protected]>--- arch/x86/include/asm/pci.h | 13 +++++++++++++ include/asm-generic/pci.h | 4 ++++ 2 files changed, 17 insertions(+)arch/x86/include/asm/pci.h +13
-0@@ -20,6 +20,9 @@ struct pci_sysdata {#ifdef CONFIG_X86_64 void *iommu;/* IOMMU private data */ #endif +#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN +void *fwnode;/* IRQ domain for MSI assignment */ +#endif };extern int pci_routeirq;@@ -41,6 +44,16 @@ static inline int pci_proc_domain(struct pci_bus *bus) }#endif +#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN +static inline void *_pci_root_bus_fwnode (struct pci_bus *bus) +{ +struct pci_sysdata *sd = bus->sysdata; +return sd->fwnode; +} + +#define pci_root_bus_fwnode _pci_root_bus_fwnode +#endif +/* Can be used to override the logic in pci_scan_bus for skipping already-configured bus numbers - to be used for buggy BIOSes or architectures with incomplete PCI setup by the loader */ include/asm-generic/pci.h +0
-0
include/asm-generic/pci.h static inline int pci_get_legacy_ide_irq(struct pci_dev *dev, int channel) · line 26
Quoted 6 lines
include/asm-generic/pci.h +3
-0@@ -21,4 +21,8 @@ static inline int pci_get_legacy_ide_irq(struct pci_dev *dev, int channel)#define PCI_DMA_BUS_IS_PHYS (1)#endif +#ifndef pci_root_bus_fwnode +#define pci_root_bus_fwnode (bus) ((void)(bus),NULL) +#endif
Hi Jakeo,
For x86, all PCI devices share the same MSI controller. But I'm
not sure whether it may have per-bus/per-device MSI controllers on other archs. If there may be multiple MSI controllers serving PCI devices under the same PCI root, it would be better to use some thing like pci_get_msi_fwnode(bus) or similar.
Thanks,
Gerry
Quoted 3 lines
include/asm-generic/pci.h +1
-0+#endif /* _ASM_GENERIC_PCI_H */
RE: [PATCH v6 4/7] PCI: Add fwnode_handle to pci_sysdata
Quoted 37 lines
-----Original Message-----
From: Jiang Liu [mailto:[email protected]]
Sent: Wednesday, December 2, 2015 10:53 PM
To: Jake Oshins <[email protected]>; [email protected]; KY Srinivasan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Haiyang Zhang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v6 4/7] PCI: Addfwnode_handletopci_sysdatainclude/asm-generic/pci.h static inline int pci_get_legacy_ide_irq(struct pci_dev
Quoted 21 lines · On 2015/11/3 5:33, [email protected] wrote:
From: Jake Oshins <[email protected]>
arch/x86/include/asm/pci.h +10
-0+#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN +static inline void *_pci_root_bus_fwnode (struct pci_bus *bus) +{ +struct pci_sysdata *sd = bus->sysdata; +return sd->fwnode; +} + +#define pci_root_bus_fwnode _pci_root_bus_fwnode +#endif +/* Can be used to override the logic in pci_scan_bus for skipping already-configured bus numbers - to be used for buggy BIOSes or architectures with incomplete PCI setup by the loader */ include/asm-generic/pci.h +0
-0@@ -21,4 +21,8 @@ static inline int pci_get_legacy_ide_irq(struct pci_dev*dev, int channel)
Quoted 3 lines
include/asm-generic/pci.h +0
-0#define PCI_DMA_BUS_IS_PHYS (1)#endif
Quoted 12 lines
include/asm-generic/pci.h static inline int pci_get_legacy_ide_irq(struct pci_dev *dev, int channel) · line 26
Quoted 3 lines
include/asm-generic/pci.h +3
-0+#ifndef pci_root_bus_fwnode +#define pci_root_bus_fwnode (bus) ((void)(bus),NULL) +#endif Hi Jakeo,
For x86, all PCI devices share the same MSI controller. But I'm
not sure whether it may have per-bus/per-device MSI controllers on other archs. If there may be multiple MSI controllers serving PCI devices under the same PCI root, it would be better to use some thing likepci_get_msi_fwnode(bus)or similar.
Thanks,
Gerry
Certainly other architectures have per-bus MSI controllers, though usually it's per root complex. In case you're not familiar with PCI Express terms (and I apologize for this if you are) a root complex is one instance of PCI Express, where traffic is defined between every part of it. If your traffic leaves PCI Express and goes onto some processor-specific bus, it has left the root complex. The root complex is usually modeled as a root PCI bus, a set of root port, and a set of legacy and/or embedded endpoints, which amounts to saying that some PCI/E devices are built into the root complex and not connected with links.
I don't have a use case at the moment for anything other than an MSI controller for a root complex. If you'd like me to change this so that it more naturally extends to a situation where you'd want to use it for a specific bus, I can do that. Please let me know.
And thanks again for your review.
-- Jake Oshins
PATCH v6 5/7 PCI: irqdomain: Look up IRQ domain by fwnode_handle
In reply to [email protected]From: Jake Oshins <[email protected]>
This patch adds a second way of finding an IRQ domain associated with a root PCI bus. After looking to see if one can be found through the OF tree, it attempts to look up the IRQ domain through an fwnode_handle stored in the pci_sysdata struct.
--- drivers/pci/probe.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/pci/probe.c +14 -0
@@ -671,6 +671,20 @@ static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)*/ d =pci_host_bridge_of_msi_domain (bus); +#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN +/* +* If no IRQ domain was found via the OF tree, try looking it up +* directly through the fwnode_handle. +*/ +if (!d) { +struct fwnode_handle *fwnode =pci_root_bus_fwnode (bus); + +if (fwnode) + d =irq_find_matching_fwnode (fwnode, +DOMAIN_BUS_PCI_MSI ); + } +#endif +return d; }
Signature · 1 line
1.9.1
PATCH v6 6/7 drivers:hv: Define the channel type of Hyper-V PCI Express pass-through
In reply to [email protected]From: Jake Oshins <[email protected]>
This defines the channel type for PCI front-ends in Hyper-V VMs.
--- include/linux/hyperv.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/hyperv.h +11 -0
@@ -1140,6 +1140,17 @@ u64 hv_do_hypercall(u64 control, void *input, void *output); }/* +* PCI Express Pass Through +* {44C4F61D-4444-4400-9D52-802E27EDE19F} +*/ + +#define HV_PCIE_GUID \ + .guid = { \ + 0x1D, 0xF6, 0xC4, 0x44, 0x44, 0x44, 0x00, 0x44, \ + 0x9D, 0x52, 0x80, 0x2E, 0x27, 0xED, 0xE1, 0x9F \ + } + +/* * Common header for Hyper-V ICs */
Signature · 1 line
1.9.1
PATCH v6 7/7 PCI: hv: New paravirtual PCI front-end for Hyper-V VMs
In reply to [email protected]From: Jake Oshins <[email protected]>
This patch introduces a new driver which exposes a root PCI bus whenever a PCI Express device is passed through to a guest VM under Hyper-V. The device can be single- or multi-function. The interrupts for the devices are managed by an IRQ domain, implemented within the driver.
--- MAINTAINERS | 1 + drivers/pci/Kconfig | 7 + drivers/pci/host/Makefile | 1 + drivers/pci/host/hv_pcifront.c | 2267 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 2276 insertions(+) create mode 100644 drivers/pci/host/hv_pcifront.c
MAINTAINERS +1 -0
@@ -5073,6 +5073,7 @@ F: arch/x86/kernel/cpu/mshyperv.c F: drivers/hid/hid-hyperv.c F: drivers/hv/ F: drivers/input/serio/hyperv-keyboard.c +F: drivers/pci/host/hv_pcifront.c F: drivers/net/hyperv/ F: drivers/scsi/storvsc_drv.c F: drivers/video/fbdev/hyperv_fb.c
drivers/pci/Kconfig +7 -0
@@ -118,4 +118,11 @@ config PCI_LABEL def_bool y if (DMI || ACPI) select NLS +config HYPERV_VPCI + tristate "Hyper-V PCI Frontend" + depends on PCI && X86 && HYPERV && PCI_MSI && PCI_MSI_IRQ_DOMAIN + help + The PCI device frontend driver allows the kernel to import arbitrary + PCI devices from a PCI backend to support PCI driver domains. + source "drivers/pci/host/Kconfig"
drivers/pci/host/Makefile +1 -0
@@ -2,6 +2,7 @@ obj-$(CONFIG_PCIE_DW) += pcie-designware.o obj-$(CONFIG_PCI_DRA7XX) += pci-dra7xx.o obj-$(CONFIG_PCI_EXYNOS) += pci-exynos.o obj-$(CONFIG_PCI_IMX6) += pci-imx6.o +obj-$(CONFIG_HYPERV_VPCI) += hv_pcifront.o obj-$(CONFIG_PCI_MVEBU) += pci-mvebu.o obj-$(CONFIG_PCI_TEGRA) += pci-tegra.o obj-$(CONFIG_PCI_RCAR_GEN2) += pci-rcar-gen2.o
drivers/pci/host/hv_pcifront.c new file +2267 -0
2,267 lines not on this page · raw message
Signature · 1 line
1.9.1
Quoted 18 lines · On 2015/11/3 5:33, [email protected] wrote:
From: Jake Oshins <[email protected]>
This patch introduces a new driver which exposes a root PCI bus whenever a PCI Express device is passed through to a guest VM under Hyper-V. The device can be single- or multi-function. The interrupts for the devices are managed by an IRQ domain, implemented within the driver.
Signed-off-by Jake Oshins <[email protected]>--- MAINTAINERS | 1 + drivers/pci/Kconfig | 7 + drivers/pci/host/Makefile | 1 + drivers/pci/host/hv_pcifront.c | 2267 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 2276 insertions(+) create mode 100644 drivers/pci/host/hv_pcifront.cMAINTAINERS +0
-0
[...]
Quoted 22 lines
MAINTAINERS +22
-0+/* Interrupt management hooks */ + +/** + * hv_msi_free() - Free the MSI. + * @domain: The interrupt domain pointer + * @info: Extra MSI-related context + * @irq: Identifies the IRQ. + * + * The Hyper-V parent partition and hypervisor are tracking the + * messages that are in use, keeping the interrupt redirection + * table up to date. This callback sends a message that frees + * the the IRT entry and related tracking nonsense. + */ +static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info, + unsigned int irq) +{ + struct pci_delete_interrupt *int_pkt; + struct { + struct pci_packet pkt; + u8 buffer[sizeof(struct pci_delete_interrupt) - + sizeof(struct pci_message)]; + } ctxt;
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+ struct hv_pcibus_device *hbus; + struct hv_pci_dev *hpdev; + struct irq_desc *desc; + struct msi_desc *msi; + struct tran_int_desc *int_desc; + struct pci_dev *pdev; + + desc = irq_to_desc(irq); + msi = irq_desc_get_msi_desc(desc); + pdev = msi_desc_to_pci_dev(msi);
For safety, don't assume this HV MSI irqdomain is the top domain. So please use:
struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq); struct msi_desc *desc = irq_data_get_msi_desc(irq_data); struct tran_int_desc *int_desc = irq_data_get_chip_data(irq_data);
Quoted 66 lines
MAINTAINERS +66
-0+ hbus = info->data; + hpdev = lookup_hv_dev(hbus, devfn_to_wslot(pdev->devfn)); + if (!hpdev) + return; + + int_desc = irq_get_chip_data(irq); + if (int_desc) { + memset(&ctxt, 0, sizeof(ctxt)); + int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message; + int_pkt->message_type.message_type = + PCI_DELETE_INTERRUPT_MESSAGE; + int_pkt->wslot.slot = hpdev->desc.win_slot.slot; + int_pkt->int_desc = *int_desc; + vmbus_sendpacket(hbus->hdev->channel, int_pkt, sizeof(*int_pkt), + (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, + 0); + desc->irq_data.chip_data = NULL; + kfree(int_desc); + } + + hv_pcichild_dec(hpdev, hv_pcidev_ref_by_slot); +} + +static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest, + bool force) +{ + struct irq_data *parent = data->parent_data; + + return parent->chip->irq_set_affinity(parent, dest, force); +} + +void hv_irq_mask(struct irq_data *data) +{ + pci_msi_mask_irq(data); +} + +/** + * hv_irq_unmask() - "Unmask" the IRQ by setting its current + * affinity. + * @data: Describes the IRQ + * + * Build new a destination for the MSI and make a hypercall to + * update the Interrupt Redirection Table. "Device Logical ID" + * is built out of this PCI bus's instance GUID and the function + * number of the device. + */ +void hv_irq_unmask(struct irq_data *data) +{ + struct msi_desc *msi_desc = irq_data_get_msi_desc(data); + struct irq_cfg *cfg = irqd_cfg(data); + struct retarget_msi_interrupt params; + struct hv_pcibus_device *hbus; + struct cpumask *dest; + struct pci_bus *pbus; + struct pci_dev *pdev; + int cpu; + + dest = irq_data_get_affinity_mask(data); + pdev = msi_desc_to_pci_dev(msi_desc); + pbus = pdev->bus; + hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); + + memset(¶ms, 0, sizeof(params)); + params.partition_id = HV_PARTITION_ID_SELF; + params.source = 1; /* MSI(-X) */ + params.address = msi_desc->msg.address_lo;
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+ params.data = msi_desc->msg.data; + params.device_id = (hbus->hdev->dev_instance.b[5] << 24) | + (hbus->hdev->dev_instance.b[4] << 16) | + (hbus->hdev->dev_instance.b[7] << 8) | + (hbus->hdev->dev_instance.b[6] & 0xf8) | + PCI_FUNC(pdev->devfn); + params.vector = cfg->vector; + + for_each_cpu_and(cpu, dest, cpu_online_mask) + params.vp_mask |= (1 << vmbus_cpu_number_to_vp_number(cpu));
No knowledge about the HV implementation details, but feel some chances of race here between hv_irq_unmask(), hv_set_affinity() and cpu_up()/cpu_down() when accessing 'dest' and cpu_online_mask.
Quoted 99 lines
MAINTAINERS +99
-0+ + hv_do_hypercall(HVCALL_RETARGET_INTERRUPT, ¶ms, NULL); + + pci_msi_unmask_irq(data); +} + +struct compose_comp_ctxt { + struct hv_pci_compl comp_pkt; + struct tran_int_desc int_desc; +}; + +static void hv_pci_compose_compl(void *context, struct pci_response *resp, + int resp_packet_size) +{ + struct compose_comp_ctxt *comp_pkt = context; + struct pci_create_int_response *int_resp = + (struct pci_create_int_response *)resp; + + comp_pkt->comp_pkt.completion_status = resp->status; + comp_pkt->int_desc = int_resp->int_desc; + complete(&comp_pkt->comp_pkt.host_event); +} + +/** + * hv_compose_msi_msg() - Supplies a valid MSI address/data + * @data: Everything about this MSI + * @msg: Buffer that is filled in by this function + * + * This function unpacks the IRQ looking for target CPU set, IDT + * vector and mode and sends a message to the parent partition + * asking for a mapping for that tuple in this partition. The + * response supplies a data value and address to which that data + * should be written to trigger that interrupt. + */ +static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) +{ + struct irq_cfg *cfg = irqd_cfg(data); + struct hv_pcibus_device *hbus; + struct hv_pci_dev *hpdev; + struct pci_bus *pbus; + struct pci_dev *pdev; + struct pci_create_interrupt *int_pkt; + struct compose_comp_ctxt comp; + struct tran_int_desc *int_desc; + struct cpumask *affinity; + struct { + struct pci_packet pkt; + u8 buffer[sizeof(struct pci_create_interrupt) - + sizeof(struct pci_message)]; + } ctxt; + int cpu; + int ret; + + pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data)); + pbus = pdev->bus; + hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); + hpdev = lookup_hv_dev(hbus, devfn_to_wslot(pdev->devfn)); + + if (!hpdev) + goto return_null_message; + + int_desc = kzalloc(sizeof(*int_desc), GFP_KERNEL); + if (!int_desc) + goto drop_reference; + + memset(&ctxt, 0, sizeof(ctxt)); + init_completion(&comp.comp_pkt.host_event); + ctxt.pkt.completion_func = hv_pci_compose_compl; + ctxt.pkt.compl_ctxt = ∁ + int_pkt = (struct pci_create_interrupt *)&ctxt.pkt.message; + int_pkt->message_type.message_type = PCI_CREATE_INTERRUPT_MESSAGE; + int_pkt->wslot.slot = hpdev->desc.win_slot.slot; + int_pkt->int_desc.vector = cfg->vector; + int_pkt->int_desc.vector_count = 1; + int_pkt->int_desc.delivery_mode = + (apic->irq_delivery_mode == dest_LowestPrio) ? 1 : 0; + + /* + * This bit doesn't have to work on machines with more than 64 + * processors because Hyper-V only supports 64 in a guest. + */ + affinity = irq_data_get_affinity_mask(data); + for_each_cpu(cpu, affinity) { + if (cpu_is_offline(cpu)) + continue; + int_pkt->int_desc.cpu_mask |= + (1 << vmbus_cpu_number_to_vp_number(cpu)); + } + + ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, + sizeof(*int_pkt), (unsigned long)&ctxt.pkt, + VM_PKT_DATA_INBAND, + VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); + if (!ret) + wait_for_completion(&comp.comp_pkt.host_event); + + if (comp.comp_pkt.completion_status < 0) { + pr_err("Request for interrupt failed: 0x%x", + comp.comp_pkt.completion_status);
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+ goto free_int_desc; + } + + /* + * Record the assignment so that this can be unwound later. Using + * irq_set_chip_data() here would be appropriate, but the lock it takes + * is already held. + */ + *int_desc = comp.int_desc; + data->chip_data = int_desc;
hv_compose_msi_msg() may be called multiple times, so seems int_desc may get leaked.
Quoted 37 lines
MAINTAINERS +37
-0+ + /* Pass up the result. */ + msg->address_hi = comp.int_desc.address >> 32; + msg->address_lo = comp.int_desc.address & 0xffffffff; + msg->data = comp.int_desc.data; + + hv_pcichild_dec(hpdev, hv_pcidev_ref_by_slot); + return; + +free_int_desc: + kfree(int_desc); +drop_reference: + hv_pcichild_dec(hpdev, hv_pcidev_ref_by_slot); +return_null_message: + msg->address_hi = 0; + msg->address_lo = 0; + msg->data = 0; +} + +/* HW Interrupt Chip Descriptor */ +static struct irq_chip hv_msi_irq_chip = { + .name = "Hyper-V PCIe MSI", + .irq_compose_msi_msg = hv_compose_msi_msg, + .irq_set_affinity = hv_set_affinity, + .irq_ack = irq_chip_ack_parent, + .irq_mask = hv_irq_mask, + .irq_unmask = hv_irq_unmask, +}; + +static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info, + msi_alloc_info_t *arg) +{ + return arg->msi_hwirq; +} + +/** + * hv_msi_set_desc() - Create a "hardware IRQ" for a message.
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+ * @arg: Describes the device + * @desc: Describes the interrupt + * + * This function is derived from pci_msi_set_desc(), which + * creates a "hardware IRQ" based on the segment, bus, device, + * function and message numbers. Here, the bus and device + * numbers will always be zero. So, in the interest of more + * entropy, more of the bits in msi_hwirq are derived from + * things which will be non-zero. + */
It's not for entropy, but for unique. We need to uniquely identify each MSI source because we need to look up irq number by MSI source id. Actually we prefer to compatch the hwirq address space as small as possible because irqdomain use a radix tree to lookup irq by hwirq. So if bus and device number is alwasy zero, just ignore them and compact the hwirq address space:)
Quoted 18 lines
MAINTAINERS +18
-0+static void hv_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc) +{ + arg->msi_hwirq = (irq_hw_number_t)desc->msi_attrib.entry_nr | + (arg->msi_dev->devfn << 11) | + (pci_domain_nr(arg->msi_dev->bus) & 0xFFFFFFFF) << 19; +} + +static struct msi_domain_ops hv_msi_ops = { + .get_hwirq = hv_msi_domain_ops_get_hwirq, + .msi_prepare = pci_msi_prepare, + .set_desc = hv_msi_set_desc, + .msi_free = hv_msi_free, +}; + +/** + * hv_pcie_free_irq_domain - Free IRQ domain + * @hbus: The root PCI bus + */
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+static void hv_pcie_free_irq_domain(struct hv_pcibus_device *hbus) +{ + u32 irq; + int i; + + for (i = 0; i < MAX_SUPPORTED_MSI_MESSAGES; i++) { + irq = irq_find_mapping(hbus->irq_domain, i); + if (irq > 0) + irq_dispose_mapping(irq); + }
When int_desc() get called, all PCI devices, thus all PCI MSI IRQs, should have been destroy, so seems about code is redundant. Thanks,
Gerry
Quoted 38 lines
MAINTAINERS +38
-0+ + irq_domain_remove(hbus->irq_domain); +} + +/** + * hv_pcie_init_irq_domain() - Initialize IRQ domain + * @hbus: The root PCI bus + * + * This function creates an IRQ domain which will be used for + * interrupts from devices that have been passed through. These + * devices only support MSI and MSI-X, not line-based interrupts + * or simulations of line-based interrupts through PCIe's + * fabric-layer messages. Because interrupts are remapped, we + * can support multi-message MSI here. + * + * Return: '0' on success and error value on failure + */ +static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus) +{ + hbus->msi_info.chip = &hv_msi_irq_chip; + hbus->msi_info.chip_data = hbus; + hbus->msi_info.ops = &hv_msi_ops; + hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS | + MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI | + MSI_FLAG_PCI_MSIX); + hbus->msi_info.handler = handle_edge_irq; + hbus->msi_info.handler_name = "edge"; + hbus->msi_info.data = hbus; + hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode, + &hbus->msi_info, + x86_vector_domain); + if (!hbus->irq_domain) { + pr_err("Failed to build an MSI IRQ domain\n"); + return -ENODEV; + } + + return 0; +}
[...]
Quoted 15 lines
-----Original Message-----
From: Jiang Liu [mailto:[email protected]]
Sent: Wednesday, December 2, 2015 7:12 PM
To: Jake Oshins <[email protected]>; [email protected]; KY Srinivasan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Haiyang Zhang <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v6 7/7] PCI: hv: New paravirtual PCI front-end for Hyper- V VMsQuoted 2 lines · On 2015/11/3 5:33, [email protected] wrote:
From: Jake Oshins <[email protected]>
[...]
Quoted 38 lines
Quoted 16 lines
drivers/pci/host/hv_pcifront.c new file +16
-0+ +/** +* hv_irq_unmask() - "Unmask" the IRQ by setting its current +* affinity. +* @data: Describes the IRQ +* +* Build new a destination for the MSI and make a hypercall to +* update the Interrupt Redirection Table. "Device Logical ID" +* is built out of this PCI bus's instance GUID and the function +* number of the device. +*/ +void hv_irq_unmask (struct irq_data *data) +{ +struct msi_desc *msi_desc =irq_data_get_msi_desc (data); +struct irq_cfg *cfg =irqd_cfg (data); +struct retarget_msi_interrupt params;drivers/pci/host/hv_pcifront.c
Quoted 10 lines
drivers/pci/host/hv_pcifront.c new file +10
-0+struct hv_pcibus_device *hbus; +struct cpumask *dest; +struct pci_bus *pbus; +struct pci_dev *pdev; +int cpu; + + dest =irq_data_get_affinity_mask (data); + pdev =msi_desc_to_pci_dev (msi_desc); + pbus = pdev->bus; + hbus = container_of(pbus->sysdata, struct hv_pcibus_device,sysdata);
Quoted 11 lines
drivers/pci/host/hv_pcifront.c new file +11
-0+ +memset (¶ms,0 ,sizeof (params)); + params.partition_id =HV_PARTITION_ID_SELF ; + params.source =1 ;/* MSI(-X) */ + params.address = msi_desc->msg.address_lo; + params.data = msi_desc->msg.data; + params.device_id = (hbus->hdev->dev_instance.b[5 ] <<24 ) | + (hbus->hdev->dev_instance.b[4 ] <<16 ) | + (hbus->hdev->dev_instance.b[7 ] <<8 ) | + (hbus->hdev->dev_instance.b[6 ] &0xf8 ) | +PCI_FUNC (pdev->devfn);
Quoted 9 lines
drivers/pci/host/hv_pcifront.c
Quoted 4 lines
drivers/pci/host/hv_pcifront.c new file +4
-0+ params.vector = cfg->vector; + +for_each_cpu_and (cpu, dest, cpu_online_mask) + params.vp_mask |= (1 <<
vmbus_cpu_number_to_vp_number(cpu));
No knowledge about the HV implementation details, but feel some chances of race here betweenhv_irq_unmask(),hv_set_affinity()and cpu_up()/cpu_down() when accessing 'dest' andcpu_online_mask.
Thanks. Is there any architectural contract here? I tried implementing this by doing this work in the set_affinity() callback, but the vector was often wrong when that callback was invoked. (It seems to get changed just after set_affinity().) Can you suggest a durable strategy?
I'll respond to all the other comments you sent (and this one, once I understand the right response) and resend.
Thanks for your review,
Jake Oshins
Quoted 68 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: Monday, November 2, 2015 1:33 PM
To: [email protected]; KY Srinivasan <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Haiyang Zhang <[email protected]>; [email protected]; [email protected]; [email protected]
Cc: Jake Oshins <[email protected]>
Subject: [PATCH v6 0/7] PCI: hv: New paravirtual PCI front-end for Hyper-V VMsFrom: Jake Oshins <[email protected]>
This version of this patch series incorporates feedback from Andy Shevchenko.
First, export functions that allow correlating Hyper-V virtual processors and Linux cpus, along with the means for invoking a hypercall that targets interrupts at chosen vectors on specfic cpus.
Second, mark various parts of IRQ domain related code as exported, so that this PCI front-end can implement an IRQ domain as part of a module. (The alternative would be to pull all this into the kernel, which would pull in a lot of other Hyper-V related code, as this IRQ domain depends on hv_vmbus.ko.)
Third, modify PCI so that new root PCI buses can be marked with an associated
fwnode_handle, and so that root PCI buses can look up their associated IRQ domain by that handle.Fourth, introduce a new driver,
hv_pcifront, which eposes root PCI buses in a Hyper-V VM. These root PCI buses expose real PCIe devices, or PCI Virtual Functions.Jake Oshins (7):
drivers:hv: Export a function that maps Linux CPU num onto Hyper-V
proc num
drivers:hv: Exporthv_do_hypercall()
PCI: Make it possible to implement a PCI MSI IRQ Domain in a module.
PCI: Addfwnode_handletopci_sysdata
PCI: irqdomain: Look up IRQ domain byfwnode_handledrivers:hv: Define the channel type of Hyper-V PCI Express
pass-through
PCI: hv: New paravirtual PCI front-end for Hyper-V VMsMAINTAINERS | 1 + arch/x86/include/asm/msi.h | 4 + arch/x86/include/asm/pci.h | 13 + arch/x86/kernel/apic/msi.c | 5 +- arch/x86/kernel/apic/vector.c | 2 + drivers/hv/hv.c | 20 +- drivers/hv/hyperv_vmbus.h | 2 +- drivers/hv/vmbus_drv.c | 17 + drivers/pci/Kconfig | 7 + drivers/pci/host/Makefile | 1 + drivers/pci/host/hv_pcifront.c | 2267 ++++++++++++++++++++++++++++++++++++++++ drivers/pci/msi.c | 4 + drivers/pci/probe.c | 14 + include/asm-generic/pci.h | 4 + include/linux/hyperv.h | 14 + kernel/irq/chip.c | 1 + kernel/irq/irqdomain.c | 2 + 17 files changed, 2365 insertions(+), 13 deletions(-) create mode 100644 drivers/pci/host/hv_pcifront.c
Quoted 2 lines
--
1.9.1
Apparently I've been using the wrong e-mail address for Thomas Gleixner. Sorry about that.
Are there any more comments about this patch series?
Thanks,
Jake Oshins