Skip to contentSkip to conversation

vfio on spapr-ppc64

PATCH Superseded Alexey Kardashevskiy 5 participants 24 messages Jun 5, 2014 – Jun 6, 2014

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v7 cover vfio on spapr-ppc64

Yet another try with VFIO on SPAPR (server PPC64).

This adds VFIO support on SPAPR for the existing VFIO-SPAPR-TCE driver in the upstream kernel.

Individual patches have more detailed commit logs.

While patch #1 is questionable, others are pretty much ready to but I do not know via which maintainer tree - Alex or Alex? :)

Please comment. Thanks!

Changes:
v7:
* cleaned and rebased on agraf/ppc-next tree (which is on its way to upstream)

v6:
* initial set was split into 3

v5:
* rebase on top of the current upstream

v4:
* addressed all comments from Alex Williamson
* moved spapr-pci-phb-vfio-phb to new file
* split spapr-pci-phb-vfio to many smaller patches

Alexey Kardashevskiy (4):
spapr_iommu: Make in-kernel TCE table optional
vfio: Add vfio_container_spapr_get_info()
spapr_pci_vfio: Add spapr-pci-vfio-host-bridge to support vfio
vfio: Enable for spapr

 hw/misc/vfio.c              | 64 +++++++++++++++++++++++++++++++
 hw/ppc/Makefile.objs        |  3 ++
 hw/ppc/spapr_iommu.c        |  6 ++-
 hw/ppc/spapr_pci.c          |  2 +-
 hw/ppc/spapr_pci_vfio.c     | 93 +++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr_vio.c          |  2 +-
 include/hw/misc/vfio.h      | 11 ++++++
 include/hw/pci-host/spapr.h | 11 ++++++
 include/hw/ppc/spapr.h      |  4 +-
 9 files changed, 191 insertions(+), 5 deletions(-)
 create mode 100644 hw/ppc/spapr_pci_vfio.c
 create mode 100644 include/hw/misc/vfio.h
Signature · 1 line
2.0.0
#1 lore raw

PATCH v7 1/4 spapr_iommu: Make in-kernel TCE table optional

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel. Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated PCI and VFIO which is a good thing.

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but this needs kernel update.

---
 hw/ppc/spapr_iommu.c   | 6 ++++--
 hw/ppc/spapr_pci.c     | 2 +-
 hw/ppc/spapr_vio.c     | 2 +-
 include/hw/ppc/spapr.h | 4 +++-
 4 files changed, 9 insertions(+), 5 deletions(-)
hw/ppc/spapr_iommu.c +4 -2
index 3b6e373..bfd3701 100644
@@ -115,7 +115,7 @@ static int spapr_tce_table_realize(DeviceState *dev)
 {
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
 
-    if (kvm_enabled()) {
+    if (tcet->kvm_accel && kvm_enabled()) {
         tcet->table = kvmppc_create_spapr_tce(tcet->liobn,
                                               tcet->nb_table <<
                                               tcet->page_shift,
@@ -143,7 +143,8 @@ static int spapr_tce_table_realize(DeviceState *dev)
 sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn,
                                    uint64_t bus_offset,
                                    uint32_t page_shift,
-                                   uint32_t nb_table)
+                                   uint32_t nb_table,
+                                   bool kvm_accel)
 {
     sPAPRTCETable *tcet;
 
@@ -162,6 +163,7 @@ sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn,
     tcet->bus_offset = bus_offset;
     tcet->page_shift = page_shift;
     tcet->nb_table = nb_table;
+    tcet->kvm_accel = kvm_accel;
 
     object_property_add_child(OBJECT(owner), "tce-table", OBJECT(tcet), NULL);
 
hw/ppc/spapr_pci.c +1 -1
index ddfd8bb..6021f35 100644
@@ -658,7 +658,7 @@ static void spapr_phb_finish_realize(sPAPRPHBState *sphb, Error **errp)
     tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn,
                                0,
                                SPAPR_TCE_PAGE_SHIFT,
-                               0x40000000 >> SPAPR_TCE_PAGE_SHIFT);
+                               0x40000000 >> SPAPR_TCE_PAGE_SHIFT, true);
     if (!tcet) {
         error_setg(errp, "Unable to create TCE table for %s",
                    sphb->dtbusname);
hw/ppc/spapr_vio.c +1 -1
index 48b0125..16385e4 100644
@@ -460,7 +460,7 @@ static int spapr_vio_busdev_init(DeviceState *qdev)
                                         0,
                                         SPAPR_TCE_PAGE_SHIFT,
                                         pc->rtce_window_size >>
-                                        SPAPR_TCE_PAGE_SHIFT);
+                                        SPAPR_TCE_PAGE_SHIFT, true);
         address_space_init(&dev->as, spapr_tce_get_iommu(dev->tcet), qdev->id);
     }
 
include/hw/ppc/spapr.h +3 -1
index 4ffb903..7db34ff 100644
@@ -402,6 +402,7 @@ struct sPAPRTCETable {
     uint32_t page_shift;
     uint64_t *table;
     bool bypass;
+    bool kvm_accel;
     int fd;
     MemoryRegion iommu;
     QLIST_ENTRY(sPAPRTCETable) list;
@@ -413,7 +414,8 @@ int spapr_h_cas_compose_response(target_ulong addr, target_ulong size);
 sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn,
                                    uint64_t bus_offset,
                                    uint32_t page_shift,
-                                   uint32_t nb_table);
+                                   uint32_t nb_table,
+                                   bool kvm_accel);
 MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet);
 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass);
 int spapr_dma_dt(void *fdt, int node_off, const char *propname,
Signature · 1 line
2.0.0
#2 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 27 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel. Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

Quoted 8 lines

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated PCI and VFIO which is a good thing.

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation patch separately.

Quoted 87 lines
---
 hw/ppc/spapr_iommu.c   | 6 ++++--
 hw/ppc/spapr_pci.c     | 2 +-
 hw/ppc/spapr_vio.c     | 2 +-
 include/hw/ppc/spapr.h | 4 +++-
 4 files changed, 9 insertions(+), 5 deletions(-)
hw/ppc/spapr_iommu.c +4 -2
index 3b6e373..bfd3701 100644
@@ -115,7 +115,7 @@ static int spapr_tce_table_realize(DeviceState *dev)
 {
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
 
-    if (kvm_enabled()) {
+    if (tcet->kvm_accel && kvm_enabled()) {
         tcet->table = kvmppc_create_spapr_tce(tcet->liobn,
                                               tcet->nb_table <<
                                               tcet->page_shift,
@@ -143,7 +143,8 @@ static int spapr_tce_table_realize(DeviceState *dev)
 sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn,
                                    uint64_t bus_offset,
                                    uint32_t page_shift,
-                                   uint32_t nb_table)
+                                   uint32_t nb_table,
+                                   bool kvm_accel)
 {
     sPAPRTCETable *tcet;
 
@@ -162,6 +163,7 @@ sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn,
     tcet->bus_offset = bus_offset;
     tcet->page_shift = page_shift;
     tcet->nb_table = nb_table;
+    tcet->kvm_accel = kvm_accel;
 
     object_property_add_child(OBJECT(owner), "tce-table", OBJECT(tcet), NULL);
 
hw/ppc/spapr_pci.c +1 -1
index ddfd8bb..6021f35 100644
@@ -658,7 +658,7 @@ static void spapr_phb_finish_realize(sPAPRPHBState *sphb, Error **errp)
     tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn,
                                0,
                                SPAPR_TCE_PAGE_SHIFT,
-                               0x40000000 >> SPAPR_TCE_PAGE_SHIFT);
+                               0x40000000 >> SPAPR_TCE_PAGE_SHIFT, true);
     if (!tcet) {
         error_setg(errp, "Unable to create TCE table for %s",
                    sphb->dtbusname);
hw/ppc/spapr_vio.c +1 -1
index 48b0125..16385e4 100644
@@ -460,7 +460,7 @@ static int spapr_vio_busdev_init(DeviceState *qdev)
                                         0,
                                         SPAPR_TCE_PAGE_SHIFT,
                                         pc->rtce_window_size >>
-                                        SPAPR_TCE_PAGE_SHIFT);
+                                        SPAPR_TCE_PAGE_SHIFT, true);
         address_space_init(&dev->as, spapr_tce_get_iommu(dev->tcet), qdev->id);
     }
 
