Move the per-device IOMMU address space structure (RISCVIOMMUSpace) into the public header and split its allocation into separate lookup and init helpers, so the space management can be reused by other devices without code duplication. No behavioural change.
Signed-off-by: Gong Shuai <[email protected]> Signed-off-by: Wu Fei <[email protected]> --- hw/riscv/riscv-iommu.c | 68 ++++++++++++++++++++++-------------------- hw/riscv/riscv-iommu.h | 16 ++++++++++ 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c index 6381986265..986921dd5e 100644 --- a/hw/riscv/riscv-iommu.c +++ b/hw/riscv/riscv-iommu.c @@ -44,17 +44,6 @@ typedef struct RISCVIOMMUEntry RISCVIOMMUEntry; -/* Device assigned I/O address space */ -struct RISCVIOMMUSpace { - IOMMUMemoryRegion iova_mr; /* IOVA memory region for attached device */ - AddressSpace iova_as; /* IOVA address space for attached device */ - RISCVIOMMUState *iommu; /* Managing IOMMU device state */ - PCIBus *bus; /* PCI bus of the requester */ - uint8_t devfn; /* Requester identifier, AKA device_id */ - bool notifier; /* IOMMU unmap notifier enabled */ - QLIST_ENTRY(RISCVIOMMUSpace) list; -}; - typedef enum RISCVIOMMUTransTag { RISCV_IOMMU_TRANS_TAG_BY, /* Bypass */ RISCV_IOMMU_TRANS_TAG_SS, /* Single Stage */ @@ -1527,9 +1516,31 @@ 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, PCIBus *bus, - int devfn) +void riscv_iommu_init_space(RISCVIOMMUState *s, RISCVIOMMUSpace *as, + PCIBus *bus, int devfn) +{ + char name[64]; + + as->iommu = s; + as->bus = bus; + as->devfn = devfn; + + snprintf(name, sizeof(name), "riscv-iommu-%04x:%02x.%d-iova", + pci_bus_num(bus), PCI_SLOT(devfn), PCI_FUNC(devfn)); + + /* IOVA address space, untranslated addresses */ + memory_region_init_iommu(&as->iova_mr, sizeof(as->iova_mr), + TYPE_RISCV_IOMMU_MEMORY_REGION, + OBJECT(s), "riscv_iommu", UINT64_MAX); + address_space_init(&as->iova_as, MEMORY_REGION(&as->iova_mr), name); + + QLIST_INSERT_HEAD(&s->spaces, as, list); + + trace_riscv_iommu_new(s->parent_obj.id, pci_bus_num(bus), + PCI_SLOT(devfn), PCI_FUNC(devfn)); +} + +RISCVIOMMUSpace *riscv_iommu_get_space(RISCVIOMMUState *s, PCIBus *bus, int devfn) { RISCVIOMMUSpace *as; @@ -1539,27 +1550,18 @@ static AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, } } - if (as == NULL) { - char name[64]; - as = g_new0(RISCVIOMMUSpace, 1); - - as->iommu = s; - as->bus = bus; - as->devfn = devfn; - - snprintf(name, sizeof(name), "riscv-iommu-%04x:%02x.%d-iova", - pci_bus_num(bus), PCI_SLOT(devfn), PCI_FUNC(devfn)); - - /* IOVA address space, untranslated addresses */ - memory_region_init_iommu(&as->iova_mr, sizeof(as->iova_mr), - TYPE_RISCV_IOMMU_MEMORY_REGION, - OBJECT(s), "riscv_iommu", UINT64_MAX); - address_space_init(&as->iova_as, MEMORY_REGION(&as->iova_mr), name); + return as; +} - QLIST_INSERT_HEAD(&s->spaces, as, list); +/* Find or allocate address space for a given device */ +AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, int devfn) +{ + RISCVIOMMUSpace *as; - trace_riscv_iommu_new(s->parent_obj.id, pci_bus_num(bus), - PCI_SLOT(devfn), PCI_FUNC(devfn)); + as = riscv_iommu_get_space(s, bus, devfn); + if (as == NULL) { + as = g_new0(RISCVIOMMUSpace, 1); + riscv_iommu_init_space(s, as, bus, devfn); } return &as->iova_as; } diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h index cf2904c118..099faa9480 100644 --- a/hw/riscv/riscv-iommu.h +++ b/hw/riscv/riscv-iommu.h @@ -28,6 +28,17 @@ typedef enum riscv_iommu_igs_modes riscv_iommu_igs_mode; +/* Device assigned I/O address space */ +struct RISCVIOMMUSpace { + IOMMUMemoryRegion iova_mr; /* IOVA memory region for attached device */ + AddressSpace iova_as; /* IOVA address space for attached device */ + RISCVIOMMUState *iommu; /* Managing IOMMU device state */ + PCIBus *bus; /* PCI bus of the requester */ + uint8_t devfn; /* Requester identifier, AKA device_id */ + bool notifier; /* IOMMU unmap notifier enabled */ + QLIST_ENTRY(RISCVIOMMUSpace) list; +}; + struct RISCVIOMMUState { /*< private >*/ DeviceState parent_obj; @@ -112,6 +123,11 @@ DeviceState *riscv_create_iommu_sys(DeviceState *mmio_irqchip, hwaddr addr, int base_irq, bool is_32_bit); +void riscv_iommu_init_space(RISCVIOMMUState *s, RISCVIOMMUSpace *as, + PCIBus *bus, int devfn); +AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, PCIBus *bus, int devfn); +RISCVIOMMUSpace *riscv_iommu_get_space(RISCVIOMMUState *s, PCIBus *bus, int devfn); + typedef struct RISCVIOMMUContext RISCVIOMMUContext; /* Device translation context state. */ struct RISCVIOMMUContext { -- 2.53.0
