hw/arm/smmuv3: Support non PCI/PCIe devices
v6 cover hw/arm/smmuv3: Support non PCI/PCIe devices
From: chunming <[email protected]>
The current SMMU v3 model only support PCI/PCIe devices, so we update it for non-PCI/PCIe devices.
. Add independent IOMMU memory regions for non-PCI/PCIe devices . Add SID value property setting for non-PCI/PCIe devices
. Add PL330 DMA controller into "virt" machine and connect with SMMU v3 . Test PL330 DMA controller and PCIe e1000 network with SMMU v3 enabled
Notes:
You need apply PL330 memory region patch before compile "virt" machine:
https://patchew.org/QEMU/4C23C17B8E87E74E906A25A3254A03F4FA1FEC31@SHASXM03.verisilicon.com/
The old PL330 model cannot configure its memory region manually. So we update it and provide path.
The patch was reviewed and will be merged in target-arm.next for 6.2.
v6 - Reviewed by Eric:
. Rename sid_map -> peri_sid_map, num_sid -> peri_num_sid . MOve peri_sid_map and peri_num_sid to SMMUState
. Add "peri_sdev_list" looking up to replace "g_hash_table_remove()"
for non PCI/PCIe devices.
chunming (4):
hw/arm/smmuv3: Support non PCI/PCIe device connect with SMMU v3 hw/arm/smmuv3: Update CFGI commands to support non PCI/PCIe devices hw/arm/virt: Update SMMU v3 creation to support non PCI/PCIe device
connection
hw/arm/virt: Add PL330 DMA controller and connect with SMMU v3
hw/arm/smmuv3.c | 75 ++++++++++++++++++++---- hw/arm/virt.c | 110 ++++++++++++++++++++++++++++++++++- include/hw/arm/smmu-common.h | 14 ++++- include/hw/arm/virt.h | 3 + 4 files changed, 188 insertions(+), 14 deletions(-)
Signature · 1 line
2.30.2
PATCH v6 1/4 hw/arm/smmuv3: Support non PCI/PCIe device connect with SMMU v3
From: chunming <[email protected]>
. Add peri-sid-map property to store non PCI/PCIe devices SID . Create IOMMU memory regions for non PCI/PCIe devices based on their SID . Update SID getting strategy for PCI/PCIe and non PCI/PCIe devices
--- hw/arm/smmuv3.c | 46 ++++++++++++++++++++++++++++++++++++ include/hw/arm/smmu-common.h | 9 ++++++- 2 files changed, 54 insertions(+), 1 deletion(-)
hw/arm/smmuv3.c +46 -0
@@ -32,6 +32,7 @@#include "hw/arm/smmuv3.h" #include "smmuv3-internal.h" #include "smmu-internal.h" +#include "hw/qdev-properties.h" /** * smmuv3_trigger_irq - pulse @irq if enabled and update
@@ -913,6 +914,19 @@ smmuv3_invalidate_ste(gpointer key, gpointer value, gpointer user_data)return true; } +static SMMUDevice *smmu_find_peri_sdev (SMMUState *s,uint16_t sid) +{ +SMMUDevice *sdev; + + QLIST_FOREACH(sdev, &s->peri_sdev_list, next) { +if (smmu_get_sid (sdev) == sid) { +return sdev; + } + } + +return NULL ; +} +static int smmuv3_cmdq_consume (SMMUv3State *s) {SMMUState *bs =ARM_SMMU (s);
@@ -1437,6 +1451,9 @@ static void smmu_realize(DeviceState *d, Error **errp)SMMUv3Class *c =ARM_SMMUV3_GET_CLASS (s);SysBusDevice *dev =SYS_BUS_DEVICE (d);Error *local_err =NULL ; +SMMUDevice *sdev; +char *name =NULL ; +uint16_t sid =0 ; c->parent_realize (d, &local_err);if (local_err) {
@@ -1454,6 +1471,28 @@ static void smmu_realize(DeviceState *d, Error **errp)sysbus_init_mmio (dev, &sys->iomem);smmu_init_irq (s, dev); + +/* Create IOMMU memory region for peripheral devices based on their SID */ +for (int i =0 ; i < sys->peri_num_sid; i++) { + sid = sys->peri_sid_map[i]; + sdev =smmu_find_peri_sdev (sys, sid); +if (sdev) { +continue ; + } + + sdev =g_new0 (SMMUDevice,1 ); + sdev->smmu = sys; + sdev->bus =NULL ; + sdev->devfn = sid; + + name =g_strdup_printf ("%s-peri-%d" , sys->mrtypename, sid); +memory_region_init_iommu (&sdev->iommu,sizeof (sdev->iommu), + sys->mrtypename, +OBJECT (sys), name,1ULL <<SMMU_MAX_VA_BITS ); + +QLIST_INSERT_HEAD (&sys->peri_sdev_list, sdev, next); +g_free (name); + } }static const VMStateDescription vmstate_smmuv3_queue = {
@@ -1506,6 +1545,12 @@ static void smmuv3_instance_init(Object *obj)/* Nothing much to do here as of now */ } +static Property smmuv3_properties[] = { +DEFINE_PROP_ARRAY ("peri-sid-map" , SMMUState, peri_num_sid, peri_sid_map, + qdev_prop_uint16, uint16_t), +DEFINE_PROP_END_OF_LIST (), +}; +static void smmuv3_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass);
@@ -1515,6 +1560,7 @@ static void smmuv3_class_init(ObjectClass *klass, void *data)device_class_set_parent_reset (dc, smmu_reset, &c->parent_reset); c->parent_realize = dc->realize; dc->realize = smmu_realize; +device_class_set_props (dc, smmuv3_properties); } static int smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu,
include/hw/arm/smmu-common.h +8 -1
@@ -117,6 +117,9 @@ struct SMMUState { QLIST_HEAD(, SMMUDevice) devices_with_notifiers;uint8_t bus_num;PCIBus *primary_bus; + QLIST_HEAD(, SMMUDevice) peri_sdev_list; +uint32_t peri_num_sid; +uint16_t *peri_sid_map; };struct SMMUBaseClass {
@@ -138,7 +141,11 @@ SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num);/* Return the stream ID of an SMMU device */ static inline uint16_t smmu_get_sid (SMMUDevice *sdev) { -return PCI_BUILD_BDF (pci_bus_num (sdev->bus), sdev->devfn); +if (sdev->bus ==NULL ) { +return sdev->devfn; + }else { +return PCI_BUILD_BDF (pci_bus_num (sdev->bus), sdev->devfn); + } }/**
Signature · 1 line
2.30.2
PATCH v6 2/4 hw/arm/smmuv3: Update CFGI commands to support non PCI/PCIe devices
In reply to chunmingFrom: chunming <[email protected]>
"smmu_iommu_mr" function can't get MR according to SID for non PCI/PCIe devices. Look up in the platform device list: peri_sdev_list for non PCI/PCIe devices.
--- hw/arm/smmuv3.c | 29 ++++++++++++++++++----------- include/hw/arm/smmu-common.h | 5 ++++- 2 files changed, 22 insertions(+), 12 deletions(-)
hw/arm/smmuv3.c +18 -11
@@ -985,14 +985,17 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)break ; } -if (!mr) { -break ; +if (mr) { +trace_smmuv3_cmdq_cfgi_ste (sid); + sdev =container_of (mr, SMMUDevice, iommu); +smmuv3_flush_config (sdev); } -trace_smmuv3_cmdq_cfgi_ste (sid); - sdev =container_of (mr, SMMUDevice, iommu); -smmuv3_flush_config (sdev); - + sdev =smmu_find_peri_sdev (bs, sid); +if (sdev) { +trace_smmuv3_cmdq_cfgi_ste (sid); +smmuv3_flush_config (sdev); + }break ; }case SMMU_CMD_CFGI_STE_RANGE :/* same as SMMU_CMD_CFGI_ALL */
@@ -1027,13 +1030,17 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)break ; } -if (!mr) { -break ; +if (mr) { +trace_smmuv3_cmdq_cfgi_cd (sid); + sdev =container_of (mr, SMMUDevice, iommu); +smmuv3_flush_config (sdev); } -trace_smmuv3_cmdq_cfgi_cd (sid); - sdev =container_of (mr, SMMUDevice, iommu); -smmuv3_flush_config (sdev); + sdev =smmu_find_peri_sdev (bs, sid); +if (sdev) { +trace_smmuv3_cmdq_cfgi_cd (sid); +smmuv3_flush_config (sdev); + }break ; }case SMMU_CMD_TLBI_NH_ASID :
include/hw/arm/smmu-common.h +4 -1
@@ -161,7 +161,10 @@ int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm,*/ SMMUTransTableInfo *select_tt (SMMUTransCfg *cfg,dma_addr_t iova); -/* Return the iommu mr associated to @sid, or NULL if none */ +/** +* Return the iommu mr associated to @sid, or NULL if none +* Only for PCI device, check smmu_find_peri_sdev for peripheral device +*/ IOMMUMemoryRegion *smmu_iommu_mr (SMMUState *s,uint32_t sid);#define SMMU_IOTLB_MAX_SIZE 256
Signature · 1 line
2.30.2
PATCH v6 3/4 hw/arm/virt: Update SMMU v3 creation to support non PCI/PCIe device connection
In reply to chunmingFrom: chunming <[email protected]>
. Add "smmuv3_sidmap" to set non PCI/PCIe devices SID value . Pass non PCI/PCIe devices SID value to SMMU v3 model creation
--- hw/arm/virt.c | 14 ++++++++++++++ include/hw/arm/virt.h | 1 + 2 files changed, 15 insertions(+)
hw/arm/virt.c +14 -0
@@ -204,6 +204,9 @@ static const char *valid_cpus[] = { ARM_CPU_TYPE_NAME("max"), }; +static const uint16_t smmuv3_peri_sidmap[] = { +}; +static bool cpu_type_valid (const char *cpu) {int i;
@@ -1244,6 +1247,15 @@ static void create_smmu(const VirtMachineState *vms,object_property_set_link (OBJECT (dev),"primary-bus" ,OBJECT (bus), &error_abort); + +qdev_prop_set_uint32 (dev,"len-peri-sid-map" , +ARRAY_SIZE (smmuv3_peri_sidmap)); + +for (i =0 ; i <ARRAY_SIZE (smmuv3_peri_sidmap); i++) { + g_autofree char *propname = g_strdup_printf("peri-sid-map[%d]", i); +qdev_prop_set_uint16 (dev, propname, smmuv3_peri_sidmap[i]); + } +sysbus_realize_and_unref (SYS_BUS_DEVICE (dev), &error_fatal);sysbus_mmio_map (SYS_BUS_DEVICE (dev),0 , base);for (i =0 ; i <NUM_SMMU_IRQS ; i++) {
@@ -2762,6 +2774,8 @@ static void virt_instance_init(Object *obj) vms->irqmap = a15irqmap; + vms->peri_sidmap = smmuv3_peri_sidmap; +virt_flash_create (vms); vms->oem_id =g_strndup (ACPI_BUILD_APPNAME6 ,6 );
include/hw/arm/virt.h +1 -0
@@ -167,6 +167,7 @@ struct VirtMachineState {PCIBus *bus;char *oem_id;char *oem_table_id; +const uint16_t *peri_sidmap; };#define VIRT_ECAM_ID (high) (high ? VIRT_HIGH_PCIE_ECAM : VIRT_PCIE_ECAM)
Signature · 1 line
2.30.2
PATCH v6 4/4 hw/arm/virt: Add PL330 DMA controller and connect with SMMU v3
In reply to chunmingFrom: chunming <[email protected]>
. Store SMMU v3 device in virtual machine then non PCI/PCIe can get its memory region later . Add PL330 DMA controller to test SMMU v3 connection and function
. The default SID for PL330 is 1 but we test other values, it works well
--- hw/arm/virt.c | 96 ++++++++++++++++++++++++++++++++++++++++++- include/hw/arm/virt.h | 2 + 2 files changed, 97 insertions(+), 1 deletion(-)
hw/arm/virt.c +95 -1
@@ -143,6 +143,7 @@ static const MemMapEntry base_memmap[] = { [VIRT_GIC_REDIST] = { 0x080A0000, 0x00F60000 }, [VIRT_UART] = { 0x09000000, 0x00001000 }, [VIRT_RTC] = { 0x09010000, 0x00001000 }, + [VIRT_DMA] = { 0x09011000, 0x00001000 }, [VIRT_FW_CFG] = { 0x09020000, 0x00000018 }, [VIRT_GPIO] = { 0x09030000, 0x00001000 }, [VIRT_SECURE_UART] = { 0x09040000, 0x00001000 },
@@ -188,6 +189,7 @@ static const int a15irqmap[] = { [VIRT_GPIO] = 7, [VIRT_SECURE_UART] = 8, [VIRT_ACPI_GED] = 9, + [VIRT_DMA] = 10, [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */ [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */ [VIRT_SMMU] = 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */
@@ -205,6 +207,7 @@ static const char *valid_cpus[] = { };static const uint16_t smmuv3_peri_sidmap[] = { + [VIRT_DMA ] =1 , };static bool cpu_type_valid (const char *cpu)
@@ -792,6 +795,93 @@ static void create_uart(const VirtMachineState *vms, int uart,g_free (nodename); } +static void create_dma (const VirtMachineState *vms) +{ +int i; +char *nodename; +hwaddr base = vms->memmap[VIRT_DMA ].base; +hwaddr size = vms->memmap[VIRT_DMA ].size; +int irq = vms->irqmap[VIRT_DMA ]; +int sid = vms->peri_sidmap[VIRT_DMA ]; +const char compat[] ="arm,pl330\0arm,primecell" ; +const char irq_names[] ="abort\0dma0\0dma1\0dma2\0dma3\0dma4\0dma5\0dma6\0dma7" ; +DeviceState *dev; +MachineState *ms =MACHINE (vms); +SysBusDevice *busdev; +DeviceState *smmuv3_dev; +SMMUState *smmuv3_sys; +Object *smmuv3_memory; + + dev =qdev_new ("pl330" ); + +if (vms->iommu ==VIRT_IOMMU_SMMUV3 && vms->iommu_phandle) { + smmuv3_dev = vms->smmuv3; + smmuv3_sys =ARM_SMMU (smmuv3_dev); + g_autofree char *memname = g_strdup_printf("%s-peri-%d[0]", + smmuv3_sys->mrtypename, + sid); + + smmuv3_memory =object_property_get_link (OBJECT (smmuv3_dev), + memname, &error_abort); + +object_property_set_link (OBJECT (dev),"memory" , +OBJECT (smmuv3_memory), + &error_fatal); + }else { +object_property_set_link (OBJECT (dev),"memory" , +OBJECT (get_system_memory ()), + &error_fatal); + } + +qdev_prop_set_uint8 (dev,"num_chnls" ,8 ); +qdev_prop_set_uint8 (dev,"num_periph_req" ,4 ); +qdev_prop_set_uint8 (dev,"num_events" ,16 ); +qdev_prop_set_uint8 (dev,"data_width" ,64 ); +qdev_prop_set_uint8 (dev,"wr_cap" ,8 ); +qdev_prop_set_uint8 (dev,"wr_q_dep" ,16 ); +qdev_prop_set_uint8 (dev,"rd_cap" ,8 ); +qdev_prop_set_uint8 (dev,"rd_q_dep" ,16 ); +qdev_prop_set_uint16 (dev,"data_buffer_dep" ,256 ); + + busdev =SYS_BUS_DEVICE (dev); +sysbus_realize_and_unref (busdev, &error_fatal); +sysbus_mmio_map (busdev,0 , base); + +for (i =0 ; i <9 ; ++i) { +sysbus_connect_irq (busdev, i,qdev_get_gpio_in (vms->gic, irq + i)); + } + + nodename =g_strdup_printf ("/pl330@%" PRIx64, base); +qemu_fdt_add_subnode (ms->fdt, nodename); +qemu_fdt_setprop (ms->fdt, nodename,"compatible" , compat,sizeof (compat)); +qemu_fdt_setprop_sized_cells (ms->fdt, nodename,"reg" , +2 , base,2 , size); +qemu_fdt_setprop_cells (ms->fdt, nodename,"interrupts" , +GIC_FDT_IRQ_TYPE_SPI , irq,GIC_FDT_IRQ_FLAGS_LEVEL_HI , +GIC_FDT_IRQ_TYPE_SPI , irq +1 ,GIC_FDT_IRQ_FLAGS_LEVEL_HI , +GIC_FDT_IRQ_TYPE_SPI , irq +2 ,GIC_FDT_IRQ_FLAGS_LEVEL_HI , +GIC_FDT_IRQ_TYPE_SPI , irq +3 ,GIC_FDT_IRQ_FLAGS_LEVEL_HI , +GIC_FDT_IRQ_TYPE_SPI , irq +4 ,GIC_FDT_IRQ_FLAGS_LEVEL_HI , +GIC_FDT_IRQ_TYPE_SPI , irq +5 ,GIC_FDT_IRQ_FLAGS_LEVEL_HI , +GIC_FDT_IRQ_TYPE_SPI , irq +6 ,GIC_FDT_IRQ_FLAGS_LEVEL_HI , +GIC_FDT_IRQ_TYPE_SPI , irq +7 ,GIC_FDT_IRQ_FLAGS_LEVEL_HI , +GIC_FDT_IRQ_TYPE_SPI , irq +8 ,GIC_FDT_IRQ_FLAGS_LEVEL_HI ); + +qemu_fdt_setprop (ms->fdt, nodename,"interrupt-names" , irq_names, +sizeof (irq_names)); + +qemu_fdt_setprop_cell (ms->fdt, nodename,"clocks" , vms->clock_phandle); +qemu_fdt_setprop_string (ms->fdt, nodename,"clock-names" ,"apb_pclk" ); + +if (vms->iommu ==VIRT_IOMMU_SMMUV3 && vms->iommu_phandle) { +qemu_fdt_setprop_cells (ms->fdt, nodename,"iommus" , + vms->iommu_phandle, sid); +qemu_fdt_setprop (ms->fdt, nodename,"dma-coherent" ,NULL ,0 ); + } + +g_free (nodename); +} +static void create_rtc (const VirtMachineState *vms) {char *nodename;
@@ -1226,7 +1316,7 @@ static void create_pcie_irq_map(const MachineState *ms, 0x7 /* PCI irq */); } -static void create_smmu (const VirtMachineState *vms, +static void create_smmu (VirtMachineState *vms,PCIBus *bus) {char *node;
@@ -1248,6 +1338,8 @@ static void create_smmu(const VirtMachineState *vms,object_property_set_link (OBJECT (dev),"primary-bus" ,OBJECT (bus), &error_abort); + vms->smmuv3 = dev; +qdev_prop_set_uint32 (dev,"len-peri-sid-map" ,ARRAY_SIZE (smmuv3_peri_sidmap));
@@ -2079,6 +2171,8 @@ static void machvirt_init(MachineState *machine)create_pcie (vms); +create_dma (vms); +if (has_ged && aarch64 && firmware_loaded &&virt_is_acpi_enabled (vms)) { vms->acpi_dev =create_acpi_ged (vms); }else {
include/hw/arm/virt.h +2 -0
@@ -72,6 +72,7 @@ enum {VIRT_UART ,VIRT_MMIO ,VIRT_RTC , +VIRT_DMA ,VIRT_FW_CFG ,VIRT_PCIE , VIRT_PCIE_MMIO,
@@ -168,6 +169,7 @@ struct VirtMachineState {char *oem_id;char *oem_table_id;const uint16_t *peri_sidmap; +DeviceState *smmuv3; };#define VIRT_ECAM_ID (high) (high ? VIRT_HIGH_PCIE_ECAM : VIRT_PCIE_ECAM)
Signature · 1 line
2.30.2