include/hw/ppc/spapr.h +3 -1
index 4ffb903..7db34ff 100644
@@ -402,6 +402,7 @@ struct sPAPRTCETable {
     uint32_t page_shift;
     uint64_t *table;
     bool bypass;
+    bool kvm_accel;
     int fd;
     MemoryRegion iommu;
     QLIST_ENTRY(sPAPRTCETable) list;
@@ -413,7 +414,8 @@ int spapr_h_cas_compose_response(target_ulong addr, target_ulong size);
 sPAPRTCETable *spapr_tce_new_table(DeviceState *owner, uint32_t liobn,
                                    uint64_t bus_offset,
                                    uint32_t page_shift,
-                                   uint32_t nb_table);
+                                   uint32_t nb_table,
+                                   bool kvm_accel);
 MemoryRegion *spapr_tce_get_iommu(sPAPRTCETable *tcet);
 void spapr_tce_set_bypass(sPAPRTCETable *tcet, bool bypass);
 int spapr_dma_dt(void *fdt, int node_off, const char *propname,
Signature · 1 line
Alexey
#3 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 31 lines · Alexey Kardashevskiy
Quoted 30 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel. Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

Quoted 8 lines
Quoted 5 lines

This is a workaround but it lets me have one IOMMU device for VIO, emulated PCI and VFIO which is a good thing.

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation patch separately.

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for vfio and false for emulated devices. Then the spapr_iommu file can check on the capability (and default to false for now, since it doesn't exist yet).

That way you don't have to reserve a CAP today.

Alex

#4 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 39 lines · Alexander Graf
Quoted 37 lines · Alexey Kardashevskiy
Quoted 35 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel. Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated PCI and VFIO which is a good thing.

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but this needs kernel update.

Quoted 6 lines
Quoted 2 lines

Never mind, I'll make it a capability. I'll post capability reservation patch separately.

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for vfio and false for emulated devices. Then the spapr_iommu file can check on the capability (and default to false for now, since it doesn't exist yet).

Is that ok if the flag does not have to do anything with VFIO per se? :)

Quoted 1 line

That way you don't have to reserve a CAP today.

Why exactly cannot we do that today?

How do we proceed with the rest of this patchset? Thanks!

Signature · 1 line
Alexey
#5 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 36 lines · Alexey Kardashevskiy
Quoted 35 lines · Alexander Graf
Quoted 34 lines · Alexey Kardashevskiy
Quoted 33 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel. Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated PCI and VFIO which is a good thing.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation patch separately.

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for vfio and false for emulated devices. Then the spapr_iommu file can check on the capability (and default to false for now, since it doesn't exist yet).

Is that ok if the flag does not have to do anything with VFIO per se? :)

The flag means "use in-kernel acceleration if the vfio coupling capability is available", no?

Quoted 4 lines
Quoted 3 lines

That way you don't have to reserve a CAP today.

Why exactly cannot we do that today?

Because the CAP namespace isn't a garbage bin we can just throw IDs at. Maybe we realize during patch review that we need completely different CAPs.

Alex

#6 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 45 lines · Alexander Graf
Quoted 43 lines · Alexey Kardashevskiy
Quoted 42 lines · Alexander Graf
Quoted 41 lines · Alexey Kardashevskiy
Quoted 30 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating
TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel.
Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

Quoted 8 lines

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated
PCI and VFIO which is a good thing.

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation patch separately.

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for vfio and false for emulated devices. Then the spapr_iommu file can check on the capability (and default to false for now, since it doesn't exist yet).

Is that ok if the flag does not have to do anything with VFIO per se? :)

The flag means "use in-kernel acceleration if the vfio coupling capability is available", no?

It is a flag of sPAPRTCETable which is not supposed to know about VFIO at all, it is just an IOMMU. But if you are ok with it, I have no reason to be unhappy either :)

Quoted 5 lines
Quoted 2 lines
Quoted 1 line

That way you don't have to reserve a CAP today.

Why exactly cannot we do that today?

Because the CAP namespace isn't a garbage bin we can just throw IDs at. Maybe we realize during patch review that we need completely different CAPs.

That was my first plan - to wait for KVM_CAP_SPAPR_TCE_64 be available in the kernel.

Signature · 1 line
Alexey
#7 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 57 lines · Alexey Kardashevskiy
Quoted 42 lines · Alexander Graf
Quoted 41 lines · Alexey Kardashevskiy
Quoted 40 lines · Alexander Graf
Quoted 39 lines · Alexey Kardashevskiy
Quoted 38 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating
TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel.
Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated
PCI and VFIO which is a good thing.

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but this needs kernel update.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Never mind, I'll make it a capability. I'll post capability reservation patch separately.

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for vfio and false for emulated devices. Then the spapr_iommu file can check on the capability (and default to false for now, since it doesn't exist yet).

Is that ok if the flag does not have to do anything with VFIO per se? :)

The flag means "use in-kernel acceleration if the vfio coupling capability is available", no?

It is a flag of sPAPRTCETable which is not supposed to know about VFIO at all, it is just an IOMMU. But if you are ok with it, I have no reason to be unhappy either :)

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

That way you don't have to reserve a CAP today.

Why exactly cannot we do that today?

Because the CAP namespace isn't a garbage bin we can just throw IDs at. Maybe we realize during patch review that we need completely different CAPs.

That was my first plan - to wait for KVM_CAP_SPAPR_TCE_64 be available in the kernel.

So all you need are 64bit TCEs with bus_offset? What about the missing in-kernel modification of the shadow TCEs on H_PUT_TCE? I thought that's what this is really about.

Alex

#8 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 65 lines · Alexander Graf
Quoted 63 lines · Alexey Kardashevskiy
Quoted 47 lines · Alexander Graf
Quoted 46 lines · Alexey Kardashevskiy
Quoted 45 lines · Alexander Graf
Quoted 44 lines · Alexey Kardashevskiy
Quoted 34 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating
TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without
switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class
so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel.
Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

Quoted 7 lines

This is a workaround but it lets me have one IOMMU device for VIO, emulated
PCI and VFIO which is a good thing.

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but
this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation patch separately.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for vfio and false for emulated devices. Then the spapr_iommu file can check on
the capability (and default to false for now, since it doesn't exist yet).

Is that ok if the flag does not have to do anything with VFIO per se? :)

The flag means "use in-kernel acceleration if the vfio coupling capability is available", no?

It is a flag of sPAPRTCETable which is not supposed to know about VFIO at all, it is just an IOMMU. But if you are ok with it, I have no reason to be unhappy either :)

Quoted 1 line
Quoted 1 line
Quoted 1 line

That way you don't have to reserve a CAP today.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why exactly cannot we do that today?

Because the CAP namespace isn't a garbage bin we can just throw IDs at. Maybe we realize during patch review that we need completely different CAPs.

That was my first plan - to wait for KVM_CAP_SPAPR_TCE_64 be available in the kernel.

So all you need are 64bit TCEs with bus_offset?

No. I need 64bit IOBAs a.k.a. PCI bus addresses. The default DMA window is just 1 or 2GB and it is mapped at 0 on PCI bus.

TCEs are 64 bit already.

Quoted 3 lines

What about the missing
in-kernel modification of the shadow TCEs on H_PUT_TCE? I thought that's what this is really about.

This I do not understand :(

Signature · 1 line
Alexey
#9 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 69 lines · Alexey Kardashevskiy
Quoted 68 lines · Alexander Graf
Quoted 67 lines · Alexey Kardashevskiy
Quoted 47 lines · Alexander Graf
Quoted 46 lines · Alexey Kardashevskiy
Quoted 45 lines · Alexander Graf
Quoted 44 lines · Alexey Kardashevskiy
Quoted 34 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating
TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without
switching to QEMU. At the moment this is used for emulated devices only and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class
so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel.
Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users choose whether to use acceleration or not. At the moment it is enabled for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

Quoted 7 lines

This is a workaround but it lets me have one IOMMU device for VIO, emulated
PCI and VFIO which is a good thing.

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but
this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation patch separately.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for vfio and false for emulated devices. Then the spapr_iommu file can check on
the capability (and default to false for now, since it doesn't exist yet).

Is that ok if the flag does not have to do anything with VFIO per se? :)

The flag means "use in-kernel acceleration if the vfio coupling capability is available", no?

It is a flag of sPAPRTCETable which is not supposed to know about VFIO at all, it is just an IOMMU. But if you are ok with it, I have no reason to be unhappy either :)

Quoted 5 lines
Quoted 2 lines
Quoted 1 line

That way you don't have to reserve a CAP today.

Why exactly cannot we do that today?

Because the CAP namespace isn't a garbage bin we can just throw IDs at. Maybe we realize during patch review that we need completely different CAPs.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

That was my first plan - to wait for KVM_CAP_SPAPR_TCE_64 be available in the kernel.

So all you need are 64bit TCEs with bus_offset?

No. I need 64bit IOBAs a.k.a. PCI bus addresses. The default DMA window is just 1 or 2GB and it is mapped at 0 on PCI bus.

TCEs are 64 bit already.

Ok, so the guest has to tell the PCI device to write to a specific window. That's a shame :).

Quoted 5 lines
Quoted 4 lines

What about the missing
in-kernel modification of the shadow TCEs on H_PUT_TCE? I thought that's what this is really about.

