From: Matthew Rosato <mjrosato@linux.ibm.com>
To: linux-s390@vger.kernel.org
Cc: kvm@vger.kernel.org, david@redhat.com, thuth@redhat.com,
linux-kernel@vger.kernel.org, vneethv@linux.ibm.com,
agordeev@linux.ibm.com, imbrenda@linux.ibm.com, will@kernel.org,
frankja@linux.ibm.com, corbet@lwn.net, linux-doc@vger.kernel.org,
pasic@linux.ibm.com, jgg@nvidia.com,
gerald.schaefer@linux.ibm.com, borntraeger@linux.ibm.com,
farman@linux.ibm.com, gor@linux.ibm.com, schnelle@linux.ibm.com,
hca@linux.ibm.com, alex.williamson@redhat.com,
freude@linux.ibm.com, pmorel@linux.ibm.com, cohuck@redhat.com,
oberpar@linux.ibm.com, iommu@lists.linux-foundation.org,
svens@linux.ibm.com, pbonzini@redhat.com
Subject: [PATCH v4 22/32] KVM: s390: pci: routines for (dis)associating zPCI devices with a KVM
Date: Mon, 14 Mar 2022 15:44:41 -0400 [thread overview]
Message-ID: <20220314194451.58266-23-mjrosato@linux.ibm.com> (raw)
In-Reply-To: <20220314194451.58266-1-mjrosato@linux.ibm.com>
These routines will be wired into a KVM ioctl, to be issued from
userspace to (dis)associate a specific zPCI device with the issuing
KVM. This will create/delete a relationship between KVM, zPCI device
and the associated IOMMU domain for the device.
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
---
arch/s390/include/asm/kvm_host.h | 2 +
arch/s390/include/asm/kvm_pci.h | 2 +
arch/s390/kvm/kvm-s390.c | 5 +
arch/s390/kvm/pci.c | 225 +++++++++++++++++++++++++++++++
arch/s390/kvm/pci.h | 5 +
5 files changed, 239 insertions(+)
diff --git a/arch/s390/include/asm/kvm_host.h b/arch/s390/include/asm/kvm_host.h
index bf61ab05f98c..bd171abbb8ef 100644
--- a/arch/s390/include/asm/kvm_host.h
+++ b/arch/s390/include/asm/kvm_host.h
@@ -965,6 +965,8 @@ struct kvm_arch{
DECLARE_BITMAP(idle_mask, KVM_MAX_VCPUS);
struct kvm_s390_gisa_interrupt gisa_int;
struct kvm_s390_pv pv;
+ struct list_head kzdev_list;
+ spinlock_t kzdev_list_lock;
};
#define KVM_HVA_ERR_BAD (-1UL)
diff --git a/arch/s390/include/asm/kvm_pci.h b/arch/s390/include/asm/kvm_pci.h
index ebc0da5d9ac1..47ce18b5bddd 100644
--- a/arch/s390/include/asm/kvm_pci.h
+++ b/arch/s390/include/asm/kvm_pci.h
@@ -21,6 +21,8 @@ struct kvm_zdev {
struct zpci_dev *zdev;
struct kvm *kvm;
struct iommu_domain *dom; /* Used to invoke IOMMU API for RPCIT */
+ struct notifier_block nb;
+ struct list_head entry;
};
int kvm_s390_pci_dev_open(struct zpci_dev *zdev);
diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
index d91b2547f0bf..84acaf59a7d3 100644
--- a/arch/s390/kvm/kvm-s390.c
+++ b/arch/s390/kvm/kvm-s390.c
@@ -2775,6 +2775,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
kvm_s390_crypto_init(kvm);
+ if (IS_ENABLED(CONFIG_VFIO_PCI))
+ kvm_s390_pci_init_list(kvm);
+
mutex_init(&kvm->arch.float_int.ais_lock);
spin_lock_init(&kvm->arch.float_int.lock);
for (i = 0; i < FIRQ_LIST_COUNT; i++)
@@ -2860,6 +2863,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
if (!kvm_is_ucontrol(kvm))
gmap_remove(kvm->arch.gmap);
kvm_s390_destroy_adapters(kvm);
+ if (IS_ENABLED(CONFIG_VFIO_PCI))
+ kvm_s390_pci_clear_list(kvm);
kvm_s390_clear_float_irqs(kvm);
kvm_s390_vsie_destroy(kvm);
KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
diff --git a/arch/s390/kvm/pci.c b/arch/s390/kvm/pci.c
index 1c42d25de697..28fe95f13c33 100644
--- a/arch/s390/kvm/pci.c
+++ b/arch/s390/kvm/pci.c
@@ -9,6 +9,7 @@
#include <linux/kvm_host.h>
#include <linux/pci.h>
+#include <linux/vfio.h>
#include <asm/kvm_pci.h>
#include <asm/pci.h>
#include <asm/pci_insn.h>
@@ -23,6 +24,22 @@ static inline int __set_irq_noiib(u16 ctl, u8 isc)
return zpci_set_irq_ctrl(ctl, isc, &iib);
}
+static struct kvm_zdev *get_kzdev_by_fh(struct kvm *kvm, u32 fh)
+{
+ struct kvm_zdev *kzdev, *retval = NULL;
+
+ spin_lock(&kvm->arch.kzdev_list_lock);
+ list_for_each_entry(kzdev, &kvm->arch.kzdev_list, entry) {
+ if (kzdev->zdev->fh == fh) {
+ retval = kzdev;
+ break;
+ }
+ }
+ spin_unlock(&kvm->arch.kzdev_list_lock);
+
+ return retval;
+}
+
/* Caller must hold the aift lock before calling this function */
void kvm_s390_pci_aen_exit(void)
{
@@ -153,6 +170,20 @@ int kvm_s390_pci_aen_init(u8 nisc)
return rc;
}
+static int kvm_s390_pci_group_notifier(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct kvm_zdev *kzdev = container_of(nb, struct kvm_zdev, nb);
+
+ if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
+ if (!data || !kzdev->zdev)
+ return NOTIFY_DONE;
+ kzdev->kvm = data;
+ }
+
+ return NOTIFY_OK;
+}
+
int kvm_s390_pci_dev_open(struct zpci_dev *zdev)
{
struct kvm_zdev *kzdev;
@@ -179,6 +210,200 @@ void kvm_s390_pci_dev_release(struct zpci_dev *zdev)
}
EXPORT_SYMBOL_GPL(kvm_s390_pci_dev_release);
+static struct vfio_device *get_vdev(struct device *dev)
+{
+ struct vfio_device *(*fn)(struct device *dev);
+ struct vfio_device *vdev;
+
+ fn = symbol_get(vfio_device_get_from_dev);
+ if (!fn)
+ return NULL;
+
+ vdev = fn(dev);
+
+ symbol_put(vfio_device_get_from_dev);
+
+ return vdev;
+}
+
+static void put_vdev(struct vfio_device *vdev)
+{
+ void (*fn)(struct vfio_device *vdev);
+
+ fn = symbol_get(vfio_device_put);
+ if (!fn)
+ return;
+
+ fn(vdev);
+
+ symbol_put(vfio_device_put);
+}
+
+static int register_notifier(struct device *dev, struct notifier_block *nb)
+{
+ int (*fn)(struct device *dev, enum vfio_notify_type type,
+ unsigned long *events, struct notifier_block *nb);
+ unsigned long events = VFIO_GROUP_NOTIFY_SET_KVM;
+ int rc;
+
+ fn = symbol_get(vfio_register_notifier);
+ if (!fn)
+ return -EINVAL;
+
+ rc = fn(dev, VFIO_GROUP_NOTIFY, &events, nb);
+
+ symbol_put(vfio_register_notifier);
+
+ return rc;
+}
+
+static int unregister_notifier(struct device *dev, struct notifier_block *nb)
+{
+ int (*fn)(struct device *dev, enum vfio_notify_type type,
+ struct notifier_block *nb);
+ int rc;
+
+ fn = symbol_get(vfio_unregister_notifier);
+ if (!fn)
+ return -EINVAL;
+
+ rc = fn(dev, VFIO_GROUP_NOTIFY, nb);
+
+ symbol_put(vfio_unregister_notifier);
+
+ return rc;
+}
+
+int kvm_s390_pci_zpci_start(struct kvm *kvm, struct zpci_dev *zdev)
+{
+ struct vfio_device *vdev;
+ struct pci_dev *pdev;
+ int rc;
+
+ rc = kvm_s390_pci_dev_open(zdev);
+ if (rc)
+ return rc;
+
+ pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
+ if (!pdev) {
+ rc = -ENODEV;
+ goto exit_err;
+ }
+
+ vdev = get_vdev(&pdev->dev);
+ if (!vdev) {
+ pci_dev_put(pdev);
+ rc = -ENODEV;
+ goto exit_err;
+ }
+
+ zdev->kzdev->nb.notifier_call = kvm_s390_pci_group_notifier;
+
+ /*
+ * At this point, a KVM should already be associated with this device,
+ * so registering the notifier now should immediately trigger the
+ * event. We also want to know if the KVM association is later removed
+ * to ensure proper cleanup happens.
+ */
+ rc = register_notifier(vdev->dev, &zdev->kzdev->nb);
+
+ put_vdev(vdev);
+ pci_dev_put(pdev);
+
+ /* Make sure the registered KVM matches the KVM issuing the ioctl */
+ if (rc || zdev->kzdev->kvm != kvm) {
+ rc = -ENODEV;
+ goto exit_err;
+ }
+
+ /* Must support KVM-managed IOMMU to proceed */
+ if (IS_ENABLED(CONFIG_S390_KVM_IOMMU))
+ rc = zpci_iommu_attach_kvm(zdev, kvm);
+ else
+ rc = -EINVAL;
+
+ if (rc)
+ goto exit_err;
+
+ spin_lock(&kvm->arch.kzdev_list_lock);
+ list_add_tail(&zdev->kzdev->entry, &kvm->arch.kzdev_list);
+ spin_unlock(&kvm->arch.kzdev_list_lock);
+ return 0;
+
+exit_err:
+ kvm_s390_pci_dev_release(zdev);
+ return rc;
+}
+
+int kvm_s390_pci_zpci_stop(struct kvm *kvm, struct zpci_dev *zdev)
+{
+ struct vfio_device *vdev;
+ struct pci_dev *pdev;
+ int rc = 0;
+
+ if (!zdev || !zdev->kzdev)
+ return -EINVAL;
+
+ pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
+ if (!pdev) {
+ rc = -ENODEV;
+ goto exit_err;
+ }
+
+ vdev = get_vdev(&pdev->dev);
+ if (!vdev) {
+ pci_dev_put(pdev);
+ rc = -ENODEV;
+ goto exit_err;
+ }
+
+ spin_lock(&kvm->arch.kzdev_list_lock);
+ list_del(&zdev->kzdev->entry);
+ spin_unlock(&kvm->arch.kzdev_list_lock);
+
+ rc = unregister_notifier(vdev->dev, &zdev->kzdev->nb);
+
+ put_vdev(vdev);
+ pci_dev_put(pdev);
+
+exit_err:
+ kvm_s390_pci_dev_release(zdev);
+ return rc;
+}
+
+void kvm_s390_pci_init_list(struct kvm *kvm)
+{
+ spin_lock_init(&kvm->arch.kzdev_list_lock);
+ INIT_LIST_HEAD(&kvm->arch.kzdev_list);
+}
+
+void kvm_s390_pci_clear_list(struct kvm *kvm)
+{
+ struct kvm_zdev *tmp, *kzdev;
+ struct vfio_device *vdev;
+ struct pci_dev *pdev;
+ LIST_HEAD(remove);
+
+ spin_lock(&kvm->arch.kzdev_list_lock);
+ list_for_each_entry_safe(kzdev, tmp, &kvm->arch.kzdev_list, entry)
+ list_move_tail(&kzdev->entry, &remove);
+ spin_unlock(&kvm->arch.kzdev_list_lock);
+
+ list_for_each_entry_safe(kzdev, tmp, &remove, entry) {
+ pdev = pci_get_slot(kzdev->zdev->zbus->bus, kzdev->zdev->devfn);
+ if (pdev) {
+ vdev = get_vdev(&pdev->dev);
+ if (vdev) {
+ unregister_notifier(vdev->dev,
+ &kzdev->nb);
+ put_vdev(vdev);
+ }
+ pci_dev_put(pdev);
+ }
+ kvm_s390_pci_dev_release(kzdev->zdev);
+ }
+}
+
int kvm_s390_pci_init(void)
{
aift = kzalloc(sizeof(struct zpci_aift), GFP_KERNEL);
diff --git a/arch/s390/kvm/pci.h b/arch/s390/kvm/pci.h
index 25cb1c787190..a95d9fdc91be 100644
--- a/arch/s390/kvm/pci.h
+++ b/arch/s390/kvm/pci.h
@@ -47,6 +47,11 @@ static inline struct kvm *kvm_s390_pci_si_to_kvm(struct zpci_aift *aift,
int kvm_s390_pci_aen_init(u8 nisc);
void kvm_s390_pci_aen_exit(void);
+int kvm_s390_pci_zpci_start(struct kvm *kvm, struct zpci_dev *zdev);
+int kvm_s390_pci_zpci_stop(struct kvm *kvm, struct zpci_dev *zdev);
+void kvm_s390_pci_init_list(struct kvm *kvm);
+void kvm_s390_pci_clear_list(struct kvm *kvm);
+
int kvm_s390_pci_init(void);
#endif /* __KVM_S390_PCI_H */
--
2.27.0
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu
next prev parent reply other threads:[~2022-03-14 19:49 UTC|newest]
Thread overview: 66+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-14 19:44 [PATCH v4 00/32] KVM: s390: enable zPCI for interpretive execution Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 01/32] s390/sclp: detect the zPCI load/store interpretation facility Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 02/32] s390/sclp: detect the AISII facility Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 03/32] s390/sclp: detect the AENI facility Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 04/32] s390/sclp: detect the AISI facility Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 05/32] s390/airq: pass more TPI info to airq handlers Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 06/32] s390/airq: allow for airq structure that uses an input vector Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 07/32] s390/pci: externalize the SIC operation controls and routine Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 08/32] s390/pci: stash associated GISA designation Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 09/32] s390/pci: export some routines related to RPCIT processing Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 10/32] s390/pci: stash dtsm and maxstbl Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 11/32] s390/pci: add helper function to find device by handle Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 12/32] s390/pci: get SHM information from list pci Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 13/32] s390/pci: return status from zpci_refresh_trans Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 14/32] iommu: introduce iommu_domain_alloc_type and the KVM type Matthew Rosato
2022-03-14 21:36 ` Jason Gunthorpe via iommu
2022-03-15 10:49 ` Robin Murphy
2022-03-17 5:47 ` Tian, Kevin
2022-03-17 13:52 ` Jason Gunthorpe via iommu
2022-03-18 2:23 ` Tian, Kevin
2022-03-18 14:13 ` Jason Gunthorpe via iommu
2022-03-19 7:51 ` Tian, Kevin
2022-03-21 14:07 ` Jason Gunthorpe via iommu
2022-03-22 7:30 ` Tian, Kevin
2022-03-14 19:44 ` [PATCH v4 15/32] vfio: introduce KVM-owned IOMMU type Matthew Rosato
2022-03-14 21:38 ` Jason Gunthorpe via iommu
2022-03-15 13:49 ` Matthew Rosato
2022-03-15 14:38 ` Jason Gunthorpe via iommu
2022-03-15 16:29 ` Matthew Rosato
2022-03-15 17:25 ` Jason Gunthorpe via iommu
2022-03-17 18:51 ` Matthew Rosato
2022-03-14 22:50 ` Alex Williamson
2022-03-14 23:18 ` Jason Gunthorpe via iommu
2022-03-15 7:57 ` Tian, Kevin
2022-03-15 14:17 ` Matthew Rosato
2022-03-15 17:01 ` Matthew Rosato
2022-03-15 13:36 ` Matthew Rosato
2022-03-15 14:55 ` Jason Gunthorpe via iommu
2022-03-15 16:04 ` Matthew Rosato
2022-03-15 17:18 ` Jason Gunthorpe via iommu
2022-03-18 7:01 ` Tian, Kevin
2022-03-18 13:46 ` Jason Gunthorpe via iommu
2022-03-19 7:47 ` Tian, Kevin
2022-03-14 19:44 ` [PATCH v4 16/32] vfio-pci/zdev: add function handle to clp base capability Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 17/32] KVM: s390: pci: add basic kvm_zdev structure Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 18/32] iommu/s390: add support for IOMMU_DOMAIN_KVM Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 19/32] KVM: s390: pci: do initial setup for AEN interpretation Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 20/32] KVM: s390: pci: enable host forwarding of Adapter Event Notifications Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 21/32] KVM: s390: mechanism to enable guest zPCI Interpretation Matthew Rosato
2022-03-14 19:44 ` Matthew Rosato [this message]
2022-03-14 21:46 ` [PATCH v4 22/32] KVM: s390: pci: routines for (dis)associating zPCI devices with a KVM Jason Gunthorpe via iommu
2022-03-15 16:39 ` Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 23/32] KVM: s390: pci: provide routines for enabling/disabling interpretation Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 24/32] KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 25/32] KVM: s390: pci: provide routines for enabling/disabling IOAT assist Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 26/32] KVM: s390: pci: handle refresh of PCI translations Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 27/32] KVM: s390: intercept the rpcit instruction Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 28/32] KVM: s390: add KVM_S390_ZPCI_OP to manage guest zPCI devices Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 29/32] vfio-pci/zdev: add DTSM to clp group capability Matthew Rosato
2022-03-14 21:49 ` Jason Gunthorpe via iommu
2022-03-15 14:39 ` Matthew Rosato
2022-03-15 14:56 ` Jason Gunthorpe via iommu
2022-03-14 19:44 ` [PATCH v4 30/32] KVM: s390: introduce CPU feature for zPCI Interpretation Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 31/32] MAINTAINERS: additional files related kvm s390 pci passthrough Matthew Rosato
2022-03-14 19:44 ` [PATCH v4 32/32] MAINTAINERS: update s390 IOMMU entry Matthew Rosato
2022-03-14 19:52 ` [PATCH v4 00/32] KVM: s390: enable zPCI for interpretive execution Matthew Rosato
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220314194451.58266-23-mjrosato@linux.ibm.com \
--to=mjrosato@linux.ibm.com \
--cc=agordeev@linux.ibm.com \
--cc=alex.williamson@redhat.com \
--cc=borntraeger@linux.ibm.com \
--cc=cohuck@redhat.com \
--cc=corbet@lwn.net \
--cc=david@redhat.com \
--cc=farman@linux.ibm.com \
--cc=frankja@linux.ibm.com \
--cc=freude@linux.ibm.com \
--cc=gerald.schaefer@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=imbrenda@linux.ibm.com \
--cc=iommu@lists.linux-foundation.org \
--cc=jgg@nvidia.com \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=oberpar@linux.ibm.com \
--cc=pasic@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=pmorel@linux.ibm.com \
--cc=schnelle@linux.ibm.com \
--cc=svens@linux.ibm.com \
--cc=thuth@redhat.com \
--cc=vneethv@linux.ibm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox