PCI passthru on Hyper-V (Part II)
Subsystems (7)
cover PCI passthru on Hyper-V (Part II)
This patch series implements interrupt remapping part of the PCI passthru feature on Hyper-V when Linux is running as a privileged VM. These patches complement the Part I of the feature at:
https://lore.kernel.org/linux-hyperv/[email protected]/T/#t
Testing and other details are listed there.
Thanks,
-Mukesh
Mukesh R (3):
mshv: Import declarations for irq remap and add irqbypass support
hyperv: Implement irq remap for passthru devices
mshv: Implement guest irq migration for passthru'd devices
arch/x86/hyperv/irqdomain.c | 18 +- drivers/hv/Kconfig | 1 + drivers/hv/mshv_eventfd.c | 500 +++++++++++++++++++++++++++- drivers/hv/mshv_eventfd.h | 3 + drivers/iommu/hyperv-iommu-root.c | 14 + drivers/pci/controller/pci-hyperv.c | 10 + include/asm-generic/mshyperv.h | 4 + include/hyperv/hvgdk_mini.h | 3 + include/hyperv/hvhdk.h | 17 + 9 files changed, 564 insertions(+), 6 deletions(-)
Signature · 1 line
2.51.2.vfs.0.1
PATCH 1/3 mshv: Import declarations for irq remap and add irqbypass support
For the irq map/remap hypercalls, copy relevant data structures from hypervisor public headers into Linux equivalents. Also, update Kconfig and mshv_irqfd for irqbypass. Please note, irqbypass is required for doing passthru on MSHV. This because there is really no way of knowing the linux irq in the mshv_irqfd_assign and mshv_irqfd_update paths without it. The linux irq is setup upfront by VFIO before irqfd assign/update happens.
--- drivers/hv/Kconfig | 1 + drivers/hv/mshv_eventfd.h | 3 +++ include/hyperv/hvgdk_mini.h | 3 +++ include/hyperv/hvhdk.h | 17 +++++++++++++++++ 4 files changed, 24 insertions(+)
drivers/hv/Kconfig +1 -0
@@ -75,6 +75,7 @@ config MSHV_ROOT # no particular order, making it impossible to reassemble larger pages depends on PAGE_SIZE_4KB select EVENTFD + select IRQ_BYPASS_MANAGER select VIRT_XFER_TO_GUEST_WORK select HMM_MIRROR select MMU_NOTIFIER
drivers/hv/mshv_eventfd.h +3 -0
@@ -9,6 +9,7 @@#define __LINUX_MSHV_EVENTFD_H#include <linux/poll.h> +#include <linux/irqbypass.h> #include "mshv.h" #include "mshv_root.h"
@@ -37,6 +38,8 @@ struct mshv_irqfd {struct mshv_irqfd_resampler *irqfd_resampler;struct eventfd_ctx *irqfd_resamplefd;struct hlist_node irqfd_resampler_hnode; +struct irq_bypass_consumer irqfd_bypass_cons; +struct irq_bypass_producer *irqfd_bypass_prod; };void mshv_eventfd_init (struct mshv_partition *partition);
include/hyperv/hvgdk_mini.h +3 -0
@@ -59,6 +59,8 @@ struct hv_u128 {#define HV_PARTITION_ID_INVALID ((u64)0)#define HV_PARTITION_ID_SELF ((u64)-1) +#define HV_MAX_VPS 256/* HV_MAXIMUM_PROCESSORS */ +/* Hyper-V specific model specific registers (MSRs) */ #if defined(CONFIG_X86 )
@@ -508,6 +510,7 @@ union hv_vp_assist_msr_contents { /* HV_REGISTER_VP_ASSIST_PAGE */#define HVCALL_UNMAP_VP_STATE_PAGE 0x00e2#define HVCALL_GET_VP_STATE 0x00e3#define HVCALL_SET_VP_STATE 0x00e4 +#define HVCALL_GET_VPSET_FROM_MDA 0x00e5#define HVCALL_GET_VP_CPUID_VALUES 0x00f4#define HVCALL_GET_PARTITION_PROPERTY_EX 0x0101#define HVCALL_MMIO_READ 0x0106
include/hyperv/hvhdk.h +17 -0
@@ -952,4 +952,21 @@ struct hv_input_modify_sparse_spa_page_host_access {#define HV_MODIFY_SPA_PAGE_HOST_ACCESS_LARGE_PAGE 0x4#define HV_MODIFY_SPA_PAGE_HOST_ACCESS_HUGE_PAGE 0x8 +#ifdef CONFIG_X86 + +struct hv_input_get_vp_set_from_mda {/* HV_OUTPUT_GET_VP_SET_FROM_MDA */ +u64 target_partid; +u64 dest_address; +u8 input_vtl; +u8 destmode_logical;/* true => mode is logical */ +u16 reserved0;/* mbz */ +u32 reserved1;/* mbz */ +} __packed; + +union hv_output_get_vp_set_from_mda {/* HV_OUTPUT_GET_VP_SET_FROM_MDA */ +struct hv_vpset target_vpset; +u64 bitset_buffer[HV_GENERIC_SET_QWORD_COUNT (HV_MAX_VPS )]; +} __packed; + +#endif /* CONFIG_X86 */ #endif /* _HV_HVHDK_H */
Signature · 1 line
2.51.2.vfs.0.1
PATCH 2/3 hyperv: Implement irq remap for passthru devices
In reply to Mukesh RImplement interrupt remapping for direct attached and domain attached devices on Hyper-V.
Please note there are few constraints when it comes to mapping device interrupts on Hyper-V. For example, the hypervisor will not allow mapping device interrupts to root if the device is a direct attached device. Since the target guest cpu and vector info is not available during the initial VFIO irq setup, we work around by skipping this initial map. Then later during irqbypass trigger, when both guest target cpu vector are available, we do the map in the hypervisor, update the device, and enable the interrupt vector on the device. Rather than special case direct attached, we do same for domain attached also. This implies irqbypass is required for MSHV pci device passthru. Also noteworthy is that the hypervisor will automatically setup any direct hw injection like posted interrupts.
--- arch/x86/hyperv/irqdomain.c | 18 +- drivers/hv/mshv_eventfd.c | 422 +++++++++++++++++++++++++++- drivers/iommu/hyperv-iommu-root.c | 14 + drivers/pci/controller/pci-hyperv.c | 10 + include/asm-generic/mshyperv.h | 4 + 5 files changed, 464 insertions(+), 4 deletions(-)
arch/x86/hyperv/irqdomain.c +16 -2
@@ -222,7 +222,7 @@ int hv_map_msi_interrupt(struct irq_data *data, msidesc =irq_data_get_msi_desc (data); pdev =msi_desc_to_pci_dev (msidesc); - hv_devid.as_uint64 =hv_build_devid_type_pci (pdev); + hv_devid.as_uint64 =hv_devid_from_pdev (pdev); cpu =cpumask_first (irq_data_get_effective_affinity_mask (data)); return hv_map_interrupt(hv_current_partition_id, hv_devid, false, cpu,
@@ -258,6 +258,20 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)return ; } +/* +* For direct attached devices, we cannot map interrupts in the +* hypervisor because it will not allow it until we have guest target +* vcpu and vector. So defer it until irqbypass. Also, do the same +* for domain attached devices for simplicity. +*/ +if (hv_pcidev_is_pthru_dev (pdev)) { +if (data->chip_data) +entry_to_msi_msg (data->chip_data, msg); +else +memset (msg,0 ,sizeof (struct msi_msg )); +return ; + } +if (data->chip_data) {/* * This interrupt is already mapped. Let's unmap first.
@@ -297,7 +311,7 @@ static int hv_unmap_msi_interrupt(struct pci_dev *pdev, {union hv_device_id hv_devid; - hv_devid.as_uint64 =hv_build_devid_type_pci (pdev); + hv_devid.as_uint64 =hv_devid_from_pdev (pdev);return hv_unmap_interrupt (hv_devid.as_uint64, irq_entry); }
drivers/hv/mshv_eventfd.c +420 -2
464 lines not on this page · raw message
drivers/iommu/hyperv-iommu-root.c +14 -0
@@ -219,6 +219,20 @@ u64 hv_build_devid_oftype(struct pci_dev *pdev, enum hv_device_type type) }EXPORT_SYMBOL_GPL (hv_build_devid_oftype); +/* Build device id for the interrupt path */ +u64 hv_devid_from_pdev (struct pci_dev *pdev) +{ +enum hv_device_type dev_type; + +if (hv_pcidev_is_attached_dev (pdev)) + dev_type =HV_DEVICE_TYPE_LOGICAL ; +else + dev_type =HV_DEVICE_TYPE_PCI ; + +return hv_build_devid_oftype (pdev, dev_type); +} +EXPORT_SYMBOL_GPL (hv_devid_from_pdev); +/* Create a new device domain in the hypervisor */ static int hv_iommu_create_hyp_devdom (struct hv_domain *hvdom) {
drivers/pci/controller/pci-hyperv.c +10 -0
@@ -1745,6 +1745,16 @@ static void hv_irq_mask(struct irq_data *data)static void hv_irq_unmask (struct irq_data *data) { +struct pci_dev *pdev; +struct msi_desc *msi_desc; + + msi_desc =irq_data_get_msi_desc (data); + pdev =msi_desc_to_pci_dev (msi_desc); + +/* Done during bypass setup in mshv_eventfd.c: mshv_irqfd_assign() */ +if (hv_pcidev_is_pthru_dev (pdev)) +return ; +hv_arch_irq_unmask (data);if (data->parent_data->chip->irq_unmask)
include/asm-generic/mshyperv.h +4 -0
@@ -341,6 +341,7 @@ u64 hv_get_current_partid(void);bool hv_pcidev_is_attached_dev (struct pci_dev *pdev);bool hv_pcidev_is_pthru_dev (struct pci_dev *pdev);u64 hv_build_devid_oftype (struct pci_dev *pdev,enum hv_device_type type); +u64 hv_devid_from_pdev (struct pci_dev *pdev);#else /* Remove following after arm64 implementation is done */
@@ -354,6 +355,9 @@ static inline u64 hv_build_devid_oftype(struct pci_dev *pdev, enum hv_device_type type) {return 0 ; } +static inline u64 hv_devid_from_pdev (struct pci_dev *pdev) +{return 0 ; } +static inline u64 hv_get_current_partid (void ) {return HV_PARTITION_ID_INVALID ; }#endif /* IS_ENABLED(CONFIG_HYPERV_IOMMU) */
Signature · 1 line
2.51.2.vfs.0.1
PATCH 3/3 mshv: Implement guest irq migration for passthru'd devices
In reply to Mukesh RAsk the hypervisor to retarget interrupts to new guest cpu or vector upon guest irq migration. This happens in the irqfd update path.
--- drivers/hv/mshv_eventfd.c | 78 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-)
drivers/hv/mshv_eventfd.c +76 -2
@@ -192,6 +192,77 @@ static int mshv_map_device_interrupt(u64 ptid, union hv_device_id hv_devid, } +/* NOTE: caller does spin_lock_irq on pt_irqfds_lock, hence no disable here */ +static void mshv_do_guest_irq_retarget (u64 partid,struct mshv_irqfd *irqfd) +{ +int rc, var_size; +u64 status; +union hv_device_id hv_devid; +struct hv_input_get_vp_set_from_mda *mda_input; +union hv_output_get_vp_set_from_mda *mda_output; +struct hv_retarget_device_interrupt *remap_inp; +struct pci_dev *pdev; +struct irq_data *irqdata; +struct mshv_lapic_irq *lapic_irq = &irqfd->irqfd_lapic_irq; +struct hv_interrupt_entry *inte =NULL ; + +if (!irqfd->irqfd_girq_ent.girq_entry_valid || + irqfd->irqfd_bypass_prod ==NULL ) +return ; + + rc =mshv_parse_mshv_irqfd (irqfd, &pdev, &irqdata); +if (rc) +return ; + + inte = irqdata->chip_data; +if (inte ==NULL ) +return ; + + hv_devid.as_uint64 =hv_devid_from_pdev (pdev); + + + mda_input = *this_cpu_ptr (hyperv_pcpu_input_arg); + mda_output = *this_cpu_ptr (hyperv_pcpu_output_arg); + + rc =hv_vpset_from_hyp_disabled (mda_input, mda_output, lapic_irq, + partid); +if (rc) +return ; + + remap_inp = *this_cpu_ptr (hyperv_pcpu_input_arg); +memset (remap_inp,0 ,sizeof (*remap_inp)); + + rc =hv_copy_vpset (&remap_inp->int_target.vp_set, + &mda_output->target_vpset); +if (rc <=0 ) { +pr_err ("Hyper-V: ptid %lld - vpset copy failed (%d)\n" , + partid, rc); +return ; + } + +/* +* var-sized hcall: var-size starts after vp_mask (thus vp_set.format +* does not count, but vp_set.valid_bank_mask does). +*/ + var_size = rc +1 ; + + remap_inp->partition_id = partid; + remap_inp->device_id = hv_devid.as_uint64; + remap_inp->int_target.vector = lapic_irq->lapic_vector; + remap_inp->int_target.flags =HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET ; + + remap_inp->int_entry.source = inte->source; + remap_inp->int_entry.msi_entry.as_uint64 = inte->msi_entry.as_uint64; + + status =hv_do_rep_hypercall (HVCALL_RETARGET_INTERRUPT ,0 , var_size, + remap_inp,NULL ); + +if (!hv_result_success (status)) +hv_status_err (status,"pt:%lld vec:%d lapic-id:%lld\n" , + partid, lapic_irq->lapic_vector, + lapic_irq->lapic_apic_id); +} +static int mshv_unmap_device_interrupt (union hv_device_id hv_devid,struct hv_interrupt_entry *irq_entry) {
@@ -728,9 +799,12 @@ static void mshv_irqfd_update(struct mshv_partition *pt, struct mshv_irqfd *irqfd) {write_seqcount_begin (&irqfd->irqfd_irqe_sc); - irqfd->irqfd_girq_ent =mshv_ret_girq_entry (pt, - irqfd->irqfd_irqnum); + irqfd->irqfd_girq_ent =mshv_ret_girq_entry (pt, irqfd->irqfd_irqnum);mshv_copy_girq_info (&irqfd->irqfd_girq_ent, &irqfd->irqfd_lapic_irq); + +#if IS_ENABLED (CONFIG_X86_64 ) +mshv_do_guest_irq_retarget (pt->pt_id, irqfd); +#endif write_seqcount_end (&irqfd->irqfd_irqe_sc); }
Signature · 1 line
2.51.2.vfs.0.1