This I do not understand :(

How does real mode H_PUT_TCE emulation know that it needs to notify user space to establish the map?

Alex

#10 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 85 lines · Alexander Graf
Quoted 83 lines · Alexey Kardashevskiy
Quoted 81 lines · Alexander Graf
Quoted 72 lines · Alexey Kardashevskiy
Quoted 60 lines · Alexander Graf
Quoted 59 lines · Alexey Kardashevskiy
Quoted 58 lines · Alexander Graf
Quoted 45 lines · Alexey Kardashevskiy
Quoted 44 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating
TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without
switching to QEMU. At the moment this is used for emulated devices only
and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE
handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be
notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class
so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not
to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel.
Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users
choose whether to use acceleration or not. At the moment it is enabled
for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated
PCI and VFIO which is a good thing.

Quoted 6 lines
Quoted 3 lines

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but
this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation
patch separately.

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for
vfio and false for emulated devices. Then the spapr_iommu file can check on
the capability (and default to false for now, since it doesn't exist yet).

Quoted 4 lines
Quoted 1 line

Is that ok if the flag does not have to do anything with VFIO per se? :)

The flag means "use in-kernel acceleration if the vfio coupling capability
is available", no?

It is a flag of sPAPRTCETable which is not supposed to know about VFIO at all, it is just an IOMMU. But if you are ok with it, I have no reason to be
unhappy either :)

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

That way you don't have to reserve a CAP today.

Why exactly cannot we do that today?

Because the CAP namespace isn't a garbage bin we can just throw IDs at. Maybe we realize during patch review that we need completely different CAPs.

That was my first plan - to wait for KVM_CAP_SPAPR_TCE_64 be available in the kernel.

So all you need are 64bit TCEs with bus_offset?

Quoted 7 lines
Quoted 4 lines

No. I need 64bit IOBAs a.k.a. PCI bus addresses. The default DMA window is just 1 or 2GB and it is mapped at 0 on PCI bus.

TCEs are 64 bit already.

Ok, so the guest has to tell the PCI device to write to a specific window. That's a shame :).

No. Guest tells the device some address, that's it. Guest allocates those addresses from some window which host, guest and PHB know about but not the device. What is a shame here?

Quoted 9 lines
Quoted 6 lines
Quoted 5 lines

What about the missing
in-kernel modification of the shadow TCEs on H_PUT_TCE? I thought that's what this is really about.

This I do not understand :(

How does real mode H_PUT_TCE emulation know that it needs to notify user space to establish the map?

If it wants to pass control to the user space, it returns H_TOO_HARD. This happens, for example, if LIOBN was not registered in KVM.

Signature · 1 line
Alexey
#11 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 87 lines · Alexey Kardashevskiy
Quoted 86 lines · Alexander Graf
Quoted 85 lines · Alexey Kardashevskiy
Quoted 73 lines · Alexander Graf
Quoted 72 lines · Alexey Kardashevskiy
Quoted 60 lines · Alexander Graf
Quoted 59 lines · Alexey Kardashevskiy
Quoted 58 lines · Alexander Graf
Quoted 45 lines · Alexey Kardashevskiy
Quoted 44 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating
TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without
switching to QEMU. At the moment this is used for emulated devices only
and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE
handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be
notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class
so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not
to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling that sPAPRTCETable should not try allocating TCE table in the host kernel.
Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users
choose whether to use acceleration or not. At the moment it is enabled
for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated
PCI and VFIO which is a good thing.

Quoted 6 lines
Quoted 3 lines

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but
this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation
patch separately.

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for
vfio and false for emulated devices. Then the spapr_iommu file can check on
the capability (and default to false for now, since it doesn't exist yet).

Quoted 4 lines
Quoted 1 line

Is that ok if the flag does not have to do anything with VFIO per se? :)

The flag means "use in-kernel acceleration if the vfio coupling capability
is available", no?

It is a flag of sPAPRTCETable which is not supposed to know about VFIO at all, it is just an IOMMU. But if you are ok with it, I have no reason to be
unhappy either :)

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

That way you don't have to reserve a CAP today.

Why exactly cannot we do that today?

Because the CAP namespace isn't a garbage bin we can just throw IDs at. Maybe we realize during patch review that we need completely different CAPs.

That was my first plan - to wait for KVM_CAP_SPAPR_TCE_64 be available in the kernel.

So all you need are 64bit TCEs with bus_offset?

No. I need 64bit IOBAs a.k.a. PCI bus addresses. The default DMA window is just 1 or 2GB and it is mapped at 0 on PCI bus.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

TCEs are 64 bit already.

Ok, so the guest has to tell the PCI device to write to a specific window. That's a shame :).

No. Guest tells the device some address, that's it. Guest allocates those addresses from some window which host, guest and PHB know about but not the device. What is a shame here?

It would be nicer if the guest had full control over the virtual address range of a PCI device.

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

What about the missing
in-kernel modification of the shadow TCEs on H_PUT_TCE? I thought that's what this is really about.

This I do not understand :(

How does real mode H_PUT_TCE emulation know that it needs to notify user space to establish the map?

If it wants to pass control to the user space, it returns H_TOO_HARD. This happens, for example, if LIOBN was not registered in KVM.

So how does KVM_CAP_SPAPR_TCE_64 help here? With KVM_CAP_SPAPR_TCE_64 we can still not map VFIO devices' TCE tables because we're missing all the magic to link the virtual TCE table to a physical TCE table.

Alex

#12 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

Quoted 110 lines · Alexander Graf
Quoted 94 lines · Alexey Kardashevskiy
Quoted 93 lines · Alexander Graf
Quoted 92 lines · Alexey Kardashevskiy
Quoted 80 lines · Alexander Graf
Quoted 79 lines · Alexey Kardashevskiy
Quoted 64 lines · Alexander Graf
Quoted 63 lines · Alexey Kardashevskiy
Quoted 62 lines · Alexander Graf
Quoted 49 lines · Alexey Kardashevskiy
Quoted 48 lines · Alexey Kardashevskiy

POWER KVM supports an KVM_CAP_SPAPR_TCE capability which allows allocating
TCE tables in the host kernel memory and handle H_PUT_TCE requests targeted to specific LIOBN (logical bus number) right in the host without
switching to QEMU. At the moment this is used for emulated devices only
and the handler only puts TCE to the table. If the in-kernel H_PUT_TCE
handler finds a LIOBN and corresponding table, it will put a TCE to the table and complete hypercall execution. The user space will not be
notified.

Upcoming VFIO support is going to use the same sPAPRTCETable device class
so KVM_CAP_SPAPR_TCE is going to be used as well. That means that TCE
tables for VFIO are going to be allocated in the host as well. However VFIO operates with real IOMMU tables and simple copying of a TCE to the real hardware TCE table will not work as guest physical
to host physical address translation is requited.

So until the host kernel gets VFIO support for H_PUT_TCE, we better not
to register VFIO's TCE in the host.

This adds a bool @kvm_accel flag to the sPAPRTCETable device telling
that sPAPRTCETable should not try allocating TCE table in the host kernel.
Instead, the table will be created in QEMU.

This adds an kvm_accel parameter to spapr_tce_new_table() to let users
choose whether to use acceleration or not. At the moment it is enabled
for VIO and emulated PCI. Upcoming VFIO support will set it to false.

---

This is a workaround but it lets me have one IOMMU device for VIO, emulated
PCI and VFIO which is a good thing.

Quoted 6 lines
Quoted 3 lines

The other way around would be a new KVM_CAP_SPAPR_TCE_VFIO capability but
this needs kernel update.

Never mind, I'll make it a capability. I'll post capability reservation
patch separately.

Just rename the flag from "kvm_accel" to "vfio_accel", set it to true for
vfio and false for emulated devices. Then the spapr_iommu file can check on
the capability (and default to false for now, since it doesn't exist yet).

Quoted 5 lines
Quoted 2 lines

Is that ok if the flag does not have to do anything with VFIO per se? :)

The flag means "use in-kernel acceleration if the vfio coupling capability
is available", no?

It is a flag of sPAPRTCETable which is not supposed to know about VFIO at
all, it is just an IOMMU. But if you are ok with it, I have no reason to be
unhappy either :)

Quoted 1 line
Quoted 1 line
Quoted 1 line

That way you don't have to reserve a CAP today.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Why exactly cannot we do that today?

Because the CAP namespace isn't a garbage bin we can just throw IDs at. Maybe we realize during patch review that we need completely different CAPs.

That was my first plan - to wait for KVM_CAP_SPAPR_TCE_64 be available in
the kernel.

So all you need are 64bit TCEs with bus_offset?

No. I need 64bit IOBAs a.k.a. PCI bus addresses. The default DMA window is just 1 or 2GB and it is mapped at 0 on PCI bus.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

TCEs are 64 bit already.

Ok, so the guest has to tell the PCI device to write to a specific window. That's a shame :).

