hw/arm/smmuv3: Add VMID to TLB tagging

Allow TLB to be tagged with VMID.

If stage-1 is only supported, VMID is set to -1 and ignored from STE
and CMD_TLBI_NH* cmds.

Update smmu_iotlb_insert trace event to have vmid.

Signed-off-by: Mostafa Saleh <smostafa@google.com>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Message-id: 20230516203327.2051088-8-smostafa@google.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Mostafa Saleh 2023-05-25 10:37:50 +01:00 committed by Peter Maydell
parent cd617556ad
commit 2eaeb7d593
5 changed files with 39 additions and 22 deletions

View File

@ -38,7 +38,7 @@ static guint smmu_iotlb_key_hash(gconstpointer v)
/* Jenkins hash */ /* Jenkins hash */
a = b = c = JHASH_INITVAL + sizeof(*key); a = b = c = JHASH_INITVAL + sizeof(*key);
a += key->asid + key->level + key->tg; a += key->asid + key->vmid + key->level + key->tg;
b += extract64(key->iova, 0, 32); b += extract64(key->iova, 0, 32);
c += extract64(key->iova, 32, 32); c += extract64(key->iova, 32, 32);
@ -53,13 +53,15 @@ static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)
SMMUIOTLBKey *k1 = (SMMUIOTLBKey *)v1, *k2 = (SMMUIOTLBKey *)v2; SMMUIOTLBKey *k1 = (SMMUIOTLBKey *)v1, *k2 = (SMMUIOTLBKey *)v2;
return (k1->asid == k2->asid) && (k1->iova == k2->iova) && return (k1->asid == k2->asid) && (k1->iova == k2->iova) &&
(k1->level == k2->level) && (k1->tg == k2->tg); (k1->level == k2->level) && (k1->tg == k2->tg) &&
(k1->vmid == k2->vmid);
} }
SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova, SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint16_t vmid, uint64_t iova,
uint8_t tg, uint8_t level) uint8_t tg, uint8_t level)
{ {
SMMUIOTLBKey key = {.asid = asid, .iova = iova, .tg = tg, .level = level}; SMMUIOTLBKey key = {.asid = asid, .vmid = vmid, .iova = iova,
.tg = tg, .level = level};
return key; return key;
} }
@ -78,7 +80,8 @@ SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
uint64_t mask = subpage_size - 1; uint64_t mask = subpage_size - 1;
SMMUIOTLBKey key; SMMUIOTLBKey key;
key = smmu_get_iotlb_key(cfg->asid, iova & ~mask, tg, level); key = smmu_get_iotlb_key(cfg->asid, cfg->s2cfg.vmid,
iova & ~mask, tg, level);
entry = g_hash_table_lookup(bs->iotlb, &key); entry = g_hash_table_lookup(bs->iotlb, &key);
if (entry) { if (entry) {
break; break;
@ -88,13 +91,13 @@ SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
if (entry) { if (entry) {
cfg->iotlb_hits++; cfg->iotlb_hits++;
trace_smmu_iotlb_lookup_hit(cfg->asid, iova, trace_smmu_iotlb_lookup_hit(cfg->asid, cfg->s2cfg.vmid, iova,
cfg->iotlb_hits, cfg->iotlb_misses, cfg->iotlb_hits, cfg->iotlb_misses,
100 * cfg->iotlb_hits / 100 * cfg->iotlb_hits /
(cfg->iotlb_hits + cfg->iotlb_misses)); (cfg->iotlb_hits + cfg->iotlb_misses));
} else { } else {
cfg->iotlb_misses++; cfg->iotlb_misses++;
trace_smmu_iotlb_lookup_miss(cfg->asid, iova, trace_smmu_iotlb_lookup_miss(cfg->asid, cfg->s2cfg.vmid, iova,
cfg->iotlb_hits, cfg->iotlb_misses, cfg->iotlb_hits, cfg->iotlb_misses,
100 * cfg->iotlb_hits / 100 * cfg->iotlb_hits /
(cfg->iotlb_hits + cfg->iotlb_misses)); (cfg->iotlb_hits + cfg->iotlb_misses));
@ -111,8 +114,10 @@ void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *new)
smmu_iotlb_inv_all(bs); smmu_iotlb_inv_all(bs);
} }
*key = smmu_get_iotlb_key(cfg->asid, new->entry.iova, tg, new->level); *key = smmu_get_iotlb_key(cfg->asid, cfg->s2cfg.vmid, new->entry.iova,
trace_smmu_iotlb_insert(cfg->asid, new->entry.iova, tg, new->level); tg, new->level);
trace_smmu_iotlb_insert(cfg->asid, cfg->s2cfg.vmid, new->entry.iova,
tg, new->level);
g_hash_table_insert(bs->iotlb, key, new); g_hash_table_insert(bs->iotlb, key, new);
} }
@ -130,8 +135,7 @@ static gboolean smmu_hash_remove_by_asid(gpointer key, gpointer value,
return SMMU_IOTLB_ASID(*iotlb_key) == asid; return SMMU_IOTLB_ASID(*iotlb_key) == asid;
} }
static gboolean smmu_hash_remove_by_asid_vmid_iova(gpointer key, gpointer value,
static gboolean smmu_hash_remove_by_asid_iova(gpointer key, gpointer value,
gpointer user_data) gpointer user_data)
{ {
SMMUTLBEntry *iter = (SMMUTLBEntry *)value; SMMUTLBEntry *iter = (SMMUTLBEntry *)value;
@ -142,18 +146,21 @@ static gboolean smmu_hash_remove_by_asid_iova(gpointer key, gpointer value,
if (info->asid >= 0 && info->asid != SMMU_IOTLB_ASID(iotlb_key)) { if (info->asid >= 0 && info->asid != SMMU_IOTLB_ASID(iotlb_key)) {
return false; return false;
} }
if (info->vmid >= 0 && info->vmid != SMMU_IOTLB_VMID(iotlb_key)) {
return false;
}
return ((info->iova & ~entry->addr_mask) == entry->iova) || return ((info->iova & ~entry->addr_mask) == entry->iova) ||
((entry->iova & ~info->mask) == info->iova); ((entry->iova & ~info->mask) == info->iova);
} }
void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova, void smmu_iotlb_inv_iova(SMMUState *s, int asid, int vmid, dma_addr_t iova,
uint8_t tg, uint64_t num_pages, uint8_t ttl) uint8_t tg, uint64_t num_pages, uint8_t ttl)
{ {
/* if tg is not set we use 4KB range invalidation */ /* if tg is not set we use 4KB range invalidation */
uint8_t granule = tg ? tg * 2 + 10 : 12; uint8_t granule = tg ? tg * 2 + 10 : 12;
if (ttl && (num_pages == 1) && (asid >= 0)) { if (ttl && (num_pages == 1) && (asid >= 0)) {
SMMUIOTLBKey key = smmu_get_iotlb_key(asid, iova, tg, ttl); SMMUIOTLBKey key = smmu_get_iotlb_key(asid, vmid, iova, tg, ttl);
if (g_hash_table_remove(s->iotlb, &key)) { if (g_hash_table_remove(s->iotlb, &key)) {
return; return;
@ -166,10 +173,11 @@ void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova,
SMMUIOTLBPageInvInfo info = { SMMUIOTLBPageInvInfo info = {
.asid = asid, .iova = iova, .asid = asid, .iova = iova,
.vmid = vmid,
.mask = (num_pages * 1 << granule) - 1}; .mask = (num_pages * 1 << granule) - 1};
g_hash_table_foreach_remove(s->iotlb, g_hash_table_foreach_remove(s->iotlb,
smmu_hash_remove_by_asid_iova, smmu_hash_remove_by_asid_vmid_iova,
&info); &info);
} }

View File

@ -132,9 +132,11 @@ static inline int pgd_concat_idx(int start_level, int granule_sz,
} }
#define SMMU_IOTLB_ASID(key) ((key).asid) #define SMMU_IOTLB_ASID(key) ((key).asid)
#define SMMU_IOTLB_VMID(key) ((key).vmid)
typedef struct SMMUIOTLBPageInvInfo { typedef struct SMMUIOTLBPageInvInfo {
int asid; int asid;
int vmid;
uint64_t iova; uint64_t iova;
uint64_t mask; uint64_t mask;
} SMMUIOTLBPageInvInfo; } SMMUIOTLBPageInvInfo;

View File

@ -1067,7 +1067,7 @@ static void smmuv3_s1_range_inval(SMMUState *s, Cmd *cmd)
{ {
dma_addr_t end, addr = CMD_ADDR(cmd); dma_addr_t end, addr = CMD_ADDR(cmd);
uint8_t type = CMD_TYPE(cmd); uint8_t type = CMD_TYPE(cmd);
uint16_t vmid = CMD_VMID(cmd); int vmid = -1;
uint8_t scale = CMD_SCALE(cmd); uint8_t scale = CMD_SCALE(cmd);
uint8_t num = CMD_NUM(cmd); uint8_t num = CMD_NUM(cmd);
uint8_t ttl = CMD_TTL(cmd); uint8_t ttl = CMD_TTL(cmd);
@ -1076,6 +1076,12 @@ static void smmuv3_s1_range_inval(SMMUState *s, Cmd *cmd)
uint64_t num_pages; uint64_t num_pages;
uint8_t granule; uint8_t granule;
int asid = -1; int asid = -1;
SMMUv3State *smmuv3 = ARM_SMMUV3(s);
/* Only consider VMID if stage-2 is supported. */
if (STAGE2_SUPPORTED(smmuv3)) {
vmid = CMD_VMID(cmd);
}
if (type == SMMU_CMD_TLBI_NH_VA) { if (type == SMMU_CMD_TLBI_NH_VA) {
asid = CMD_ASID(cmd); asid = CMD_ASID(cmd);
@ -1084,7 +1090,7 @@ static void smmuv3_s1_range_inval(SMMUState *s, Cmd *cmd)
if (!tg) { if (!tg) {
trace_smmuv3_s1_range_inval(vmid, asid, addr, tg, 1, ttl, leaf); trace_smmuv3_s1_range_inval(vmid, asid, addr, tg, 1, ttl, leaf);
smmuv3_inv_notifiers_iova(s, asid, addr, tg, 1); smmuv3_inv_notifiers_iova(s, asid, addr, tg, 1);
smmu_iotlb_inv_iova(s, asid, addr, tg, 1, ttl); smmu_iotlb_inv_iova(s, asid, vmid, addr, tg, 1, ttl);
return; return;
} }
@ -1102,7 +1108,7 @@ static void smmuv3_s1_range_inval(SMMUState *s, Cmd *cmd)
num_pages = (mask + 1) >> granule; num_pages = (mask + 1) >> granule;
trace_smmuv3_s1_range_inval(vmid, asid, addr, tg, num_pages, ttl, leaf); trace_smmuv3_s1_range_inval(vmid, asid, addr, tg, num_pages, ttl, leaf);
smmuv3_inv_notifiers_iova(s, asid, addr, tg, num_pages); smmuv3_inv_notifiers_iova(s, asid, addr, tg, num_pages);
smmu_iotlb_inv_iova(s, asid, addr, tg, num_pages, ttl); smmu_iotlb_inv_iova(s, asid, vmid, addr, tg, num_pages, ttl);
addr += mask + 1; addr += mask + 1;
} }
} }

View File

@ -14,9 +14,9 @@ smmu_iotlb_inv_all(void) "IOTLB invalidate all"
smmu_iotlb_inv_asid(uint16_t asid) "IOTLB invalidate asid=%d" 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_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" smmu_inv_notifiers_mr(const char *name) "iommu mr=%s"
smmu_iotlb_lookup_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_lookup_hit(uint16_t asid, uint16_t vmid, uint64_t addr, uint32_t hit, uint32_t miss, uint32_t p) "IOTLB cache HIT asid=%d vmid=%d addr=0x%"PRIx64" hit=%d miss=%d hit rate=%d"
smmu_iotlb_lookup_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_lookup_miss(uint16_t asid, uint16_t vmid, uint64_t addr, uint32_t hit, uint32_t miss, uint32_t p) "IOTLB cache MISS asid=%d vmid=%d addr=0x%"PRIx64" hit=%d miss=%d hit rate=%d"
smmu_iotlb_insert(uint16_t asid, uint64_t addr, uint8_t tg, uint8_t level) "IOTLB ++ asid=%d addr=0x%"PRIx64" tg=%d level=%d" smmu_iotlb_insert(uint16_t asid, uint16_t vmid, uint64_t addr, uint8_t tg, uint8_t level) "IOTLB ++ asid=%d vmid=%d addr=0x%"PRIx64" tg=%d level=%d"
# smmuv3.c # 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)" smmuv3_read_mmio(uint64_t addr, uint64_t val, unsigned size, uint32_t r) "addr: 0x%"PRIx64" val:0x%"PRIx64" size: 0x%x(%d)"

View File

@ -125,6 +125,7 @@ typedef struct SMMUPciBus {
typedef struct SMMUIOTLBKey { typedef struct SMMUIOTLBKey {
uint64_t iova; uint64_t iova;
uint16_t asid; uint16_t asid;
uint16_t vmid;
uint8_t tg; uint8_t tg;
uint8_t level; uint8_t level;
} SMMUIOTLBKey; } SMMUIOTLBKey;
@ -188,11 +189,11 @@ IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid);
SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
SMMUTransTableInfo *tt, hwaddr iova); SMMUTransTableInfo *tt, hwaddr iova);
void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry); void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry);
SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova, SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint16_t vmid, uint64_t iova,
uint8_t tg, uint8_t level); uint8_t tg, uint8_t level);
void smmu_iotlb_inv_all(SMMUState *s); void smmu_iotlb_inv_all(SMMUState *s);
void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid); void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid);
void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova, void smmu_iotlb_inv_iova(SMMUState *s, int asid, int vmid, dma_addr_t iova,
uint8_t tg, uint64_t num_pages, uint8_t ttl); uint8_t tg, uint64_t num_pages, uint8_t ttl);
/* Unmap the range of all the notifiers registered to any IOMMU mr */ /* Unmap the range of all the notifiers registered to any IOMMU mr */