Avoid contention for PCIIOMMUOps between IOMMU and PCIe host
cover Avoid contention for PCIIOMMUOps between IOMMU and PCIe host
When PCIIOMMUOps.get_address_space() is already registered by the pci host, e.g. TYPE_DESIGNWARE_PCIE_HOST, IOMMU cannot overwrite this hook without breaking the PCIe translation rule, which means that IOMMU and the pci host cannot coexist.
This RFC introduces a new callback, PCIIOMMUOps.set_memory_region(), and an API, pci_setup_iommu_downstream_mem, to avoid the contention for PCIIOMMUOps.get_address_space().
Jason Chien (6):
include/hw/pci: Add a callback to set the downstream memory region of
a pci bus
hw/pci: Add an API to set the downstream memory region of a PCI bus hw/pci-host: Enable DW PCIe host to send memory transactions over
specific mr
hw/riscv/riscv-iommu: Allow PCI hosts with iommu_ops registered
connecting to the IOMMU
hw/riscv/riscv-iommu: Acquire device IDs dynamically include/hw/pci: Send PCI dma requests with memory attributes
containing BDF
hw/pci-host/designware.c | 18 +++++++++++++++--- hw/pci/pci.c | 6 ++++++ hw/riscv/riscv-iommu.c | 21 +++++++++++++++++++-- include/hw/pci-host/designware.h | 2 ++ include/hw/pci/pci.h | 20 ++++++++++++++++++++ include/hw/pci/pci_device.h | 15 ++++++++++++--- 6 files changed, 74 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.2
RFC PATCH 1/6 include/hw/pci: Add a callback to set the downstream memory region of a pci bus
PCIIOMMUOps.set_memory_region() is introduced to set the downstream memory region of a pci bus. When PCIIOMMUOps.get_address_space() is already registered by the pci host(e.g. TYPE_DESIGNWARE_PCIE_HOST), PCIIOMMUOps.set_memory_region() should be registered as well, so an IOMMU can designate the downstream memory region for the pci bus.
--- include/hw/pci/pci.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/hw/pci/pci.h +13 -0
@@ -388,6 +388,19 @@ typedef struct PCIIOMMUOps {* @devfn: device and function number */ AddressSpace * (*get_address_space)(PCIBus *bus,void *opaque,int devfn); +/** +* @set_memory_region: set the downstream memory region for a set of +* devices on a PCI bus. +* +* Optional callback, which should be implemented when get_address_space() +* is registered by the PCI host(e.g. TYPE_DESIGNWARE_PCIE_HOST), which +* makes an IOMMU not able to register get_address_space() again. +* +* @opaque: the data passed to pci_setup_iommu(). +* +* @mr: the downsteam memory region +*/ +void (*set_memory_region)(void *opaque,MemoryRegion *mr);/** * @set_iommu_device: attach a HostIOMMUDevice to a vIOMMU *
Signature · 1 line
2.43.2
RFC PATCH 2/6 hw/pci: Add an API to set the downstream memory region of a PCI bus
In reply to Jason Chienpci_setup_iommu_downstream_mem() invokes PCIIOMMUOps.set_memory_region() to set the downstream memroy region of a PCI bus.
--- hw/pci/pci.c | 6 ++++++ include/hw/pci/pci.h | 7 +++++++ 2 files changed, 13 insertions(+)
hw/pci/pci.c +6 -0
@@ -2811,6 +2811,12 @@ void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *ops, void *opaque) bus->iommu_opaque = opaque; } +void pci_setup_iommu_downstream_mem (PCIBus *bus,MemoryRegion *mr) +{ +assert (bus->iommu_ops && bus->iommu_ops->set_memory_region); + bus->iommu_ops->set_memory_region (bus->iommu_opaque, mr); +} +static void pci_dev_get_w64 (PCIBus *b,PCIDevice *dev,void *opaque) {Range *range = opaque;
include/hw/pci/pci.h +7 -0
@@ -450,6 +450,13 @@ void pci_device_unset_iommu_device(PCIDevice *dev);* @opaque: passed to callbacks of the @ops structure. */ void pci_setup_iommu (PCIBus *bus,const PCIIOMMUOps *ops,void *opaque); +/** +* pci_setup_iommu_downstream_mem: Designate a memory region for a PCIBus +* +* @bus: the #PCIBus being updated. +* @mr: the designated memory region. +*/ +void pci_setup_iommu_downstream_mem (PCIBus *bus,MemoryRegion *mr);pcibus_t pci_bar_address (PCIDevice *d,int reg,uint8_t type,pcibus_t size);
Signature · 1 line
2.43.2
RFC PATCH 3/6 hw/pci-host: Enable DW PCIe host to send memory transactions over specific mr
In reply to Jason ChienCurrent Designware PCIe host cannot connect to an IOMMU, since it has registered PCIIOMMUOps.get_address_space() and an IOMMU cannot overwrite PCIIOMMUOps.get_address_space() without breaking the PCIe translation rules.
This commit implements designware_pcie_host_set_mem(), which is used to register PCIIOMMUOps.set_memory_region(), so an IOMMU can designate the downstream memory region for the PCIe devices.
--- hw/pci-host/designware.c | 18 +++++++++++++++--- include/hw/pci-host/designware.h | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-)
hw/pci-host/designware.c +15 -3
@@ -395,7 +395,6 @@ static void designware_pcie_root_realize(PCIDevice *dev, Error **errp) {DesignwarePCIERoot *root =DESIGNWARE_PCIE_ROOT (dev);DesignwarePCIEHost *host =designware_pcie_root_to_host (root); -MemoryRegion *host_mem =get_system_memory ();MemoryRegion *address_space = &host->pci.memory;PCIBridge *br =PCI_BRIDGE (dev);DesignwarePCIEViewport *viewport;
@@ -436,7 +435,7 @@ static void designware_pcie_root_realize(PCIDevice *dev, Error **errp) viewport->cr[0 ] =DESIGNWARE_PCIE_ATU_TYPE_MEM ; source = &host->pci.address_space_root; - destination = host_mem; + destination = &host->bridge_mr; direction ="Inbound" ;/*
@@ -461,7 +460,7 @@ static void designware_pcie_root_realize(PCIDevice *dev, Error **errp) destination = &host->pci.memory; direction ="Outbound" ; - source = host_mem; + source =get_system_memory ();/* * Configure MemoryRegion implementing CPU -> PCI memory
@@ -666,8 +665,16 @@ static AddressSpace *designware_pcie_host_set_iommu(PCIBus *bus, void *opaque,return &s->pci.address_space; } +void designware_pcie_host_set_mem (void *opaque,MemoryRegion *mr) +{ +DesignwarePCIEHost *s =DESIGNWARE_PCIE_HOST (opaque); + +memory_region_add_subregion_overlap (&s->bridge_mr,0 , mr,INT32_MAX ); +} +static const PCIIOMMUOps designware_iommu_ops = { .get_address_space = designware_pcie_host_set_iommu, + .set_memory_region = designware_pcie_host_set_mem, };static void designware_pcie_host_realize (DeviceState *dev,Error **errp)
@@ -703,6 +710,11 @@ static void designware_pcie_host_realize(DeviceState *dev, Error **errp) TYPE_PCIE_BUS); pci->bus->flags |=PCI_BUS_EXTENDED_CONFIG_SPACE ; +memory_region_init (&s->bridge_mr,OBJECT (s), +"pcie-bus-bridge-memory" ,UINT64_MAX ); +memory_region_add_subregion (&s->bridge_mr,0x0 ,get_system_memory ()); +address_space_init (&s->bridge_as, &s->bridge_mr,"pcie-bus-bridge-space" ); + memory_region_init(&s->pci.address_space_root, OBJECT(s), "pcie-bus-address-space-root",
include/hw/pci-host/designware.h +2 -0
@@ -89,6 +89,8 @@ struct DesignwarePCIEHost { } pci;MemoryRegion mmio; +AddressSpace bridge_as; +MemoryRegion bridge_mr; };#endif /* DESIGNWARE_H */
Signature · 1 line
2.43.2
RFC PATCH 4/6 hw/riscv/riscv-iommu: Allow PCI hosts with iommu_ops registered connecting to the IOMMU
In reply to Jason Chienpci_setup_iommu_downstream_mem() is called to set the downstream memory region of the PCIe devices.
--- hw/riscv/riscv-iommu.c | 8 ++++++++ 1 file changed, 8 insertions(+)
hw/riscv/riscv-iommu.c +8 -0
@@ -2436,6 +2436,14 @@ void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus,QLIST_INSERT_AFTER (last, iommu, iommus); }else if (!bus->iommu_ops && !bus->iommu_opaque) {pci_setup_iommu (bus, &riscv_iommu_ops, iommu); + }else if (bus->iommu_ops && bus->iommu_ops->set_memory_region) { +/* +* TODO: +* All memory transactions of this bus will be directed to this AS. +* We need to distinguish the source device dynamically. +*/ +AddressSpace *as =riscv_iommu_space (iommu,0 ); +pci_setup_iommu_downstream_mem (bus, as->root); }else {error_setg (errp,"can't register secondary IOMMU for PCI bus #%d" ,pci_bus_num (bus));
Signature · 1 line
2.43.2
Re: [RFC PATCH 4/6] hw/riscv/riscv-iommu: Allow PCI hosts with iommu_ops registered connecting to the IOMMU
Quoted 5 lines · Jason Chien
pci_setup_iommu_downstream_mem()is called to set the downstream memory region of the PCIe devices.Signed-off-by Jason Chien <[email protected]>---
Quoted 22 lines
hw/riscv/riscv-iommu.c | 8 ++++++++ 1 file changed, 8 insertions(+)hw/riscv/riscv-iommu.c +8
-0@@ -2436,6 +2436,14 @@ void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus,QLIST_INSERT_AFTER (last, iommu, iommus); }else if (!bus->iommu_ops && !bus->iommu_opaque) {pci_setup_iommu (bus, &riscv_iommu_ops, iommu); + }else if (bus->iommu_ops && bus->iommu_ops->set_memory_region) { +/* +* TODO: +* All memory transactions of this bus will be directed to this AS. +* We need to distinguish the source device dynamically. +*/ +AddressSpace *as =riscv_iommu_space (iommu,0 ); +pci_setup_iommu_downstream_mem (bus, as->root); }else {error_setg (errp,"can't register secondary IOMMU for PCI bus #%d" ,pci_bus_num (bus));
RFC PATCH 5/6 hw/riscv/riscv-iommu: Acquire device IDs dynamically
In reply to Jason ChienWhen all memory transactions from a PCIe host write to the same IOMMU memory region, we need to distinguish the source device dynamically.
--- hw/riscv/riscv-iommu.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
hw/riscv/riscv-iommu.c +13 -4
@@ -48,6 +48,7 @@ struct RISCVIOMMUSpace {RISCVIOMMUState *iommu;/* Managing IOMMU device state */ uint32_t devid;/* Requester identifier, AKA device_id */ bool notifier;/* IOMMU unmap notifier enabled */ +bool dynamic_devid;/* Acquiring device_id dynamically */ QLIST_ENTRY (RISCVIOMMUSpace) list; };
@@ -1184,7 +1185,8 @@ static void riscv_iommu_ctx_put(RISCVIOMMUState *s, void *ref) }/* Find or allocate address space for a given device */ -static AddressSpace *riscv_iommu_space (RISCVIOMMUState *s,uint32_t devid) +static AddressSpace *riscv_iommu_space (RISCVIOMMUState *s,uint32_t devid, +bool dynamic) {RISCVIOMMUSpace *as;
@@ -1203,6 +1205,7 @@ static AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, uint32_t devid) as->iommu = s; as->devid = devid; + as->dynamic_devid = dynamic;snprintf (name,sizeof (name),"riscv-iommu-%04x:%02x.%d-iova" ,PCI_BUS_NUM (as->devid),PCI_SLOT (as->devid),PCI_FUNC (as->devid));
@@ -2415,7 +2418,8 @@ static AddressSpace *riscv_iommu_find_as(PCIBus *bus, void *opaque, int devfn)/* Find first matching IOMMU */ while (s !=NULL && as ==NULL ) { - as =riscv_iommu_space (s,PCI_BUILD_BDF (pci_bus_num (bus), devfn)); + as =riscv_iommu_space (s,PCI_BUILD_BDF (pci_bus_num (bus), devfn), + false); s = s->iommus.le_next; }
@@ -2438,11 +2442,10 @@ void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus,pci_setup_iommu (bus, &riscv_iommu_ops, iommu); }else if (bus->iommu_ops && bus->iommu_ops->set_memory_region) {/* -* TODO: * All memory transactions of this bus will be directed to this AS. * We need to distinguish the source device dynamically. */ -AddressSpace *as =riscv_iommu_space (iommu,0 ); +AddressSpace *as =riscv_iommu_space (iommu,0 , true);pci_setup_iommu_downstream_mem (bus, as->root); }else { error_setg(errp, "can't register secondary IOMMU for PCI bus #%d",
@@ -2453,6 +2456,12 @@ void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus,static int riscv_iommu_memory_region_index (IOMMUMemoryRegion *iommu_mr,MemTxAttrs attrs) { +RISCVIOMMUSpace *as =container_of (iommu_mr, RISCVIOMMUSpace, iova_mr); + +if (as->dynamic_devid) { + as->devid = attrs.requester_id; + } +return attrs.unspecified ?RISCV_IOMMU_NOPROCID : (int )attrs.pid; }
Signature · 1 line
2.43.2
Re: [RFC PATCH 5/6] hw/riscv/riscv-iommu: Acquire device IDs dynamically
Quoted 5 lines · Jason Chien
When all memory transactions from a PCIe host write to the same IOMMU memory region, we need to distinguish the source device dynamically.
Signed-off-by Jason Chien <[email protected]>---
I'm not sure whether this should be squashed in patch 4 but the code LGTM, so either way:
Quoted 69 lines
hw/riscv/riscv-iommu.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)hw/riscv/riscv-iommu.c +13
-4@@ -48,6 +48,7 @@ struct RISCVIOMMUSpace {RISCVIOMMUState *iommu;/* Managing IOMMU device state */ uint32_t devid;/* Requester identifier, AKA device_id */ bool notifier;/* IOMMU unmap notifier enabled */ +bool dynamic_devid;/* Acquiring device_id dynamically */ QLIST_ENTRY (RISCVIOMMUSpace) list; };@@ -1184,7 +1185,8 @@ static void riscv_iommu_ctx_put(RISCVIOMMUState *s, void *ref) }/* Find or allocate address space for a given device */ -static AddressSpace *riscv_iommu_space (RISCVIOMMUState *s,uint32_t devid) +static AddressSpace *riscv_iommu_space (RISCVIOMMUState *s,uint32_t devid, +bool dynamic) {RISCVIOMMUSpace *as;@@ -1203,6 +1205,7 @@ static AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, uint32_t devid) as->iommu = s; as->devid = devid; + as->dynamic_devid = dynamic;snprintf (name,sizeof (name),"riscv-iommu-%04x:%02x.%d-iova" ,PCI_BUS_NUM (as->devid),PCI_SLOT (as->devid),PCI_FUNC (as->devid));@@ -2415,7 +2418,8 @@ static AddressSpace *riscv_iommu_find_as(PCIBus *bus, void *opaque, int devfn)/* Find first matching IOMMU */ while (s !=NULL && as ==NULL ) { - as =riscv_iommu_space (s,PCI_BUILD_BDF (pci_bus_num (bus), devfn)); + as =riscv_iommu_space (s,PCI_BUILD_BDF (pci_bus_num (bus), devfn), + false); s = s->iommus.le_next; }@@ -2438,11 +2442,10 @@ void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus,pci_setup_iommu (bus, &riscv_iommu_ops, iommu); }else if (bus->iommu_ops && bus->iommu_ops->set_memory_region) {/* -* TODO: * All memory transactions of this bus will be directed to this AS. * We need to distinguish the source device dynamically. */ -AddressSpace *as =riscv_iommu_space (iommu,0 ); +AddressSpace *as =riscv_iommu_space (iommu,0 , true);pci_setup_iommu_downstream_mem (bus, as->root); }else { error_setg(errp, "can't register secondary IOMMU for PCI bus #%d",@@ -2453,6 +2456,12 @@ void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus,static int riscv_iommu_memory_region_index (IOMMUMemoryRegion *iommu_mr,MemTxAttrs attrs) { +RISCVIOMMUSpace *as =container_of (iommu_mr, RISCVIOMMUSpace, iova_mr); + +if (as->dynamic_devid) { + as->devid = attrs.requester_id; + } +return attrs.unspecified ?RISCV_IOMMU_NOPROCID : (int )attrs.pid; }
RFC PATCH 6/6 include/hw/pci: Send PCI dma requests with memory attributes containing BDF
In reply to Jason ChienIn order to distinguish which PCIe device sends the request over the IOMMU, the requester_id must be set with the BDF of the device.
--- include/hw/pci/pci_device.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
include/hw/pci/pci_device.h +12 -3
@@ -263,8 +263,11 @@ static inline MemTxResult pci_dma_rw(PCIDevice *dev, dma_addr_t addr,static inline MemTxResult pci_dma_read (PCIDevice *dev,dma_addr_t addr,void *buf,dma_addr_t len) { +MemTxAttrs attrs = {}; + attrs.requester_id =pci_requester_id (dev); +return pci_dma_rw (dev, addr, buf, len, -DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_TO_DEVICE , attrs); }/**
@@ -282,8 +285,11 @@ static inline MemTxResult pci_dma_read(PCIDevice *dev, dma_addr_t addr,static inline MemTxResult pci_dma_write (PCIDevice *dev,dma_addr_t addr,const void *buf,dma_addr_t len) { +MemTxAttrs attrs = {}; + attrs.requester_id =pci_requester_id (dev); +return pci_dma_rw (dev, addr, (void *) buf, len, -DMA_DIRECTION_FROM_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_FROM_DEVICE , attrs); }#define PCI_DMA_DEFINE_LDST (_l, _s, _bits) \
@@ -327,8 +333,11 @@ PCI_DMA_DEFINE_LDST(q_be, q_be, 64);static inline void *pci_dma_map (PCIDevice *dev,dma_addr_t addr,dma_addr_t *plen,DMADirection dir) { +MemTxAttrs attrs = {}; + attrs.requester_id =pci_requester_id (dev); +return dma_memory_map (pci_get_address_space (dev), addr, plen, dir, -MEMTXATTRS_UNSPECIFIED ); + attrs); } static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,
Signature · 1 line
2.43.2
Ping.
Jason Chien <[email protected]> 於 2024年12月16日 週一 上午8:49寫道:
Quoted 33 lines
When
PCIIOMMUOps.get_address_space()is already registered by the pci host, e.g.TYPE_DESIGNWARE_PCIE_HOST, IOMMU cannot overwrite this hook without breaking the PCIe translation rule, which means that IOMMU and the pci host cannot coexist.This RFC introduces a new callback,
PCIIOMMUOps.set_memory_region(), and an API,pci_setup_iommu_downstream_mem, to avoid the contention forPCIIOMMUOps.get_address_space().Jason Chien (6):
include/hw/pci: Add a callback to set the downstream memory region of
a pci bus
hw/pci: Add an API to set the downstream memory region of a PCI bus hw/pci-host: Enable DW PCIe host to send memory transactions over
specific mr
hw/riscv/riscv-iommu: Allow PCI hosts withiommu_opsregistered
connecting to the IOMMU
hw/riscv/riscv-iommu: Acquire device IDs dynamicallyinclude/hw/pci: Send PCI dma requests with memory attributes
containing BDFhw/pci-host/designware.c | 18 +++++++++++++++--- hw/pci/pci.c | 6 ++++++ hw/riscv/riscv-iommu.c | 21 +++++++++++++++++++-- include/hw/pci-host/designware.h | 2 ++ include/hw/pci/pci.h | 20 ++++++++++++++++++++ include/hw/pci/pci_device.h | 15 ++++++++++++--- 6 files changed, 74 insertions(+), 8 deletions(-)--
2.43.2