No. Guest tells the device some address, that's it. Guest allocates those addresses from some window which host, guest and PHB know about but not the device. What is a shame here?

It would be nicer if the guest had full control over the virtual address range of a PCI device.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

What about the missing
in-kernel modification of the shadow TCEs on H_PUT_TCE? I thought that's what this is really about.

This I do not understand :(

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

How does real mode H_PUT_TCE emulation know that it needs to notify user space to establish the map?

If it wants to pass control to the user space, it returns H_TOO_HARD. This happens, for example, if LIOBN was not registered in KVM.

So how does KVM_CAP_SPAPR_TCE_64 help here? With KVM_CAP_SPAPR_TCE_64 we can still not map VFIO devices' TCE tables because we're missing all the magic to link the virtual TCE table to a physical TCE table.

It does not help here indeeed, I did not say it would ;) I just wanted to do the preparations first, and this means I need to reserve capability numbers (which is normally very tough process). Since one capability is straightforward to implement, I included this into the set.

Signature · 1 line
Alexey
#13 lore raw

Re: [Qemu-devel] [PATCH v7 1/4] spapr_iommu: Make in-kernel TCE table optional

In reply to Alexander Graf
Quoted 3 lines · Alexander Graf

It would be nicer if the guest had full control over the virtual address range of a PCI device.

It does ... within a HW window which can be different between P7 and P8.

On P7 all PEs on a PHB share a single DMA address space that gets sliced up, I won't get into details on what kind of slices are available, suffice to say we provide a single smallish window in 32-bit space for each PE and the guest controls the 4k TCEs in there.

On P8, each PE has its own DMA address space which has 2 wnidows, one at 0 and one at 0x0800_0000_0000_0000.

By default we configure the 0 window for 32-bit/4K TCE remapping (we set it to 2G window) for compatibility with existing PAPR expectations.

The high window is used in the host as a bypass. We disable TCEs and use a direct mapping to physical memory through it instead to allow the host drivers that are 64-bit DMA capable to have the fastest possible access to memory.

When we pass-through a device today we disable that second window for obvious reasons.

With Alexey patches, we'll be able to control it which will in turn allow us to implement the PAPR "DDW" extension which allows the guest to populate that second window. Typically the guest will use it to create a full mapping of its entire address space in 64-bit space using the largest possible TCE size (whose size is constrained by the page size used to back the guest memory).

Here too, within those windows, the guest has control of the mappings.

Cheers,
Ben.

#14 lore raw

PATCH v7 2/4 vfio: Add vfio_container_spapr_get_info()

In reply to Alexey Kardashevskiy

To perform DMA mapping via TCE table correctly, the guest must know where DMA window is located on the PCI bus. A hypervisor is expected to provide such information. Since QEMU has no control over this setting, we need a way to obtain a start address and size from the host VFIO driver.

This adds a helper which returns the default DMA window properties for the specific IOMMU group. The upstream kernel implements this ioctl already.

---
Changes:
v7:
* do not return a group fd from the helper

v6:
* added dup() to protect group_fd from accidental disposal

v5:
* reworked to reflect change in vfio_get_group() from one of previous patches change

v4:
* fixed possible leaks on error paths

---
 hw/misc/vfio.c         | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/misc/vfio.h | 11 +++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 include/hw/misc/vfio.h
hw/misc/vfio.c +36 -0
index 7437c2e..99141f3 100644
@@ -39,6 +39,7 @@
 #include "qemu/range.h"
 #include "sysemu/kvm.h"
 #include "sysemu/sysemu.h"
+#include "hw/misc/vfio.h"
 
 /* #define DEBUG_VFIO */
 #ifdef DEBUG_VFIO
@@ -4318,3 +4319,38 @@ static void register_vfio_pci_dev_type(void)
 }
 
 type_init(register_vfio_pci_dev_type)
+
+int vfio_container_spapr_get_info(AddressSpace *as,
+                                  int32_t groupid,
+                                  struct vfio_iommu_spapr_tce_info *info)
+{
+    VFIOGroup *group;
+    VFIOContainer *container;
+    int ret, fd;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        return -1;
+    }
+    container = group->container;
+    if (!group->container) {
+        goto put_group_exit;
+    }
+    fd = container->fd;
+    if (!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
+        goto put_group_exit;
+    }
+    ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, info);
+    if (ret) {
+        error_report("vfio: failed to get iommu info for container: %s",
+                     strerror(errno));
+        goto put_group_exit;
+    }
+
+    return 0;
+
+put_group_exit:
+    vfio_put_group(group);
+
+    return -1;
+}
include/hw/misc/vfio.h new file +11 -0
new file mode 100644
index 0000000..e82f5a3
@@ -0,0 +1,11 @@
+#ifndef VFIO_API_H
+#define VFIO_API_H
+
+#include "qemu/typedefs.h"
+#include <linux/vfio.h>
+
+extern int vfio_container_spapr_get_info(AddressSpace *as,
+                                         int32_t groupid,
+                                         struct vfio_iommu_spapr_tce_info *info);
+
+#endif
Signature · 1 line
2.0.0
#15 lore raw

Re: [Qemu-devel] [PATCH v7 2/4] vfio: Add vfio_container_spapr_get_info()

Quoted 9 lines · Alexey Kardashevskiy

To perform DMA mapping via TCE table correctly, the guest must know where DMA window is located on the PCI bus. A hypervisor is expected to provide such information. Since QEMU has no control over this setting, we need a way to obtain a start address and size from the host VFIO driver.

This adds a helper which returns the default DMA window properties for the specific IOMMU group. The upstream kernel implements this ioctl already.

Couldn't this be done with Gavin's vfio_pci_container_ioctl()? Thanks,

Alex

Quoted 89 lines

---
Changes:
v7:
* do not return a group fd from the helper

v6:
* added dup() to protect group_fd from accidental disposal

v5:
* reworked to reflect change in vfio_get_group() from one of previous patches change

v4:
* fixed possible leaks on error paths

---
 hw/misc/vfio.c         | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/misc/vfio.h | 11 +++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 include/hw/misc/vfio.h
hw/misc/vfio.c +36 -0
index 7437c2e..99141f3 100644
@@ -39,6 +39,7 @@
 #include "qemu/range.h"
 #include "sysemu/kvm.h"
 #include "sysemu/sysemu.h"
+#include "hw/misc/vfio.h"
 
 /* #define DEBUG_VFIO */
 #ifdef DEBUG_VFIO
@@ -4318,3 +4319,38 @@ static void register_vfio_pci_dev_type(void)
 }
 
 type_init(register_vfio_pci_dev_type)
+
+int vfio_container_spapr_get_info(AddressSpace *as,
+                                  int32_t groupid,
+                                  struct vfio_iommu_spapr_tce_info *info)
+{
+    VFIOGroup *group;
+    VFIOContainer *container;
+    int ret, fd;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        return -1;
+    }
+    container = group->container;
+    if (!group->container) {
+        goto put_group_exit;
+    }
+    fd = container->fd;
+    if (!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
+        goto put_group_exit;
+    }
+    ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, info);
+    if (ret) {
+        error_report("vfio: failed to get iommu info for container: %s",
+                     strerror(errno));
+        goto put_group_exit;
+    }
+
+    return 0;
+
+put_group_exit:
+    vfio_put_group(group);
+
+    return -1;
+}
include/hw/misc/vfio.h new file +11 -0
new file mode 100644
index 0000000..e82f5a3
@@ -0,0 +1,11 @@
+#ifndef VFIO_API_H
+#define VFIO_API_H
+
+#include "qemu/typedefs.h"
+#include <linux/vfio.h>
+
+extern int vfio_container_spapr_get_info(AddressSpace *as,
+                                         int32_t groupid,
+                                         struct vfio_iommu_spapr_tce_info *info);
+
+#endif
#16 lore raw

Re: [Qemu-devel] [PATCH v7 2/4] vfio: Add vfio_container_spapr_get_info()

Quoted 7 lines · Alex Williamson
Quoted 6 lines · Alexey Kardashevskiy

To perform DMA mapping via TCE table correctly, the guest must know where DMA window is located on the PCI bus. A hypervisor is expected to provide such information. Since QEMU has no control over this setting, we need a way to obtain a start address and size from the host VFIO driver.

Quoted 5 lines
Quoted 3 lines

This adds a helper which returns the default DMA window properties for the specific IOMMU group. The upstream kernel implements this ioctl already.

Couldn't this be done with Gavin's vfio_pci_container_ioctl()? Thanks,

Good point, I missed that. I'll merge two helpers into one and repost, then Gavin will use it.

Quoted 96 lines

Alex

Quoted 89 lines

---
Changes:
v7:
* do not return a group fd from the helper

v6:
* added dup() to protect group_fd from accidental disposal

v5:
* reworked to reflect change in vfio_get_group() from one of previous patches change

