ARM SMMUv3: IOTLB Emulation and VHOST Support
v3 cover ARM SMMUv3: IOTLB Emulation and VHOST Support
This series brings translation configuration caching and IOTLB emulation. The last patch implements VHOST integration and allows to run VSMMUv3 along with VHOST emulated end points.
The first patch fixes the passthrough mode bug reported by Jia. It reworks the translate function and this series needed to be rebased on it.
Best Regards
Eric
This series can be found at:
v1: https://github.com/eauger/qemu/tree/v2.12.0-vsmmu-optim-v3
Previous version at:
v1: https://github.com/eauger/qemu/tree/v2.12.0-vsmmu-optim-v2
History:
v2 -> v3:
- collected Peter's R-b
- do not use float in trace points anymore
v1 -> v2:
- addition of the 1st patch (including addr_mask fix)
- addition of the per device mutex as on intel iommu
v1: [1] v12 last 3 patches resent in this series
[1] [PATCH v12 00/17] ARM SMMUv3 Emulation Support
https://lists.gnu.org/archive/html/qemu-devel/2018-04/msg04344.html
Eric Auger (3):
hw/arm/smmuv3: Cache/invalidate config data hw/arm/smmuv3: IOTLB emulation hw/arm/smmuv3: Add notifications on invalidation
Jia He (1):
hw/arm/smmuv3: Fix translate error handling
hw/arm/smmu-common.c | 118 +++++++++++- hw/arm/smmuv3-internal.h | 12 +- hw/arm/smmuv3.c | 420 +++++++++++++++++++++++++++++++++++++++---- hw/arm/trace-events | 27 ++- include/hw/arm/smmu-common.h | 24 +++ include/hw/arm/smmuv3.h | 1 + 6 files changed, 558 insertions(+), 44 deletions(-)
Signature · 1 line
2.5.5
PATCH v3 1/4 hw/arm/smmuv3: Fix translate error handling
From: Jia He <[email protected]>
In case the STE's config is "Bypass" we currently don't set the IOMMUTLBEntry perm flags and the access does not succeed. Also if the config is 0b0xx (Aborted/Reserved), decode_ste and smmuv3_decode_config currently returns -EINVAL and we don't enter the expected code path: we record an event whereas we should not.
This patch fixes those bugs and simplifies the error handling. decode_ste and smmuv3_decode_config now return 0 if aborted or bypassed config was found. Only bad config info produces negative error values. In smmuv3_translate we more clearly differentiate errors, bypass/smmu disabled, aborted and success cases. Also trace points are differentiated.
---
v2 -> v3:
- added Peter's R-b
v1 -> v2:
- set addr_mask in case of bypass and disabled. Mandated for vhost use case since a411c84b561baa94b28165c52f21c33517ee8f59 "exec: extract address_space_translate_iommu, fix page_mask corner case"
--- hw/arm/smmuv3-internal.h | 12 +++++- hw/arm/smmuv3.c | 96 +++++++++++++++++++++++++++++++++--------------- hw/arm/trace-events | 7 ++-- 3 files changed, 80 insertions(+), 35 deletions(-)
hw/arm/smmuv3-internal.h +10 -2
@@ -23,6 +23,14 @@#include "hw/arm/smmu-common.h" +typedef enum SMMUTranslationStatus { +SMMU_TRANS_DISABLE , +SMMU_TRANS_ABORT , +SMMU_TRANS_BYPASS , +SMMU_TRANS_ERROR , +SMMU_TRANS_SUCCESS , +}SMMUTranslationStatus ; +/* MMIO Registers */ REG32 (IDR0 ,0x0 )
@@ -315,7 +323,7 @@ enum { /* Command completion notification *//* Events */ typedef enum SMMUEventType { - SMMU_EVT_OK = 0x00, + SMMU_EVT_NONE = 0x00, SMMU_EVT_F_UUT , SMMU_EVT_C_BAD_STREAMID , SMMU_EVT_F_STE_FETCH ,
@@ -337,7 +345,7 @@ typedef enum SMMUEventType { } SMMUEventType;static const char *event_stringify[] = { - [SMMU_EVT_OK ] ="SMMU_EVT_OK" , + [SMMU_EVT_NONE ] ="no recorded event" , [SMMU_EVT_F_UUT ] ="SMMU_EVT_F_UUT" , [SMMU_EVT_C_BAD_STREAMID ] ="SMMU_EVT_C_BAD_STREAMID" , [SMMU_EVT_F_STE_FETCH ] ="SMMU_EVT_F_STE_FETCH" ,
hw/arm/smmuv3.c +66 -30
179 lines not on this page · raw message
hw/arm/trace-events +4 -3
@@ -33,9 +33,10 @@ smmuv3_record_event(const char *type, uint32_t sid) "%s sid=%d" 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, uint64_t l1ptr, int l1_ste_offset, uint64_t l2ptr, int l2_ste_offset, int max_l2_ste) "strtab_base:0x%"PRIx64" l1ptr:0x%"PRIx64" l1_off:0x%x, l2ptr:0x%"PRIx64" l2_off:0x%x max_l2_ste:%d" smmuv3_get_ste(uint64_t addr) "STE addr: 0x%"PRIx64 -smmuv3_translate_bypass(const char *n, uint16_t sid, uint64_t addr, bool is_write) "%s sid=%d bypass iova:0x%"PRIx64" is_write=%d" -smmuv3_translate_in(uint16_t sid, int pci_bus_num, uint64_t strtab_base) "SID:0x%x bus:%d strtab_base:0x%"PRIx64 +smmuv3_translate_disable(const char *n, uint16_t sid, uint64_t addr, bool is_write) "%s sid=%d bypass (smmu disabled) iova:0x%"PRIx64" is_write=%d" +smmuv3_translate_bypass(const char *n, uint16_t sid, uint64_t addr, bool is_write) "%s sid=%d STE bypass iova:0x%"PRIx64" is_write=%d" +smmuv3_translate_abort(const char *n, uint16_t sid, uint64_t addr, bool is_write) "%s sid=%d abort on iova:0x%"PRIx64" is_write=%d" +smmuv3_translate_success(const char *n, uint16_t sid, uint64_t iova, uint64_t translated, int perm) "%s sid=%d iova=0x%"PRIx64" translated=0x%"PRIx64" perm=0x%x" smmuv3_get_cd(uint64_t addr) "CD addr: 0x%"PRIx64 -smmuv3_translate(const char *n, uint16_t sid, uint64_t iova, uint64_t translated, int perm) "%s sid=%d iova=0x%"PRIx64" translated=0x%"PRIx64" perm=0x%x" smmuv3_decode_cd(uint32_t oas) "oas=%d" smmuv3_decode_cd_tt(int i, uint32_t tsz, uint64_t ttb, uint32_t granule_sz) "TT[%d]:tsz:%d ttb:0x%"PRIx64" granule_sz:%d"
Signature · 1 line
2.5.5
PATCH v3 2/4 hw/arm/smmuv3: Cache/invalidate config data
In reply to Eric AugerLet's cache config data to avoid fetching and parsing STE/CD structures on each translation. We invalidate them on data structure invalidation commands.
We put in place a per-smmu mutex to protect the config cache. This will be useful too to protect the IOTLB cache. The caches can be accessed without BQL, ie. in IO dataplane. The same kind of mutex was put in place in the intel viommu.
---
v2 -> v3:
- use uint32_t instead of float in cache hit rate traces
- change the format of the new traces
- added Peter's R-b
v1 -> v2:
- restore mutex
v1:
- only insert the new config if decode_cfg succeeds
- use smmu_get_sid for trace_* and store hits/misses in the SMMUDevice
- s/smmuv3_put_config/smmuv3_flush_config
- document smmuv3_get_config
- removing the mutex as BQL does the job
--- hw/arm/smmu-common.c | 24 +++++++- hw/arm/smmuv3.c | 135 +++++++++++++++++++++++++++++++++++++++++-- hw/arm/trace-events | 6 ++ include/hw/arm/smmu-common.h | 5 ++ include/hw/arm/smmuv3.h | 1 + 5 files changed, 164 insertions(+), 7 deletions(-)
hw/arm/smmu-common.c +22 -2
@@ -310,6 +310,24 @@ static AddressSpace *smmu_find_add_as(PCIBus *bus, void *opaque, int devfn)return &sdev->as; } +IOMMUMemoryRegion *smmu_iommu_mr (SMMUState *s,uint32_t sid) +{ +uint8_t bus_n, devfn; +SMMUPciBus *smmu_bus; +SMMUDevice *smmu; + + bus_n =PCI_BUS_NUM (sid); + smmu_bus =smmu_find_smmu_pcibus (s, bus_n); +if (smmu_bus) { + devfn = sid &0x7 ; + smmu = smmu_bus->pbdev[devfn]; +if (smmu) { +return &smmu->iommu; + } + } +return NULL ; +} +static void smmu_base_realize (DeviceState *dev,Error **errp) {SMMUState *s =ARM_SMMU (dev);
@@ -321,7 +339,7 @@ static void smmu_base_realize(DeviceState *dev, Error **errp)error_propagate (errp, local_err);return ; } - + s->configs =g_hash_table_new_full (NULL ,NULL ,NULL , g_free); s->smmu_pcibus_by_busptr =g_hash_table_new (NULL ,NULL );if (s->primary_bus) {
@@ -333,7 +351,9 @@ static void smmu_base_realize(DeviceState *dev, Error **errp)static void smmu_base_reset (DeviceState *dev) { -/* will be filled later on */ +SMMUState *s =ARM_SMMU (dev); + +g_hash_table_remove_all (s->configs); }static Property smmu_dev_properties[] = {
hw/arm/smmuv3.c +130 -5
210 lines not on this page · raw message
hw/arm/trace-events +6 -0
@@ -40,3 +40,9 @@ smmuv3_translate_success(const char *n, uint16_t sid, uint64_t iova, uint64_t tr smmuv3_get_cd(uint64_t addr) "CD addr: 0x%"PRIx64 smmuv3_decode_cd(uint32_t oas) "oas=%d" smmuv3_decode_cd_tt(int i, uint32_t tsz, uint64_t ttb, uint32_t granule_sz) "TT[%d]:tsz:%d ttb:0x%"PRIx64" granule_sz:%d" +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_cfgi_cd(uint32_t sid) "streamid = %d" +smmuv3_config_cache_hit(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache HIT for sid %d (hits=%d, misses=%d, hit rate=%d)" +smmuv3_config_cache_miss(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache MISS for sid %d (hits=%d, misses=%d, hit rate=%d)" +smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid %d"
include/hw/arm/smmu-common.h +5 -0
@@ -75,6 +75,8 @@ typedef struct SMMUDevice {int devfn;IOMMUMemoryRegion iommu;AddressSpace as; +uint32_t cfg_cache_hits; +uint32_t cfg_cache_misses; } SMMUDevice; typedef struct SMMUNotifierNode {
@@ -142,4 +144,7 @@ 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 */ +IOMMUMemoryRegion *smmu_iommu_mr (SMMUState *s,uint32_t sid); +#endif /* HW_ARM_SMMU_COMMON */
include/hw/arm/smmuv3.h +1 -0
@@ -59,6 +59,7 @@ typedef struct SMMUv3State {SMMUQueue eventq, cmdq;qemu_irq irq[4 ]; +QemuMutex mutex; } SMMUv3State; typedef enum {
Signature · 1 line
2.5.5
PATCH v3 3/4 hw/arm/smmuv3: IOTLB emulation
In reply to Eric AugerWe emulate a TLB cache of size SMMU_IOTLB_MAX_SIZE=256. It is implemented as a hash table whose key is a combination of the 16b asid and 48b IOVA (Jenkins hash).
Entries are invalidated on TLB invalidation commands, either globally, or per asid, or per asid/iova.
---
v2 -> v3:
- use uint32_t instead of float when printing the iotlb hit rate
- use const local variables in smmu_iotlb_key_equal
v1 -> v2:
- add comment about Jenkins Hash
- remove init of iotlb_hits, misses
v1:
- Add new trace point when smmu is bypassed
- s/iotlb_miss/iotlb_misses, s/iotlb_hit/iotlb_hits
- use SMMUIOTLBKey as a key
Credit to Tomasz Nowicki who did the first implementation of this IOTLB implementation, inspired of intel_iommu implementation.
--- hw/arm/smmu-common.c | 60 +++++++++++++++++++++++++++ hw/arm/smmuv3.c | 98 ++++++++++++++++++++++++++++++++++++++++++-- hw/arm/trace-events | 9 ++++ include/hw/arm/smmu-common.h | 13 ++++++ 4 files changed, 176 insertions(+), 4 deletions(-)
hw/arm/smmu-common.c +60 -0
@@ -24,11 +24,43 @@#include "qom/cpu.h" #include "hw/qdev-properties.h" #include "qapi/error.h" +#include "qemu/jhash.h" #include "qemu/error-report.h" #include "hw/arm/smmu-common.h" #include "smmu-internal.h" +/* IOTLB Management */ + +inline void smmu_iotlb_inv_all (SMMUState *s) +{ +trace_smmu_iotlb_inv_all (); +g_hash_table_remove_all (s->iotlb); +} + +static gboolean smmu_hash_remove_by_asid (gpointer key,gpointer value, +gpointer user_data) +{ +uint16_t asid = *(uint16_t *)user_data; +SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key; + +return iotlb_key->asid == asid; +} + +inline void smmu_iotlb_inv_iova (SMMUState *s,uint16_t asid,dma_addr_t iova) +{ +SMMUIOTLBKey key = {.asid = asid, .iova = iova}; + +trace_smmu_iotlb_inv_iova (asid, iova); +g_hash_table_remove (s->iotlb, &key); +} + +inline void smmu_iotlb_inv_asid (SMMUState *s,uint16_t asid) +{ +trace_smmu_iotlb_inv_asid (asid); +g_hash_table_foreach_remove (s->iotlb, smmu_hash_remove_by_asid, &asid); +} +/* VMSAv8-64 Translation */ /**
@@ -328,6 +360,31 @@ IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid)return NULL ; } +static guint smmu_iotlb_key_hash (gconstpointer v) +{ +SMMUIOTLBKey *key = (SMMUIOTLBKey *)v; +uint32_t a, b, c; + +/* Jenkins hash */ + a = b = c =JHASH_INITVAL +sizeof (*key); + a += key->asid; + b +=extract64 (key->iova,0 ,32 ); + c +=extract64 (key->iova,32 ,32 ); + +__jhash_mix (a, b, c); +__jhash_final (a, b, c); + +return c; +} + +static gboolean smmu_iotlb_key_equal (gconstpointer v1,gconstpointer v2) +{ +const SMMUIOTLBKey *k1 = v1; +const SMMUIOTLBKey *k2 = v2; + +return (k1->asid == k2->asid) && (k1->iova == k2->iova); +} +static void smmu_base_realize (DeviceState *dev,Error **errp) {SMMUState *s =ARM_SMMU (dev);
@@ -340,6 +397,8 @@ static void smmu_base_realize(DeviceState *dev, Error **errp)return ; } s->configs =g_hash_table_new_full (NULL ,NULL ,NULL , g_free); + s->iotlb =g_hash_table_new_full (smmu_iotlb_key_hash, smmu_iotlb_key_equal, + g_free, g_free); s->smmu_pcibus_by_busptr =g_hash_table_new (NULL ,NULL );if (s->primary_bus) {
@@ -354,6 +413,7 @@ static void smmu_base_reset(DeviceState *dev)SMMUState *s =ARM_SMMU (dev);g_hash_table_remove_all (s->configs); +g_hash_table_remove_all (s->iotlb); }static Property smmu_dev_properties[] = {
hw/arm/smmuv3.c +94 -4
142 lines not on this page · raw message
hw/arm/trace-events +9 -0
@@ -12,6 +12,11 @@ smmu_ptw_invalid_pte(int stage, int level, uint64_t baseaddr, uint64_t pteaddr, smmu_ptw_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_ptw_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_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_iotlb_cache_hit(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, uint32_t p) "IOTLB cache HIT asid=%d addr=0x%"PRIx64" hit=%d miss=%d hit rate=%d" +smmu_iotlb_cache_miss(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, uint32_t p) "IOTLB cache MISS asid=%d addr=0x%"PRIx64" hit=%d miss=%d hit rate=%d" +smmu_iotlb_inv_all(void) "IOTLB invalidate all" +smmu_iotlb_inv_asid(uint16_t asid) "IOTLB invalidate asid=%d" +smmu_iotlb_inv_iova(uint16_t asid, uint64_t addr) "IOTLB invalidate asid=%d addr=0x%"PRIx64 #hw/arm/smmuv3.c smmuv3_read_mmio(uint64_t addr, uint64_t val, unsigned size, uint32_t r) "addr: 0x%"PRIx64" val:0x%"PRIx64" size: 0x%x(%d)"
@@ -45,4 +50,8 @@ smmuv3_cmdq_cfgi_ste_range(int start, int end) "start=0x%d - end=0x%d" smmuv3_cmdq_cfgi_cd(uint32_t sid) "streamid = %d" smmuv3_config_cache_hit(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache HIT for sid %d (hits=%d, misses=%d, hit rate=%d)" smmuv3_config_cache_miss(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache MISS for sid %d (hits=%d, misses=%d, hit rate=%d)" +smmuv3_cmdq_tlbi_nh_va(int vmid, int asid, uint64_t addr, bool leaf) "vmid =%d asid =%d addr=0x%"PRIx64" leaf=%d" +smmuv3_cmdq_tlbi_nh_vaa(int vmid, uint64_t addr) "vmid =%d addr=0x%"PRIx64 +smmuv3_cmdq_tlbi_nh(void) "" +smmuv3_cmdq_tlbi_nh_asid(uint16_t asid) "asid=%d" smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid %d"
include/hw/arm/smmu-common.h +13 -0
@@ -67,6 +67,8 @@ typedef struct SMMUTransCfg {uint8_t tbi;/* Top Byte Ignore */ uint16_t asid;SMMUTransTableInfo tt[2 ]; +uint32_t iotlb_hits;/* counts IOTLB hits for this asid */ +uint32_t iotlb_misses;/* counts IOTLB misses for this asid */ } SMMUTransCfg; typedef struct SMMUDevice {
@@ -89,6 +91,11 @@ typedef struct SMMUPciBus {SMMUDevice *pbdev[0 ];/* Parent array is sparse, so dynamically alloc */ } SMMUPciBus; +typedef struct SMMUIOTLBKey { +uint64_t iova; +uint16_t asid; +}SMMUIOTLBKey ; + typedef struct SMMUState { /* <private> */ SysBusDevice dev;
@@ -147,4 +154,10 @@ SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova);/* Return the iommu mr associated to @sid, or NULL if none */ IOMMUMemoryRegion *smmu_iommu_mr (SMMUState *s,uint32_t sid); +#define SMMU_IOTLB_MAX_SIZE 256 + +void smmu_iotlb_inv_all (SMMUState *s); +void smmu_iotlb_inv_asid (SMMUState *s,uint16_t asid); +void smmu_iotlb_inv_iova (SMMUState *s,uint16_t asid,dma_addr_t iova); +#endif /* HW_ARM_SMMU_COMMON */
Signature · 1 line
2.5.5
PATCH v3 4/4 hw/arm/smmuv3: Add notifications on invalidation
In reply to Eric AugerOn TLB invalidation commands, let's call registered IOMMU notifiers. Those can only be UNMAP notifiers. SMMUv3 does not support notification on MAP (VFIO).
This patch allows vhost use case where IOTLB API is notified on each guest IOTLB invalidation.
---
v2 -> v3:
- added Peter's R-b
--- hw/arm/smmu-common.c | 34 +++++++++++++++ hw/arm/smmuv3.c | 99 +++++++++++++++++++++++++++++++++++++++++++- hw/arm/trace-events | 5 +++ include/hw/arm/smmu-common.h | 6 +++ 4 files changed, 142 insertions(+), 2 deletions(-)
hw/arm/smmu-common.c +34 -0
@@ -385,6 +385,40 @@ static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)return (k1->asid == k2->asid) && (k1->iova == k2->iova); } +/* Unmap the whole notifier's range */ +static void smmu_unmap_notifier_range (IOMMUNotifier *n) +{ +IOMMUTLBEntry entry; + + entry.target_as = &address_space_memory; + entry.iova = n->start; + entry.perm =IOMMU_NONE ; + entry.addr_mask = n->end - n->start; + +memory_region_notify_one (n, &entry); +} + +/* Unmap all notifiers attached to @mr */ +inline void smmu_inv_notifiers_mr (IOMMUMemoryRegion *mr) +{ +IOMMUNotifier *n; + +trace_smmu_inv_notifiers_mr (mr->parent_obj.name); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmu_unmap_notifier_range (n); + } +} + +/* Unmap all notifiers of all mr's */ +void smmu_inv_notifiers_all (SMMUState *s) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +smmu_inv_notifiers_mr (&node->sdev->iommu); + } +} +static void smmu_base_realize (DeviceState *dev,Error **errp) {SMMUState *s =ARM_SMMU (dev);
hw/arm/smmuv3.c +97 -2
136 lines not on this page · raw message
hw/arm/trace-events +5 -0
@@ -17,6 +17,7 @@ smmu_iotlb_cache_miss(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, smmu_iotlb_inv_all(void) "IOTLB invalidate all" smmu_iotlb_inv_asid(uint16_t asid) "IOTLB invalidate asid=%d" smmu_iotlb_inv_iova(uint16_t asid, uint64_t addr) "IOTLB invalidate asid=%d addr=0x%"PRIx64 +smmu_inv_notifiers_mr(const char *name) "iommu mr=%s" #hw/arm/smmuv3.c smmuv3_read_mmio(uint64_t addr, uint64_t val, unsigned size, uint32_t r) "addr: 0x%"PRIx64" val:0x%"PRIx64" size: 0x%x(%d)"
@@ -55,3 +56,7 @@ smmuv3_cmdq_tlbi_nh_vaa(int vmid, uint64_t addr) "vmid =%d addr=0x%"PRIx64 smmuv3_cmdq_tlbi_nh(void) "" smmuv3_cmdq_tlbi_nh_asid(uint16_t asid) "asid=%d" smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid %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_inv_notifiers_iova(const char *name, uint16_t asid, uint64_t iova) "iommu mr=%s asid=%d iova=0x%"PRIx64 +
include/hw/arm/smmu-common.h +6 -0
@@ -160,4 +160,10 @@ void smmu_iotlb_inv_all(SMMUState *s);void smmu_iotlb_inv_asid (SMMUState *s,uint16_t asid);void smmu_iotlb_inv_iova (SMMUState *s,uint16_t asid,dma_addr_t iova); +/* Unmap the range of all the notifiers registered to any IOMMU mr */ +void smmu_inv_notifiers_all (SMMUState *s); + +/* Unmap the range of all the notifiers registered to @mr */ +void smmu_inv_notifiers_mr (IOMMUMemoryRegion *mr); +#endif /* HW_ARM_SMMU_COMMON */
Signature · 1 line
2.5.5
Re: [Qemu-arm] [PATCH v3 4/4] hw/arm/smmuv3: Add notifications on invalidation
H
On 6/21/2018 7:16 PM, Eric Auger Wrote:
Quoted 177 lines
On TLB invalidation commands, let's call registered IOMMU notifiers. Those can only be UNMAP notifiers. SMMUv3 does not support notification on MAP (VFIO).
This patch allows vhost use case where IOTLB API is notified on each guest IOTLB invalidation.
Signed-off-by Eric Auger <[email protected]>Reviewed-by Peter Maydell <[email protected]>---
v2 -> v3:
- added Peter's R-b--- hw/arm/smmu-common.c | 34 +++++++++++++++ hw/arm/smmuv3.c | 99 +++++++++++++++++++++++++++++++++++++++++++- hw/arm/trace-events | 5 +++ include/hw/arm/smmu-common.h | 6 +++ 4 files changed, 142 insertions(+), 2 deletions(-)hw/arm/smmu-common.c +34
-0@@ -385,6 +385,40 @@ static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)return (k1->asid == k2->asid) && (k1->iova == k2->iova); } +/* Unmap the whole notifier's range */ +static void smmu_unmap_notifier_range (IOMMUNotifier *n) +{ +IOMMUTLBEntry entry; + + entry.target_as = &address_space_memory; + entry.iova = n->start; + entry.perm =IOMMU_NONE ; + entry.addr_mask = n->end - n->start; + +memory_region_notify_one (n, &entry); +} + +/* Unmap all notifiers attached to @mr */ +inline void smmu_inv_notifiers_mr (IOMMUMemoryRegion *mr) +{ +IOMMUNotifier *n; + +trace_smmu_inv_notifiers_mr (mr->parent_obj.name); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmu_unmap_notifier_range (n); + } +} + +/* Unmap all notifiers of all mr's */ +void smmu_inv_notifiers_all (SMMUState *s) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +smmu_inv_notifiers_mr (&node->sdev->iommu); + } +} +static void smmu_base_realize (DeviceState *dev,Error **errp) {SMMUState *s =ARM_SMMU (dev);hw/arm/smmuv3.c +70
-0@@ -780,6 +780,68 @@ epilogue:return entry; } +/** +* smmuv3_notify_iova - call the notifier @n for a given +* @asid and @iova tuple. +* +* @mr: IOMMU mr region handle +* @n: notifier to be called +* @asid: address space ID or negative value if we don't care +* @iova: iova +*/ +static void smmuv3_notify_iova (IOMMUMemoryRegion *mr, +IOMMUNotifier *n, +int asid, +dma_addr_t iova) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUEventInfo event = {}; +SMMUTransTableInfo *tt; +SMMUTransCfg *cfg; +IOMMUTLBEntry entry; + + cfg =smmuv3_get_config (sdev, &event); +if (!cfg) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s error decoding the configuration for iommu mr=%s\n" , + __func__, mr->parent_obj.name); +return ; + } + +if (asid >=0 && cfg->asid != asid) { +return ; + } + + tt =select_tt (cfg, iova); +if (!tt) { +return ; + } + + entry.target_as = &address_space_memory; + entry.iova = iova; + entry.addr_mask = (1 << tt->granule_sz) -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); +} + +/* invalidate an asid/iova tuple in all mr's */ +static void smmuv3_inv_notifiers_iova (SMMUState *s,int asid,dma_addr_t iova) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_inv_notifiers_iova (mr->parent_obj.name, asid, iova); + + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_notify_iova (mr, n, asid, iova); + } + } +} +static int smmuv3_cmdq_consume (SMMUv3State *s) {SMMUState *bs =ARM_SMMU (s);@@ -899,12 +961,14 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)uint16_t asid =CMD_ASID (&cmd);trace_smmuv3_cmdq_tlbi_nh_asid (asid); +smmu_inv_notifiers_all (&s->smmu_state);smmu_iotlb_inv_asid (bs, asid);break ; }case SMMU_CMD_TLBI_NH_ALL :case SMMU_CMD_TLBI_NSNH_ALL :trace_smmuv3_cmdq_tlbi_nh (); +smmu_inv_notifiers_all (&s->smmu_state);smmu_iotlb_inv_all (bs);break ;case SMMU_CMD_TLBI_NH_VAA :@@ -913,6 +977,7 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)uint16_t vmid =CMD_VMID (&cmd);trace_smmuv3_cmdq_tlbi_nh_vaa (vmid, addr); +smmuv3_inv_notifiers_iova (bs,-1 , addr);smmu_iotlb_inv_all (bs);break ; }@@ -924,6 +989,7 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)bool leaf =CMD_LEAF (&cmd);trace_smmuv3_cmdq_tlbi_nh_va (vmid, asid, addr, leaf); +smmuv3_inv_notifiers_iova (bs, asid, addr);smmu_iotlb_inv_iova (bs, asid, addr);break ; }@@ -1402,9 +1468,38 @@ 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 ;
hw/arm/smmuv3.c static void smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 1484
Quoted 10 lines
hw/arm/smmuv3.c +10
-0+SMMUNotifierNode *next_node =NULL ; + +if (new ==IOMMU_NOTIFIER_MAP ) { +int bus_num =pci_bus_num (sdev->bus); +PCIDevice *pcidev =pci_find_device (sdev->bus, bus_num, sdev->devfn); + +warn_report ("SMMUv3 does not support notification on MAP: " +"device %s will not function properly" , pcidev->name); + } +
ah, I know why there is no such warning in my testing server.
the IOMMUNotifierFlag is initialized with IOMMU_NOTIFIER_ALL=(IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP). Should this condition be considerred?
Cheers,
Jia
Quoted 59 lines
hw/arm/smmuv3.c +17
-2if (old ==IOMMU_NOTIFIER_NONE ) { -warn_report ("SMMUV3 does not support vhost/vfio integration yet: " -"devices of those types will not function properly" ); +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 ; + } } }hw/arm/trace-events +5
-0@@ -17,6 +17,7 @@ smmu_iotlb_cache_miss(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, smmu_iotlb_inv_all(void) "IOTLB invalidate all" smmu_iotlb_inv_asid(uint16_t asid) "IOTLB invalidate asid=%d" smmu_iotlb_inv_iova(uint16_t asid, uint64_t addr) "IOTLB invalidate asid=%d addr=0x%"PRIx64 +smmu_inv_notifiers_mr(const char *name) "iommu mr=%s" #hw/arm/smmuv3.c smmuv3_read_mmio(uint64_t addr, uint64_t val, unsigned size, uint32_t r) "addr: 0x%"PRIx64" val:0x%"PRIx64" size: 0x%x(%d)"@@ -55,3 +56,7 @@ smmuv3_cmdq_tlbi_nh_vaa(int vmid, uint64_t addr) "vmid =%d addr=0x%"PRIx64 smmuv3_cmdq_tlbi_nh(void) "" smmuv3_cmdq_tlbi_nh_asid(uint16_t asid) "asid=%d" smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid %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_inv_notifiers_iova(const char *name, uint16_t asid, uint64_t iova) "iommu mr=%s asid=%d iova=0x%"PRIx64 +include/hw/arm/smmu-common.h +6
-0@@ -160,4 +160,10 @@ void smmu_iotlb_inv_all(SMMUState *s);void smmu_iotlb_inv_asid (SMMUState *s,uint16_t asid);void smmu_iotlb_inv_iova (SMMUState *s,uint16_t asid,dma_addr_t iova); +/* Unmap the range of all the notifiers registered to any IOMMU mr */ +void smmu_inv_notifiers_all (SMMUState *s); + +/* Unmap the range of all the notifiers registered to @mr */ +void smmu_inv_notifiers_mr (IOMMUMemoryRegion *mr); +#endif /* HW_ARM_SMMU_COMMON */
Re: [Qemu-arm] [Qemu-devel] [PATCH v3 4/4] hw/arm/smmuv3: Add notifications on invalidation
Hello Jia,
Quoted 185 lines · Jia He
H
On 6/21/2018 7:16 PM, Eric Auger Wrote:
Quoted 182 lines
On TLB invalidation commands, let's call registered IOMMU notifiers. Those can only be UNMAP notifiers. SMMUv3 does not support notification on MAP (VFIO).
This patch allows vhost use case where IOTLB API is notified on each guest IOTLB invalidation.
Signed-off-by Eric Auger <[email protected]>Reviewed-by Peter Maydell <[email protected]>---
v2 -> v3:
- added Peter's R-b--- hw/arm/smmu-common.c | 34 +++++++++++++++ hw/arm/smmuv3.c | 99 +++++++++++++++++++++++++++++++++++++++++++- hw/arm/trace-events | 5 +++ include/hw/arm/smmu-common.h | 6 +++ 4 files changed, 142 insertions(+), 2 deletions(-)hw/arm/smmu-common.c +34
-0@@ -385,6 +385,40 @@ static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)return (k1->asid == k2->asid) && (k1->iova == k2->iova); } +/* Unmap the whole notifier's range */ +static void smmu_unmap_notifier_range (IOMMUNotifier *n) +{ +IOMMUTLBEntry entry; + + entry.target_as = &address_space_memory; + entry.iova = n->start; + entry.perm =IOMMU_NONE ; + entry.addr_mask = n->end - n->start; + +memory_region_notify_one (n, &entry); +} + +/* Unmap all notifiers attached to @mr */ +inline void smmu_inv_notifiers_mr (IOMMUMemoryRegion *mr) +{ +IOMMUNotifier *n; + +trace_smmu_inv_notifiers_mr (mr->parent_obj.name); + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmu_unmap_notifier_range (n); + } +} + +/* Unmap all notifiers of all mr's */ +void smmu_inv_notifiers_all (SMMUState *s) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +smmu_inv_notifiers_mr (&node->sdev->iommu); + } +} +static void smmu_base_realize (DeviceState *dev,Error **errp) {SMMUState *s =ARM_SMMU (dev);hw/arm/smmuv3.c +75
-0@@ -780,6 +780,68 @@ epilogue:return entry; } +/** +* smmuv3_notify_iova - call the notifier @n for a given +* @asid and @iova tuple. +* +* @mr: IOMMU mr region handle +* @n: notifier to be called +* @asid: address space ID or negative value if we don't care +* @iova: iova +*/ +static void smmuv3_notify_iova (IOMMUMemoryRegion *mr, +IOMMUNotifier *n, +int asid, +dma_addr_t iova) +{ +SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); +SMMUEventInfo event = {}; +SMMUTransTableInfo *tt; +SMMUTransCfg *cfg; +IOMMUTLBEntry entry; + + cfg =smmuv3_get_config (sdev, &event); +if (!cfg) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s error decoding the configuration for iommu mr=%s\n" , + __func__, mr->parent_obj.name); +return ; + } + +if (asid >=0 && cfg->asid != asid) { +return ; + } + + tt =select_tt (cfg, iova); +if (!tt) { +return ; + } + + entry.target_as = &address_space_memory; + entry.iova = iova; + entry.addr_mask = (1 << tt->granule_sz) -1 ; + entry.perm =IOMMU_NONE ; + +memory_region_notify_one (n, &entry); +} + +/* invalidate an asid/iova tuple in all mr's */ +static void smmuv3_inv_notifiers_iova (SMMUState *s,int asid,dma_addr_t iova) +{ +SMMUNotifierNode *node; + + QLIST_FOREACH(node, &s->notifiers_list, next) { +IOMMUMemoryRegion *mr = &node->sdev->iommu; +IOMMUNotifier *n; + +trace_smmuv3_inv_notifiers_iova (mr->parent_obj.name, asid, iova); + + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_notify_iova (mr, n, asid, iova); + } + } +} +static int smmuv3_cmdq_consume (SMMUv3State *s) {SMMUState *bs =ARM_SMMU (s);@@ -899,12 +961,14 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)uint16_t asid =CMD_ASID (&cmd);trace_smmuv3_cmdq_tlbi_nh_asid (asid); +smmu_inv_notifiers_all (&s->smmu_state);smmu_iotlb_inv_asid (bs, asid);break ; }case SMMU_CMD_TLBI_NH_ALL :case SMMU_CMD_TLBI_NSNH_ALL :trace_smmuv3_cmdq_tlbi_nh (); +smmu_inv_notifiers_all (&s->smmu_state);smmu_iotlb_inv_all (bs);break ;case SMMU_CMD_TLBI_NH_VAA :@@ -913,6 +977,7 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)uint16_t vmid =CMD_VMID (&cmd);trace_smmuv3_cmdq_tlbi_nh_vaa (vmid, addr); +smmuv3_inv_notifiers_iova (bs,-1 , addr);smmu_iotlb_inv_all (bs);break ; }@@ -924,6 +989,7 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)bool leaf =CMD_LEAF (&cmd);trace_smmuv3_cmdq_tlbi_nh_va (vmid, asid, addr, leaf); +smmuv3_inv_notifiers_iova (bs, asid, addr);smmu_iotlb_inv_iova (bs, asid, addr);break ; }@@ -1402,9 +1468,38 @@ 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 (new ==IOMMU_NOTIFIER_MAP ) { +int bus_num =pci_bus_num (sdev->bus); +PCIDevice *pcidev =pci_find_device (sdev->bus, bus_num, sdev->devfn);
Quoted 8 lines
hw/arm/smmuv3.c static void smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 1484
Quoted 5 lines
hw/arm/smmuv3.c +5
-0+ +warn_report ("SMMUv3 does not support notification on MAP: " +"device %s will not function properly" , pcidev->name); + } +ah, I know why there is no such warning in my testing server.
the IOMMUNotifierFlag is initialized with IOMMU_NOTIFIER_ALL=(IOMMU_NOTIFIER_MAP |IOMMU_NOTIFIER_UNMAP). Should this condition be considerred?
Hum yes it is a bug. I will fix this asap!
Thanks
Eric
Quoted 63 lines
Cheers,
JiaQuoted 59 lines
hw/arm/smmuv3.c +17
-2if (old ==IOMMU_NOTIFIER_NONE ) { -warn_report ("SMMUV3 does not support vhost/vfio integration yet: " -"devices of those types will not function properly" ); +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 ; + } } }hw/arm/trace-events +5
-0@@ -17,6 +17,7 @@ smmu_iotlb_cache_miss(uint16_t asid, uint64_t addr, uint32_t hit, uint32_t miss, smmu_iotlb_inv_all(void) "IOTLB invalidate all" smmu_iotlb_inv_asid(uint16_t asid) "IOTLB invalidate asid=%d" smmu_iotlb_inv_iova(uint16_t asid, uint64_t addr) "IOTLB invalidate asid=%d addr=0x%"PRIx64 +smmu_inv_notifiers_mr(const char *name) "iommu mr=%s" #hw/arm/smmuv3.c smmuv3_read_mmio(uint64_t addr, uint64_t val, unsigned size, uint32_t r) "addr: 0x%"PRIx64" val:0x%"PRIx64" size: 0x%x(%d)"@@ -55,3 +56,7 @@ smmuv3_cmdq_tlbi_nh_vaa(int vmid, uint64_t addr) "vmid =%d addr=0x%"PRIx64 smmuv3_cmdq_tlbi_nh(void) "" smmuv3_cmdq_tlbi_nh_asid(uint16_t asid) "asid=%d" smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid %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_inv_notifiers_iova(const char *name, uint16_t asid, uint64_t iova) "iommu mr=%s asid=%d iova=0x%"PRIx64 +include/hw/arm/smmu-common.h +6
-0@@ -160,4 +160,10 @@ void smmu_iotlb_inv_all(SMMUState *s);void smmu_iotlb_inv_asid (SMMUState *s,uint16_t asid);void smmu_iotlb_inv_iova (SMMUState *s,uint16_t asid,dma_addr_t iova); +/* Unmap the range of all the notifiers registered to any IOMMU mr */ +void smmu_inv_notifiers_all (SMMUState *s); + +/* Unmap the range of all the notifiers registered to @mr */ +void smmu_inv_notifiers_mr (IOMMUMemoryRegion *mr); +#endif /* HW_ARM_SMMU_COMMON */