The patch introduces function kvm_vfio_eeh_dev_map(), which is expected to be called on IOCTL command issued to the VM device, in order to build the address mapping for VFIO PCI device. Signed-off-by: Gavin Shan <gwshan@xxxxxxxxxxxxxxxxxx> --- arch/powerpc/kernel/eeh_pe.c | 88 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 14 +++++++ 2 files changed, 102 insertions(+) diff --git a/arch/powerpc/kernel/eeh_pe.c b/arch/powerpc/kernel/eeh_pe.c index 9e73188..200cd5a 100644 --- a/arch/powerpc/kernel/eeh_pe.c +++ b/arch/powerpc/kernel/eeh_pe.c @@ -332,6 +332,94 @@ struct eeh_dev *eeh_vfio_dev_get(struct eeh_vfio_pci_addr *addr) return NULL; } +/** + * kvm_vfio_eeh_dev_map - Build the address mapping for VFIO device + * + * @kvm: VM descriptor + * @domain: host domain of PCI device + * @bdn: host bus/device/function number + * @buid: BUID of guest PHB + * @gbdn: guest bus/device/function number + * + * Build the address mapping between host and guest deivce. It's called + * while passing through PCI device from host to guest. + */ +int kvm_vfio_eeh_dev_map(struct kvm *kvm, int domain, + int bdn, unsigned long buid, int gbdn) +{ + struct pci_bus *bus, *pe_bus; + struct pci_dev *dev; + struct eeh_dev *edev; + struct eeh_pe *pe; + int bus_no, devfn; + + /* Find the PCI device in host side */ + bus_no = (bdn >> 8) & 0xff; + devfn = bdn & 0xff; + bus = pci_find_bus(domain, bus_no); + if (!bus) { + pr_warn("%s: PCI bus %04x:%02x not found\n", + __func__, domain, bus_no); + return -ENODEV; + } + + dev = pci_get_slot(bus, devfn); + if (!dev) { + pr_warn("%s: PCI device %04x:%02x:%02x.%01x not found\n", + __func__, domain, bus_no, + PCI_SLOT(devfn), PCI_FUNC(devfn)); + return -ENODEV; + } + + /* + * Mark the EEH device as passed. We allow dynamic change + * on the address mapping. + */ + edev = pci_dev_to_eeh_dev(dev); + if (!edev) { + pr_warn("%s: No EEH dev for PCI device %s\n", + __func__, pci_name(dev)); + return -ENODEV; + } + + /* + * The PE configuration address is exactly PCI config address + * of the PE primary bus. That has format 00BBSS00 defined in + * PAPR. + */ + pe = edev->pe; + if (!eeh_pe_passed(pe)) { + pe_bus = eeh_pe_bus_get(pe); + BUG_ON(!pe_bus); + + pe->gaddr.kvm = kvm; + pe->gaddr.buid_hi = BUID_HI(buid); + pe->gaddr.buid_lo = BUID_LO(buid); + pe->gaddr.pe_addr = pe_bus->number << 16; + eeh_pe_set_passed(pe, true); + } else if (pe->gaddr.kvm != kvm || + pe->gaddr.buid_hi != BUID_HI(buid) || + pe->gaddr.buid_lo != BUID_LO(buid)) { + pr_warn("%s: Mismatched VM or PHB on passing %s\n", + __func__, pci_name(dev)); + return -EINVAL; + } + + edev->gaddr.kvm = kvm; + edev->gaddr.buid_hi = BUID_HI(buid); + edev->gaddr.buid_lo = BUID_LO(buid); + edev->gaddr.bus = (gbdn >> 8) & 0xff; + edev->gaddr.devfn = gbdn & 0xff; + eeh_dev_set_passed(edev, true); + + pr_debug("EEH: Host PCI device %s passed to %lx-%02x:%02x.%01x\n", + pci_name(dev), buid, (gbdn >> 8) & 0xff, + PCI_SLOT(gbdn & 0xff), PCI_FUNC(gbdn & 0xff)); + + return 0; +} +EXPORT_SYMBOL_GPL(kvm_vfio_eeh_dev_map); + static void *__kvmppc_eeh_vfio_release(void *data, void *flag) { struct eeh_pe *pe = (struct eeh_pe *)data; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7d21cf9..294ce48 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1102,5 +1102,19 @@ static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val) { } #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */ + +#ifdef CONFIG_KVM_EEH +typedef int (*kvm_vfio_dev_eeh_map)(struct kvm *kvm, int domain, + int bdn, unsigned long buid, int gbdn); +extern int kvm_vfio_eeh_dev_map(struct kvm *kvm, int domain, + int bdn, unsigned long buid, int gbdn); +#else +static inline int kvm_vfio_eeh_dev_map(struct kvm *kvm, int domain, + int bdn, unsigned long buid, int gbdn) +{ + return 0; +} +#endif /* CONFIG_KVM_EEH */ + #endif -- 1.8.3.2 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html