v4:
* fixed possible leaks on error paths

---
 hw/misc/vfio.c         | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/misc/vfio.h | 11 +++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 include/hw/misc/vfio.h
hw/misc/vfio.c +36 -0
index 7437c2e..99141f3 100644
@@ -39,6 +39,7 @@
 #include "qemu/range.h"
 #include "sysemu/kvm.h"
 #include "sysemu/sysemu.h"
+#include "hw/misc/vfio.h"
 
 /* #define DEBUG_VFIO */
 #ifdef DEBUG_VFIO
@@ -4318,3 +4319,38 @@ static void register_vfio_pci_dev_type(void)
 }
 
 type_init(register_vfio_pci_dev_type)
+
+int vfio_container_spapr_get_info(AddressSpace *as,
+                                  int32_t groupid,
+                                  struct vfio_iommu_spapr_tce_info *info)
+{
+    VFIOGroup *group;
+    VFIOContainer *container;
+    int ret, fd;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        return -1;
+    }
+    container = group->container;
+    if (!group->container) {
+        goto put_group_exit;
+    }
+    fd = container->fd;
+    if (!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
+        goto put_group_exit;
+    }
+    ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, info);
+    if (ret) {
+        error_report("vfio: failed to get iommu info for container: %s",
+                     strerror(errno));
+        goto put_group_exit;
+    }
+
+    return 0;
+
+put_group_exit:
+    vfio_put_group(group);
+
+    return -1;
+}
include/hw/misc/vfio.h new file +11 -0
new file mode 100644
index 0000000..e82f5a3
@@ -0,0 +1,11 @@
+#ifndef VFIO_API_H
+#define VFIO_API_H
+
+#include "qemu/typedefs.h"
+#include <linux/vfio.h>
+
+extern int vfio_container_spapr_get_info(AddressSpace *as,
+                                         int32_t groupid,
+                                         struct vfio_iommu_spapr_tce_info *info);
+
+#endif
Signature · 1 line
Alexey
#17 lore raw

Re: [Qemu-devel] [PATCH v7 2/4] vfio: Add vfio_container_spapr_get_info()

Quoted 12 lines · Alexey Kardashevskiy
Quoted 11 lines · Alex Williamson
Quoted 9 lines · Alexey Kardashevskiy

To perform DMA mapping via TCE table correctly, the guest must know where DMA window is located on the PCI bus. A hypervisor is expected to provide such information. Since QEMU has no control over this setting, we need a way to obtain a start address and size from the host VFIO driver.

This adds a helper which returns the default DMA window properties for the specific IOMMU group. The upstream kernel implements this ioctl already.

Quoted 6 lines
Quoted 1 line

Couldn't this be done with Gavin's vfio_pci_container_ioctl()? Thanks,

Good point, I missed that. I'll merge two helpers into one and repost, then Gavin will use it.

Sure, I'll rebase my code on top of Alexey's. But Alex raised more comments about the function. I just copy & paste so that we don't miss the comments:

- "fd == 0" is valid
- In addition to fd 0 being valid, there's some white space issues here.

Passing an integer option is not very extensible, maybe a void* that gets cast to an int* for VFIO_EEH_PE_OP would be better. It's a qemu internal API though, so I'm not going to sweat saving that problem for the next user. Thanks,

Thanks,
Gavin

Quoted 101 lines
Quoted 96 lines

Alex

Quoted 89 lines

---
Changes:
v7:
* do not return a group fd from the helper

v6:
* added dup() to protect group_fd from accidental disposal

v5:
* reworked to reflect change in vfio_get_group() from one of previous patches change

v4:
* fixed possible leaks on error paths

---
 hw/misc/vfio.c         | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/misc/vfio.h | 11 +++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 include/hw/misc/vfio.h
hw/misc/vfio.c +36 -0
index 7437c2e..99141f3 100644
@@ -39,6 +39,7 @@
 #include "qemu/range.h"
 #include "sysemu/kvm.h"
 #include "sysemu/sysemu.h"
+#include "hw/misc/vfio.h"
 
 /* #define DEBUG_VFIO */
 #ifdef DEBUG_VFIO
@@ -4318,3 +4319,38 @@ static void register_vfio_pci_dev_type(void)
 }
 
 type_init(register_vfio_pci_dev_type)
+
+int vfio_container_spapr_get_info(AddressSpace *as,
+                                  int32_t groupid,
+                                  struct vfio_iommu_spapr_tce_info *info)
+{
+    VFIOGroup *group;
+    VFIOContainer *container;
+    int ret, fd;
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        return -1;
+    }
+    container = group->container;
+    if (!group->container) {
+        goto put_group_exit;
+    }
+    fd = container->fd;
+    if (!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
+        goto put_group_exit;
+    }
+    ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, info);
+    if (ret) {
+        error_report("vfio: failed to get iommu info for container: %s",
+                     strerror(errno));
+        goto put_group_exit;
+    }
+
+    return 0;
+
+put_group_exit:
+    vfio_put_group(group);
+
+    return -1;
+}
include/hw/misc/vfio.h new file +11 -0
new file mode 100644
index 0000000..e82f5a3
@@ -0,0 +1,11 @@
+#ifndef VFIO_API_H
+#define VFIO_API_H
+
+#include "qemu/typedefs.h"
+#include <linux/vfio.h>
+
+extern int vfio_container_spapr_get_info(AddressSpace *as,
+                                         int32_t groupid,
+                                         struct vfio_iommu_spapr_tce_info *info);
+
+#endif
Signature · 1 line
Alexey
#18 lore raw

PATCH v7 3/4 spapr_pci_vfio: Add spapr-pci-vfio-host-bridge to support vfio

In reply to Alexey Kardashevskiy

The patch adds a spapr-pci-vfio-host-bridge device type which is a PCI Host Bridge with VFIO support. The new device inherits from the spapr-pci-host-bridge device and adds an "iommu" property which is an IOMMU id. This ID represents a minimal entity for which IOMMU isolation can be guaranteed. In SPAPR architecture IOMMU group is called a Partitionable Endpoint (PE).

Current implementation supports one IOMMU id per QEMU VFIO PHB. Since SPAPR allows multiple PHB for no extra cost, this does not seem to be a problem. This limitation may change in the future though.

Example of use:
Configure and Add 3 functions of a multifunctional device to QEMU:
(the NEC PCI USB card is used as an example here):
-device spapr-pci-vfio-host-bridge,id=USB,iommu=4,index=7 \
-device vfio-pci,host=4:0:1.0,addr=1.0,bus=USB,multifunction=true -device vfio-pci,host=4:0:1.1,addr=1.1,bus=USB
-device vfio-pci,host=4:0:1.2,addr=1.2,bus=USB

where:
* index=7 is a QEMU PHB index (used as source for MMIO/MSI/IO windows offset);
* iommu=4 is an IOMMU id which can be found in sysfs:
[aik@vpl2 ~]$ cd /sys/bus/pci/devices/0004:00:00.0/
[aik@vpl2 0004:00:00.0]$ ls -l iommu_group
lrwxrwxrwx 1 root root 0 Jun 5 12:49 iommu_group -> ../../../kernel/iommu_groups/4

---
Changes:
v7:
* remove bunch of properties from VFIO PHB such as "scan", "multifunction", "force_addr" - let management softwsare deal with it
* removed traces used in scan() (which is also removed)
* updated license
* disables in-kernel TCE table ("false" in spapr_tce_new_table())

v5:
* added handling of possible failure of spapr_vfio_new_table()

v4:
* moved IOMMU changes to separate patches
* moved spapr-pci-vfio-host-bridge to new file

---
 hw/ppc/Makefile.objs        |  3 ++
 hw/ppc/spapr_pci_vfio.c     | 93 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/pci-host/spapr.h | 11 ++++++
 3 files changed, 107 insertions(+)
 create mode 100644 hw/ppc/spapr_pci_vfio.c
hw/ppc/Makefile.objs +3 -0
index ea747f0..edd44d0 100644
@@ -4,6 +4,9 @@ obj-y += ppc.o ppc_booke.o
 obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
 obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
 obj-$(CONFIG_PSERIES) += spapr_pci.o
+ifeq ($(CONFIG_PCI)$(CONFIG_PSERIES)$(CONFIG_LINUX), yyy)
+obj-y += spapr_pci_vfio.o
+endif
 # PowerPC 4xx boards
 obj-y += ppc405_boards.o ppc4xx_devs.o ppc405_uc.o ppc440_bamboo.o
 obj-y += ppc4xx_pci.o
hw/ppc/spapr_pci_vfio.c new file +93 -0

93 lines not on this page · raw message

include/hw/pci-host/spapr.h +11 -0
index 0934518..6808e96 100644
@@ -30,10 +30,14 @@
 #define SPAPR_MSIX_MAX_DEVS 32
 
 #define TYPE_SPAPR_PCI_HOST_BRIDGE "spapr-pci-host-bridge"
