ARM SMMUv3 Emulation Support
v6 cover ARM SMMUv3 Emulation Support
This series implements the emulation code for ARM SMMUv3. This is the continuation of Prem's work [1].
This v6 fixes some VFIO integration issues:
- Block replay was corrected.
- Range unmap is done before the replay (vhost issue).
- Introduction of a new CMDQ_OP_TLBI_NH_VA_AM command to handle invalidation of hugepages. DPDK was tested with a single assigned VF*.
Also 64b MMIO accesses should be fixed now.
For VFIO integration, a quirk is needed on guest side in the arm-smmu-v3 driver [2]. This quirk now can used both in dt and ACPI modes.
The smmu is instantiated when passing the smmu option to machvirt:
"-M virt-2.10,smmu". Most probably I will change this to instantiate it using -device option in next version and add an option to select caching mode.
The series needs to be further split to allow decent reviews and I don't expect any at this stage. However testing really is welcome.
I tested the following use cases:
- booted a guest in dt and acpi mode with an iommu_platform virtio-net-pci device (using dma ops). Tested with the following guest combinations: 4K page - 39 bit VA, 4K - 48b, 64K - 39b, 64K - 48b.
- booted a guest [2] with assigned PCIe device virtual functions:
- AMD Overdrive and igbvf passthrough (using gsi direct mapping)
- Cavium ThunderX and ixgbevf passthrough (using KVM MSI routing)
- ran DPDK testpmd on guest using a single passthroughed igbvf.
* dpdk testpmd using Using 2 igbvf's does not work at the moment:
- the issue is on guest side the 2 devices are put in the same domain by vfio_iommu_type1_attach_group() and mappings are replayed on a single devicephysical smmu
only for the 1st device. This causes an smmu fault. I will address this in next revision.
Known limitations:
- no VMSAv8-32 suport
- no nested stage support (S1 + S2)
- no support for HYP mappings
- register fine emulation, commands, interrupts and errors were not accurately tested. Handling is sufficient to run use cases described above though.
Best Regards
Eric
This series can be found at:
v6: https://github.com/eauger/qemu/tree/v2.10.0-rc2-SMMU-v6
v5: https://github.com/eauger/qemu/tree/v2.9-SMMU-v5
References:
[1] Prem's last iteration:
[2] [RFC v2 0/4] arm-smmu-v3 tlbi-on-map option
History:
v5 -> v6:
- Rebase on 2.10 and IOMMUMemoryRegion
- add ACPI TLBI_ON_MAP support (VFIO integration also works in ACPI mode)
- fix block replay
- handle implementation defined SMMU_CMD_TLBI_NH_VA_AM cmd (goes along with TLBI_ON_MAP FW quirk)
- replay systematically unmap the whole range first
- smmuv3_map_hook does not unmap anymore and the unmap is done before the replay
- add and use smmuv3_context_device_invalidate instead of blindly replaying everything
v4 -> v5:
- initial_level now part of SMMUTransCfg
- smmu_page_walk_64 takes into account the max input size
- implement sys->iommu_ops.replay and sys->iommu_ops.notify_flag_changed
- smmuv3_translate: bug fix: don't walk on bypass
- smmu_update_qreg: fix PROD index update
- I did not yet address Peter's comments as the code is not mature enough to be split into sub patches.
v3 -> v4 [Eric]:
- page table walk rewritten to allow scan of the page table within a range of IOVA. This prepares for VFIO integration and replay.
- configuration parsing partially reworked.
- do not advertise unsupported/untested features: S2, S1 + S2, HYP, PRI, ATS, ..
- added ACPI table generation
- migrated to dynamic traces
- mingw compilation fix
v2 -> v3 [Eric]:
- rebased on 2.9
- mostly code and patch reorganization to ease the review process
- optional patches removed. They may be handled separately. I am currently working on ACPI enablement.
- optional instantiation of the smmu in mach-virt
- removed [2/9] (fdt functions) since not mandated
- start splitting main patch into base and derived object
- no new function feature added
v1 -> v2 [Prem]:
- Adopted review comments from Eric Auger
- Make SMMU_DPRINTF to internally call qemu_log (since translation requests are too many, we need control on the type of log we want)
- SMMUTransCfg modified to suite simplicity
- Change RegInfo to uint64 register array
- Code cleanup
- Test cleanups
- Reshuffled patches
v0 -> v1 [Prem]:
- As per SMMUv3 spec 16.0 (only is_ste_consistant() is noticeable)
- Reworked register access/update logic
- Factored out translation code for
- single point bug fix
- sharing/removal in future
- (optional) Unit tests added, with PCI test device
- S1 with 4k/64k, S1+S2 with 4k/64k
- (S1 or S2) only can be verified by Linux 4.7 driver
- (optional) Priliminary ACPI support
v0 [Prem]:
- Implements SMMUv3 spec 11.0
- Supported for PCIe devices,
- Command Queue and Event Queue supported
- LPAE only, S1 is supported and Tested, S2 not tested
- BE mode Translation not supported
- IRQ support (legacy, no MSI)
- Tested with DPDK and e1000
Eric Auger (6):
hw/arm/smmu-common: smmu base class
hw/arm/virt: Add 2.11 machine type
hw/arm/virt: Add tlbi-on-map property to the smmuv3 node target/arm/kvm: Translate the MSI doorbell in kvm_arch_fixup_msi_route hw/arm/smmuv3: VFIO integration
hw/arm/virt-acpi-build: Use the ACPI_IORT_SMMU_V3_CACHING_MODE model
Prem Mallappa (3):
hw/arm/smmuv3: smmuv3 emulation model hw/arm/virt: Add SMMUv3 to the virt board hw/arm/virt-acpi-build: Add smmuv3 node in IORT table
default-configs/aarch64-softmmu.mak | 1 + hw/arm/Makefile.objs | 1 + hw/arm/smmu-common.c | 493 ++++++++++++ hw/arm/smmu-internal.h | 89 +++ hw/arm/smmuv3-internal.h | 652 ++++++++++++++++ hw/arm/smmuv3.c | 1412 +++++++++++++++++++++++++++++++++++ hw/arm/trace-events | 62 ++ hw/arm/virt-acpi-build.c | 58 +- hw/arm/virt.c | 110 ++- include/hw/acpi/acpi-defs.h | 15 + include/hw/arm/smmu-common.h | 126 ++++ include/hw/arm/smmuv3.h | 89 +++ include/hw/arm/virt.h | 5 + include/hw/compat.h | 3 + target/arm/kvm.c | 27 + target/arm/trace-events | 3 + 16 files changed, 3137 insertions(+), 9 deletions(-) create mode 100644 hw/arm/smmu-common.c create mode 100644 hw/arm/smmu-internal.h create mode 100644 hw/arm/smmuv3-internal.h create mode 100644 hw/arm/smmuv3.c create mode 100644 include/hw/arm/smmu-common.h create mode 100644 include/hw/arm/smmuv3.h
Signature · 1 line
2.5.5
RFC v6 1/9 hw/arm/smmu-common: smmu base class
Introduces the base device and class for the ARM smmu. Implements VMSAv8-64 table lookup and translation. VMSAv8-32 is not implemented.
---
v5 -> v6:
- use IOMMUMemoryRegion
- remove initial_lookup_level()
- fix block replay
v4 -> v5:
- add initial level in translation config
- implement block pte
- rename must_translate into nofail
- introduce call_entry_hook
- small changes to dynamic traces
- smmu_page_walk code moved from smmuv3.c to this file
- remove smmu_translate*
v3 -> v4:
- reworked page table walk to prepare for VFIO integration (capability to scan a range of IOVA). Same function is used for translate for a single iova. This is largely inspired from intel_iommu.c
- as the translate function was not straightforward to me, I tried to stick more closely to the VMSA spec.
- remove support of nested stage (kernel driver does not support it anyway)
- introduce smmu-internal.h to put page table definitions
- added smmu_find_as_from_bus_num
- SMMU_PCI_BUS_MAX and SMMU_PCI_DEVFN_MAX in smmu-common header
- new fields in SMMUState:
- iommu_ops, smmu_as_by_busptr, smmu_as_by_bus_num
- use error_report and trace events
- add aa64[] field in SMMUTransCfg
v3:
- moved the base code in a separate patch to ease the review.
- clearer separation between base class and smmuv3 class
- translate_* only implemented as class methods
--- default-configs/aarch64-softmmu.mak | 1 + hw/arm/Makefile.objs | 1 + hw/arm/smmu-common.c | 493 ++++++++++++++++++++++++++++++++++++ hw/arm/smmu-internal.h | 89 +++++++ hw/arm/trace-events | 14 + include/hw/arm/smmu-common.h | 126 +++++++++ 6 files changed, 724 insertions(+) create mode 100644 hw/arm/smmu-common.c create mode 100644 hw/arm/smmu-internal.h create mode 100644 include/hw/arm/smmu-common.h
default-configs/aarch64-softmmu.mak +1 -0
@@ -7,3 +7,4 @@ CONFIG_AUX=y CONFIG_DDC=y CONFIG_DPCD=y CONFIG_XLNX_ZYNQMP=y +CONFIG_ARM_SMMUV3=y
hw/arm/Makefile.objs +1 -0
@@ -19,3 +19,4 @@ obj-$(CONFIG_FSL_IMX31) += fsl-imx31.o kzm.o obj-$(CONFIG_FSL_IMX6) += fsl-imx6.o sabrelite.o obj-$(CONFIG_ASPEED_SOC) += aspeed_soc.o aspeed.o obj-$(CONFIG_MPS2) += mps2.o +obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o
hw/arm/smmu-common.c new file +493 -0
493 lines not on this page · raw message
hw/arm/smmu-internal.h new file +89 -0
89 lines not on this page · raw message
hw/arm/trace-events +14 -0
@@ -2,3 +2,17 @@ # hw/arm/virt-acpi-build.c virt_acpi_setup(void) "No fw cfg or ACPI disabled. Bailing out." + +# hw/arm/smmu-common.c + +smmu_page_walk_64(int stage, uint64_t baseaddr, int first_level, uint64_t start, uint64_t end) "stage=%d, baseaddr=0x%"PRIx64", first level=%d, start=0x%"PRIx64", end=0x%"PRIx64 +smmu_page_walk_level_in(int level, uint64_t baseaddr, int granule_sz, uint64_t start, uint64_t end, uint64_t subpage_size) "level=%d baseaddr=0x%"PRIx64" granule=%d, start=0x%"PRIx64" end=0x%"PRIx64", subpage_size=0x%lx" +smmu_page_walk_level(int level, uint64_t iova, size_t subpage_size, uint64_t baseaddr, uint32_t offset, uint64_t pte) "level=%d iova=0x%lx subpage_sz=0x%lx baseaddr=0x%"PRIx64" offset=%d => pte=0x%lx" +smmu_page_walk_level_res_invalid_pte(int stage, int level, uint64_t baseaddr, uint64_t pteaddr, uint32_t offset, uint64_t pte) "stage=%d level=%d base@=0x%"PRIx64" pte@=0x%"PRIx64" offset=%d pte=0x%lx" +smmu_page_walk_level_page_pte(int stage, int level, uint64_t iova, uint64_t baseaddr, uint64_t pteaddr, uint64_t pte, uint64_t address) "stage=%d level=%d iova=0x%"PRIx64" base@=0x%"PRIx64" pte@=0x%"PRIx64" pte=0x%"PRIx64" page address = 0x%"PRIx64 +smmu_page_walk_level_block_pte(int stage, int level, uint64_t baseaddr, uint64_t pteaddr, uint64_t pte, uint64_t iova, uint64_t gpa, int bsize_mb) "stage=%d level=%d base@=0x%"PRIx64" pte@=0x%"PRIx64" pte=0x%"PRIx64" iova=0x%"PRIx64" block address = 0x%"PRIx64" block size = %d MiB" +smmu_page_walk_level_table_pte(int stage, int level, uint64_t baseaddr, uint64_t pteaddr, uint64_t pte, uint64_t address) "stage=%d, level=%d base@=0x%"PRIx64" pte@=0x%"PRIx64" pte=0x%"PRIx64" next table address = 0x%"PRIx64 +smmu_get_pte(uint64_t baseaddr, int index, uint64_t pteaddr, uint64_t pte) "baseaddr=0x%"PRIx64" index=0x%x, pteaddr=0x%"PRIx64", pte=0x%"PRIx64 +smmu_set_translated_address(hwaddr iova, hwaddr pa) "iova = 0x%"PRIx64" -> pa = 0x%"PRIx64 +smmu_walk_pgtable(hwaddr iova, bool is_write) "Input addr: 0x%"PRIx64", is_write=%d" +smmu_walk_pgtable_out(hwaddr addr, uint32_t mask, int perm) "DONE: o/p addr:0x%"PRIx64" mask:0x%x perm:%d"
include/hw/arm/smmu-common.h new file +126 -0
126 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC v6 2/9 hw/arm/smmuv3: smmuv3 emulation model
In reply to Eric AugerFrom: Prem Mallappa <[email protected]>
Introduces the SMMUv3 derived model. This is based on System MMUv3 specification (v17).
---
v5 -> v6:
- Use IOMMUMemoryregion
- regs become uint32_t and fix 64b MMIO access (.impl)
- trace_smmuv3_write/read_mmio take the size param
v4 -> v5:
- change smmuv3_translate proto (IOMMUAccessFlags flag)
- has_stagex replaced by is_ste_stagex
- smmu_cfg_populate removed
- added smmuv3_decode_config and reworked error management
- remwork the naming of IOMMU mrs
- fix SMMU_CMDQ_CONS offset
v3 -> v4
- smmu_irq_update
- fix hash key allocation
- set smmu_iommu_ops
- set SMMU_REG_CR0,
- smmuv3_translate: ret.perm not set in bypass mode
- use trace events
- renamed STM2U64 into L1STD_L2PTR and STMSPAN into L1STD_SPAN
- rework smmu_find_ste
- fix tg2granule in TT0/0b10 corresponds to 16kB
v2 -> v3:
- move creation of include/hw/arm/smmuv3.h to this patch to fix compil issue
- compilation allowed
- fix sbus allocation in smmu_init_pci_iommu
- restructure code into headers
- misc cleanups
--- hw/arm/Makefile.objs | 2 +- hw/arm/smmuv3-internal.h | 651 ++++++++++++++++++++++++++ hw/arm/smmuv3.c | 1152 ++++++++++++++++++++++++++++++++++++++++++++++ hw/arm/trace-events | 34 ++ include/hw/arm/smmuv3.h | 89 ++++ 5 files changed, 1927 insertions(+), 1 deletion(-) create mode 100644 hw/arm/smmuv3-internal.h create mode 100644 hw/arm/smmuv3.c create mode 100644 include/hw/arm/smmuv3.h
hw/arm/Makefile.objs +1 -1
@@ -19,4 +19,4 @@ obj-$(CONFIG_FSL_IMX31) += fsl-imx31.o kzm.o obj-$(CONFIG_FSL_IMX6) += fsl-imx6.o sabrelite.o obj-$(CONFIG_ASPEED_SOC) += aspeed_soc.o aspeed.o obj-$(CONFIG_MPS2) += mps2.o -obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o +obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o smmuv3.o
hw/arm/smmuv3-internal.h new file +651 -0
651 lines not on this page · raw message
hw/arm/smmuv3.c new file +1152 -0
1,152 lines not on this page · raw message
hw/arm/trace-events +34 -0
@@ -16,3 +16,37 @@ smmu_get_pte(uint64_t baseaddr, int index, uint64_t pteaddr, uint64_t pte) "base smmu_set_translated_address(hwaddr iova, hwaddr pa) "iova = 0x%"PRIx64" -> pa = 0x%"PRIx64 smmu_walk_pgtable(hwaddr iova, bool is_write) "Input addr: 0x%"PRIx64", is_write=%d" smmu_walk_pgtable_out(hwaddr addr, uint32_t mask, int perm) "DONE: o/p addr:0x%"PRIx64" mask:0x%x perm:%d" + +#hw/arm/smmuv3.c +smmuv3_irq_update(uint32_t error, uint32_t gerror, uint32_t gerrorn) "<<<< error:0x%x gerror:0x%x gerrorn:0x%x" +smmuv3_irq_raise(int irq) "irq:%d" +smmuv3_unhandled_cmd(uint32_t type) "Unhandled command type=%d" +smmuv3_cmdq_consume(int error, bool enabled, uint32_t prod, uint32_t cons, uint8_t wrap_prod, uint8_t wrap_cons) "error=%d, enabled=%d prod=%d cons=%d wrap.prod=%d wrap.cons=%d" +smmuv3_cmdq_consume_details(hwaddr base, uint32_t cons, uint32_t prod, uint32_t word, uint8_t wrap_cons) "CMDQ base: 0x%"PRIx64" cons:%d prod:%d val:0x%x wrap:%d" +smmuv3_cmdq_opcode(const char *opcode) "<--- %s" +smmuv3_cmdq_cfgi_ste(int streamid) " |_ streamid =%d" +smmuv3_cmdq_cfgi_ste_range(int start, int end) " |_ start=0x%d - end=0x%d" +smmuv3_cmdq_tlbi_nh_va(int asid, int vmid, uint64_t addr) " |_ asid =%d vmid =%d addr=0x%"PRIx64 +smmuv3_cmdq_consume_sev(void) "CMD_SYNC CS=SEV not supported, ignoring" +smmuv3_cmdq_consume_out(uint8_t prod_wrap, uint32_t prod, uint8_t cons_wrap, uint32_t cons) "prod_wrap:%d, prod:0x%x cons_wrap:%d cons:0x%x" +smmuv3_update(bool is_empty, uint32_t prod, uint32_t cons, uint8_t prod_wrap, uint8_t cons_wrap) "q empty:%d prod:%d cons:%d p.wrap:%d p.cons:%d" +smmuv3_update_check_cmd(int error) "cmdq not enabled or error :0x%x" +smmuv3_update_irq(bool is_pending, uint32_t gerror, uint32_t gerrorn) "irq pend: %d gerror:0x%x gerrorn:0x%x" +smmuv3_is_ste_consistent(bool cfg, bool granule_supported, bool addr_oor, uint32_t aa64, int s2ha, int s2hd, uint64_t s2ttb ) "config[1]:%d gran:%d addr:%d aa64:%d s2ha:%d s2hd:%d s2ttb:0x%"PRIx64 +smmuv3_find_ste(uint16_t sid, uint32_t features, uint16_t sid_split) "SID:0x%x features:0x%x, sid_split:0x%x" +smmuv3_find_ste_2lvl(uint64_t strtab_base, hwaddr l1ptr, int l1_ste_offset, hwaddr l2ptr, int l2_ste_offset, int max_l2_ste) "strtab_base:0x%lx l1ptr:0x%"PRIx64" l1_off:0x%x, l2ptr:0x%"PRIx64" l2_off:0x%x max_l2_ste:%d" +smmuv3_get_ste(hwaddr addr) "STE addr: 0x%"PRIx64 +smmuv3_translate_bypass(const char *n, uint16_t sid, hwaddr addr, bool is_write) "%s sid=%d bypass iova:0x%"PRIx64" is_write=%d" +smmuv3_translate_in(uint16_t sid, int pci_bus_num, hwaddr strtab_base) "SID:0x%x bus:%d strtab_base:0x%"PRIx64 +smmuv3_get_cd(hwaddr addr) "CD addr: 0x%"PRIx64 +smmuv3_translate_ok(const char *n, uint16_t sid, hwaddr iova, hwaddr translated, int perm) "%s sid=%d iova=0x%"PRIx64" translated=0x%"PRIx64" perm=0x%x" +smmuv3_update_qreg(uint32_t cons, uint64_t val) "cons written : %d val:0x%"PRIx64 +smmuv3_write_mmio(hwaddr addr, uint64_t val, unsigned size) "addr: 0x%"PRIx64" val:0x%"PRIx64" size: 0x%x" +smmuv3_write_mmio_idr(hwaddr addr, uint64_t val) "write to RO/Unimpl reg 0x%lx val64:0x%lx" +smmuv3_write_mmio_evtq_cons_bef_clear(uint32_t prod, uint32_t cons, uint8_t prod_wrap, uint8_t cons_wrap) "Before clearing interrupt prod:0x%x cons:0x%x prod.w:%d cons.w:%d" +smmuv3_write_mmio_evtq_cons_after_clear(uint32_t prod, uint32_t cons, uint8_t prod_wrap, uint8_t cons_wrap) "after clearing interrupt prod:0x%x cons:0x%x prod.w:%d cons.w:%d" +smmuv3_read_mmio(hwaddr addr, uint64_t val, unsigned size) "addr: 0x%"PRIx64" val:0x%"PRIx64" size: 0x%x" +smmuv3_dump_ste(int i, uint32_t word0, int j, uint32_t word1) "STE[%2d]: 0x%x\t STE[%2d]: 0x%x" +smmuv3_dump_cd(int i, uint32_t word0, int j, uint32_t word1) "CD[%2d]: 0x%x\t CD[%2d]: 0x%x" +smmuv3_dump_cmd(int i, uint32_t word0, int j, uint32_t word1) "CMD[%2d]: 0x%x\t CMD[%2d]: 0x%x" +smmuv3_cfg_stage(int s, uint32_t oas, uint32_t tsz, uint64_t ttbr, bool aa64, uint32_t granule_sz, int initial_level) "TransCFG stage:%d oas:%d tsz:%d ttbr:0x%"PRIx64" aa64:%d granule_sz:%d, initial_level = %d"
include/hw/arm/smmuv3.h new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
2.5.5
RFC v6 3/9 hw/arm/virt: Add SMMUv3 to the virt board
In reply to Eric AugerFrom: Prem Mallappa <[email protected]>
Add code to instantiate an smmu-v3 in mach-virt. A new boolean flag is introduced in VirtMachineState to allow this instantiation. It is currently false.
---
v4 -> v5:
- add dma-coherent property
v2 -> v3:
- vbi was removed. Use vms instead
- migrate to new smmu binding format (iommu-map)
- don't use appendprop anymore
- add vms->smmu and guard instantiation with this latter
- interrupts type changed to edge
--- hw/arm/smmuv3.c | 5 +++-- hw/arm/virt.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/arm/smmuv3.h | 2 +- include/hw/arm/virt.h | 4 ++++ 4 files changed, 67 insertions(+), 3 deletions(-)
hw/arm/smmuv3.c +3 -2
@@ -1026,8 +1026,9 @@ static AddressSpace *smmu_find_add_as(PCIBus *bus, void *opaque, int devfn) sdev = sbus->pbdev[devfn];if (!sdev) { -char *name =g_strdup_printf ("%s-%d-%d" ,TYPE_SMMU_V3_DEV , -pci_bus_num (bus), devfn); +char *name =g_strdup_printf ("%s-%d-%d" , +TYPE_SMMUV3_IOMMU_MEMORY_REGION , +pci_bus_num (bus), devfn); sdev = sbus->pbdev[devfn] =g_malloc0 (sizeof (SMMUDevice)); sdev->smmu = s;
hw/arm/virt.c +59 -0
97 lines not on this page · raw message
include/hw/arm/smmuv3.h +1 -1
@@ -22,7 +22,7 @@#include "hw/arm/smmu-common.h" -#define TYPE_SMMUV3_IOMMU_MEMORY_REGION "smmuv3-iommu-iommu-memory-region" +#define TYPE_SMMUV3_IOMMU_MEMORY_REGION "smmuv3-iommu-memory-region"#define SMMU_NREGS 0x200
include/hw/arm/virt.h +4 -0
@@ -38,6 +38,7 @@#define NUM_GICV2M_SPIS 64#define NUM_VIRTIO_TRANSPORTS 32 +#define NUM_SMMU_IRQS 4#define ARCH_GICV3_MAINT_IRQ 9
@@ -59,6 +60,7 @@ enum {VIRT_GIC_V2M ,VIRT_GIC_ITS ,VIRT_GIC_REDIST , +VIRT_SMMU ,VIRT_UART ,VIRT_MMIO , VIRT_RTC,
@@ -95,6 +97,7 @@ typedef struct {bool highmem;bool its;bool virt; +bool smmu;int32_t gic_version;struct arm_boot_info bootinfo;const MemMapEntry *memmap;
@@ -105,6 +108,7 @@ typedef struct {uint32_t clock_phandle;uint32_t gic_phandle;uint32_t msi_phandle; +uint32_t smmu_phandle;int psci_conduit; } VirtMachineState;
Signature · 1 line
2.5.5
RFC v6 4/9 hw/arm/virt: Add 2.11 machine type
In reply to Eric AugerThe new machine type allows smmuv3 instantiation. A new option is introduced to turn the feature on/off (off by default).
---
v5 -> v6: machine 2_11
Another alternative would be to use the -device option as done on x86. As the smmu is a sysbus device, we would need to use the platform bus framework.
--- hw/arm/virt.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- include/hw/arm/virt.h | 1 + include/hw/compat.h | 3 +++ 3 files changed, 52 insertions(+), 2 deletions(-)
hw/arm/virt.c +48 -2
@@ -1543,6 +1543,20 @@ static void machvirt_init(MachineState *machine)create_platform_bus (vms, pic); } +static bool virt_get_smmu (Object *obj,Error **errp) +{ +VirtMachineState *vms =VIRT_MACHINE (obj); + +return vms->smmu; +} + +static void virt_set_smmu (Object *obj,bool value,Error **errp) +{ +VirtMachineState *vms =VIRT_MACHINE (obj); + + vms->smmu = value; +} +static bool virt_get_secure (Object *obj,Error **errp) {VirtMachineState *vms =VIRT_MACHINE (obj);
@@ -1698,7 +1712,7 @@ static void machvirt_machine_init(void) }type_init (machvirt_machine_init); -static void virt_2_10_instance_init (Object *obj) +static void virt_2_11_instance_init (Object *obj) {VirtMachineState *vms =VIRT_MACHINE (obj);VirtMachineClass *vmc =VIRT_MACHINE_GET_CLASS (vms);
@@ -1754,14 +1768,46 @@ static void virt_2_10_instance_init(Object *obj) NULL); } +if (vmc->no_smmu) { + vms->smmu = false; + }else { +/* Default disallows smmu instantiation */ + vms->smmu = false; +object_property_add_bool (obj,"smmu" , virt_get_smmu, + virt_set_smmu,NULL ); +object_property_set_description (obj,"smmu" , +"Set on/off to enable/disable " +"smmu instantiation (default off)" , +NULL ); + } + vms->memmap = a15memmap; vms->irqmap = a15irqmap; } +static void virt_machine_2_11_options (MachineClass *mc) +{ +} +DEFINE_VIRT_MACHINE_AS_LATEST(2, 11) + +#define VIRT_COMPAT_2_10 \ + HW_COMPAT_2_10 + +static void virt_2_10_instance_init (Object *obj) +{ +virt_2_11_instance_init (obj); +} +static void virt_machine_2_10_options (MachineClass *mc) { +VirtMachineClass *vmc =VIRT_MACHINE_CLASS (OBJECT_CLASS (mc)); + +virt_machine_2_11_options (mc); +SET_MACHINE_COMPAT (mc,VIRT_COMPAT_2_10 ); + + vmc->no_smmu = true; } -DEFINE_VIRT_MACHINE_AS_LATEST(2, 10) +DEFINE_VIRT_MACHINE(2, 10)#define VIRT_COMPAT_2_9 \ HW_COMPAT_2_9
include/hw/arm/virt.h +1 -0
@@ -86,6 +86,7 @@ typedef struct {bool disallow_affinity_adjustment;bool no_its;bool no_pmu; +bool no_smmu;bool claim_edge_triggered_timers; } VirtMachineClass;
include/hw/compat.h +3 -0
@@ -1,6 +1,9 @@#ifndef HW_COMPAT_H #define HW_COMPAT_H +#define HW_COMPAT_2_10 \ +/* empty */ +#define HW_COMPAT_2_9 \ {\ .driver = "pci-bridge",\
Signature · 1 line
2.5.5
RFC v6 5/9 hw/arm/virt-acpi-build: Add smmuv3 node in IORT table
In reply to Eric AugerFrom: Prem Mallappa <[email protected]>
This patch builds the smmuv3 node in the ACPI IORT table.
The RID space of the root complex, which spans 0x0-0x10000 maps to streamid space 0x0-0x10000 in smmuv3, which in turn maps to deviceid space 0x0-0x10000 in the ITS group.
The guest must feature the IOMMU probe deferral series
(https://lkml.org/lkml/2017/4/10/214) wich fixes streamid multiple lookup. This bug is not related to the SMMU emulation.
---
v2 -> v3:
- integrate into the existing IORT table made up of ITS, RC nodes
- take into account vms->smmu
- match linux actbl2.h acpi_iort_smmu_v3 field names
--- hw/arm/virt-acpi-build.c | 56 +++++++++++++++++++++++++++++++++++++++------ include/hw/acpi/acpi-defs.h | 15 ++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-)
hw/arm/virt-acpi-build.c +49 -7
@@ -393,19 +393,26 @@ build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned xsdt_tbl_offset) }static void -build_iort (GArray *table_data,BIOSLinker *linker) +build_iort (GArray *table_data,BIOSLinker *linker,VirtMachineState *vms) { -int iort_start = table_data->len; +int nb_nodes, iort_start = table_data->len;AcpiIortIdMapping *idmap;AcpiIortItsGroup *its;AcpiIortTable *iort; -size_t node_size, iort_length; +AcpiIortSmmu3 *smmu; +size_t node_size, iort_length, smmu_offset =0 ;AcpiIortRC *rc; iort =acpi_data_push (table_data,sizeof (*iort)); +if (vms->smmu) { + nb_nodes =3 ;/* RC, ITS, SMMUv3 */ + }else { + nb_nodes =2 ;/* RC, ITS */ + } + iort_length =sizeof (*iort); - iort->node_count =cpu_to_le32 (2 );/* RC and ITS nodes */ + iort->node_count =cpu_to_le32 (nb_nodes); iort->node_offset =cpu_to_le32 (sizeof (*iort));/* ITS group node */
@@ -418,6 +425,35 @@ build_iort(GArray *table_data, BIOSLinker *linker) its->its_count =cpu_to_le32 (1 ); its->identifiers[0 ] =0 ;/* MADT translation_id */ +if (vms->smmu) { +int irq = vms->irqmap[VIRT_SMMU ]; + +/* SMMUv3 node */ + smmu_offset =cpu_to_le32 (iort->node_offset + node_size); + node_size =sizeof (*smmu) +sizeof (*idmap); + iort_length += node_size; + smmu =acpi_data_push (table_data, node_size); + + + smmu->type =ACPI_IORT_NODE_SMMU_V3 ; + smmu->length =cpu_to_le16 (node_size); + smmu->mapping_count =cpu_to_le32 (1 ); + smmu->mapping_offset =cpu_to_le32 (sizeof (*smmu)); + smmu->base_address =cpu_to_le64 (vms->memmap[VIRT_SMMU ].base); + smmu->event_gsiv =cpu_to_le32 (irq); + smmu->pri_gsiv =cpu_to_le32 (irq +1 ); + smmu->gerr_gsiv =cpu_to_le32 (irq +2 ); + smmu->sync_gsiv =cpu_to_le32 (irq +3 ); + +/* Identity RID mapping covering the whole input RID range */ + idmap = &smmu->id_mapping_array[0 ]; + idmap->input_base =0 ; + idmap->id_count =cpu_to_le32 (0xFFFF ); + idmap->output_base =0 ; +/* output IORT node is the ITS group node (the first node) */ + idmap->output_reference =cpu_to_le32 (iort->node_offset); + } +/* Root Complex Node */ node_size =sizeof (*rc) +sizeof (*idmap); iort_length += node_size;
@@ -438,8 +474,14 @@ build_iort(GArray *table_data, BIOSLinker *linker) idmap->input_base =0 ; idmap->id_count =cpu_to_le32 (0xFFFF ); idmap->output_base =0 ; -/* output IORT node is the ITS group node (the first node) */ - idmap->output_reference =cpu_to_le32 (iort->node_offset); + +if (vms->smmu) { +/* output IORT node is the smmuv3 node */ + idmap->output_reference =cpu_to_le32 (smmu_offset); + }else { +/* output IORT node is the ITS group node (the first node) */ + idmap->output_reference =cpu_to_le32 (iort->node_offset); + } iort->length =cpu_to_le32 (iort_length);
@@ -782,7 +824,7 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)if (its_class_name () && !vmc->no_its) {acpi_add_table (table_offsets, tables_blob); -build_iort (tables_blob, tables->linker); +build_iort (tables_blob, tables->linker, vms); }/* XSDT is pointed to by RSDP */
include/hw/acpi/acpi-defs.h +15 -0
@@ -697,6 +697,21 @@ struct AcpiIortItsGroup { }QEMU_PACKED ;typedef struct AcpiIortItsGroup AcpiIortItsGroup ; +struct AcpiIortSmmu3 { +ACPI_IORT_NODE_HEADER_DEF + uint64_t base_address; +uint32_t flags; +uint32_t reserved2; +uint64_t vatos_address; +uint32_t model; +uint32_t event_gsiv; +uint32_t pri_gsiv; +uint32_t gerr_gsiv; +uint32_t sync_gsiv; +AcpiIortIdMapping id_mapping_array[0 ]; +}QEMU_PACKED ; +typedef struct AcpiIortSmmu3 AcpiIortSmmu3 ; +struct AcpiIortRC {ACPI_IORT_NODE_HEADER_DEF AcpiIortMemoryAccess memory_properties;
Signature · 1 line
2.5.5
RFC v6 6/9 hw/arm/virt: Add tlbi-on-map property to the smmuv3 node
In reply to Eric AugerFor VFIO integration we need to update physical IOMMU mappings each time the guest updates the vIOMMU translation structures. For that, we rely on a special smmuv3 option, "tlbi-on-map" which forces TLB invalidations on map (this mode is similar to the Intel VTD caching Mode). The smmuv3 driver then sends SMMU_CMD_TLBI_NH_VA commands, upon which we will update the physical mappings.
--- hw/arm/virt.c | 1 + 1 file changed, 1 insertion(+)
hw/arm/virt.c +1 -0
@@ -1034,6 +1034,7 @@ static void create_smmu(const VirtMachineState *vms, qemu_irq *pic)qemu_fdt_setprop_cell (vms->fdt, smmu,"clocks" , vms->clock_phandle);qemu_fdt_setprop_string (vms->fdt, smmu,"clock-names" ,"apb_pclk" );qemu_fdt_setprop (vms->fdt, smmu,"dma-coherent" ,NULL ,0 ); +qemu_fdt_setprop (vms->fdt, smmu,"tlbi-on-map" ,NULL ,0 );qemu_fdt_setprop_cell (vms->fdt, smmu,"#iommu-cells" ,1 );
Signature · 1 line
2.5.5
RFC v6 7/9 target/arm/kvm: Translate the MSI doorbell in kvm_arch_fixup_msi_route
In reply to Eric AugerIn case the MSI is translated by an IOMMU we need to fixup the MSI route with the translated address.
---
v5 -> v6:
- use IOMMUMemoryRegionClass API
It is still unclear to me if we need to register an IOMMUNotifier to handle any change in the MSI doorbell which would occur behind the scene and would not lead to any call to kvm_arch_fixup_msi_route().
--- target/arm/kvm.c | 27 +++++++++++++++++++++++++++ target/arm/trace-events | 3 +++ 2 files changed, 30 insertions(+)
target/arm/kvm.c +27 -0
@@ -20,8 +20,13 @@#include "sysemu/kvm.h" #include "kvm_arm.h" #include "cpu.h" +#include "trace.h" #include "internals.h" #include "hw/arm/arm.h" +#include "hw/pci/pci.h" +#include "hw/pci/msi.h" +#include "hw/arm/smmu-common.h" +#include "hw/arm/smmuv3.h" #include "exec/memattrs.h" #include "exec/address-spaces.h" #include "hw/boards.h"
@@ -662,6 +667,28 @@ int kvm_arm_vgic_probe(void)int kvm_arch_fixup_msi_route (struct kvm_irq_routing_entry *route,uint64_t address,uint32_t data,PCIDevice *dev) { +AddressSpace *as =pci_device_iommu_address_space (dev); +IOMMUMemoryRegionClass *imrc; +IOMMUTLBEntry entry; +SMMUDevice *sdev; + +if (as == &address_space_memory) { +return 0 ; + } + +/* MSI doorbell address is translated by an IOMMU */ + sdev =container_of (as, SMMUDevice, as); + imrc =IOMMU_MEMORY_REGION_GET_CLASS (&sdev->iommu); + + entry = imrc->translate (&sdev->iommu, address,IOMMU_WO ); + + route->u.msi.address_lo = entry.translated_addr; + route->u.msi.address_hi = entry.translated_addr >>32 ; + +trace_kvm_arm_fixup_msi_route (address, sdev->devfn, + sdev->iommu.parent_obj.name, + entry.translated_addr); +return 0 ; }
target/arm/trace-events +3 -0
@@ -8,3 +8,6 @@ arm_gt_tval_write(int timer, uint64_t value) "gt_tval_write: timer %d value 0x%" arm_gt_ctl_write(int timer, uint64_t value) "gt_ctl_write: timer %d value 0x%" PRIx64 arm_gt_imask_toggle(int timer, int irqstate) "gt_ctl_write: timer %d IMASK toggle, new irqstate %d" arm_gt_cntvoff_write(uint64_t value) "gt_cntvoff_write: value 0x%" PRIx64 + +# target/arm/kvm.c +kvm_arm_fixup_msi_route(uint64_t iova, uint32_t devid, const char *name, uint64_t gpa) "MSI addr = 0x%"PRIx64" is translated for devfn=%d through %s into 0x%"PRIx64
Signature · 1 line
2.5.5
RFC v6 8/9 hw/arm/smmuv3: VFIO integration
In reply to Eric AugerThis patch allows doing PCIe passthrough with a guest exposed with a vSMMUv3. It implements the replay and notify_flag_changed iommu ops. Also on TLB and data structure invalidation commands, we replay the mappings so that the physical IOMMU implements updated stage 1 settings (Guest IOVA -> Guest PA) + stage 2 settings.
This works only if the guest smmuv3 driver implements the "tlbi-on-map" option.
---
v5 -> v6:
- use IOMMUMemoryRegion
- handle implementation defined SMMU_CMD_TLBI_NH_VA_AM cmd (goes along with TLBI_ON_MAP FW quirk)
- replay systematically unmap the whole range first
- smmuv3_map_hook does not unmap anymore and the unmap is done before the replay
- add and use smmuv3_context_device_invalidate instead of blindly replaying everything
--- hw/arm/smmuv3-internal.h | 1 + hw/arm/smmuv3.c | 265 ++++++++++++++++++++++++++++++++++++++++++++++- hw/arm/trace-events | 14 +++ 3 files changed, 277 insertions(+), 3 deletions(-)
hw/arm/smmuv3-internal.h +1 -0
@@ -344,6 +344,7 @@ enum {SMMU_CMD_RESUME =0x44 ,SMMU_CMD_STALL_TERM ,SMMU_CMD_SYNC ,/* 0x46 */ + SMMU_CMD_TLBI_NH_VA_AM = 0x8F, /* VIOMMU Impl Defined */ }; static const char *cmd_stringify[] = {
hw/arm/smmuv3.c +262 -3
326 lines not on this page · raw message
hw/arm/trace-events +14 -0
@@ -27,6 +27,7 @@ smmuv3_cmdq_opcode(const char *opcode) "<--- %s" smmuv3_cmdq_cfgi_ste(int streamid) " |_ streamid =%d" smmuv3_cmdq_cfgi_ste_range(int start, int end) " |_ start=0x%d - end=0x%d" smmuv3_cmdq_tlbi_nh_va(int asid, int vmid, uint64_t addr) " |_ asid =%d vmid =%d addr=0x%"PRIx64 +smmuv3_cmdq_tlbi_nh_va_am(int asid, int am, size_t size, uint64_t addr) " |_ asid =%d am =%d size=0x%lx addr=0x%"PRIx64 smmuv3_cmdq_consume_sev(void) "CMD_SYNC CS=SEV not supported, ignoring" smmuv3_cmdq_consume_out(uint8_t prod_wrap, uint32_t prod, uint8_t cons_wrap, uint32_t cons) "prod_wrap:%d, prod:0x%x cons_wrap:%d cons:0x%x" smmuv3_update(bool is_empty, uint32_t prod, uint32_t cons, uint8_t prod_wrap, uint8_t cons_wrap) "q empty:%d prod:%d cons:%d p.wrap:%d p.cons:%d"
@@ -50,3 +51,16 @@ smmuv3_dump_ste(int i, uint32_t word0, int j, uint32_t word1) "STE[%2d]: 0x%x\t smmuv3_dump_cd(int i, uint32_t word0, int j, uint32_t word1) "CD[%2d]: 0x%x\t CD[%2d]: 0x%x" smmuv3_dump_cmd(int i, uint32_t word0, int j, uint32_t word1) "CMD[%2d]: 0x%x\t CMD[%2d]: 0x%x" smmuv3_cfg_stage(int s, uint32_t oas, uint32_t tsz, uint64_t ttbr, bool aa64, uint32_t granule_sz, int initial_level) "TransCFG stage:%d oas:%d tsz:%d ttbr:0x%"PRIx64" aa64:%d granule_sz:%d, initial_level = %d" + +smmuv3_replay(uint16_t sid, bool enabled) "sid=%d, enabled=%d" +smmuv3_replay_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_map_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s" +smmuv3_notify_flag_del(const char *iommu) "DEL SMMUNotifier node for iommu mr=%s" +smmuv3_replay_single(const char *name, uint64_t iova, void *n) "iommu mr=%s iova=0x%"PRIx64" n=%p" +smmuv3_replay_range(const char *name, uint64_t iova, size_t size, void *n) "iommu mr=%s iova=0x%"PRIx64" size=0x%lx n=%p" +smmuv3_replay_all(const char *name) "iommu mr=%s" +smmuv3_notify_all(const char *name, uint64_t iova) "iommu mr=%s iova=0x%"PRIx64 +smmuv3_unmap_notifier(uint8_t bus, uint8_t slot, uint8_t fn, uint64_t iova, uint64_t size) "Device %02x:%02x.%x start 0x%"PRIx64" size 0x%"PRIx64 +smmuv3_context_device_invalidate(uint32_t sid) "sid=%d" +
Signature · 1 line
2.5.5
Re: [Qemu-arm] [RFC v6 8/9] hw/arm/smmuv3: VFIO integration
Hi Eric,
Quoted 11 lines · Eric Auger
This patch allows doing PCIe passthrough with a guest exposed with a vSMMUv3. It implements the replay and
notify_flag_changediommu ops. Also on TLB and data structure invalidation commands, we replay the mappings so that the physical IOMMU implements updated stage 1 settings (Guest IOVA -> Guest PA) + stage 2 settings.This works only if the guest smmuv3 driver implements the "tlbi-on-map" option.
Signed-off-by Eric Auger <[email protected]>
Appreciate if you could point to some documentation that explains the attach, detach, map,unmap flows for a passthrough device with emulated IOMMU.
Thanks.
Quoted 399 lines
---
v5 -> v6:
- use IOMMUMemoryRegion
- handle implementation definedSMMU_CMD_TLBI_NH_VA_AMcmd (goes along withTLBI_ON_MAPFW quirk)
- replay systematically unmap the whole range first
-smmuv3_map_hookdoes not unmap anymore and the unmap is done before the replay
- add and usesmmuv3_context_device_invalidateinstead of blindly replaying everything--- hw/arm/smmuv3-internal.h | 1 + hw/arm/smmuv3.c | 265 ++++++++++++++++++++++++++++++++++++++++++++++- hw/arm/trace-events | 14 +++ 3 files changed, 277 insertions(+), 3 deletions(-)hw/arm/smmuv3-internal.h +1
-0@@ -344,6 +344,7 @@ enum {SMMU_CMD_RESUME =0x44 ,SMMU_CMD_STALL_TERM ,SMMU_CMD_SYNC ,/* 0x46 */ + SMMU_CMD_TLBI_NH_VA_AM = 0x8F, /* VIOMMU Impl Defined */ }; static const char *cmd_stringify[] = {hw/arm/smmuv3.c +262
-3@@ -25,6 +25,7 @@#include "exec/address-spaces.h" #include "trace.h" #include "qemu/error-report.h" +#include "exec/target_page.h" #include "hw/arm/smmuv3.h" #include "smmuv3-internal.h" @@ -143,6 +144,71 @@ static MemTxResult smmu_read_cmdq(SMMUV3State *s, Cmd *cmd)return ret; } +static void smmuv3_replay_all (SMMUState *s) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +trace_smmuv3_replay_all (node->sdev->iommu.parent_obj.name); +memory_region_iommu_replay_all (&node->sdev->iommu); + } +} + +/* Replay the mappings for a given streamid */ +static void smmuv3_context_device_invalidate (SMMUState *s,uint16_t sid) +{ +uint8_t bus_n, devfn; +SMMUPciBus *smmu_bus; +SMMUDevice *smmu; + +trace_smmuv3_context_device_invalidate (sid); + bus_n =PCI_BUS_NUM (sid); + smmu_bus =smmu_find_as_from_bus_num (s, bus_n); +if (smmu_bus) { + devfn =PCI_FUNC (sid); + smmu = smmu_bus->pbdev[devfn]; +if (smmu) { +memory_region_iommu_replay_all (&smmu->iommu); + } + } +} + +static void smmuv3_replay_single (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova); + +static void smmuv3_replay_range (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova,size_t nb_pages); + +static void smmuv3_notify_single (SMMUState *s,uint64_t iova) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_notify_all (node->sdev->iommu.parent_obj.name, iova); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_replay_single (mr, n, iova); + } + } +} + +static void smmuv3_notify_range (SMMUState *s,uint64_t iova,size_t size) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_notify_all (node->sdev->iommu.parent_obj.name, iova); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_replay_range (mr, n, iova, size); + } + } +} +static int smmu_cmdq_consume (SMMUV3State *s) {uint32_t error =SMMU_CMD_ERR_NONE ;@@ -178,28 +244,38 @@ static int smmu_cmdq_consume(SMMUV3State *s)break ;case SMMU_CMD_PREFETCH_CONFIG :case SMMU_CMD_PREFETCH_ADDR : +break ;case SMMU_CMD_CFGI_STE : {uint32_t streamid = cmd.word[1 ];trace_smmuv3_cmdq_cfgi_ste (streamid); -break ; +smmuv3_context_device_invalidate (&s->smmu_state, streamid); +break ; }case SMMU_CMD_CFGI_STE_RANGE :/* same as SMMU_CMD_CFGI_ALL */ { -uint32_t start = cmd.word[1 ], range, end; +uint32_t start = cmd.word[1 ], range, end, i; range =extract32 (cmd.word[2 ],0 ,5 ); end = start + (1 << (range +1 )) -1 ;trace_smmuv3_cmdq_cfgi_ste_range (start, end); +for (i = start; i <= end; i++) { +smmuv3_context_device_invalidate (&s->smmu_state, i); + }break ; }case SMMU_CMD_CFGI_CD :case SMMU_CMD_CFGI_CD_ALL : + { +uint32_t streamid = cmd.word[1 ]; + +smmuv3_context_device_invalidate (&s->smmu_state, streamid);break ; + }case SMMU_CMD_TLBI_NH_ALL :case SMMU_CMD_TLBI_NH_ASID : -printf ("%s TLBI* replay\n" , __func__); +smmuv3_replay_all (&s->smmu_state);break ;case SMMU_CMD_TLBI_NH_VA : {@@ -210,6 +286,20 @@ static int smmu_cmdq_consume(SMMUV3State *s)uint64_t addr = high <<32 | (low <<12 );trace_smmuv3_cmdq_tlbi_nh_va (asid, vmid, addr); +smmuv3_notify_single (&s->smmu_state, addr); +break ; + } +case SMMU_CMD_TLBI_NH_VA_AM : + { +int asid =extract32 (cmd.word[1 ],16 ,16 ); +int am =extract32 (cmd.word[1 ],0 ,16 ); +uint64_t low =extract32 (cmd.word[2 ],12 ,20 ); +uint64_t high = cmd.word[3 ]; +uint64_t addr = high <<32 | (low <<12 ); +size_t size = am <<12 ; + +trace_smmuv3_cmdq_tlbi_nh_va_am (asid, am, addr, size); +smmuv3_notify_range (&s->smmu_state, addr, size);break ; }case SMMU_CMD_TLBI_NH_VAA :@@ -222,6 +312,7 @@ static int smmu_cmdq_consume(SMMUV3State *s)case SMMU_CMD_TLBI_S12_VMALL :case SMMU_CMD_TLBI_S2_IPA :case SMMU_CMD_TLBI_NSNH_ALL : +smmuv3_replay_all (&s->smmu_state);break ;case SMMU_CMD_ATC_INV :case SMMU_CMD_PRI_RESP :@@ -804,6 +895,172 @@ out:return entry; } +static int smmuv3_replay_hook (IOMMUTLBEntry *entry,void *private) +{ +trace_smmuv3_replay_hook (entry->iova, entry->translated_addr, + entry->addr_mask, entry->perm); +memory_region_notify_one ((IOMMUNotifier *)private, entry); +return 0 ; +} + +static int smmuv3_map_hook (IOMMUTLBEntry *entry,void *private) +{ +trace_smmuv3_map_hook (entry->iova, entry->translated_addr, + entry->addr_mask, entry->perm); +memory_region_notify_one ((IOMMUNotifier *)private, entry); +return 0 ; +} + +/* Unmap the whole range in the notifier's scope. */ +static void smmuv3_unmap_notifier (SMMUDevice *sdev,IOMMUNotifier *n) +{ +IOMMUTLBEntry entry; +hwaddr size; +hwaddr start = n->start; +hwaddr end = n->end; + + size = end - start +1 ; + + entry.target_as = &address_space_memory; +/* Adjust iova for the size */ + entry.iova = n->start & ~(size -1 ); +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ; + +/* TODO: check start/end/size/mask */ + +trace_smmuv3_unmap_notifier (pci_bus_num (sdev->bus), +PCI_SLOT (sdev->devfn), +PCI_FUNC (sdev->devfn), + entry.iova, size); + +memory_region_notify_one (n, &entry); +} + +static void smmuv3_replay (IOMMUMemoryRegion *mr,IOMMUNotifier *n) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +int ret; + +smmuv3_unmap_notifier (sdev, n); + + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } +/* is the smmu enabled */ + sbc->page_walk_64 (&cfg,0 , (1ULL << (64 - cfg.tsz)) -1 , false, + smmuv3_replay_hook, n); +} +static void smmuv3_replay_range (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova,size_t size) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +IOMMUTLBEntry entry; +int ret; + +trace_smmuv3_replay_range (mr->parent_obj.name, iova, size, n); + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } + +/* first unmap */ + entry.target_as = &address_space_memory; + entry.iova = iova & ~(size -1 ); + entry.addr_mask = size -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); + +/* then figure out if a new mapping needs to be applied */ + sbc->page_walk_64 (&cfg, iova, iova + entry.addr_mask , false, + smmuv3_map_hook, n); +} + +static void smmuv3_replay_single (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +size_t target_page_size =qemu_target_page_size (); +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +IOMMUTLBEntry entry; +int ret; + +trace_smmuv3_replay_single (mr->parent_obj.name, iova, n); + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } + +/* first unmap */ + entry.target_as = &address_space_memory; + entry.iova = iova & ~(target_page_size -1 ); + entry.addr_mask = target_page_size -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); + +/* then figure out if a new mapping needs to be applied */ + sbc->page_walk_64 (&cfg, iova, iova +1 , false, + smmuv3_map_hook, n); +} + +static void smmuv3_notify_flag_changed (IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old, +IOMMUNotifierFlag new) +{ +SMMUDevice *sdev =container_of (iommu, SMMUDevice, iommu); +SMMUV3State *s3 = sdev->smmu; +SMMUState *s = &(s3->smmu_state); +SMMUNotifierNode *node =NULL ; +SMMUNotifierNode *next_node =NULL ; + +if (old ==IOMMU_NOTIFIER_NONE ) { +trace_smmuv3_notify_flag_add (iommu->parent_obj.name); + node =g_malloc0 (sizeof (*node)); + node->sdev = sdev; +QLIST_INSERT_HEAD (&s->notifiers_list, node, next); +return ; + } + +/* update notifier node with new flags */ + QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { +if (node->sdev == sdev) { +if (new ==IOMMU_NOTIFIER_NONE ) { +trace_smmuv3_notify_flag_del (iommu->parent_obj.name); +QLIST_REMOVE (node, next); +g_free (node); + } +return ; + } + } +}static inline void smmu_update_base_reg (SMMUV3State *s,uint64_t *base,uint64_t val)@@ -1125,6 +1382,8 @@ static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass,IOMMUMemoryRegionClass *imrc =IOMMU_MEMORY_REGION_CLASS (klass); imrc->translate = smmuv3_translate; + imrc->notify_flag_changed = smmuv3_notify_flag_changed; + imrc->replay = smmuv3_replay; }static const TypeInfo smmuv3_type_info = {hw/arm/trace-events +14
-0@@ -27,6 +27,7 @@ smmuv3_cmdq_opcode(const char *opcode) "<--- %s" smmuv3_cmdq_cfgi_ste(int streamid) " |_ streamid =%d" smmuv3_cmdq_cfgi_ste_range(int start, int end) " |_ start=0x%d - end=0x%d" smmuv3_cmdq_tlbi_nh_va(int asid, int vmid, uint64_t addr) " |_ asid =%d vmid =%d addr=0x%"PRIx64 +smmuv3_cmdq_tlbi_nh_va_am(int asid, int am, size_t size, uint64_t addr) " |_ asid =%d am =%d size=0x%lx addr=0x%"PRIx64 smmuv3_cmdq_consume_sev(void) "CMD_SYNC CS=SEV not supported, ignoring" smmuv3_cmdq_consume_out(uint8_t prod_wrap, uint32_t prod, uint8_t cons_wrap, uint32_t cons) "prod_wrap:%d, prod:0x%x cons_wrap:%d cons:0x%x" smmuv3_update(bool is_empty, uint32_t prod, uint32_t cons, uint8_t prod_wrap, uint8_t cons_wrap) "q empty:%d prod:%d cons:%d p.wrap:%d p.cons:%d"@@ -50,3 +51,16 @@ smmuv3_dump_ste(int i, uint32_t word0, int j, uint32_t word1) "STE[%2d]: 0x%x\t smmuv3_dump_cd(int i, uint32_t word0, int j, uint32_t word1) "CD[%2d]: 0x%x\t CD[%2d]: 0x%x" smmuv3_dump_cmd(int i, uint32_t word0, int j, uint32_t word1) "CMD[%2d]: 0x%x\t CMD[%2d]: 0x%x" smmuv3_cfg_stage(int s, uint32_t oas, uint32_t tsz, uint64_t ttbr, bool aa64, uint32_t granule_sz, int initial_level) "TransCFG stage:%d oas:%d tsz:%d ttbr:0x%"PRIx64" aa64:%d granule_sz:%d, initial_level = %d" + +smmuv3_replay(uint16_t sid, bool enabled) "sid=%d, enabled=%d" +smmuv3_replay_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_map_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s" +smmuv3_notify_flag_del(const char *iommu) "DEL SMMUNotifier node for iommu mr=%s" +smmuv3_replay_single(const char *name, uint64_t iova, void *n) "iommu mr=%s iova=0x%"PRIx64" n=%p" +smmuv3_replay_range(const char *name, uint64_t iova, size_t size, void *n) "iommu mr=%s iova=0x%"PRIx64" size=0x%lx n=%p" +smmuv3_replay_all(const char *name) "iommu mr=%s" +smmuv3_notify_all(const char *name, uint64_t iova) "iommu mr=%s iova=0x%"PRIx64 +smmuv3_unmap_notifier(uint8_t bus, uint8_t slot, uint8_t fn, uint64_t iova, uint64_t size) "Device %02x:%02x.%x start 0x%"PRIx64" size 0x%"PRIx64 +smmuv3_context_device_invalidate(uint32_t sid) "sid=%d" +Signature · 1 line
2.5.5
Signature · 1 line
Linu cherian
Re: [Qemu-arm] [RFC v6 8/9] hw/arm/smmuv3: VFIO integration
In reply to Eric AugerHi Eric,
Quoted 10 lines · Eric Auger
This patch allows doing PCIe passthrough with a guest exposed with a vSMMUv3. It implements the replay and
notify_flag_changediommu ops. Also on TLB and data structure invalidation commands, we replay the mappings so that the physical IOMMU implements updated stage 1 settings (Guest IOVA -> Guest PA) + stage 2 settings.This works only if the guest smmuv3 driver implements the "tlbi-on-map" option.
Signed-off-by Eric Auger <[email protected]>
Tried out launching a guest with Qemu option "-machine virt-2.10,smmu" and a 1G Ethernet controller as vfio-pci device. It works fine for me.
Qemu source: https://github.com/eauger/qemu.git Branch: v2.10.0-rc2-SMMU-v6
But had to make this change,
hw/arm/virt.c +1 -1
@@ -1806,7 +1806,7 @@ static void virt_machine_2_10_options(MachineClass *mc)virt_machine_2_11_options (mc);SET_MACHINE_COMPAT (mc,VIRT_COMPAT_2_10 ); - vmc->no_smmu = true; + vmc->no_smmu = false; }DEFINE_VIRT_MACHINE (2 ,10 )
so that qemu doesnt complain about "Property .smmu not found"
Will let you know if i have updates on further testing.
Thanks.
Quoted 400 lines
---
v5 -> v6:
- use IOMMUMemoryRegion
- handle implementation definedSMMU_CMD_TLBI_NH_VA_AMcmd (goes along withTLBI_ON_MAPFW quirk)
- replay systematically unmap the whole range first
-smmuv3_map_hookdoes not unmap anymore and the unmap is done before the replay
- add and usesmmuv3_context_device_invalidateinstead of blindly replaying everything--- hw/arm/smmuv3-internal.h | 1 + hw/arm/smmuv3.c | 265 ++++++++++++++++++++++++++++++++++++++++++++++- hw/arm/trace-events | 14 +++ 3 files changed, 277 insertions(+), 3 deletions(-)hw/arm/smmuv3-internal.h +1
-0@@ -344,6 +344,7 @@ enum {SMMU_CMD_RESUME =0x44 ,SMMU_CMD_STALL_TERM ,SMMU_CMD_SYNC ,/* 0x46 */ + SMMU_CMD_TLBI_NH_VA_AM = 0x8F, /* VIOMMU Impl Defined */ }; static const char *cmd_stringify[] = {hw/arm/smmuv3.c +262
-3@@ -25,6 +25,7 @@#include "exec/address-spaces.h" #include "trace.h" #include "qemu/error-report.h" +#include "exec/target_page.h" #include "hw/arm/smmuv3.h" #include "smmuv3-internal.h" @@ -143,6 +144,71 @@ static MemTxResult smmu_read_cmdq(SMMUV3State *s, Cmd *cmd)return ret; } +static void smmuv3_replay_all (SMMUState *s) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +trace_smmuv3_replay_all (node->sdev->iommu.parent_obj.name); +memory_region_iommu_replay_all (&node->sdev->iommu); + } +} + +/* Replay the mappings for a given streamid */ +static void smmuv3_context_device_invalidate (SMMUState *s,uint16_t sid) +{ +uint8_t bus_n, devfn; +SMMUPciBus *smmu_bus; +SMMUDevice *smmu; + +trace_smmuv3_context_device_invalidate (sid); + bus_n =PCI_BUS_NUM (sid); + smmu_bus =smmu_find_as_from_bus_num (s, bus_n); +if (smmu_bus) { + devfn =PCI_FUNC (sid); + smmu = smmu_bus->pbdev[devfn]; +if (smmu) { +memory_region_iommu_replay_all (&smmu->iommu); + } + } +} + +static void smmuv3_replay_single (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova); + +static void smmuv3_replay_range (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova,size_t nb_pages); + +static void smmuv3_notify_single (SMMUState *s,uint64_t iova) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_notify_all (node->sdev->iommu.parent_obj.name, iova); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_replay_single (mr, n, iova); + } + } +} + +static void smmuv3_notify_range (SMMUState *s,uint64_t iova,size_t size) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_notify_all (node->sdev->iommu.parent_obj.name, iova); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_replay_range (mr, n, iova, size); + } + } +} +static int smmu_cmdq_consume (SMMUV3State *s) {uint32_t error =SMMU_CMD_ERR_NONE ;@@ -178,28 +244,38 @@ static int smmu_cmdq_consume(SMMUV3State *s)break ;case SMMU_CMD_PREFETCH_CONFIG :case SMMU_CMD_PREFETCH_ADDR : +break ;case SMMU_CMD_CFGI_STE : {uint32_t streamid = cmd.word[1 ];trace_smmuv3_cmdq_cfgi_ste (streamid); -break ; +smmuv3_context_device_invalidate (&s->smmu_state, streamid); +break ; }case SMMU_CMD_CFGI_STE_RANGE :/* same as SMMU_CMD_CFGI_ALL */ { -uint32_t start = cmd.word[1 ], range, end; +uint32_t start = cmd.word[1 ], range, end, i; range =extract32 (cmd.word[2 ],0 ,5 ); end = start + (1 << (range +1 )) -1 ;trace_smmuv3_cmdq_cfgi_ste_range (start, end); +for (i = start; i <= end; i++) { +smmuv3_context_device_invalidate (&s->smmu_state, i); + }break ; }case SMMU_CMD_CFGI_CD :case SMMU_CMD_CFGI_CD_ALL : + { +uint32_t streamid = cmd.word[1 ]; + +smmuv3_context_device_invalidate (&s->smmu_state, streamid);break ; + }case SMMU_CMD_TLBI_NH_ALL :case SMMU_CMD_TLBI_NH_ASID : -printf ("%s TLBI* replay\n" , __func__); +smmuv3_replay_all (&s->smmu_state);break ;case SMMU_CMD_TLBI_NH_VA : {@@ -210,6 +286,20 @@ static int smmu_cmdq_consume(SMMUV3State *s)uint64_t addr = high <<32 | (low <<12 );trace_smmuv3_cmdq_tlbi_nh_va (asid, vmid, addr); +smmuv3_notify_single (&s->smmu_state, addr); +break ; + } +case SMMU_CMD_TLBI_NH_VA_AM : + { +int asid =extract32 (cmd.word[1 ],16 ,16 ); +int am =extract32 (cmd.word[1 ],0 ,16 ); +uint64_t low =extract32 (cmd.word[2 ],12 ,20 ); +uint64_t high = cmd.word[3 ]; +uint64_t addr = high <<32 | (low <<12 ); +size_t size = am <<12 ; + +trace_smmuv3_cmdq_tlbi_nh_va_am (asid, am, addr, size); +smmuv3_notify_range (&s->smmu_state, addr, size);break ; }case SMMU_CMD_TLBI_NH_VAA :@@ -222,6 +312,7 @@ static int smmu_cmdq_consume(SMMUV3State *s)case SMMU_CMD_TLBI_S12_VMALL :case SMMU_CMD_TLBI_S2_IPA :case SMMU_CMD_TLBI_NSNH_ALL : +smmuv3_replay_all (&s->smmu_state);break ;case SMMU_CMD_ATC_INV :case SMMU_CMD_PRI_RESP :@@ -804,6 +895,172 @@ out:return entry; } +static int smmuv3_replay_hook (IOMMUTLBEntry *entry,void *private) +{ +trace_smmuv3_replay_hook (entry->iova, entry->translated_addr, + entry->addr_mask, entry->perm); +memory_region_notify_one ((IOMMUNotifier *)private, entry); +return 0 ; +} + +static int smmuv3_map_hook (IOMMUTLBEntry *entry,void *private) +{ +trace_smmuv3_map_hook (entry->iova, entry->translated_addr, + entry->addr_mask, entry->perm); +memory_region_notify_one ((IOMMUNotifier *)private, entry); +return 0 ; +} + +/* Unmap the whole range in the notifier's scope. */ +static void smmuv3_unmap_notifier (SMMUDevice *sdev,IOMMUNotifier *n) +{ +IOMMUTLBEntry entry; +hwaddr size; +hwaddr start = n->start; +hwaddr end = n->end; + + size = end - start +1 ; + + entry.target_as = &address_space_memory; +/* Adjust iova for the size */ + entry.iova = n->start & ~(size -1 ); +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ; + +/* TODO: check start/end/size/mask */ + +trace_smmuv3_unmap_notifier (pci_bus_num (sdev->bus), +PCI_SLOT (sdev->devfn), +PCI_FUNC (sdev->devfn), + entry.iova, size); + +memory_region_notify_one (n, &entry); +} + +static void smmuv3_replay (IOMMUMemoryRegion *mr,IOMMUNotifier *n) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +int ret; + +smmuv3_unmap_notifier (sdev, n); + + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } +/* is the smmu enabled */ + sbc->page_walk_64 (&cfg,0 , (1ULL << (64 - cfg.tsz)) -1 , false, + smmuv3_replay_hook, n); +} +static void smmuv3_replay_range (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova,size_t size) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +IOMMUTLBEntry entry; +int ret; + +trace_smmuv3_replay_range (mr->parent_obj.name, iova, size, n); + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } + +/* first unmap */ + entry.target_as = &address_space_memory; + entry.iova = iova & ~(size -1 ); + entry.addr_mask = size -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); + +/* then figure out if a new mapping needs to be applied */ + sbc->page_walk_64 (&cfg, iova, iova + entry.addr_mask , false, + smmuv3_map_hook, n); +} + +static void smmuv3_replay_single (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +size_t target_page_size =qemu_target_page_size (); +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +IOMMUTLBEntry entry; +int ret; + +trace_smmuv3_replay_single (mr->parent_obj.name, iova, n); + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } + +/* first unmap */ + entry.target_as = &address_space_memory; + entry.iova = iova & ~(target_page_size -1 ); + entry.addr_mask = target_page_size -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); + +/* then figure out if a new mapping needs to be applied */ + sbc->page_walk_64 (&cfg, iova, iova +1 , false, + smmuv3_map_hook, n); +} + +static void smmuv3_notify_flag_changed (IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old, +IOMMUNotifierFlag new) +{ +SMMUDevice *sdev =container_of (iommu, SMMUDevice, iommu); +SMMUV3State *s3 = sdev->smmu; +SMMUState *s = &(s3->smmu_state); +SMMUNotifierNode *node =NULL ; +SMMUNotifierNode *next_node =NULL ; + +if (old ==IOMMU_NOTIFIER_NONE ) { +trace_smmuv3_notify_flag_add (iommu->parent_obj.name); + node =g_malloc0 (sizeof (*node)); + node->sdev = sdev; +QLIST_INSERT_HEAD (&s->notifiers_list, node, next); +return ; + } + +/* update notifier node with new flags */ + QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { +if (node->sdev == sdev) { +if (new ==IOMMU_NOTIFIER_NONE ) { +trace_smmuv3_notify_flag_del (iommu->parent_obj.name); +QLIST_REMOVE (node, next); +g_free (node); + } +return ; + } + } +}static inline void smmu_update_base_reg (SMMUV3State *s,uint64_t *base,uint64_t val)@@ -1125,6 +1382,8 @@ static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass,IOMMUMemoryRegionClass *imrc =IOMMU_MEMORY_REGION_CLASS (klass); imrc->translate = smmuv3_translate; + imrc->notify_flag_changed = smmuv3_notify_flag_changed; + imrc->replay = smmuv3_replay; }static const TypeInfo smmuv3_type_info = {hw/arm/trace-events +14
-0@@ -27,6 +27,7 @@ smmuv3_cmdq_opcode(const char *opcode) "<--- %s" smmuv3_cmdq_cfgi_ste(int streamid) " |_ streamid =%d" smmuv3_cmdq_cfgi_ste_range(int start, int end) " |_ start=0x%d - end=0x%d" smmuv3_cmdq_tlbi_nh_va(int asid, int vmid, uint64_t addr) " |_ asid =%d vmid =%d addr=0x%"PRIx64 +smmuv3_cmdq_tlbi_nh_va_am(int asid, int am, size_t size, uint64_t addr) " |_ asid =%d am =%d size=0x%lx addr=0x%"PRIx64 smmuv3_cmdq_consume_sev(void) "CMD_SYNC CS=SEV not supported, ignoring" smmuv3_cmdq_consume_out(uint8_t prod_wrap, uint32_t prod, uint8_t cons_wrap, uint32_t cons) "prod_wrap:%d, prod:0x%x cons_wrap:%d cons:0x%x" smmuv3_update(bool is_empty, uint32_t prod, uint32_t cons, uint8_t prod_wrap, uint8_t cons_wrap) "q empty:%d prod:%d cons:%d p.wrap:%d p.cons:%d"@@ -50,3 +51,16 @@ smmuv3_dump_ste(int i, uint32_t word0, int j, uint32_t word1) "STE[%2d]: 0x%x\t smmuv3_dump_cd(int i, uint32_t word0, int j, uint32_t word1) "CD[%2d]: 0x%x\t CD[%2d]: 0x%x" smmuv3_dump_cmd(int i, uint32_t word0, int j, uint32_t word1) "CMD[%2d]: 0x%x\t CMD[%2d]: 0x%x" smmuv3_cfg_stage(int s, uint32_t oas, uint32_t tsz, uint64_t ttbr, bool aa64, uint32_t granule_sz, int initial_level) "TransCFG stage:%d oas:%d tsz:%d ttbr:0x%"PRIx64" aa64:%d granule_sz:%d, initial_level = %d" + +smmuv3_replay(uint16_t sid, bool enabled) "sid=%d, enabled=%d" +smmuv3_replay_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_map_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s" +smmuv3_notify_flag_del(const char *iommu) "DEL SMMUNotifier node for iommu mr=%s" +smmuv3_replay_single(const char *name, uint64_t iova, void *n) "iommu mr=%s iova=0x%"PRIx64" n=%p" +smmuv3_replay_range(const char *name, uint64_t iova, size_t size, void *n) "iommu mr=%s iova=0x%"PRIx64" size=0x%lx n=%p" +smmuv3_replay_all(const char *name) "iommu mr=%s" +smmuv3_notify_all(const char *name, uint64_t iova) "iommu mr=%s iova=0x%"PRIx64 +smmuv3_unmap_notifier(uint8_t bus, uint8_t slot, uint8_t fn, uint64_t iova, uint64_t size) "Device %02x:%02x.%x start 0x%"PRIx64" size 0x%"PRIx64 +smmuv3_context_device_invalidate(uint32_t sid) "sid=%d" +Signature · 1 line
2.5.5
Signature · 1 line
Linu cherian
Re: [Qemu-arm] [Qemu-devel] [RFC v6 8/9] hw/arm/smmuv3: VFIO integration
Hi Linu,
Quoted 10 lines · Linu Cherian
Hi Eric,
Quoted 6 lines · Eric Auger
This patch allows doing PCIe passthrough with a guest exposed with a vSMMUv3. It implements the replay and
notify_flag_changediommu ops. Also on TLB and data structure invalidation commands, we replay the mappings so that the physical IOMMU implements updated stage 1 settings (Guest IOVA -> Guest PA) + stage 2 settings.
Quoted 7 lines
Quoted 4 lines
This works only if the guest smmuv3 driver implements the "tlbi-on-map" option.
Signed-off-by Eric Auger <[email protected]>Tried out launching a guest with Qemu option "-machine virt-2.10,smmu" and a 1G Ethernet controller as vfio-pci device. It works fine for me.
Hum sorry, I forgot to update the cover letter. You need to use -machine virt-2.11,smmu for the instantiation as the 2.10 machine was released as part of 2.10 and those changes only apply on 2.11 mach virt introduced in this series. Please apologize for the pain.
I am going to release a new version this week fixing my last DPDK bug (at least I am aware of ). In this new version, the instantiation method will change to -device smmuv3 which is closer to what is done on Intel.
By the way I will also take time to provide some more info about the VFIO integration as it is implemented in this series although this latter may evolve due to NAK of kernel FW quirk.
Thank you for testing!
Best Regards
Eric
Quoted 423 lines
Qemu source: https://github.com/eauger/qemu.git Branch: v2.10.0-rc2-SMMU-v6
But had to make this change,
hw/arm/virt.c +1
-1@@ -1806,7 +1806,7 @@ static void virt_machine_2_10_options(MachineClass *mc)virt_machine_2_11_options (mc);SET_MACHINE_COMPAT (mc,VIRT_COMPAT_2_10 ); - vmc->no_smmu = true; + vmc->no_smmu = false; }DEFINE_VIRT_MACHINE (2 ,10 )so that qemu doesnt complain about "Property .smmu not found"
Will let you know if i have updates on further testing.
Thanks.
Quoted 400 lines
---
v5 -> v6:
- use IOMMUMemoryRegion
- handle implementation definedSMMU_CMD_TLBI_NH_VA_AMcmd (goes along withTLBI_ON_MAPFW quirk)
- replay systematically unmap the whole range first
-smmuv3_map_hookdoes not unmap anymore and the unmap is done before the replay
- add and usesmmuv3_context_device_invalidateinstead of blindly replaying everything--- hw/arm/smmuv3-internal.h | 1 + hw/arm/smmuv3.c | 265 ++++++++++++++++++++++++++++++++++++++++++++++- hw/arm/trace-events | 14 +++ 3 files changed, 277 insertions(+), 3 deletions(-)hw/arm/smmuv3-internal.h +1
-0@@ -344,6 +344,7 @@ enum {SMMU_CMD_RESUME =0x44 ,SMMU_CMD_STALL_TERM ,SMMU_CMD_SYNC ,/* 0x46 */ + SMMU_CMD_TLBI_NH_VA_AM = 0x8F, /* VIOMMU Impl Defined */ }; static const char *cmd_stringify[] = {hw/arm/smmuv3.c +262
-3@@ -25,6 +25,7 @@#include "exec/address-spaces.h" #include "trace.h" #include "qemu/error-report.h" +#include "exec/target_page.h" #include "hw/arm/smmuv3.h" #include "smmuv3-internal.h" @@ -143,6 +144,71 @@ static MemTxResult smmu_read_cmdq(SMMUV3State *s, Cmd *cmd)return ret; } +static void smmuv3_replay_all (SMMUState *s) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +trace_smmuv3_replay_all (node->sdev->iommu.parent_obj.name); +memory_region_iommu_replay_all (&node->sdev->iommu); + } +} + +/* Replay the mappings for a given streamid */ +static void smmuv3_context_device_invalidate (SMMUState *s,uint16_t sid) +{ +uint8_t bus_n, devfn; +SMMUPciBus *smmu_bus; +SMMUDevice *smmu; + +trace_smmuv3_context_device_invalidate (sid); + bus_n =PCI_BUS_NUM (sid); + smmu_bus =smmu_find_as_from_bus_num (s, bus_n); +if (smmu_bus) { + devfn =PCI_FUNC (sid); + smmu = smmu_bus->pbdev[devfn]; +if (smmu) { +memory_region_iommu_replay_all (&smmu->iommu); + } + } +} + +static void smmuv3_replay_single (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova); + +static void smmuv3_replay_range (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova,size_t nb_pages); + +static void smmuv3_notify_single (SMMUState *s,uint64_t iova) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_notify_all (node->sdev->iommu.parent_obj.name, iova); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_replay_single (mr, n, iova); + } + } +} + +static void smmuv3_notify_range (SMMUState *s,uint64_t iova,size_t size) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_notify_all (node->sdev->iommu.parent_obj.name, iova); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_replay_range (mr, n, iova, size); + } + } +} +static int smmu_cmdq_consume (SMMUV3State *s) {uint32_t error =SMMU_CMD_ERR_NONE ;@@ -178,28 +244,38 @@ static int smmu_cmdq_consume(SMMUV3State *s)break ;case SMMU_CMD_PREFETCH_CONFIG :case SMMU_CMD_PREFETCH_ADDR : +break ;case SMMU_CMD_CFGI_STE : {uint32_t streamid = cmd.word[1 ];trace_smmuv3_cmdq_cfgi_ste (streamid); -break ; +smmuv3_context_device_invalidate (&s->smmu_state, streamid); +break ; }case SMMU_CMD_CFGI_STE_RANGE :/* same as SMMU_CMD_CFGI_ALL */ { -uint32_t start = cmd.word[1 ], range, end; +uint32_t start = cmd.word[1 ], range, end, i; range =extract32 (cmd.word[2 ],0 ,5 ); end = start + (1 << (range +1 )) -1 ;trace_smmuv3_cmdq_cfgi_ste_range (start, end); +for (i = start; i <= end; i++) { +smmuv3_context_device_invalidate (&s->smmu_state, i); + }break ; }case SMMU_CMD_CFGI_CD :case SMMU_CMD_CFGI_CD_ALL : + { +uint32_t streamid = cmd.word[1 ]; + +smmuv3_context_device_invalidate (&s->smmu_state, streamid);break ; + }case SMMU_CMD_TLBI_NH_ALL :case SMMU_CMD_TLBI_NH_ASID : -printf ("%s TLBI* replay\n" , __func__); +smmuv3_replay_all (&s->smmu_state);break ;case SMMU_CMD_TLBI_NH_VA : {@@ -210,6 +286,20 @@ static int smmu_cmdq_consume(SMMUV3State *s)uint64_t addr = high <<32 | (low <<12 );trace_smmuv3_cmdq_tlbi_nh_va (asid, vmid, addr); +smmuv3_notify_single (&s->smmu_state, addr); +break ; + } +case SMMU_CMD_TLBI_NH_VA_AM : + { +int asid =extract32 (cmd.word[1 ],16 ,16 ); +int am =extract32 (cmd.word[1 ],0 ,16 ); +uint64_t low =extract32 (cmd.word[2 ],12 ,20 ); +uint64_t high = cmd.word[3 ]; +uint64_t addr = high <<32 | (low <<12 ); +size_t size = am <<12 ; + +trace_smmuv3_cmdq_tlbi_nh_va_am (asid, am, addr, size); +smmuv3_notify_range (&s->smmu_state, addr, size);break ; }case SMMU_CMD_TLBI_NH_VAA :@@ -222,6 +312,7 @@ static int smmu_cmdq_consume(SMMUV3State *s)case SMMU_CMD_TLBI_S12_VMALL :case SMMU_CMD_TLBI_S2_IPA :case SMMU_CMD_TLBI_NSNH_ALL : +smmuv3_replay_all (&s->smmu_state);break ;case SMMU_CMD_ATC_INV :case SMMU_CMD_PRI_RESP :@@ -804,6 +895,172 @@ out:return entry; } +static int smmuv3_replay_hook (IOMMUTLBEntry *entry,void *private) +{ +trace_smmuv3_replay_hook (entry->iova, entry->translated_addr, + entry->addr_mask, entry->perm); +memory_region_notify_one ((IOMMUNotifier *)private, entry); +return 0 ; +} + +static int smmuv3_map_hook (IOMMUTLBEntry *entry,void *private) +{ +trace_smmuv3_map_hook (entry->iova, entry->translated_addr, + entry->addr_mask, entry->perm); +memory_region_notify_one ((IOMMUNotifier *)private, entry); +return 0 ; +} + +/* Unmap the whole range in the notifier's scope. */ +static void smmuv3_unmap_notifier (SMMUDevice *sdev,IOMMUNotifier *n) +{ +IOMMUTLBEntry entry; +hwaddr size; +hwaddr start = n->start; +hwaddr end = n->end; + + size = end - start +1 ; + + entry.target_as = &address_space_memory; +/* Adjust iova for the size */ + entry.iova = n->start & ~(size -1 ); +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ; + +/* TODO: check start/end/size/mask */ + +trace_smmuv3_unmap_notifier (pci_bus_num (sdev->bus), +PCI_SLOT (sdev->devfn), +PCI_FUNC (sdev->devfn), + entry.iova, size); + +memory_region_notify_one (n, &entry); +} + +static void smmuv3_replay (IOMMUMemoryRegion *mr,IOMMUNotifier *n) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +int ret; + +smmuv3_unmap_notifier (sdev, n); + + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } +/* is the smmu enabled */ + sbc->page_walk_64 (&cfg,0 , (1ULL << (64 - cfg.tsz)) -1 , false, + smmuv3_replay_hook, n); +} +static void smmuv3_replay_range (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova,size_t size) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +IOMMUTLBEntry entry; +int ret; + +trace_smmuv3_replay_range (mr->parent_obj.name, iova, size, n); + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } + +/* first unmap */ + entry.target_as = &address_space_memory; + entry.iova = iova & ~(size -1 ); + entry.addr_mask = size -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); + +/* then figure out if a new mapping needs to be applied */ + sbc->page_walk_64 (&cfg, iova, iova + entry.addr_mask , false, + smmuv3_map_hook, n); +} + +static void smmuv3_replay_single (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +size_t target_page_size =qemu_target_page_size (); +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +IOMMUTLBEntry entry; +int ret; + +trace_smmuv3_replay_single (mr->parent_obj.name, iova, n); + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } + +/* first unmap */ + entry.target_as = &address_space_memory; + entry.iova = iova & ~(target_page_size -1 ); + entry.addr_mask = target_page_size -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); + +/* then figure out if a new mapping needs to be applied */ + sbc->page_walk_64 (&cfg, iova, iova +1 , false, + smmuv3_map_hook, n); +} + +static void smmuv3_notify_flag_changed (IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old, +IOMMUNotifierFlag new) +{ +SMMUDevice *sdev =container_of (iommu, SMMUDevice, iommu); +SMMUV3State *s3 = sdev->smmu; +SMMUState *s = &(s3->smmu_state); +SMMUNotifierNode *node =NULL ; +SMMUNotifierNode *next_node =NULL ; + +if (old ==IOMMU_NOTIFIER_NONE ) { +trace_smmuv3_notify_flag_add (iommu->parent_obj.name); + node =g_malloc0 (sizeof (*node)); + node->sdev = sdev; +QLIST_INSERT_HEAD (&s->notifiers_list, node, next); +return ; + } + +/* update notifier node with new flags */ + QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { +if (node->sdev == sdev) { +if (new ==IOMMU_NOTIFIER_NONE ) { +trace_smmuv3_notify_flag_del (iommu->parent_obj.name); +QLIST_REMOVE (node, next); +g_free (node); + } +return ; + } + } +}static inline void smmu_update_base_reg (SMMUV3State *s,uint64_t *base,uint64_t val)@@ -1125,6 +1382,8 @@ static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass,IOMMUMemoryRegionClass *imrc =IOMMU_MEMORY_REGION_CLASS (klass); imrc->translate = smmuv3_translate; + imrc->notify_flag_changed = smmuv3_notify_flag_changed; + imrc->replay = smmuv3_replay; }static const TypeInfo smmuv3_type_info = {hw/arm/trace-events +14
-0@@ -27,6 +27,7 @@ smmuv3_cmdq_opcode(const char *opcode) "<--- %s" smmuv3_cmdq_cfgi_ste(int streamid) " |_ streamid =%d" smmuv3_cmdq_cfgi_ste_range(int start, int end) " |_ start=0x%d - end=0x%d" smmuv3_cmdq_tlbi_nh_va(int asid, int vmid, uint64_t addr) " |_ asid =%d vmid =%d addr=0x%"PRIx64 +smmuv3_cmdq_tlbi_nh_va_am(int asid, int am, size_t size, uint64_t addr) " |_ asid =%d am =%d size=0x%lx addr=0x%"PRIx64 smmuv3_cmdq_consume_sev(void) "CMD_SYNC CS=SEV not supported, ignoring" smmuv3_cmdq_consume_out(uint8_t prod_wrap, uint32_t prod, uint8_t cons_wrap, uint32_t cons) "prod_wrap:%d, prod:0x%x cons_wrap:%d cons:0x%x" smmuv3_update(bool is_empty, uint32_t prod, uint32_t cons, uint8_t prod_wrap, uint8_t cons_wrap) "q empty:%d prod:%d cons:%d p.wrap:%d p.cons:%d"@@ -50,3 +51,16 @@ smmuv3_dump_ste(int i, uint32_t word0, int j, uint32_t word1) "STE[%2d]: 0x%x\t smmuv3_dump_cd(int i, uint32_t word0, int j, uint32_t word1) "CD[%2d]: 0x%x\t CD[%2d]: 0x%x" smmuv3_dump_cmd(int i, uint32_t word0, int j, uint32_t word1) "CMD[%2d]: 0x%x\t CMD[%2d]: 0x%x" smmuv3_cfg_stage(int s, uint32_t oas, uint32_t tsz, uint64_t ttbr, bool aa64, uint32_t granule_sz, int initial_level) "TransCFG stage:%d oas:%d tsz:%d ttbr:0x%"PRIx64" aa64:%d granule_sz:%d, initial_level = %d" + +smmuv3_replay(uint16_t sid, bool enabled) "sid=%d, enabled=%d" +smmuv3_replay_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_map_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s" +smmuv3_notify_flag_del(const char *iommu) "DEL SMMUNotifier node for iommu mr=%s" +smmuv3_replay_single(const char *name, uint64_t iova, void *n) "iommu mr=%s iova=0x%"PRIx64" n=%p" +smmuv3_replay_range(const char *name, uint64_t iova, size_t size, void *n) "iommu mr=%s iova=0x%"PRIx64" size=0x%lx n=%p" +smmuv3_replay_all(const char *name) "iommu mr=%s" +smmuv3_notify_all(const char *name, uint64_t iova) "iommu mr=%s iova=0x%"PRIx64 +smmuv3_unmap_notifier(uint8_t bus, uint8_t slot, uint8_t fn, uint64_t iova, uint64_t size) "Device %02x:%02x.%x start 0x%"PRIx64" size 0x%"PRIx64 +smmuv3_context_device_invalidate(uint32_t sid) "sid=%d" +Signature · 1 line
2.5.5
Re: [Qemu-arm] [Qemu-devel] [RFC v6 8/9] hw/arm/smmuv3: VFIO integration
Quoted 26 lines · Auger Eric
Hi Linu,
Quoted 10 lines · Linu Cherian
Hi Eric,
Quoted 6 lines · Eric Auger
This patch allows doing PCIe passthrough with a guest exposed with a vSMMUv3. It implements the replay and
notify_flag_changediommu ops. Also on TLB and data structure invalidation commands, we replay the mappings so that the physical IOMMU implements updated stage 1 settings (Guest IOVA -> Guest PA) + stage 2 settings.Quoted 7 lines
Quoted 4 lines
This works only if the guest smmuv3 driver implements the "tlbi-on-map" option.
Signed-off-by Eric Auger <[email protected]>Tried out launching a guest with Qemu option "-machine virt-2.10,smmu" and a 1G Ethernet controller as vfio-pci device. It works fine for me.
Hum sorry, I forgot to update the cover letter. You need to use -machine virt-2.11,smmu for the instantiation as the 2.10 machine was released as part of 2.10 and those changes only apply on 2.11 mach virt introduced in this series. Please apologize for the pain.
Quoted 7 lines
I am going to release a new version this week fixing my last DPDK bug (at least I am aware of ). In this new version, the instantiation method will change to -device smmuv3 which is closer to what is done on Intel.
By the way I will also take time to provide some more info about the VFIO integration as it is implemented in this series although this latter may evolve due to NAK of kernel FW quirk.
Yes I think changing compat string to avoid pretending this is smmuv3 was a hard requirement. So you might need to do -device qemu-smmuv3 if you want VFIO support with this PV quirk.
For now, how about a subset of the functionality with VFIO disabled? People can use it with virtio for now.
Quoted 428 lines
Thank you for testing!
Best Regards
Eric
Quoted 423 lines
Qemu source: https://github.com/eauger/qemu.git Branch: v2.10.0-rc2-SMMU-v6
But had to make this change,
hw/arm/virt.c +1
-1@@ -1806,7 +1806,7 @@ static void virt_machine_2_10_options(MachineClass *mc)virt_machine_2_11_options (mc);SET_MACHINE_COMPAT (mc,VIRT_COMPAT_2_10 ); - vmc->no_smmu = true; + vmc->no_smmu = false; }DEFINE_VIRT_MACHINE (2 ,10 )so that qemu doesnt complain about "Property .smmu not found"
Will let you know if i have updates on further testing.
Thanks.
Quoted 400 lines
---
v5 -> v6:
- use IOMMUMemoryRegion
- handle implementation definedSMMU_CMD_TLBI_NH_VA_AMcmd (goes along withTLBI_ON_MAPFW quirk)
- replay systematically unmap the whole range first
-smmuv3_map_hookdoes not unmap anymore and the unmap is done before the replay
- add and usesmmuv3_context_device_invalidateinstead of blindly replaying everything--- hw/arm/smmuv3-internal.h | 1 + hw/arm/smmuv3.c | 265 ++++++++++++++++++++++++++++++++++++++++++++++- hw/arm/trace-events | 14 +++ 3 files changed, 277 insertions(+), 3 deletions(-)hw/arm/smmuv3-internal.h +1
-0@@ -344,6 +344,7 @@ enum {SMMU_CMD_RESUME =0x44 ,SMMU_CMD_STALL_TERM ,SMMU_CMD_SYNC ,/* 0x46 */ + SMMU_CMD_TLBI_NH_VA_AM = 0x8F, /* VIOMMU Impl Defined */ }; static const char *cmd_stringify[] = {hw/arm/smmuv3.c +262
-3@@ -25,6 +25,7 @@#include "exec/address-spaces.h" #include "trace.h" #include "qemu/error-report.h" +#include "exec/target_page.h" #include "hw/arm/smmuv3.h" #include "smmuv3-internal.h" @@ -143,6 +144,71 @@ static MemTxResult smmu_read_cmdq(SMMUV3State *s, Cmd *cmd)return ret; } +static void smmuv3_replay_all (SMMUState *s) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +trace_smmuv3_replay_all (node->sdev->iommu.parent_obj.name); +memory_region_iommu_replay_all (&node->sdev->iommu); + } +} + +/* Replay the mappings for a given streamid */ +static void smmuv3_context_device_invalidate (SMMUState *s,uint16_t sid) +{ +uint8_t bus_n, devfn; +SMMUPciBus *smmu_bus; +SMMUDevice *smmu; + +trace_smmuv3_context_device_invalidate (sid); + bus_n =PCI_BUS_NUM (sid); + smmu_bus =smmu_find_as_from_bus_num (s, bus_n); +if (smmu_bus) { + devfn =PCI_FUNC (sid); + smmu = smmu_bus->pbdev[devfn]; +if (smmu) { +memory_region_iommu_replay_all (&smmu->iommu); + } + } +} + +static void smmuv3_replay_single (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova); + +static void smmuv3_replay_range (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova,size_t nb_pages); + +static void smmuv3_notify_single (SMMUState *s,uint64_t iova) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_notify_all (node->sdev->iommu.parent_obj.name, iova); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_replay_single (mr, n, iova); + } + } +} + +static void smmuv3_notify_range (SMMUState *s,uint64_t iova,size_t size) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_notify_all (node->sdev->iommu.parent_obj.name, iova); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_replay_range (mr, n, iova, size); + } + } +} +static int smmu_cmdq_consume (SMMUV3State *s) {uint32_t error =SMMU_CMD_ERR_NONE ;@@ -178,28 +244,38 @@ static int smmu_cmdq_consume(SMMUV3State *s)break ;case SMMU_CMD_PREFETCH_CONFIG :case SMMU_CMD_PREFETCH_ADDR : +break ;case SMMU_CMD_CFGI_STE : {uint32_t streamid = cmd.word[1 ];trace_smmuv3_cmdq_cfgi_ste (streamid); -break ; +smmuv3_context_device_invalidate (&s->smmu_state, streamid); +break ; }case SMMU_CMD_CFGI_STE_RANGE :/* same as SMMU_CMD_CFGI_ALL */ { -uint32_t start = cmd.word[1 ], range, end; +uint32_t start = cmd.word[1 ], range, end, i; range =extract32 (cmd.word[2 ],0 ,5 ); end = start + (1 << (range +1 )) -1 ;trace_smmuv3_cmdq_cfgi_ste_range (start, end); +for (i = start; i <= end; i++) { +smmuv3_context_device_invalidate (&s->smmu_state, i); + }break ; }case SMMU_CMD_CFGI_CD :case SMMU_CMD_CFGI_CD_ALL : + { +uint32_t streamid = cmd.word[1 ]; + +smmuv3_context_device_invalidate (&s->smmu_state, streamid);break ; + }case SMMU_CMD_TLBI_NH_ALL :case SMMU_CMD_TLBI_NH_ASID : -printf ("%s TLBI* replay\n" , __func__); +smmuv3_replay_all (&s->smmu_state);break ;case SMMU_CMD_TLBI_NH_VA : {@@ -210,6 +286,20 @@ static int smmu_cmdq_consume(SMMUV3State *s)uint64_t addr = high <<32 | (low <<12 );trace_smmuv3_cmdq_tlbi_nh_va (asid, vmid, addr); +smmuv3_notify_single (&s->smmu_state, addr); +break ; + } +case SMMU_CMD_TLBI_NH_VA_AM : + { +int asid =extract32 (cmd.word[1 ],16 ,16 ); +int am =extract32 (cmd.word[1 ],0 ,16 ); +uint64_t low =extract32 (cmd.word[2 ],12 ,20 ); +uint64_t high = cmd.word[3 ]; +uint64_t addr = high <<32 | (low <<12 ); +size_t size = am <<12 ; + +trace_smmuv3_cmdq_tlbi_nh_va_am (asid, am, addr, size); +smmuv3_notify_range (&s->smmu_state, addr, size);break ; }case SMMU_CMD_TLBI_NH_VAA :@@ -222,6 +312,7 @@ static int smmu_cmdq_consume(SMMUV3State *s)case SMMU_CMD_TLBI_S12_VMALL :case SMMU_CMD_TLBI_S2_IPA :case SMMU_CMD_TLBI_NSNH_ALL : +smmuv3_replay_all (&s->smmu_state);break ;case SMMU_CMD_ATC_INV :case SMMU_CMD_PRI_RESP :@@ -804,6 +895,172 @@ out:return entry; } +static int smmuv3_replay_hook (IOMMUTLBEntry *entry,void *private) +{ +trace_smmuv3_replay_hook (entry->iova, entry->translated_addr, + entry->addr_mask, entry->perm); +memory_region_notify_one ((IOMMUNotifier *)private, entry); +return 0 ; +} + +static int smmuv3_map_hook (IOMMUTLBEntry *entry,void *private) +{ +trace_smmuv3_map_hook (entry->iova, entry->translated_addr, + entry->addr_mask, entry->perm); +memory_region_notify_one ((IOMMUNotifier *)private, entry); +return 0 ; +} + +/* Unmap the whole range in the notifier's scope. */ +static void smmuv3_unmap_notifier (SMMUDevice *sdev,IOMMUNotifier *n) +{ +IOMMUTLBEntry entry; +hwaddr size; +hwaddr start = n->start; +hwaddr end = n->end; + + size = end - start +1 ; + + entry.target_as = &address_space_memory; +/* Adjust iova for the size */ + entry.iova = n->start & ~(size -1 ); +/* This field is meaningless for unmap */ + entry.translated_addr =0 ; + entry.perm =IOMMU_NONE ; + entry.addr_mask = size -1 ; + +/* TODO: check start/end/size/mask */ + +trace_smmuv3_unmap_notifier (pci_bus_num (sdev->bus), +PCI_SLOT (sdev->devfn), +PCI_FUNC (sdev->devfn), + entry.iova, size); + +memory_region_notify_one (n, &entry); +} + +static void smmuv3_replay (IOMMUMemoryRegion *mr,IOMMUNotifier *n) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +int ret; + +smmuv3_unmap_notifier (sdev, n); + + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } +/* is the smmu enabled */ + sbc->page_walk_64 (&cfg,0 , (1ULL << (64 - cfg.tsz)) -1 , false, + smmuv3_replay_hook, n); +} +static void smmuv3_replay_range (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova,size_t size) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +IOMMUTLBEntry entry; +int ret; + +trace_smmuv3_replay_range (mr->parent_obj.name, iova, size, n); + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } + +/* first unmap */ + entry.target_as = &address_space_memory; + entry.iova = iova & ~(size -1 ); + entry.addr_mask = size -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); + +/* then figure out if a new mapping needs to be applied */ + sbc->page_walk_64 (&cfg, iova, iova + entry.addr_mask , false, + smmuv3_map_hook, n); +} + +static void smmuv3_replay_single (IOMMUMemoryRegion *mr,IOMMUNotifier *n, +uint64_t iova) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUV3State *s = sdev->smmu; +size_t target_page_size =qemu_target_page_size (); +SMMUBaseClass *sbc =SMMU_DEVICE_GET_CLASS (s); +SMMUTransCfg cfg = {}; +IOMMUTLBEntry entry; +int ret; + +trace_smmuv3_replay_single (mr->parent_obj.name, iova, n); + ret =smmuv3_decode_config (mr, &cfg); +if (ret) { +error_report ("%s error decoding the configuration for iommu mr=%s" , + __func__, mr->parent_obj.name); + } + +if (cfg.disabled || cfg.bypassed) { +return ; + } + +/* first unmap */ + entry.target_as = &address_space_memory; + entry.iova = iova & ~(target_page_size -1 ); + entry.addr_mask = target_page_size -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); + +/* then figure out if a new mapping needs to be applied */ + sbc->page_walk_64 (&cfg, iova, iova +1 , false, + smmuv3_map_hook, n); +} + +static void smmuv3_notify_flag_changed (IOMMUMemoryRegion *iommu, +IOMMUNotifierFlag old, +IOMMUNotifierFlag new) +{ +SMMUDevice *sdev =container_of (iommu, SMMUDevice, iommu); +SMMUV3State *s3 = sdev->smmu; +SMMUState *s = &(s3->smmu_state); +SMMUNotifierNode *node =NULL ; +SMMUNotifierNode *next_node =NULL ; + +if (old ==IOMMU_NOTIFIER_NONE ) { +trace_smmuv3_notify_flag_add (iommu->parent_obj.name); + node =g_malloc0 (sizeof (*node)); + node->sdev = sdev; +QLIST_INSERT_HEAD (&s->notifiers_list, node, next); +return ; + } + +/* update notifier node with new flags */ + QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { +if (node->sdev == sdev) { +if (new ==IOMMU_NOTIFIER_NONE ) { +trace_smmuv3_notify_flag_del (iommu->parent_obj.name); +QLIST_REMOVE (node, next); +g_free (node); + } +return ; + } + } +}static inline void smmu_update_base_reg (SMMUV3State *s,uint64_t *base,uint64_t val)@@ -1125,6 +1382,8 @@ static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass,IOMMUMemoryRegionClass *imrc =IOMMU_MEMORY_REGION_CLASS (klass); imrc->translate = smmuv3_translate; + imrc->notify_flag_changed = smmuv3_notify_flag_changed; + imrc->replay = smmuv3_replay; }static const TypeInfo smmuv3_type_info = {hw/arm/trace-events +14
-0@@ -27,6 +27,7 @@ smmuv3_cmdq_opcode(const char *opcode) "<--- %s" smmuv3_cmdq_cfgi_ste(int streamid) " |_ streamid =%d" smmuv3_cmdq_cfgi_ste_range(int start, int end) " |_ start=0x%d - end=0x%d" smmuv3_cmdq_tlbi_nh_va(int asid, int vmid, uint64_t addr) " |_ asid =%d vmid =%d addr=0x%"PRIx64 +smmuv3_cmdq_tlbi_nh_va_am(int asid, int am, size_t size, uint64_t addr) " |_ asid =%d am =%d size=0x%lx addr=0x%"PRIx64 smmuv3_cmdq_consume_sev(void) "CMD_SYNC CS=SEV not supported, ignoring" smmuv3_cmdq_consume_out(uint8_t prod_wrap, uint32_t prod, uint8_t cons_wrap, uint32_t cons) "prod_wrap:%d, prod:0x%x cons_wrap:%d cons:0x%x" smmuv3_update(bool is_empty, uint32_t prod, uint32_t cons, uint8_t prod_wrap, uint8_t cons_wrap) "q empty:%d prod:%d cons:%d p.wrap:%d p.cons:%d"@@ -50,3 +51,16 @@ smmuv3_dump_ste(int i, uint32_t word0, int j, uint32_t word1) "STE[%2d]: 0x%x\t smmuv3_dump_cd(int i, uint32_t word0, int j, uint32_t word1) "CD[%2d]: 0x%x\t CD[%2d]: 0x%x" smmuv3_dump_cmd(int i, uint32_t word0, int j, uint32_t word1) "CMD[%2d]: 0x%x\t CMD[%2d]: 0x%x" smmuv3_cfg_stage(int s, uint32_t oas, uint32_t tsz, uint64_t ttbr, bool aa64, uint32_t granule_sz, int initial_level) "TransCFG stage:%d oas:%d tsz:%d ttbr:0x%"PRIx64" aa64:%d granule_sz:%d, initial_level = %d" + +smmuv3_replay(uint16_t sid, bool enabled) "sid=%d, enabled=%d" +smmuv3_replay_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_map_hook(hwaddr iova, hwaddr pa, hwaddr mask, int perm) "iova=0x%"PRIx64" pa=0x%" PRIx64" mask=0x%"PRIx64" perm=%d" +smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s" +smmuv3_notify_flag_del(const char *iommu) "DEL SMMUNotifier node for iommu mr=%s" +smmuv3_replay_single(const char *name, uint64_t iova, void *n) "iommu mr=%s iova=0x%"PRIx64" n=%p" +smmuv3_replay_range(const char *name, uint64_t iova, size_t size, void *n) "iommu mr=%s iova=0x%"PRIx64" size=0x%lx n=%p" +smmuv3_replay_all(const char *name) "iommu mr=%s" +smmuv3_notify_all(const char *name, uint64_t iova) "iommu mr=%s iova=0x%"PRIx64 +smmuv3_unmap_notifier(uint8_t bus, uint8_t slot, uint8_t fn, uint64_t iova, uint64_t size) "Device %02x:%02x.%x start 0x%"PRIx64" size 0x%"PRIx64 +smmuv3_context_device_invalidate(uint32_t sid) "sid=%d" +Signature · 1 line
2.5.5
RFC v6 9/9 hw/arm/virt-acpi-build: Use the ACPI_IORT_SMMU_V3_CACHING_MODE model
In reply to Eric AugerTo allow VFIO use case, let's set the smmu model to ACPI_IORT_SMMU_V3_CACHING_MODE.
An important notice is this model is not standardized in the ACPI IORT as this work is a proof of concept.
We also set the COHACC override flag which seems to be mandated.
--- hw/arm/virt-acpi-build.c | 2 ++ 1 file changed, 2 insertions(+)
hw/arm/virt-acpi-build.c +2 -0
@@ -437,6 +437,7 @@ build_iort(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms) smmu->type =ACPI_IORT_NODE_SMMU_V3 ; smmu->length =cpu_to_le16 (node_size); + smmu->model =0x3 ;/* ACPI_IORT_SMMU_V3_CACHING_MODE */ smmu->mapping_count =cpu_to_le32 (1 ); smmu->mapping_offset =cpu_to_le32 (sizeof (*smmu)); smmu->base_address =cpu_to_le64 (vms->memmap[VIRT_SMMU ].base);
@@ -444,6 +445,7 @@ build_iort(GArray *table_data, BIOSLinker *linker, VirtMachineState *vms) smmu->pri_gsiv =cpu_to_le32 (irq +1 ); smmu->gerr_gsiv =cpu_to_le32 (irq +2 ); smmu->sync_gsiv =cpu_to_le32 (irq +3 ); + smmu->flags =0x1 ;/* COHACC Override */ /* Identity RID mapping covering the whole input RID range */ idmap = &smmu->id_mapping_array[0 ];
Signature · 1 line
2.5.5
This series seems to have some coding style problems. See output below for more information:
Hi,
This series seems to have some coding style problems. See output below for more information:
Message-id: [email protected]
Subject: [Qemu-devel] [RFC v6 0/9] ARM SMMUv3 Emulation Support
Type: series
=== TEST SCRIPT BEGIN ===
#!/bin/bash
BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0
git config --local diff.renamelimit 0
git config --local diff.renames True
commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do
echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
failed=1
echo
fi
n=$((n+1))done
exit $failed
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'3e62cedd73 hw/arm/virt-acpi-build: Use the ACPI_IORT_SMMU_V3_CACHING_MODE model 3a51094ee9 hw/arm/smmuv3: VFIO integration815575515f target/arm/kvm: Translate the MSI doorbell in kvm_arch_fixup_msi_route f72bc6ae67 hw/arm/virt: Add tlbi-on-map property to the smmuv3 node 52a2569fe7 hw/arm/virt-acpi-build: Add smmuv3 node in IORT table 62c3c2fcae hw/arm/virt: Add 2.11 machine type80671b4e90 hw/arm/virt: Add SMMUv3 to the virt boardae1c203d73 hw/arm/smmuv3: smmuv3 emulation modelbcb7d6e45d hw/arm/smmu-common: smmu base class
=== OUTPUT BEGIN ===
Checking PATCH 1/9: hw/arm/smmu-common: smmu base class...
Checking PATCH 2/9: hw/arm/smmuv3: smmuv3 emulation model...
Checking PATCH 3/9: hw/arm/virt: Add SMMUv3 to the virt board...
Checking PATCH 4/9: hw/arm/virt: Add 2.11 machine type...
Checking PATCH 5/9: hw/arm/virt-acpi-build: Add smmuv3 node in IORT table... Checking PATCH 6/9: hw/arm/virt: Add tlbi-on-map property to the smmuv3 node... Checking PATCH 7/9: target/arm/kvm: Translate the MSI doorbell in kvm_arch_fixup_msi_route... Checking PATCH 8/9: hw/arm/smmuv3: VFIO integration...
Checking PATCH 9/9: hw/arm/virt-acpi-build: Use the ACPI_IORT_SMMU_V3_CACHING_MODE model...
ERROR: code indent should never use tabs
#26: FILE: hw/arm/virt-acpi-build.c:440:
+^I^Ismmu->model = 0x3; /* ACPI_IORT_SMMU_V3_CACHING_MODE */$
total: 1 errors, 0 warnings, 14 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]