+#define TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE "spapr-pci-vfio-host-bridge"
 
 #define SPAPR_PCI_HOST_BRIDGE(obj) \
     OBJECT_CHECK(sPAPRPHBState, (obj), TYPE_SPAPR_PCI_HOST_BRIDGE)
 
+#define SPAPR_PCI_VFIO_HOST_BRIDGE(obj) \
+    OBJECT_CHECK(sPAPRPHBVFIOState, (obj), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)
+
 #define SPAPR_PCI_HOST_BRIDGE_CLASS(klass) \
      OBJECT_CLASS_CHECK(sPAPRPHBClass, (klass), TYPE_SPAPR_PCI_HOST_BRIDGE)
 #define SPAPR_PCI_HOST_BRIDGE_GET_CLASS(obj) \
@@ -41,6 +45,7 @@
 
 typedef struct sPAPRPHBClass sPAPRPHBClass;
 typedef struct sPAPRPHBState sPAPRPHBState;
+typedef struct sPAPRPHBVFIOState sPAPRPHBVFIOState;
 
 struct sPAPRPHBClass {
     PCIHostBridgeClass parent_class;
@@ -76,6 +81,12 @@ struct sPAPRPHBState {
     QLIST_ENTRY(sPAPRPHBState) list;
 };
 
+struct sPAPRPHBVFIOState {
+    sPAPRPHBState phb;
+
+    int32_t iommugroupid;
+};
+
 #define SPAPR_PCI_BASE_BUID          0x800000020000000ULL
 
 #define SPAPR_PCI_WINDOW_BASE        0x10000000000ULL
Signature · 1 line
2.0.0
#19 lore raw

Re: [Qemu-devel] [PATCH v7 3/4] spapr_pci_vfio: Add spapr-pci-vfio-host-bridge to support vfio

Quoted 111 lines · Alexey Kardashevskiy

The patch adds a spapr-pci-vfio-host-bridge device type which is a PCI Host Bridge with VFIO support. The new device inherits from the spapr-pci-host-bridge device and adds an "iommu" property which is an IOMMU id. This ID represents a minimal entity for which IOMMU isolation can be guaranteed. In SPAPR architecture IOMMU group is called a Partitionable Endpoint (PE).

Current implementation supports one IOMMU id per QEMU VFIO PHB. Since SPAPR allows multiple PHB for no extra cost, this does not seem to be a problem. This limitation may change in the future though.

Example of use:
Configure and Add 3 functions of a multifunctional device to QEMU:
(the NEC PCI USB card is used as an example here):

(quoted diff) +0 -4
-device spapr-pci-vfio-host-bridge,id=USB,iommu=4,index=7 \
-device vfio-pci,host=4:0:1.0,addr=1.0,bus=USB,multifunction=true
-device vfio-pci,host=4:0:1.1,addr=1.1,bus=USB
-device vfio-pci,host=4:0:1.2,addr=1.2,bus=USB

where:
* index=7 is a QEMU PHB index (used as source for MMIO/MSI/IO windows offset);
* iommu=4 is an IOMMU id which can be found in sysfs:
[aik@vpl2 ~]$ cd /sys/bus/pci/devices/0004:00:00.0/
[aik@vpl2 0004:00:00.0]$ ls -l iommu_group
lrwxrwxrwx 1 root root 0 Jun 5 12:49 iommu_group -> ../../../kernel/iommu_groups/4

---
Changes:
v7:
* remove bunch of properties from VFIO PHB such as "scan", "multifunction", "force_addr" - let management softwsare deal with it
* removed traces used in scan() (which is also removed)
* updated license
* disables in-kernel TCE table ("false" in spapr_tce_new_table())

v5:
* added handling of possible failure of spapr_vfio_new_table()

v4:
* moved IOMMU changes to separate patches
* moved spapr-pci-vfio-host-bridge to new file

---
  hw/ppc/Makefile.objs        |  3 ++
  hw/ppc/spapr_pci_vfio.c     | 93 +++++++++++++++++++++++++++++++++++++++++++++
  include/hw/pci-host/spapr.h | 11 ++++++
  3 files changed, 107 insertions(+)

create mode 100644 hw/ppc/spapr_pci_vfio.c

hw/ppc/Makefile.objs +3 -0
index ea747f0..edd44d0 100644
@@ -4,6 +4,9 @@ obj-y += ppc.o ppc_booke.o
  obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
  obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
  obj-$(CONFIG_PSERIES) += spapr_pci.o
+ifeq ($(CONFIG_PCI)$(CONFIG_PSERIES)$(CONFIG_LINUX), yyy)
+obj-y += spapr_pci_vfio.o
+endif
  # PowerPC 4xx boards
  obj-y += ppc405_boards.o ppc4xx_devs.o ppc405_uc.o ppc440_bamboo.o
  obj-y += ppc4xx_pci.o
hw/ppc/spapr_pci_vfio.c new file +41 -0
new file mode 100644
index 0000000..ec52a18
@@ -0,0 +1,93 @@
+/*
+ * QEMU sPAPR PCI host for VFIO
+ *
+ * Copyright (c) 2011-2014 Alexey Kardashevskiy, IBM Corporation.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License,
+ *  or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "hw/ppc/spapr.h"
+#include "hw/pci-host/spapr.h"
+#include "hw/misc/vfio.h"
+
+static Property spapr_phb_vfio_properties[] = {
+    DEFINE_PROP_INT32("iommu", sPAPRPHBVFIOState, iommugroupid, -1),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void spapr_phb_vfio_finish_realize(sPAPRPHBState *sphb, Error **errp)
+{
+    sPAPRPHBVFIOState *svphb = SPAPR_PCI_VFIO_HOST_BRIDGE(sphb);
+    struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
+    int ret;
+    sPAPRTCETable *tcet;
+    uint32_t liobn = svphb->phb.dma_liobn;
+
+    if (svphb->iommugroupid == -1) {
+        error_setg(errp, "Wrong IOMMU group ID %d", svphb->iommugroupid);
+        return;
+    }
+

hw/ppc/spapr_pci_vfio.c line 51

Quoted 10 lines
hw/ppc/spapr_pci_vfio.c new file +10 -0
+    ret = vfio_container_spapr_get_info(&svphb->phb.iommu_as,
+                                        svphb->iommugroupid,
+                                        &info);
+    if (ret) {
+        error_setg_errno(errp, -ret,
+                         "spapr-vfio: get info from container failed");
+        return;
+    }
+
+    tcet = spapr_tce_new_table(DEVICE(sphb), liobn, info.dma32_window_start,

So what happens when the device gets a different offset? Will it write into other devices or will DMAs just get ignored?

Alex

Quoted 82 lines
hw/ppc/spapr_pci_vfio.c new file +42 -0
+                               SPAPR_TCE_PAGE_SHIFT,
+                               info.dma32_window_size >> SPAPR_TCE_PAGE_SHIFT,
+                               false);
+    if (!tcet) {
+        error_setg(errp, "spapr-vfio: failed to create VFIO TCE table");
+        return;
+    }
+
+    /* Register default 32bit DMA window */
+    memory_region_add_subregion(&sphb->iommu_root, tcet->bus_offset,
+                                spapr_tce_get_iommu(tcet));
+}
+
+static void spapr_phb_vfio_reset(DeviceState *qdev)
+{
+    /* Do nothing */
+}
+
+static void spapr_phb_vfio_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    sPAPRPHBClass *spc = SPAPR_PCI_HOST_BRIDGE_CLASS(klass);
+
+    dc->props = spapr_phb_vfio_properties;
+    dc->reset = spapr_phb_vfio_reset;
+    spc->finish_realize = spapr_phb_vfio_finish_realize;
+}
+
+static const TypeInfo spapr_phb_vfio_info = {
+    .name          = TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE,
+    .parent        = TYPE_SPAPR_PCI_HOST_BRIDGE,
+    .instance_size = sizeof(sPAPRPHBVFIOState),
+    .class_init    = spapr_phb_vfio_class_init,
+    .class_size    = sizeof(sPAPRPHBClass),
+};
+
+static void spapr_pci_vfio_register_types(void)
+{
+    type_register_static(&spapr_phb_vfio_info);
+}
+
+type_init(spapr_pci_vfio_register_types)
include/hw/pci-host/spapr.h +11 -0
index 0934518..6808e96 100644
@@ -30,10 +30,14 @@
  #define SPAPR_MSIX_MAX_DEVS 32
 
  #define TYPE_SPAPR_PCI_HOST_BRIDGE "spapr-pci-host-bridge"
+#define TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE "spapr-pci-vfio-host-bridge"
 
  #define SPAPR_PCI_HOST_BRIDGE(obj) \
      OBJECT_CHECK(sPAPRPHBState, (obj), TYPE_SPAPR_PCI_HOST_BRIDGE)
 
+#define SPAPR_PCI_VFIO_HOST_BRIDGE(obj) \
+    OBJECT_CHECK(sPAPRPHBVFIOState, (obj), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)
+
  #define SPAPR_PCI_HOST_BRIDGE_CLASS(klass) \
       OBJECT_CLASS_CHECK(sPAPRPHBClass, (klass), TYPE_SPAPR_PCI_HOST_BRIDGE)
  #define SPAPR_PCI_HOST_BRIDGE_GET_CLASS(obj) \
@@ -41,6 +45,7 @@
 
  typedef struct sPAPRPHBClass sPAPRPHBClass;
  typedef struct sPAPRPHBState sPAPRPHBState;
+typedef struct sPAPRPHBVFIOState sPAPRPHBVFIOState;
 
  struct sPAPRPHBClass {
      PCIHostBridgeClass parent_class;
@@ -76,6 +81,12 @@ struct sPAPRPHBState {
      QLIST_ENTRY(sPAPRPHBState) list;
  };
 
+struct sPAPRPHBVFIOState {
+    sPAPRPHBState phb;
+
+    int32_t iommugroupid;
+};
+
  #define SPAPR_PCI_BASE_BUID          0x800000020000000ULL
 
  #define SPAPR_PCI_WINDOW_BASE        0x10000000000ULL
#20 lore raw

Re: [Qemu-devel] [PATCH v7 3/4] spapr_pci_vfio: Add spapr-pci-vfio-host-bridge to support vfio

Quoted 122 lines · Alexander Graf
Quoted 120 lines · Alexey Kardashevskiy

The patch adds a spapr-pci-vfio-host-bridge device type which is a PCI Host Bridge with VFIO support. The new device inherits from the spapr-pci-host-bridge device and adds an "iommu" property which is an IOMMU id. This ID represents a minimal entity for which IOMMU isolation can be guaranteed. In SPAPR architecture IOMMU group is called a Partitionable Endpoint (PE).

Current implementation supports one IOMMU id per QEMU VFIO PHB. Since SPAPR allows multiple PHB for no extra cost, this does not seem to be a problem. This limitation may change in the future though.

Example of use:
Configure and Add 3 functions of a multifunctional device to QEMU:
(the NEC PCI USB card is used as an example here):

(quoted diff) +0 -4
-device spapr-pci-vfio-host-bridge,id=USB,iommu=4,index=7 \
-device vfio-pci,host=4:0:1.0,addr=1.0,bus=USB,multifunction=true
-device vfio-pci,host=4:0:1.1,addr=1.1,bus=USB
-device vfio-pci,host=4:0:1.2,addr=1.2,bus=USB

where:
* index=7 is a QEMU PHB index (used as source for MMIO/MSI/IO windows offset);
* iommu=4 is an IOMMU id which can be found in sysfs:
[aik@vpl2 ~]$ cd /sys/bus/pci/devices/0004:00:00.0/ [aik@vpl2 0004:00:00.0]$ ls -l iommu_group
lrwxrwxrwx 1 root root 0 Jun 5 12:49 iommu_group -> ../../../kernel/iommu_groups/4

---
Changes:
v7:
* remove bunch of properties from VFIO PHB such as "scan", "multifunction", "force_addr" - let management softwsare deal with it
* removed traces used in scan() (which is also removed)
* updated license
* disables in-kernel TCE table ("false" in spapr_tce_new_table())

v5:
* added handling of possible failure of spapr_vfio_new_table()

v4:
* moved IOMMU changes to separate patches
* moved spapr-pci-vfio-host-bridge to new file

---
  hw/ppc/Makefile.objs        |  3 ++
  hw/ppc/spapr_pci_vfio.c     | 93
+++++++++++++++++++++++++++++++++++++++++++++
  include/hw/pci-host/spapr.h | 11 ++++++
  3 files changed, 107 insertions(+)

create mode 100644 hw/ppc/spapr_pci_vfio.c

hw/ppc/Makefile.objs +3 -0
index ea747f0..edd44d0 100644
@@ -4,6 +4,9 @@ obj-y += ppc.o ppc_booke.o
  obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
  obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
  obj-$(CONFIG_PSERIES) += spapr_pci.o
+ifeq ($(CONFIG_PCI)$(CONFIG_PSERIES)$(CONFIG_LINUX), yyy)
+obj-y += spapr_pci_vfio.o
+endif
  # PowerPC 4xx boards
  obj-y += ppc405_boards.o ppc4xx_devs.o ppc405_uc.o ppc440_bamboo.o
  obj-y += ppc4xx_pci.o
hw/ppc/spapr_pci_vfio.c new file +29 -0
new file mode 100644
index 0000000..ec52a18
@@ -0,0 +1,93 @@
+/*
+ * QEMU sPAPR PCI host for VFIO
+ *
+ * Copyright (c) 2011-2014 Alexey Kardashevskiy, IBM Corporation.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License,
+ *  or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "hw/ppc/spapr.h"
+#include "hw/pci-host/spapr.h"
+#include "hw/misc/vfio.h"
+
+static Property spapr_phb_vfio_properties[] = {
+    DEFINE_PROP_INT32("iommu", sPAPRPHBVFIOState, iommugroupid, -1),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void spapr_phb_vfio_finish_realize(sPAPRPHBState *sphb, Error

**errp)

hw/ppc/spapr_pci_vfio.c new file +18 -0
+{
+    sPAPRPHBVFIOState *svphb = SPAPR_PCI_VFIO_HOST_BRIDGE(sphb);
+    struct vfio_iommu_spapr_tce_info info = { .argsz = sizeof(info) };
+    int ret;
+    sPAPRTCETable *tcet;
+    uint32_t liobn = svphb->phb.dma_liobn;
+
+    if (svphb->iommugroupid == -1) {
+        error_setg(errp, "Wrong IOMMU group ID %d", svphb->iommugroupid);
+        return;
+    }
+
+    ret = vfio_container_spapr_get_info(&svphb->phb.iommu_as,
+                                        svphb->iommugroupid,
+                                        &info);
+    if (ret) {
+        error_setg_errno(errp, -ret,
+                         "spapr-vfio: get info from container failed");
Quoted 8 lines
Quoted 5 lines
hw/ppc/spapr_pci_vfio.c new file +4 -0
+        return;
+    }
+
+    tcet = spapr_tce_new_table(DEVICE(sphb), liobn,

info.dma32_window_start,

So what happens when the device gets a different offset? Will it write into other devices or will DMAs just get ignored?

The device gets just an address which it will do DMA to/from. "Window" means here that PHB (or PCI bridge) is configured not to block access to that window and block accesses outside the window. And the window can only be configured on the host size where real PHB is available for programming, we do this in powernv code (IODA, IODA2, P7IOC, P7IOC2 - these PHBs).

Quoted 82 lines

Alex

Quoted 77 lines
hw/ppc/spapr_pci_vfio.c new file +2 -0
+                               SPAPR_TCE_PAGE_SHIFT,
+                               info.dma32_window_size >>

SPAPR_TCE_PAGE_SHIFT,

hw/ppc/spapr_pci_vfio.c new file +40 -0
+                               false);
+    if (!tcet) {
+        error_setg(errp, "spapr-vfio: failed to create VFIO TCE table");
+        return;
+    }
+
+    /* Register default 32bit DMA window */
+    memory_region_add_subregion(&sphb->iommu_root, tcet->bus_offset,
+                                spapr_tce_get_iommu(tcet));
+}
+
+static void spapr_phb_vfio_reset(DeviceState *qdev)
+{
+    /* Do nothing */
+}
+
+static void spapr_phb_vfio_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    sPAPRPHBClass *spc = SPAPR_PCI_HOST_BRIDGE_CLASS(klass);
+
+    dc->props = spapr_phb_vfio_properties;
+    dc->reset = spapr_phb_vfio_reset;
+    spc->finish_realize = spapr_phb_vfio_finish_realize;
+}
+
+static const TypeInfo spapr_phb_vfio_info = {
+    .name          = TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE,
+    .parent        = TYPE_SPAPR_PCI_HOST_BRIDGE,
+    .instance_size = sizeof(sPAPRPHBVFIOState),
+    .class_init    = spapr_phb_vfio_class_init,
+    .class_size    = sizeof(sPAPRPHBClass),
+};
+
+static void spapr_pci_vfio_register_types(void)
+{
+    type_register_static(&spapr_phb_vfio_info);
+}
+
+type_init(spapr_pci_vfio_register_types)
include/hw/pci-host/spapr.h +9 -0
index 0934518..6808e96 100644
@@ -30,10 +30,14 @@
  #define SPAPR_MSIX_MAX_DEVS 32
    #define TYPE_SPAPR_PCI_HOST_BRIDGE "spapr-pci-host-bridge"
+#define TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE "spapr-pci-vfio-host-bridge"
    #define SPAPR_PCI_HOST_BRIDGE(obj) \
      OBJECT_CHECK(sPAPRPHBState, (obj), TYPE_SPAPR_PCI_HOST_BRIDGE)
  +#define SPAPR_PCI_VFIO_HOST_BRIDGE(obj) \
+    OBJECT_CHECK(sPAPRPHBVFIOState, (obj), TYPE_SPAPR_PCI_VFIO_HOST_BRIDGE)
+
  #define SPAPR_PCI_HOST_BRIDGE_CLASS(klass) \
       OBJECT_CLASS_CHECK(sPAPRPHBClass, (klass),
 TYPE_SPAPR_PCI_HOST_BRIDGE)
  #define SPAPR_PCI_HOST_BRIDGE_GET_CLASS(obj) \
@@ -41,6 +45,7 @@
    typedef struct sPAPRPHBClass sPAPRPHBClass;
  typedef struct sPAPRPHBState sPAPRPHBState;
+typedef struct sPAPRPHBVFIOState sPAPRPHBVFIOState;
    struct sPAPRPHBClass {
      PCIHostBridgeClass parent_class;
@@ -76,6 +81,12 @@ struct sPAPRPHBState {
      QLIST_ENTRY(sPAPRPHBState) list;
  };
  +struct sPAPRPHBVFIOState {
+    sPAPRPHBState phb;
+
+    int32_t iommugroupid;
+};
+
  #define SPAPR_PCI_BASE_BUID          0x800000020000000ULL
    #define SPAPR_PCI_WINDOW_BASE        0x10000000000ULL
Signature · 1 line
Alexey
#21 lore raw

PATCH v7 4/4 vfio: Enable for spapr

In reply to Alexey Kardashevskiy

This turns the sPAPR support on and enables VFIO container use in the kernel.

This extends vfio_connect_container to support VFIO_SPAPR_TCE_IOMMU type in the host kernel.

This registers a memory listener which sPAPR IOMMU will notify when executing H_PUT_TCE/etc DMA calls. The listener then will notify the host kernel about DMA map/unmap operation via VFIO_IOMMU_MAP_DMA/ VFIO_IOMMU_UNMAP_DMA ioctls.

This executes VFIO_IOMMU_ENABLE ioctl to make sure that the IOMMU is free of mappings and can be exclusively given to the user. At the moment SPAPR is the only platform requiring this call to be implemented.

---
Changes:
v7:
* added more details in commit log

v5:
* multiple returns converted to gotos

v4:
* fixed format string to use %m which is a glibc extension:
"Print output of strerror(errno). No argument is required."

---
 hw/misc/vfio.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
hw/misc/vfio.c +28 -0
index 99141f3..c8e3aff 100644
@@ -3650,6 +3650,34 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as)
 
         container->iommu_data.type1.initialized = true;
 
+    } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
+        ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
+        if (ret) {
+            error_report("vfio: failed to set group container: %m");
+            ret = -errno;
+            goto free_container_exit;
+        }
+
+        ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_SPAPR_TCE_IOMMU);
+        if (ret) {
+            error_report("vfio: failed to set iommu for container: %m");
+            ret = -errno;
+            goto free_container_exit;
+        }
+
+        ret = ioctl(fd, VFIO_IOMMU_ENABLE);
+        if (ret) {
+            error_report("vfio: failed to enable container: %m");
+            ret = -errno;
+            goto free_container_exit;
+        }
+
+        container->iommu_data.type1.listener = vfio_memory_listener;
+        container->iommu_data.release = vfio_listener_release;
+
+        memory_listener_register(&container->iommu_data.type1.listener,
+                                 container->space->as);
+
     } else {
         error_report("vfio: No available IOMMU models");
         ret = -EINVAL;
Signature · 1 line
2.0.0
#22 lore raw

Re: [Qemu-devel] [PATCH v7 4/4] vfio: Enable for spapr

Quoted 45 lines · Alexey Kardashevskiy

This turns the sPAPR support on and enables VFIO container use in the kernel.

This extends vfio_connect_container to support VFIO_SPAPR_TCE_IOMMU type in the host kernel.

This registers a memory listener which sPAPR IOMMU will notify when executing H_PUT_TCE/etc DMA calls. The listener then will notify the host kernel about DMA map/unmap operation via VFIO_IOMMU_MAP_DMA/ VFIO_IOMMU_UNMAP_DMA ioctls.

This executes VFIO_IOMMU_ENABLE ioctl to make sure that the IOMMU is free of mappings and can be exclusively given to the user. At the moment SPAPR is the only platform requiring this call to be implemented.

---
Changes:
v7:
* added more details in commit log

v5:
* multiple returns converted to gotos

v4:
* fixed format string to use %m which is a glibc extension:
"Print output of strerror(errno). No argument is required."

---
 hw/misc/vfio.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
hw/misc/vfio.c +6 -0
index 99141f3..c8e3aff 100644
@@ -3650,6 +3650,34 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as)
 
         container->iommu_data.type1.initialized = true;
 
+    } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
+        ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
+        if (ret) {
+            error_report("vfio: failed to set group container: %m");
+            ret = -errno;
+            goto free_container_exit;

hw/misc/vfio.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as) · line 3668

Quoted 10 lines
hw/misc/vfio.c +10 -0
+        }
+
+        ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_SPAPR_TCE_IOMMU);
+        if (ret) {
+            error_report("vfio: failed to set iommu for container: %m");
+            ret = -errno;
+            goto free_container_exit;
+        }
+
+        ret = ioctl(fd, VFIO_IOMMU_ENABLE);

Where's the matching DISABLE? Do we need a different release wrapper that includes a disable? Thanks,

Alex

Quoted 15 lines
hw/misc/vfio.c +12 -0
+        if (ret) {
+            error_report("vfio: failed to enable container: %m");
+            ret = -errno;
+            goto free_container_exit;
+        }
+
+        container->iommu_data.type1.listener = vfio_memory_listener;
+        container->iommu_data.release = vfio_listener_release;
+
+        memory_listener_register(&container->iommu_data.type1.listener,
+                                 container->space->as);
+
     } else {
         error_report("vfio: No available IOMMU models");
         ret = -EINVAL;
#23 lore raw

Re: [Qemu-devel] [PATCH v7 4/4] vfio: Enable for spapr

Quoted 51 lines · Alex Williamson
Quoted 50 lines · Alexey Kardashevskiy

This turns the sPAPR support on and enables VFIO container use in the kernel.

This extends vfio_connect_container to support VFIO_SPAPR_TCE_IOMMU type in the host kernel.

This registers a memory listener which sPAPR IOMMU will notify when executing H_PUT_TCE/etc DMA calls. The listener then will notify the host kernel about DMA map/unmap operation via VFIO_IOMMU_MAP_DMA/ VFIO_IOMMU_UNMAP_DMA ioctls.

This executes VFIO_IOMMU_ENABLE ioctl to make sure that the IOMMU is free of mappings and can be exclusively given to the user. At the moment SPAPR is the only platform requiring this call to be implemented.

---
Changes:
v7:
* added more details in commit log

v5:
* multiple returns converted to gotos

v4:
* fixed format string to use %m which is a glibc extension:
"Print output of strerror(errno). No argument is required."

---
 hw/misc/vfio.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
hw/misc/vfio.c +11 -0
index 99141f3..c8e3aff 100644
@@ -3650,6 +3650,34 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as)
 
         container->iommu_data.type1.initialized = true;
 
+    } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
+        ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
+        if (ret) {
+            error_report("vfio: failed to set group container: %m");
+            ret = -errno;
+            goto free_container_exit;
+        }
+
+        ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_SPAPR_TCE_IOMMU);
+        if (ret) {
+            error_report("vfio: failed to set iommu for container: %m");
Quoted 8 lines

hw/misc/vfio.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as) · line 3668

Quoted 5 lines
hw/misc/vfio.c +5 -0
+            ret = -errno;
+            goto free_container_exit;
+        }
+
+        ret = ioctl(fd, VFIO_IOMMU_ENABLE);

Where's the matching DISABLE? Do we need a different release wrapper that includes a disable? Thanks,

It happens in the host kernel when container fd is closed. I added VFIO_IOMMU_DISABLE for symmetry but do not really need it.

Quoted 21 lines

Alex

Quoted 15 lines
hw/misc/vfio.c +12 -0
+        if (ret) {
+            error_report("vfio: failed to enable container: %m");
+            ret = -errno;
+            goto free_container_exit;
+        }
+
+        container->iommu_data.type1.listener = vfio_memory_listener;
+        container->iommu_data.release = vfio_listener_release;
+
+        memory_listener_register(&container->iommu_data.type1.listener,
+                                 container->space->as);
+
     } else {
         error_report("vfio: No available IOMMU models");
         ret = -EINVAL;
Signature · 1 line
Alexey