xref: /openbmc/qemu/hw/arm/smmu-common.c (revision 2eefd4fcec4b8fe41ceee2a8f00cdec1fe81b75c)
1527773eeSEric Auger /*
2527773eeSEric Auger  * Copyright (C) 2014-2016 Broadcom Corporation
3527773eeSEric Auger  * Copyright (c) 2017 Red Hat, Inc.
4527773eeSEric Auger  * Written by Prem Mallappa, Eric Auger
5527773eeSEric Auger  *
6527773eeSEric Auger  * This program is free software; you can redistribute it and/or modify
7527773eeSEric Auger  * it under the terms of the GNU General Public License version 2 as
8527773eeSEric Auger  * published by the Free Software Foundation.
9527773eeSEric Auger  *
10527773eeSEric Auger  * This program is distributed in the hope that it will be useful,
11527773eeSEric Auger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12527773eeSEric Auger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13527773eeSEric Auger  * GNU General Public License for more details.
14527773eeSEric Auger  *
15527773eeSEric Auger  * Author: Prem Mallappa <pmallapp@broadcom.com>
16527773eeSEric Auger  *
17527773eeSEric Auger  */
18527773eeSEric Auger 
19527773eeSEric Auger #include "qemu/osdep.h"
20527773eeSEric Auger #include "trace.h"
21527773eeSEric Auger #include "exec/target_page.h"
222e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
23527773eeSEric Auger #include "hw/qdev-properties.h"
24527773eeSEric Auger #include "qapi/error.h"
25cc27ed81SEric Auger #include "qemu/jhash.h"
260b8fa32fSMarkus Armbruster #include "qemu/module.h"
27527773eeSEric Auger 
28527773eeSEric Auger #include "qemu/error-report.h"
29527773eeSEric Auger #include "hw/arm/smmu-common.h"
3093641948SEric Auger #include "smmu-internal.h"
3193641948SEric Auger 
32cc27ed81SEric Auger /* IOTLB Management */
33cc27ed81SEric Auger 
smmu_iotlb_key_hash(gconstpointer v)3460a61f1bSEric Auger static guint smmu_iotlb_key_hash(gconstpointer v)
3560a61f1bSEric Auger {
3660a61f1bSEric Auger     SMMUIOTLBKey *key = (SMMUIOTLBKey *)v;
3760a61f1bSEric Auger     uint32_t a, b, c;
3860a61f1bSEric Auger 
3960a61f1bSEric Auger     /* Jenkins hash */
4060a61f1bSEric Auger     a = b = c = JHASH_INITVAL + sizeof(*key);
412eaeb7d5SMostafa Saleh     a += key->asid + key->vmid + key->level + key->tg;
4260a61f1bSEric Auger     b += extract64(key->iova, 0, 32);
4360a61f1bSEric Auger     c += extract64(key->iova, 32, 32);
4460a61f1bSEric Auger 
4560a61f1bSEric Auger     __jhash_mix(a, b, c);
4660a61f1bSEric Auger     __jhash_final(a, b, c);
4760a61f1bSEric Auger 
4860a61f1bSEric Auger     return c;
4960a61f1bSEric Auger }
5060a61f1bSEric Auger 
smmu_iotlb_key_equal(gconstpointer v1,gconstpointer v2)5160a61f1bSEric Auger static gboolean smmu_iotlb_key_equal(gconstpointer v1, gconstpointer v2)
5260a61f1bSEric Auger {
539e54dee7SEric Auger     SMMUIOTLBKey *k1 = (SMMUIOTLBKey *)v1, *k2 = (SMMUIOTLBKey *)v2;
5460a61f1bSEric Auger 
559e54dee7SEric Auger     return (k1->asid == k2->asid) && (k1->iova == k2->iova) &&
562eaeb7d5SMostafa Saleh            (k1->level == k2->level) && (k1->tg == k2->tg) &&
572eaeb7d5SMostafa Saleh            (k1->vmid == k2->vmid);
5860a61f1bSEric Auger }
5960a61f1bSEric Auger 
smmu_get_iotlb_key(int asid,int vmid,uint64_t iova,uint8_t tg,uint8_t level)60d8838226SMostafa Saleh SMMUIOTLBKey smmu_get_iotlb_key(int asid, int vmid, uint64_t iova,
619e54dee7SEric Auger                                 uint8_t tg, uint8_t level)
6260a61f1bSEric Auger {
632eaeb7d5SMostafa Saleh     SMMUIOTLBKey key = {.asid = asid, .vmid = vmid, .iova = iova,
642eaeb7d5SMostafa Saleh                         .tg = tg, .level = level};
6560a61f1bSEric Auger 
6660a61f1bSEric Auger     return key;
6760a61f1bSEric Auger }
6860a61f1bSEric Auger 
smmu_iotlb_lookup_all_levels(SMMUState * bs,SMMUTransCfg * cfg,SMMUTransTableInfo * tt,hwaddr iova)697eb57be1SMostafa Saleh static SMMUTLBEntry *smmu_iotlb_lookup_all_levels(SMMUState *bs,
707eb57be1SMostafa Saleh                                                   SMMUTransCfg *cfg,
717eb57be1SMostafa Saleh                                                   SMMUTransTableInfo *tt,
727eb57be1SMostafa Saleh                                                   hwaddr iova)
736808bca9SEric Auger {
749e54dee7SEric Auger     uint8_t tg = (tt->granule_sz - 10) / 2;
759e54dee7SEric Auger     uint8_t inputsize = 64 - tt->tsz;
769e54dee7SEric Auger     uint8_t stride = tt->granule_sz - 3;
779e54dee7SEric Auger     uint8_t level = 4 - (inputsize - 4) / stride;
789e54dee7SEric Auger     SMMUTLBEntry *entry = NULL;
799e54dee7SEric Auger 
809e54dee7SEric Auger     while (level <= 3) {
819e54dee7SEric Auger         uint64_t subpage_size = 1ULL << level_shift(level, tt->granule_sz);
829e54dee7SEric Auger         uint64_t mask = subpage_size - 1;
839e54dee7SEric Auger         SMMUIOTLBKey key;
849e54dee7SEric Auger 
852eaeb7d5SMostafa Saleh         key = smmu_get_iotlb_key(cfg->asid, cfg->s2cfg.vmid,
862eaeb7d5SMostafa Saleh                                  iova & ~mask, tg, level);
879e54dee7SEric Auger         entry = g_hash_table_lookup(bs->iotlb, &key);
889e54dee7SEric Auger         if (entry) {
899e54dee7SEric Auger             break;
909e54dee7SEric Auger         }
919e54dee7SEric Auger         level++;
929e54dee7SEric Auger     }
937eb57be1SMostafa Saleh     return entry;
947eb57be1SMostafa Saleh }
957eb57be1SMostafa Saleh 
967eb57be1SMostafa Saleh /**
977eb57be1SMostafa Saleh  * smmu_iotlb_lookup - Look up for a TLB entry.
987eb57be1SMostafa Saleh  * @bs: SMMU state which includes the TLB instance
997eb57be1SMostafa Saleh  * @cfg: Configuration of the translation
1007eb57be1SMostafa Saleh  * @tt: Translation table info (granule and tsz)
1017eb57be1SMostafa Saleh  * @iova: IOVA address to lookup
1027eb57be1SMostafa Saleh  *
1037eb57be1SMostafa Saleh  * returns a valid entry on success, otherwise NULL.
1047eb57be1SMostafa Saleh  * In case of nested translation, tt can be updated to include
1057eb57be1SMostafa Saleh  * the granule of the found entry as it might different from
1067eb57be1SMostafa Saleh  * the IOVA granule.
1077eb57be1SMostafa Saleh  */
smmu_iotlb_lookup(SMMUState * bs,SMMUTransCfg * cfg,SMMUTransTableInfo * tt,hwaddr iova)1087eb57be1SMostafa Saleh SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
1097eb57be1SMostafa Saleh                                 SMMUTransTableInfo *tt, hwaddr iova)
1107eb57be1SMostafa Saleh {
1117eb57be1SMostafa Saleh     SMMUTLBEntry *entry = NULL;
1127eb57be1SMostafa Saleh 
1137eb57be1SMostafa Saleh     entry = smmu_iotlb_lookup_all_levels(bs, cfg, tt, iova);
1147eb57be1SMostafa Saleh     /*
1157eb57be1SMostafa Saleh      * For nested translation also try the s2 granule, as the TLB will insert
1167eb57be1SMostafa Saleh      * it if the size of s2 tlb entry was smaller.
1177eb57be1SMostafa Saleh      */
1187eb57be1SMostafa Saleh     if (!entry && (cfg->stage == SMMU_NESTED) &&
1197eb57be1SMostafa Saleh         (cfg->s2cfg.granule_sz != tt->granule_sz)) {
1207eb57be1SMostafa Saleh         tt->granule_sz = cfg->s2cfg.granule_sz;
1217eb57be1SMostafa Saleh         entry = smmu_iotlb_lookup_all_levels(bs, cfg, tt, iova);
1227eb57be1SMostafa Saleh     }
1236808bca9SEric Auger 
1246808bca9SEric Auger     if (entry) {
1256808bca9SEric Auger         cfg->iotlb_hits++;
1262eaeb7d5SMostafa Saleh         trace_smmu_iotlb_lookup_hit(cfg->asid, cfg->s2cfg.vmid, iova,
1276808bca9SEric Auger                                     cfg->iotlb_hits, cfg->iotlb_misses,
1286808bca9SEric Auger                                     100 * cfg->iotlb_hits /
1296808bca9SEric Auger                                     (cfg->iotlb_hits + cfg->iotlb_misses));
1306808bca9SEric Auger     } else {
1316808bca9SEric Auger         cfg->iotlb_misses++;
1322eaeb7d5SMostafa Saleh         trace_smmu_iotlb_lookup_miss(cfg->asid, cfg->s2cfg.vmid, iova,
1336808bca9SEric Auger                                      cfg->iotlb_hits, cfg->iotlb_misses,
1346808bca9SEric Auger                                      100 * cfg->iotlb_hits /
1356808bca9SEric Auger                                      (cfg->iotlb_hits + cfg->iotlb_misses));
1366808bca9SEric Auger     }
1376808bca9SEric Auger     return entry;
1386808bca9SEric Auger }
1396808bca9SEric Auger 
smmu_iotlb_insert(SMMUState * bs,SMMUTransCfg * cfg,SMMUTLBEntry * new)140a7550158SEric Auger void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *new)
1416808bca9SEric Auger {
1426808bca9SEric Auger     SMMUIOTLBKey *key = g_new0(SMMUIOTLBKey, 1);
1439e54dee7SEric Auger     uint8_t tg = (new->granule - 10) / 2;
1446808bca9SEric Auger 
1456808bca9SEric Auger     if (g_hash_table_size(bs->iotlb) >= SMMU_IOTLB_MAX_SIZE) {
1466808bca9SEric Auger         smmu_iotlb_inv_all(bs);
1476808bca9SEric Auger     }
1486808bca9SEric Auger 
1492eaeb7d5SMostafa Saleh     *key = smmu_get_iotlb_key(cfg->asid, cfg->s2cfg.vmid, new->entry.iova,
1502eaeb7d5SMostafa Saleh                               tg, new->level);
1512eaeb7d5SMostafa Saleh     trace_smmu_iotlb_insert(cfg->asid, cfg->s2cfg.vmid, new->entry.iova,
1522eaeb7d5SMostafa Saleh                             tg, new->level);
153a7550158SEric Auger     g_hash_table_insert(bs->iotlb, key, new);
1546808bca9SEric Auger }
1556808bca9SEric Auger 
smmu_iotlb_inv_all(SMMUState * s)1569de9fa5cSPhilippe Mathieu-Daudé void smmu_iotlb_inv_all(SMMUState *s)
157cc27ed81SEric Auger {
158cc27ed81SEric Auger     trace_smmu_iotlb_inv_all();
159cc27ed81SEric Auger     g_hash_table_remove_all(s->iotlb);
160cc27ed81SEric Auger }
161cc27ed81SEric Auger 
smmu_hash_remove_by_asid_vmid(gpointer key,gpointer value,gpointer user_data)162eb41313cSMostafa Saleh static gboolean smmu_hash_remove_by_asid_vmid(gpointer key, gpointer value,
163cc27ed81SEric Auger                                               gpointer user_data)
164cc27ed81SEric Auger {
165eb41313cSMostafa Saleh     SMMUIOTLBPageInvInfo *info = (SMMUIOTLBPageInvInfo *)user_data;
166cc27ed81SEric Auger     SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
167cc27ed81SEric Auger 
168eb41313cSMostafa Saleh     return (SMMU_IOTLB_ASID(*iotlb_key) == info->asid) &&
169eb41313cSMostafa Saleh            (SMMU_IOTLB_VMID(*iotlb_key) == info->vmid);
170cc27ed81SEric Auger }
171ccc3ee38SMostafa Saleh 
smmu_hash_remove_by_vmid(gpointer key,gpointer value,gpointer user_data)172ccc3ee38SMostafa Saleh static gboolean smmu_hash_remove_by_vmid(gpointer key, gpointer value,
173ccc3ee38SMostafa Saleh                                          gpointer user_data)
174ccc3ee38SMostafa Saleh {
175d8838226SMostafa Saleh     int vmid = *(int *)user_data;
176ccc3ee38SMostafa Saleh     SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
177ccc3ee38SMostafa Saleh 
178ccc3ee38SMostafa Saleh     return SMMU_IOTLB_VMID(*iotlb_key) == vmid;
179ccc3ee38SMostafa Saleh }
180ccc3ee38SMostafa Saleh 
smmu_hash_remove_by_vmid_s1(gpointer key,gpointer value,gpointer user_data)181b8fa4c23SMostafa Saleh static gboolean smmu_hash_remove_by_vmid_s1(gpointer key, gpointer value,
182b8fa4c23SMostafa Saleh                                             gpointer user_data)
183b8fa4c23SMostafa Saleh {
184b8fa4c23SMostafa Saleh     int vmid = *(int *)user_data;
185b8fa4c23SMostafa Saleh     SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
186b8fa4c23SMostafa Saleh 
187b8fa4c23SMostafa Saleh     return (SMMU_IOTLB_VMID(*iotlb_key) == vmid) &&
188b8fa4c23SMostafa Saleh            (SMMU_IOTLB_ASID(*iotlb_key) >= 0);
189b8fa4c23SMostafa Saleh }
190b8fa4c23SMostafa Saleh 
smmu_hash_remove_by_asid_vmid_iova(gpointer key,gpointer value,gpointer user_data)1912eaeb7d5SMostafa Saleh static gboolean smmu_hash_remove_by_asid_vmid_iova(gpointer key, gpointer value,
1929e54dee7SEric Auger                                               gpointer user_data)
193cc27ed81SEric Auger {
1949e54dee7SEric Auger     SMMUTLBEntry *iter = (SMMUTLBEntry *)value;
1959e54dee7SEric Auger     IOMMUTLBEntry *entry = &iter->entry;
1969e54dee7SEric Auger     SMMUIOTLBPageInvInfo *info = (SMMUIOTLBPageInvInfo *)user_data;
1979e54dee7SEric Auger     SMMUIOTLBKey iotlb_key = *(SMMUIOTLBKey *)key;
1989e54dee7SEric Auger 
1999e54dee7SEric Auger     if (info->asid >= 0 && info->asid != SMMU_IOTLB_ASID(iotlb_key)) {
2009e54dee7SEric Auger         return false;
2019e54dee7SEric Auger     }
2022eaeb7d5SMostafa Saleh     if (info->vmid >= 0 && info->vmid != SMMU_IOTLB_VMID(iotlb_key)) {
2032eaeb7d5SMostafa Saleh         return false;
2042eaeb7d5SMostafa Saleh     }
205d5291561SEric Auger     return ((info->iova & ~entry->addr_mask) == entry->iova) ||
206d5291561SEric Auger            ((entry->iova & ~info->mask) == info->iova);
2079e54dee7SEric Auger }
2089e54dee7SEric Auger 
smmu_hash_remove_by_vmid_ipa(gpointer key,gpointer value,gpointer user_data)2091ea8a6f5SMostafa Saleh static gboolean smmu_hash_remove_by_vmid_ipa(gpointer key, gpointer value,
2101ea8a6f5SMostafa Saleh                                              gpointer user_data)
2111ea8a6f5SMostafa Saleh {
2121ea8a6f5SMostafa Saleh     SMMUTLBEntry *iter = (SMMUTLBEntry *)value;
2131ea8a6f5SMostafa Saleh     IOMMUTLBEntry *entry = &iter->entry;
2141ea8a6f5SMostafa Saleh     SMMUIOTLBPageInvInfo *info = (SMMUIOTLBPageInvInfo *)user_data;
2151ea8a6f5SMostafa Saleh     SMMUIOTLBKey iotlb_key = *(SMMUIOTLBKey *)key;
2161ea8a6f5SMostafa Saleh 
2171ea8a6f5SMostafa Saleh     if (SMMU_IOTLB_ASID(iotlb_key) >= 0) {
2181ea8a6f5SMostafa Saleh         /* This is a stage-1 address. */
2191ea8a6f5SMostafa Saleh         return false;
2201ea8a6f5SMostafa Saleh     }
2211ea8a6f5SMostafa Saleh     if (info->vmid != SMMU_IOTLB_VMID(iotlb_key)) {
2221ea8a6f5SMostafa Saleh         return false;
2231ea8a6f5SMostafa Saleh     }
2241ea8a6f5SMostafa Saleh     return ((info->iova & ~entry->addr_mask) == entry->iova) ||
2251ea8a6f5SMostafa Saleh            ((entry->iova & ~info->mask) == info->iova);
2261ea8a6f5SMostafa Saleh }
2271ea8a6f5SMostafa Saleh 
smmu_iotlb_inv_iova(SMMUState * s,int asid,int vmid,dma_addr_t iova,uint8_t tg,uint64_t num_pages,uint8_t ttl)2282eaeb7d5SMostafa Saleh void smmu_iotlb_inv_iova(SMMUState *s, int asid, int vmid, dma_addr_t iova,
229d5291561SEric Auger                          uint8_t tg, uint64_t num_pages, uint8_t ttl)
2309e54dee7SEric Auger {
2316d9cd115SEric Auger     /* if tg is not set we use 4KB range invalidation */
2326d9cd115SEric Auger     uint8_t granule = tg ? tg * 2 + 10 : 12;
2336d9cd115SEric Auger 
234a4b6e1beSEric Auger     if (ttl && (num_pages == 1) && (asid >= 0)) {
2352eaeb7d5SMostafa Saleh         SMMUIOTLBKey key = smmu_get_iotlb_key(asid, vmid, iova, tg, ttl);
236cc27ed81SEric Auger 
2376d9cd115SEric Auger         if (g_hash_table_remove(s->iotlb, &key)) {
2386d9cd115SEric Auger             return;
2396d9cd115SEric Auger         }
2406d9cd115SEric Auger         /*
2416d9cd115SEric Auger          * if the entry is not found, let's see if it does not
2426d9cd115SEric Auger          * belong to a larger IOTLB entry
2436d9cd115SEric Auger          */
2446d9cd115SEric Auger     }
245d5291561SEric Auger 
246d5291561SEric Auger     SMMUIOTLBPageInvInfo info = {
247d5291561SEric Auger         .asid = asid, .iova = iova,
2482eaeb7d5SMostafa Saleh         .vmid = vmid,
249d5291561SEric Auger         .mask = (num_pages * 1 << granule) - 1};
250d5291561SEric Auger 
251d5291561SEric Auger     g_hash_table_foreach_remove(s->iotlb,
2522eaeb7d5SMostafa Saleh                                 smmu_hash_remove_by_asid_vmid_iova,
253d5291561SEric Auger                                 &info);
254d5291561SEric Auger }
255cc27ed81SEric Auger 
2561ea8a6f5SMostafa Saleh /*
2571ea8a6f5SMostafa Saleh  * Similar to smmu_iotlb_inv_iova(), but for Stage-2, ASID is always -1,
2581ea8a6f5SMostafa Saleh  * in Stage-1 invalidation ASID = -1, means don't care.
2591ea8a6f5SMostafa Saleh  */
smmu_iotlb_inv_ipa(SMMUState * s,int vmid,dma_addr_t ipa,uint8_t tg,uint64_t num_pages,uint8_t ttl)2601ea8a6f5SMostafa Saleh void smmu_iotlb_inv_ipa(SMMUState *s, int vmid, dma_addr_t ipa, uint8_t tg,
2611ea8a6f5SMostafa Saleh                         uint64_t num_pages, uint8_t ttl)
2621ea8a6f5SMostafa Saleh {
2631ea8a6f5SMostafa Saleh     uint8_t granule = tg ? tg * 2 + 10 : 12;
2641ea8a6f5SMostafa Saleh     int asid = -1;
2651ea8a6f5SMostafa Saleh 
2661ea8a6f5SMostafa Saleh    if (ttl && (num_pages == 1)) {
2671ea8a6f5SMostafa Saleh         SMMUIOTLBKey key = smmu_get_iotlb_key(asid, vmid, ipa, tg, ttl);
2681ea8a6f5SMostafa Saleh 
2691ea8a6f5SMostafa Saleh         if (g_hash_table_remove(s->iotlb, &key)) {
2701ea8a6f5SMostafa Saleh             return;
2711ea8a6f5SMostafa Saleh         }
2721ea8a6f5SMostafa Saleh     }
2731ea8a6f5SMostafa Saleh 
2741ea8a6f5SMostafa Saleh     SMMUIOTLBPageInvInfo info = {
2751ea8a6f5SMostafa Saleh         .iova = ipa,
2761ea8a6f5SMostafa Saleh         .vmid = vmid,
2771ea8a6f5SMostafa Saleh         .mask = (num_pages << granule) - 1};
2781ea8a6f5SMostafa Saleh 
2791ea8a6f5SMostafa Saleh     g_hash_table_foreach_remove(s->iotlb,
2801ea8a6f5SMostafa Saleh                                 smmu_hash_remove_by_vmid_ipa,
2811ea8a6f5SMostafa Saleh                                 &info);
2821ea8a6f5SMostafa Saleh }
2831ea8a6f5SMostafa Saleh 
smmu_iotlb_inv_asid_vmid(SMMUState * s,int asid,int vmid)284eb41313cSMostafa Saleh void smmu_iotlb_inv_asid_vmid(SMMUState *s, int asid, int vmid)
285cc27ed81SEric Auger {
286eb41313cSMostafa Saleh     SMMUIOTLBPageInvInfo info = {
287eb41313cSMostafa Saleh         .asid = asid,
288eb41313cSMostafa Saleh         .vmid = vmid,
289eb41313cSMostafa Saleh     };
290eb41313cSMostafa Saleh 
291eb41313cSMostafa Saleh     trace_smmu_iotlb_inv_asid_vmid(asid, vmid);
292eb41313cSMostafa Saleh     g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_asid_vmid, &info);
293cc27ed81SEric Auger }
294cc27ed81SEric Auger 
smmu_iotlb_inv_vmid(SMMUState * s,int vmid)295d8838226SMostafa Saleh void smmu_iotlb_inv_vmid(SMMUState *s, int vmid)
296ccc3ee38SMostafa Saleh {
297ccc3ee38SMostafa Saleh     trace_smmu_iotlb_inv_vmid(vmid);
298ccc3ee38SMostafa Saleh     g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_vmid, &vmid);
299ccc3ee38SMostafa Saleh }
300ccc3ee38SMostafa Saleh 
smmu_iotlb_inv_vmid_s1(SMMUState * s,int vmid)301b8fa4c23SMostafa Saleh inline void smmu_iotlb_inv_vmid_s1(SMMUState *s, int vmid)
302b8fa4c23SMostafa Saleh {
303b8fa4c23SMostafa Saleh     trace_smmu_iotlb_inv_vmid_s1(vmid);
304b8fa4c23SMostafa Saleh     g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_vmid_s1, &vmid);
305b8fa4c23SMostafa Saleh }
306b8fa4c23SMostafa Saleh 
30793641948SEric Auger /* VMSAv8-64 Translation */
30893641948SEric Auger 
30993641948SEric Auger /**
31093641948SEric Auger  * get_pte - Get the content of a page table entry located at
31193641948SEric Auger  * @base_addr[@index]
31293641948SEric Auger  */
get_pte(dma_addr_t baseaddr,uint32_t index,uint64_t * pte,SMMUPTWEventInfo * info)31393641948SEric Auger static int get_pte(dma_addr_t baseaddr, uint32_t index, uint64_t *pte,
31493641948SEric Auger                    SMMUPTWEventInfo *info)
31593641948SEric Auger {
31693641948SEric Auger     int ret;
31793641948SEric Auger     dma_addr_t addr = baseaddr + index * sizeof(*pte);
31893641948SEric Auger 
31993641948SEric Auger     /* TODO: guarantee 64-bit single-copy atomicity */
320c6445544SPeter Maydell     ret = ldq_le_dma(&address_space_memory, addr, pte, MEMTXATTRS_UNSPECIFIED);
32193641948SEric Auger 
32293641948SEric Auger     if (ret != MEMTX_OK) {
32393641948SEric Auger         info->type = SMMU_PTW_ERR_WALK_EABT;
32493641948SEric Auger         info->addr = addr;
32593641948SEric Auger         return -EINVAL;
32693641948SEric Auger     }
32793641948SEric Auger     trace_smmu_get_pte(baseaddr, index, addr, *pte);
32893641948SEric Auger     return 0;
32993641948SEric Auger }
33093641948SEric Auger 
33193641948SEric Auger /* VMSAv8-64 Translation Table Format Descriptor Decoding */
33293641948SEric Auger 
33393641948SEric Auger /**
33493641948SEric Auger  * get_page_pte_address - returns the L3 descriptor output address,
33593641948SEric Auger  * ie. the page frame
33693641948SEric Auger  * ARM ARM spec: Figure D4-17 VMSAv8-64 level 3 descriptor format
33793641948SEric Auger  */
get_page_pte_address(uint64_t pte,int granule_sz)33893641948SEric Auger static inline hwaddr get_page_pte_address(uint64_t pte, int granule_sz)
33993641948SEric Auger {
34093641948SEric Auger     return PTE_ADDRESS(pte, granule_sz);
34193641948SEric Auger }
34293641948SEric Auger 
34393641948SEric Auger /**
34493641948SEric Auger  * get_table_pte_address - return table descriptor output address,
34593641948SEric Auger  * ie. address of next level table
34693641948SEric Auger  * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
34793641948SEric Auger  */
get_table_pte_address(uint64_t pte,int granule_sz)34893641948SEric Auger static inline hwaddr get_table_pte_address(uint64_t pte, int granule_sz)
34993641948SEric Auger {
35093641948SEric Auger     return PTE_ADDRESS(pte, granule_sz);
35193641948SEric Auger }
35293641948SEric Auger 
35393641948SEric Auger /**
35493641948SEric Auger  * get_block_pte_address - return block descriptor output address and block size
35593641948SEric Auger  * ARM ARM Figure D4-16 VMSAv8-64 level0, level1, and level 2 descriptor formats
35693641948SEric Auger  */
get_block_pte_address(uint64_t pte,int level,int granule_sz,uint64_t * bsz)35793641948SEric Auger static inline hwaddr get_block_pte_address(uint64_t pte, int level,
35893641948SEric Auger                                            int granule_sz, uint64_t *bsz)
35993641948SEric Auger {
360118eee6cSEric Auger     int n = level_shift(level, granule_sz);
36193641948SEric Auger 
362118eee6cSEric Auger     *bsz = 1ULL << n;
36393641948SEric Auger     return PTE_ADDRESS(pte, n);
36493641948SEric Auger }
36593641948SEric Auger 
select_tt(SMMUTransCfg * cfg,dma_addr_t iova)36693641948SEric Auger SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova)
36793641948SEric Auger {
36893641948SEric Auger     bool tbi = extract64(iova, 55, 1) ? TBI1(cfg->tbi) : TBI0(cfg->tbi);
36993641948SEric Auger     uint8_t tbi_byte = tbi * 8;
37093641948SEric Auger 
37193641948SEric Auger     if (cfg->tt[0].tsz &&
37293641948SEric Auger         !extract64(iova, 64 - cfg->tt[0].tsz, cfg->tt[0].tsz - tbi_byte)) {
37393641948SEric Auger         /* there is a ttbr0 region and we are in it (high bits all zero) */
37493641948SEric Auger         return &cfg->tt[0];
37593641948SEric Auger     } else if (cfg->tt[1].tsz &&
376e431b8f6SJean-Philippe Brucker         sextract64(iova, 64 - cfg->tt[1].tsz, cfg->tt[1].tsz - tbi_byte) == -1) {
37793641948SEric Auger         /* there is a ttbr1 region and we are in it (high bits all one) */
37893641948SEric Auger         return &cfg->tt[1];
37993641948SEric Auger     } else if (!cfg->tt[0].tsz) {
38093641948SEric Auger         /* ttbr0 region is "everything not in the ttbr1 region" */
38193641948SEric Auger         return &cfg->tt[0];
38293641948SEric Auger     } else if (!cfg->tt[1].tsz) {
38393641948SEric Auger         /* ttbr1 region is "everything not in the ttbr0 region" */
38493641948SEric Auger         return &cfg->tt[1];
38593641948SEric Auger     }
38693641948SEric Auger     /* in the gap between the two regions, this is a Translation fault */
38793641948SEric Auger     return NULL;
38893641948SEric Auger }
38993641948SEric Auger 
390f42a0a57SMostafa Saleh /* Translate stage-1 table address using stage-2 page table. */
translate_table_addr_ipa(SMMUState * bs,dma_addr_t * table_addr,SMMUTransCfg * cfg,SMMUPTWEventInfo * info)391f42a0a57SMostafa Saleh static inline int translate_table_addr_ipa(SMMUState *bs,
392f42a0a57SMostafa Saleh                                            dma_addr_t *table_addr,
393f42a0a57SMostafa Saleh                                            SMMUTransCfg *cfg,
394f42a0a57SMostafa Saleh                                            SMMUPTWEventInfo *info)
395f42a0a57SMostafa Saleh {
396f42a0a57SMostafa Saleh     dma_addr_t addr = *table_addr;
397f42a0a57SMostafa Saleh     SMMUTLBEntry *cached_entry;
398f42a0a57SMostafa Saleh     int asid;
399f42a0a57SMostafa Saleh 
400f42a0a57SMostafa Saleh     /*
401f42a0a57SMostafa Saleh      * The translation table walks performed from TTB0 or TTB1 are always
402f42a0a57SMostafa Saleh      * performed in IPA space if stage 2 translations are enabled.
403f42a0a57SMostafa Saleh      */
404f42a0a57SMostafa Saleh     asid = cfg->asid;
405f42a0a57SMostafa Saleh     cfg->stage = SMMU_STAGE_2;
406f42a0a57SMostafa Saleh     cfg->asid = -1;
407f42a0a57SMostafa Saleh     cached_entry = smmu_translate(bs, cfg, addr, IOMMU_RO, info);
408f42a0a57SMostafa Saleh     cfg->asid = asid;
409f42a0a57SMostafa Saleh     cfg->stage = SMMU_NESTED;
410f42a0a57SMostafa Saleh 
411f42a0a57SMostafa Saleh     if (cached_entry) {
412f42a0a57SMostafa Saleh         *table_addr = CACHED_ENTRY_TO_ADDR(cached_entry, addr);
413f42a0a57SMostafa Saleh         return 0;
414f42a0a57SMostafa Saleh     }
415f42a0a57SMostafa Saleh 
416f42a0a57SMostafa Saleh     info->stage = SMMU_STAGE_2;
417f42a0a57SMostafa Saleh     info->addr = addr;
418f42a0a57SMostafa Saleh     info->is_ipa_descriptor = true;
419f42a0a57SMostafa Saleh     return -EINVAL;
420f42a0a57SMostafa Saleh }
421f42a0a57SMostafa Saleh 
42293641948SEric Auger /**
423bcc919e7SMostafa Saleh  * smmu_ptw_64_s1 - VMSAv8-64 Walk of the page tables for a given IOVA
424f42a0a57SMostafa Saleh  * @bs: smmu state which includes TLB instance
42593641948SEric Auger  * @cfg: translation config
42693641948SEric Auger  * @iova: iova to translate
42793641948SEric Auger  * @perm: access type
428a7550158SEric Auger  * @tlbe: SMMUTLBEntry (out)
42993641948SEric Auger  * @info: handle to an error info
43093641948SEric Auger  *
43193641948SEric Auger  * Return 0 on success, < 0 on error. In case of error, @info is filled
43293641948SEric Auger  * and tlbe->perm is set to IOMMU_NONE.
43393641948SEric Auger  * Upon success, @tlbe is filled with translated_addr and entry
43493641948SEric Auger  * permission rights.
43593641948SEric Auger  */
smmu_ptw_64_s1(SMMUState * bs,SMMUTransCfg * cfg,dma_addr_t iova,IOMMUAccessFlags perm,SMMUTLBEntry * tlbe,SMMUPTWEventInfo * info)436f42a0a57SMostafa Saleh static int smmu_ptw_64_s1(SMMUState *bs, SMMUTransCfg *cfg,
43793641948SEric Auger                           dma_addr_t iova, IOMMUAccessFlags perm,
438a7550158SEric Auger                           SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
43993641948SEric Auger {
44093641948SEric Auger     dma_addr_t baseaddr, indexmask;
441f6cc1980SMostafa Saleh     SMMUStage stage = cfg->stage;
44293641948SEric Auger     SMMUTransTableInfo *tt = select_tt(cfg, iova);
44393641948SEric Auger     uint8_t level, granule_sz, inputsize, stride;
44493641948SEric Auger 
44593641948SEric Auger     if (!tt || tt->disabled) {
44693641948SEric Auger         info->type = SMMU_PTW_ERR_TRANSLATION;
44793641948SEric Auger         goto error;
44893641948SEric Auger     }
44993641948SEric Auger 
45093641948SEric Auger     granule_sz = tt->granule_sz;
451bcc919e7SMostafa Saleh     stride = VMSA_STRIDE(granule_sz);
45293641948SEric Auger     inputsize = 64 - tt->tsz;
45393641948SEric Auger     level = 4 - (inputsize - 4) / stride;
454bcc919e7SMostafa Saleh     indexmask = VMSA_IDXMSK(inputsize, stride, level);
4556783a184SMostafa Saleh 
4566783a184SMostafa Saleh     baseaddr = extract64(tt->ttb, 0, cfg->oas);
45793641948SEric Auger     baseaddr &= ~indexmask;
45893641948SEric Auger 
459bcc919e7SMostafa Saleh     while (level < VMSA_LEVELS) {
46093641948SEric Auger         uint64_t subpage_size = 1ULL << level_shift(level, granule_sz);
46193641948SEric Auger         uint64_t mask = subpage_size - 1;
46293641948SEric Auger         uint32_t offset = iova_level_offset(iova, inputsize, level, granule_sz);
4631733837dSEric Auger         uint64_t pte, gpa;
46493641948SEric Auger         dma_addr_t pte_addr = baseaddr + offset * sizeof(pte);
46593641948SEric Auger         uint8_t ap;
46693641948SEric Auger 
46793641948SEric Auger         if (get_pte(baseaddr, offset, &pte, info)) {
46893641948SEric Auger                 goto error;
46993641948SEric Auger         }
470bcc919e7SMostafa Saleh         trace_smmu_ptw_level(stage, level, iova, subpage_size,
47193641948SEric Auger                              baseaddr, offset, pte);
47293641948SEric Auger 
47393641948SEric Auger         if (is_invalid_pte(pte) || is_reserved_pte(pte, level)) {
47493641948SEric Auger             trace_smmu_ptw_invalid_pte(stage, level, baseaddr,
47593641948SEric Auger                                        pte_addr, offset, pte);
4761733837dSEric Auger             break;
47793641948SEric Auger         }
47893641948SEric Auger 
4791733837dSEric Auger         if (is_table_pte(pte, level)) {
48093641948SEric Auger             ap = PTE_APTABLE(pte);
48193641948SEric Auger 
482e7c3b9d9SEric Auger             if (is_permission_fault(ap, perm) && !tt->had) {
48393641948SEric Auger                 info->type = SMMU_PTW_ERR_PERMISSION;
48493641948SEric Auger                 goto error;
48593641948SEric Auger             }
48693641948SEric Auger             baseaddr = get_table_pte_address(pte, granule_sz);
487f42a0a57SMostafa Saleh             if (cfg->stage == SMMU_NESTED) {
488f42a0a57SMostafa Saleh                 if (translate_table_addr_ipa(bs, &baseaddr, cfg, info)) {
489f42a0a57SMostafa Saleh                     goto error;
490f42a0a57SMostafa Saleh                 }
491f42a0a57SMostafa Saleh             }
49293641948SEric Auger             level++;
4931733837dSEric Auger             continue;
4941733837dSEric Auger         } else if (is_page_pte(pte, level)) {
4951733837dSEric Auger             gpa = get_page_pte_address(pte, granule_sz);
4961733837dSEric Auger             trace_smmu_ptw_page_pte(stage, level, iova,
4971733837dSEric Auger                                     baseaddr, pte_addr, pte, gpa);
4981733837dSEric Auger         } else {
4991733837dSEric Auger             uint64_t block_size;
5001733837dSEric Auger 
5011733837dSEric Auger             gpa = get_block_pte_address(pte, level, granule_sz,
5021733837dSEric Auger                                         &block_size);
5031733837dSEric Auger             trace_smmu_ptw_block_pte(stage, level, baseaddr,
5041733837dSEric Auger                                      pte_addr, pte, iova, gpa,
5051733837dSEric Auger                                      block_size >> 20);
5061733837dSEric Auger         }
50715f6c16eSLuc Michel 
50815f6c16eSLuc Michel         /*
50915f6c16eSLuc Michel          * QEMU does not currently implement HTTU, so if AFFD and PTE.AF
51015f6c16eSLuc Michel          * are 0 we take an Access flag fault. (5.4. Context Descriptor)
51115f6c16eSLuc Michel          * An Access flag fault takes priority over a Permission fault.
51215f6c16eSLuc Michel          */
51315f6c16eSLuc Michel         if (!PTE_AF(pte) && !cfg->affd) {
51415f6c16eSLuc Michel             info->type = SMMU_PTW_ERR_ACCESS;
51515f6c16eSLuc Michel             goto error;
51615f6c16eSLuc Michel         }
51715f6c16eSLuc Michel 
5181733837dSEric Auger         ap = PTE_AP(pte);
5191733837dSEric Auger         if (is_permission_fault(ap, perm)) {
5201733837dSEric Auger             info->type = SMMU_PTW_ERR_PERMISSION;
5211733837dSEric Auger             goto error;
52293641948SEric Auger         }
52393641948SEric Auger 
524bde809f0SMostafa Saleh         /*
525bde809f0SMostafa Saleh          * The address output from the translation causes a stage 1 Address
526bde809f0SMostafa Saleh          * Size fault if it exceeds the range of the effective IPA size for
527bde809f0SMostafa Saleh          * the given CD.
528bde809f0SMostafa Saleh          */
529bde809f0SMostafa Saleh         if (gpa >= (1ULL << cfg->oas)) {
530bde809f0SMostafa Saleh             info->type = SMMU_PTW_ERR_ADDR_SIZE;
531bde809f0SMostafa Saleh             goto error;
532bde809f0SMostafa Saleh         }
533bde809f0SMostafa Saleh 
5349e54dee7SEric Auger         tlbe->entry.translated_addr = gpa;
5359e54dee7SEric Auger         tlbe->entry.iova = iova & ~mask;
5369e54dee7SEric Auger         tlbe->entry.addr_mask = mask;
537d7cdf89cSMostafa Saleh         tlbe->parent_perm = PTE_AP_TO_PERM(ap);
538d7cdf89cSMostafa Saleh         tlbe->entry.perm = tlbe->parent_perm;
539a7550158SEric Auger         tlbe->level = level;
540a7550158SEric Auger         tlbe->granule = granule_sz;
5411733837dSEric Auger         return 0;
5421733837dSEric Auger     }
54393641948SEric Auger     info->type = SMMU_PTW_ERR_TRANSLATION;
54493641948SEric Auger 
54593641948SEric Auger error:
546f6cc1980SMostafa Saleh     info->stage = SMMU_STAGE_1;
547a7550158SEric Auger     tlbe->entry.perm = IOMMU_NONE;
54893641948SEric Auger     return -EINVAL;
54993641948SEric Auger }
55093641948SEric Auger 
55193641948SEric Auger /**
552e703f707SMostafa Saleh  * smmu_ptw_64_s2 - VMSAv8-64 Walk of the page tables for a given ipa
553e703f707SMostafa Saleh  * for stage-2.
554e703f707SMostafa Saleh  * @cfg: translation config
555e703f707SMostafa Saleh  * @ipa: ipa to translate
556e703f707SMostafa Saleh  * @perm: access type
557e703f707SMostafa Saleh  * @tlbe: SMMUTLBEntry (out)
558e703f707SMostafa Saleh  * @info: handle to an error info
559e703f707SMostafa Saleh  *
560e703f707SMostafa Saleh  * Return 0 on success, < 0 on error. In case of error, @info is filled
561e703f707SMostafa Saleh  * and tlbe->perm is set to IOMMU_NONE.
562e703f707SMostafa Saleh  * Upon success, @tlbe is filled with translated_addr and entry
563e703f707SMostafa Saleh  * permission rights.
564e703f707SMostafa Saleh  */
smmu_ptw_64_s2(SMMUTransCfg * cfg,dma_addr_t ipa,IOMMUAccessFlags perm,SMMUTLBEntry * tlbe,SMMUPTWEventInfo * info)565e703f707SMostafa Saleh static int smmu_ptw_64_s2(SMMUTransCfg *cfg,
566e703f707SMostafa Saleh                           dma_addr_t ipa, IOMMUAccessFlags perm,
567e703f707SMostafa Saleh                           SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
568e703f707SMostafa Saleh {
569f6cc1980SMostafa Saleh     const SMMUStage stage = SMMU_STAGE_2;
570e703f707SMostafa Saleh     int granule_sz = cfg->s2cfg.granule_sz;
571e703f707SMostafa Saleh     /* ARM DDI0487I.a: Table D8-7. */
572e703f707SMostafa Saleh     int inputsize = 64 - cfg->s2cfg.tsz;
573e703f707SMostafa Saleh     int level = get_start_level(cfg->s2cfg.sl0, granule_sz);
574e703f707SMostafa Saleh     int stride = VMSA_STRIDE(granule_sz);
575e703f707SMostafa Saleh     int idx = pgd_concat_idx(level, granule_sz, ipa);
576e703f707SMostafa Saleh     /*
577e703f707SMostafa Saleh      * Get the ttb from concatenated structure.
578e703f707SMostafa Saleh      * The offset is the idx * size of each ttb(number of ptes * (sizeof(pte))
579e703f707SMostafa Saleh      */
5806783a184SMostafa Saleh     uint64_t baseaddr = extract64(cfg->s2cfg.vttb, 0, cfg->s2cfg.eff_ps) +
5816783a184SMostafa Saleh                                   (1 << stride) * idx * sizeof(uint64_t);
582e703f707SMostafa Saleh     dma_addr_t indexmask = VMSA_IDXMSK(inputsize, stride, level);
583e703f707SMostafa Saleh 
584e703f707SMostafa Saleh     baseaddr &= ~indexmask;
585e703f707SMostafa Saleh 
586e703f707SMostafa Saleh     /*
587e703f707SMostafa Saleh      * On input, a stage 2 Translation fault occurs if the IPA is outside the
588e703f707SMostafa Saleh      * range configured by the relevant S2T0SZ field of the STE.
589e703f707SMostafa Saleh      */
590e703f707SMostafa Saleh     if (ipa >= (1ULL << inputsize)) {
591e703f707SMostafa Saleh         info->type = SMMU_PTW_ERR_TRANSLATION;
59248f9e9ebSMostafa Saleh         goto error_ipa;
593e703f707SMostafa Saleh     }
594e703f707SMostafa Saleh 
595e703f707SMostafa Saleh     while (level < VMSA_LEVELS) {
596e703f707SMostafa Saleh         uint64_t subpage_size = 1ULL << level_shift(level, granule_sz);
597e703f707SMostafa Saleh         uint64_t mask = subpage_size - 1;
598e703f707SMostafa Saleh         uint32_t offset = iova_level_offset(ipa, inputsize, level, granule_sz);
599e703f707SMostafa Saleh         uint64_t pte, gpa;
600e703f707SMostafa Saleh         dma_addr_t pte_addr = baseaddr + offset * sizeof(pte);
601e703f707SMostafa Saleh         uint8_t s2ap;
602e703f707SMostafa Saleh 
603e703f707SMostafa Saleh         if (get_pte(baseaddr, offset, &pte, info)) {
604e703f707SMostafa Saleh                 goto error;
605e703f707SMostafa Saleh         }
606e703f707SMostafa Saleh         trace_smmu_ptw_level(stage, level, ipa, subpage_size,
607e703f707SMostafa Saleh                              baseaddr, offset, pte);
608e703f707SMostafa Saleh         if (is_invalid_pte(pte) || is_reserved_pte(pte, level)) {
609e703f707SMostafa Saleh             trace_smmu_ptw_invalid_pte(stage, level, baseaddr,
610e703f707SMostafa Saleh                                        pte_addr, offset, pte);
611e703f707SMostafa Saleh             break;
612e703f707SMostafa Saleh         }
613e703f707SMostafa Saleh 
614e703f707SMostafa Saleh         if (is_table_pte(pte, level)) {
615e703f707SMostafa Saleh             baseaddr = get_table_pte_address(pte, granule_sz);
616e703f707SMostafa Saleh             level++;
617e703f707SMostafa Saleh             continue;
618e703f707SMostafa Saleh         } else if (is_page_pte(pte, level)) {
619e703f707SMostafa Saleh             gpa = get_page_pte_address(pte, granule_sz);
620e703f707SMostafa Saleh             trace_smmu_ptw_page_pte(stage, level, ipa,
621e703f707SMostafa Saleh                                     baseaddr, pte_addr, pte, gpa);
622e703f707SMostafa Saleh         } else {
623e703f707SMostafa Saleh             uint64_t block_size;
624e703f707SMostafa Saleh 
625e703f707SMostafa Saleh             gpa = get_block_pte_address(pte, level, granule_sz,
626e703f707SMostafa Saleh                                         &block_size);
627e703f707SMostafa Saleh             trace_smmu_ptw_block_pte(stage, level, baseaddr,
628e703f707SMostafa Saleh                                      pte_addr, pte, ipa, gpa,
629e703f707SMostafa Saleh                                      block_size >> 20);
630e703f707SMostafa Saleh         }
631e703f707SMostafa Saleh 
632e703f707SMostafa Saleh         /*
633e703f707SMostafa Saleh          * If S2AFFD and PTE.AF are 0 => fault. (5.2. Stream Table Entry)
634e703f707SMostafa Saleh          * An Access fault takes priority over a Permission fault.
635e703f707SMostafa Saleh          */
636e703f707SMostafa Saleh         if (!PTE_AF(pte) && !cfg->s2cfg.affd) {
637e703f707SMostafa Saleh             info->type = SMMU_PTW_ERR_ACCESS;
63848f9e9ebSMostafa Saleh             goto error_ipa;
639e703f707SMostafa Saleh         }
640e703f707SMostafa Saleh 
641e703f707SMostafa Saleh         s2ap = PTE_AP(pte);
642e703f707SMostafa Saleh         if (is_permission_fault_s2(s2ap, perm)) {
643e703f707SMostafa Saleh             info->type = SMMU_PTW_ERR_PERMISSION;
64448f9e9ebSMostafa Saleh             goto error_ipa;
645e703f707SMostafa Saleh         }
646e703f707SMostafa Saleh 
647e703f707SMostafa Saleh         /*
648e703f707SMostafa Saleh          * The address output from the translation causes a stage 2 Address
649e703f707SMostafa Saleh          * Size fault if it exceeds the effective PA output range.
650e703f707SMostafa Saleh          */
651e703f707SMostafa Saleh         if (gpa >= (1ULL << cfg->s2cfg.eff_ps)) {
652e703f707SMostafa Saleh             info->type = SMMU_PTW_ERR_ADDR_SIZE;
65348f9e9ebSMostafa Saleh             goto error_ipa;
654e703f707SMostafa Saleh         }
655e703f707SMostafa Saleh 
656e703f707SMostafa Saleh         tlbe->entry.translated_addr = gpa;
657e703f707SMostafa Saleh         tlbe->entry.iova = ipa & ~mask;
658e703f707SMostafa Saleh         tlbe->entry.addr_mask = mask;
659d7cdf89cSMostafa Saleh         tlbe->parent_perm = s2ap;
660d7cdf89cSMostafa Saleh         tlbe->entry.perm = tlbe->parent_perm;
661e703f707SMostafa Saleh         tlbe->level = level;
662e703f707SMostafa Saleh         tlbe->granule = granule_sz;
663e703f707SMostafa Saleh         return 0;
664e703f707SMostafa Saleh     }
665e703f707SMostafa Saleh     info->type = SMMU_PTW_ERR_TRANSLATION;
666e703f707SMostafa Saleh 
66748f9e9ebSMostafa Saleh error_ipa:
66848f9e9ebSMostafa Saleh     info->addr = ipa;
669e703f707SMostafa Saleh error:
670f6cc1980SMostafa Saleh     info->stage = SMMU_STAGE_2;
671e703f707SMostafa Saleh     tlbe->entry.perm = IOMMU_NONE;
672e703f707SMostafa Saleh     return -EINVAL;
673e703f707SMostafa Saleh }
674e703f707SMostafa Saleh 
675d7cdf89cSMostafa Saleh /*
676d7cdf89cSMostafa Saleh  * combine S1 and S2 TLB entries into a single entry.
677*24c32ed3SStefan Weil  * As a result the S1 entry is overridden with combined data.
678d7cdf89cSMostafa Saleh  */
combine_tlb(SMMUTLBEntry * tlbe,SMMUTLBEntry * tlbe_s2,dma_addr_t iova,SMMUTransCfg * cfg)679f42a0a57SMostafa Saleh static void combine_tlb(SMMUTLBEntry *tlbe, SMMUTLBEntry *tlbe_s2,
680f42a0a57SMostafa Saleh                         dma_addr_t iova, SMMUTransCfg *cfg)
681d7cdf89cSMostafa Saleh {
682d7cdf89cSMostafa Saleh     if (tlbe_s2->entry.addr_mask < tlbe->entry.addr_mask) {
683d7cdf89cSMostafa Saleh         tlbe->entry.addr_mask = tlbe_s2->entry.addr_mask;
684d7cdf89cSMostafa Saleh         tlbe->granule = tlbe_s2->granule;
685d7cdf89cSMostafa Saleh         tlbe->level = tlbe_s2->level;
686d7cdf89cSMostafa Saleh     }
687d7cdf89cSMostafa Saleh 
688d7cdf89cSMostafa Saleh     tlbe->entry.translated_addr = CACHED_ENTRY_TO_ADDR(tlbe_s2,
689d7cdf89cSMostafa Saleh                                     tlbe->entry.translated_addr);
690d7cdf89cSMostafa Saleh 
691d7cdf89cSMostafa Saleh     tlbe->entry.iova = iova & ~tlbe->entry.addr_mask;
692d7cdf89cSMostafa Saleh     /* parent_perm has s2 perm while perm keeps s1 perm. */
693d7cdf89cSMostafa Saleh     tlbe->parent_perm = tlbe_s2->entry.perm;
694d7cdf89cSMostafa Saleh     return;
695d7cdf89cSMostafa Saleh }
696d7cdf89cSMostafa Saleh 
697e703f707SMostafa Saleh /**
69893641948SEric Auger  * smmu_ptw - Walk the page tables for an IOVA, according to @cfg
69993641948SEric Auger  *
700f42a0a57SMostafa Saleh  * @bs: smmu state which includes TLB instance
70193641948SEric Auger  * @cfg: translation configuration
70293641948SEric Auger  * @iova: iova to translate
70393641948SEric Auger  * @perm: tentative access type
70493641948SEric Auger  * @tlbe: returned entry
70593641948SEric Auger  * @info: ptw event handle
70693641948SEric Auger  *
70793641948SEric Auger  * return 0 on success
70893641948SEric Auger  */
smmu_ptw(SMMUState * bs,SMMUTransCfg * cfg,dma_addr_t iova,IOMMUAccessFlags perm,SMMUTLBEntry * tlbe,SMMUPTWEventInfo * info)709f42a0a57SMostafa Saleh int smmu_ptw(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t iova,
710f42a0a57SMostafa Saleh              IOMMUAccessFlags perm, SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info)
71193641948SEric Auger {
712f42a0a57SMostafa Saleh     int ret;
713f42a0a57SMostafa Saleh     SMMUTLBEntry tlbe_s2;
714f42a0a57SMostafa Saleh     dma_addr_t ipa;
715f42a0a57SMostafa Saleh 
716f6cc1980SMostafa Saleh     if (cfg->stage == SMMU_STAGE_1) {
717f42a0a57SMostafa Saleh         return smmu_ptw_64_s1(bs, cfg, iova, perm, tlbe, info);
718f6cc1980SMostafa Saleh     } else if (cfg->stage == SMMU_STAGE_2) {
719e703f707SMostafa Saleh         /*
720e703f707SMostafa Saleh          * If bypassing stage 1(or unimplemented), the input address is passed
721e703f707SMostafa Saleh          * directly to stage 2 as IPA. If the input address of a transaction
722e703f707SMostafa Saleh          * exceeds the size of the IAS, a stage 1 Address Size fault occurs.
723e703f707SMostafa Saleh          * For AA64, IAS = OAS according to (IHI 0070.E.a) "3.4 Address sizes"
724e703f707SMostafa Saleh          */
725e703f707SMostafa Saleh         if (iova >= (1ULL << cfg->oas)) {
726e703f707SMostafa Saleh             info->type = SMMU_PTW_ERR_ADDR_SIZE;
727f6cc1980SMostafa Saleh             info->stage = SMMU_STAGE_1;
728e703f707SMostafa Saleh             tlbe->entry.perm = IOMMU_NONE;
729e703f707SMostafa Saleh             return -EINVAL;
730e703f707SMostafa Saleh         }
731e703f707SMostafa Saleh 
732e703f707SMostafa Saleh         return smmu_ptw_64_s2(cfg, iova, perm, tlbe, info);
733e703f707SMostafa Saleh     }
734e703f707SMostafa Saleh 
735f42a0a57SMostafa Saleh     /* SMMU_NESTED. */
736f42a0a57SMostafa Saleh     ret = smmu_ptw_64_s1(bs, cfg, iova, perm, tlbe, info);
737f42a0a57SMostafa Saleh     if (ret) {
738f42a0a57SMostafa Saleh         return ret;
739f42a0a57SMostafa Saleh     }
740f42a0a57SMostafa Saleh 
741f42a0a57SMostafa Saleh     ipa = CACHED_ENTRY_TO_ADDR(tlbe, iova);
742f42a0a57SMostafa Saleh     ret = smmu_ptw_64_s2(cfg, ipa, perm, &tlbe_s2, info);
743f42a0a57SMostafa Saleh     if (ret) {
744f42a0a57SMostafa Saleh         return ret;
745f42a0a57SMostafa Saleh     }
746f42a0a57SMostafa Saleh 
747f42a0a57SMostafa Saleh     combine_tlb(tlbe, &tlbe_s2, iova, cfg);
748f42a0a57SMostafa Saleh     return 0;
74993641948SEric Auger }
750527773eeSEric Auger 
smmu_translate(SMMUState * bs,SMMUTransCfg * cfg,dma_addr_t addr,IOMMUAccessFlags flag,SMMUPTWEventInfo * info)751a9e3f4c1SMostafa Saleh SMMUTLBEntry *smmu_translate(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t addr,
752a9e3f4c1SMostafa Saleh                              IOMMUAccessFlags flag, SMMUPTWEventInfo *info)
753a9e3f4c1SMostafa Saleh {
754a9e3f4c1SMostafa Saleh     SMMUTLBEntry *cached_entry = NULL;
755a9e3f4c1SMostafa Saleh     SMMUTransTableInfo *tt;
756a9e3f4c1SMostafa Saleh     int status;
757a9e3f4c1SMostafa Saleh 
758a9e3f4c1SMostafa Saleh     /*
7597eb57be1SMostafa Saleh      * Combined attributes used for TLB lookup, holds the attributes for
7607eb57be1SMostafa Saleh      * the input stage.
761a9e3f4c1SMostafa Saleh      */
762a9e3f4c1SMostafa Saleh     SMMUTransTableInfo tt_combined;
763a9e3f4c1SMostafa Saleh 
7647eb57be1SMostafa Saleh     if (cfg->stage == SMMU_STAGE_2) {
7657eb57be1SMostafa Saleh         /* Stage2. */
7667eb57be1SMostafa Saleh         tt_combined.granule_sz = cfg->s2cfg.granule_sz;
7677eb57be1SMostafa Saleh         tt_combined.tsz = cfg->s2cfg.tsz;
7687eb57be1SMostafa Saleh     } else {
769a9e3f4c1SMostafa Saleh         /* Select stage1 translation table. */
770a9e3f4c1SMostafa Saleh         tt = select_tt(cfg, addr);
771a9e3f4c1SMostafa Saleh         if (!tt) {
772a9e3f4c1SMostafa Saleh             info->type = SMMU_PTW_ERR_TRANSLATION;
773a9e3f4c1SMostafa Saleh             info->stage = SMMU_STAGE_1;
774a9e3f4c1SMostafa Saleh             return NULL;
775a9e3f4c1SMostafa Saleh         }
776a9e3f4c1SMostafa Saleh         tt_combined.granule_sz = tt->granule_sz;
777a9e3f4c1SMostafa Saleh         tt_combined.tsz = tt->tsz;
778a9e3f4c1SMostafa Saleh     }
779a9e3f4c1SMostafa Saleh 
7807eb57be1SMostafa Saleh     cached_entry = smmu_iotlb_lookup(bs, cfg, &tt_combined, addr);
781a9e3f4c1SMostafa Saleh     if (cached_entry) {
782d7cdf89cSMostafa Saleh         if ((flag & IOMMU_WO) && !(cached_entry->entry.perm &
783d7cdf89cSMostafa Saleh             cached_entry->parent_perm & IOMMU_WO)) {
784a9e3f4c1SMostafa Saleh             info->type = SMMU_PTW_ERR_PERMISSION;
785d7cdf89cSMostafa Saleh             info->stage = !(cached_entry->entry.perm & IOMMU_WO) ?
786d7cdf89cSMostafa Saleh                           SMMU_STAGE_1 :
787d7cdf89cSMostafa Saleh                           SMMU_STAGE_2;
788a9e3f4c1SMostafa Saleh             return NULL;
789a9e3f4c1SMostafa Saleh         }
790a9e3f4c1SMostafa Saleh         return cached_entry;
791a9e3f4c1SMostafa Saleh     }
792a9e3f4c1SMostafa Saleh 
793a9e3f4c1SMostafa Saleh     cached_entry = g_new0(SMMUTLBEntry, 1);
794f42a0a57SMostafa Saleh     status = smmu_ptw(bs, cfg, addr, flag, cached_entry, info);
795a9e3f4c1SMostafa Saleh     if (status) {
796a9e3f4c1SMostafa Saleh             g_free(cached_entry);
797a9e3f4c1SMostafa Saleh             return NULL;
798a9e3f4c1SMostafa Saleh     }
799a9e3f4c1SMostafa Saleh     smmu_iotlb_insert(bs, cfg, cached_entry);
800a9e3f4c1SMostafa Saleh     return cached_entry;
801a9e3f4c1SMostafa Saleh }
802a9e3f4c1SMostafa Saleh 
803cac994efSEric Auger /**
804cac994efSEric Auger  * The bus number is used for lookup when SID based invalidation occurs.
805cac994efSEric Auger  * In that case we lazily populate the SMMUPciBus array from the bus hash
806cac994efSEric Auger  * table. At the time the SMMUPciBus is created (smmu_find_add_as), the bus
807cac994efSEric Auger  * numbers may not be always initialized yet.
808cac994efSEric Auger  */
smmu_find_smmu_pcibus(SMMUState * s,uint8_t bus_num)809cac994efSEric Auger SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num)
810cac994efSEric Auger {
811cac994efSEric Auger     SMMUPciBus *smmu_pci_bus = s->smmu_pcibus_by_bus_num[bus_num];
812cac994efSEric Auger     GHashTableIter iter;
813cac994efSEric Auger 
8145ca0e6feSPhilippe Mathieu-Daudé     if (smmu_pci_bus) {
8155ca0e6feSPhilippe Mathieu-Daudé         return smmu_pci_bus;
8165ca0e6feSPhilippe Mathieu-Daudé     }
8175ca0e6feSPhilippe Mathieu-Daudé 
818cac994efSEric Auger     g_hash_table_iter_init(&iter, s->smmu_pcibus_by_busptr);
819cac994efSEric Auger     while (g_hash_table_iter_next(&iter, NULL, (void **)&smmu_pci_bus)) {
820cac994efSEric Auger         if (pci_bus_num(smmu_pci_bus->bus) == bus_num) {
821cac994efSEric Auger             s->smmu_pcibus_by_bus_num[bus_num] = smmu_pci_bus;
822cac994efSEric Auger             return smmu_pci_bus;
823cac994efSEric Auger         }
824cac994efSEric Auger     }
8255ca0e6feSPhilippe Mathieu-Daudé 
8265ca0e6feSPhilippe Mathieu-Daudé     return NULL;
827cac994efSEric Auger }
828cac994efSEric Auger 
smmu_find_add_as(PCIBus * bus,void * opaque,int devfn)829cac994efSEric Auger static AddressSpace *smmu_find_add_as(PCIBus *bus, void *opaque, int devfn)
830cac994efSEric Auger {
831cac994efSEric Auger     SMMUState *s = opaque;
832cac994efSEric Auger     SMMUPciBus *sbus = g_hash_table_lookup(s->smmu_pcibus_by_busptr, bus);
833cac994efSEric Auger     SMMUDevice *sdev;
8346ce9297bSEric Auger     static unsigned int index;
835cac994efSEric Auger 
836cac994efSEric Auger     if (!sbus) {
837cac994efSEric Auger         sbus = g_malloc0(sizeof(SMMUPciBus) +
838cac994efSEric Auger                          sizeof(SMMUDevice *) * SMMU_PCI_DEVFN_MAX);
839cac994efSEric Auger         sbus->bus = bus;
840cac994efSEric Auger         g_hash_table_insert(s->smmu_pcibus_by_busptr, bus, sbus);
841cac994efSEric Auger     }
842cac994efSEric Auger 
843cac994efSEric Auger     sdev = sbus->pbdev[devfn];
844cac994efSEric Auger     if (!sdev) {
8456ce9297bSEric Auger         char *name = g_strdup_printf("%s-%d-%d", s->mrtypename, devfn, index++);
8466ce9297bSEric Auger 
847cac994efSEric Auger         sdev = sbus->pbdev[devfn] = g_new0(SMMUDevice, 1);
848cac994efSEric Auger 
849cac994efSEric Auger         sdev->smmu = s;
850cac994efSEric Auger         sdev->bus = bus;
851cac994efSEric Auger         sdev->devfn = devfn;
852cac994efSEric Auger 
853cac994efSEric Auger         memory_region_init_iommu(&sdev->iommu, sizeof(sdev->iommu),
854cac994efSEric Auger                                  s->mrtypename,
855ca3fbed8SJean-Philippe Brucker                                  OBJECT(s), name, UINT64_MAX);
856cac994efSEric Auger         address_space_init(&sdev->as,
857cac994efSEric Auger                            MEMORY_REGION(&sdev->iommu), name);
858cac994efSEric Auger         trace_smmu_add_mr(name);
859cac994efSEric Auger         g_free(name);
860cac994efSEric Auger     }
861cac994efSEric Auger 
862cac994efSEric Auger     return &sdev->as;
863cac994efSEric Auger }
864cac994efSEric Auger 
865ba7d12ebSYi Liu static const PCIIOMMUOps smmu_ops = {
866ba7d12ebSYi Liu     .get_address_space = smmu_find_add_as,
867ba7d12ebSYi Liu };
868ba7d12ebSYi Liu 
smmu_find_sdev(SMMUState * s,uint32_t sid)86969970205SNicolin Chen SMMUDevice *smmu_find_sdev(SMMUState *s, uint32_t sid)
87032cfd7f3SEric Auger {
87132cfd7f3SEric Auger     uint8_t bus_n, devfn;
87232cfd7f3SEric Auger     SMMUPciBus *smmu_bus;
87332cfd7f3SEric Auger 
87432cfd7f3SEric Auger     bus_n = PCI_BUS_NUM(sid);
87532cfd7f3SEric Auger     smmu_bus = smmu_find_smmu_pcibus(s, bus_n);
87632cfd7f3SEric Auger     if (smmu_bus) {
877b78aae9bSEric Auger         devfn = SMMU_PCI_DEVFN(sid);
87869970205SNicolin Chen         return smmu_bus->pbdev[devfn];
87932cfd7f3SEric Auger     }
88032cfd7f3SEric Auger     return NULL;
88132cfd7f3SEric Auger }
88232cfd7f3SEric Auger 
883832e4222SEric Auger /* Unmap all notifiers attached to @mr */
smmu_inv_notifiers_mr(IOMMUMemoryRegion * mr)8841e793dd6SPhilippe Mathieu-Daudé static void smmu_inv_notifiers_mr(IOMMUMemoryRegion *mr)
885832e4222SEric Auger {
886832e4222SEric Auger     IOMMUNotifier *n;
887832e4222SEric Auger 
888832e4222SEric Auger     trace_smmu_inv_notifiers_mr(mr->parent_obj.name);
889832e4222SEric Auger     IOMMU_NOTIFIER_FOREACH(n, mr) {
89098332f64SJason Wang         memory_region_unmap_iommu_notifier_range(n);
891832e4222SEric Auger     }
892832e4222SEric Auger }
893832e4222SEric Auger 
894832e4222SEric Auger /* Unmap all notifiers of all mr's */
smmu_inv_notifiers_all(SMMUState * s)895832e4222SEric Auger void smmu_inv_notifiers_all(SMMUState *s)
896832e4222SEric Auger {
897c6370441SEric Auger     SMMUDevice *sdev;
898832e4222SEric Auger 
899c6370441SEric Auger     QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) {
900c6370441SEric Auger         smmu_inv_notifiers_mr(&sdev->iommu);
901832e4222SEric Auger     }
902832e4222SEric Auger }
903832e4222SEric Auger 
smmu_base_realize(DeviceState * dev,Error ** errp)904527773eeSEric Auger static void smmu_base_realize(DeviceState *dev, Error **errp)
905527773eeSEric Auger {
906cac994efSEric Auger     SMMUState *s = ARM_SMMU(dev);
907527773eeSEric Auger     SMMUBaseClass *sbc = ARM_SMMU_GET_CLASS(dev);
908527773eeSEric Auger     Error *local_err = NULL;
909527773eeSEric Auger 
910527773eeSEric Auger     sbc->parent_realize(dev, &local_err);
911527773eeSEric Auger     if (local_err) {
912527773eeSEric Auger         error_propagate(errp, local_err);
913527773eeSEric Auger         return;
914527773eeSEric Auger     }
91532cfd7f3SEric Auger     s->configs = g_hash_table_new_full(NULL, NULL, NULL, g_free);
916cc27ed81SEric Auger     s->iotlb = g_hash_table_new_full(smmu_iotlb_key_hash, smmu_iotlb_key_equal,
917cc27ed81SEric Auger                                      g_free, g_free);
918cac994efSEric Auger     s->smmu_pcibus_by_busptr = g_hash_table_new(NULL, NULL);
919cac994efSEric Auger 
920cac994efSEric Auger     if (s->primary_bus) {
921ba7d12ebSYi Liu         pci_setup_iommu(s->primary_bus, &smmu_ops, s);
922cac994efSEric Auger     } else {
923cac994efSEric Auger         error_setg(errp, "SMMU is not attached to any PCI bus!");
924cac994efSEric Auger     }
925527773eeSEric Auger }
926527773eeSEric Auger 
smmu_base_reset_hold(Object * obj,ResetType type)927ad80e367SPeter Maydell static void smmu_base_reset_hold(Object *obj, ResetType type)
928527773eeSEric Auger {
9293c1a7c41SPeter Maydell     SMMUState *s = ARM_SMMU(obj);
93032cfd7f3SEric Auger 
9318a6b3f4dSZhenzhong Duan     memset(s->smmu_pcibus_by_bus_num, 0, sizeof(s->smmu_pcibus_by_bus_num));
9328a6b3f4dSZhenzhong Duan 
93332cfd7f3SEric Auger     g_hash_table_remove_all(s->configs);
934cc27ed81SEric Auger     g_hash_table_remove_all(s->iotlb);
935527773eeSEric Auger }
936527773eeSEric Auger 
937527773eeSEric Auger static Property smmu_dev_properties[] = {
938527773eeSEric Auger     DEFINE_PROP_UINT8("bus_num", SMMUState, bus_num, 0),
939c45e7619SPhilippe Mathieu-Daudé     DEFINE_PROP_LINK("primary-bus", SMMUState, primary_bus,
940c45e7619SPhilippe Mathieu-Daudé                      TYPE_PCI_BUS, PCIBus *),
941527773eeSEric Auger     DEFINE_PROP_END_OF_LIST(),
942527773eeSEric Auger };
943527773eeSEric Auger 
smmu_base_class_init(ObjectClass * klass,void * data)944527773eeSEric Auger static void smmu_base_class_init(ObjectClass *klass, void *data)
945527773eeSEric Auger {
946527773eeSEric Auger     DeviceClass *dc = DEVICE_CLASS(klass);
9473c1a7c41SPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(klass);
948527773eeSEric Auger     SMMUBaseClass *sbc = ARM_SMMU_CLASS(klass);
949527773eeSEric Auger 
9504f67d30bSMarc-André Lureau     device_class_set_props(dc, smmu_dev_properties);
951527773eeSEric Auger     device_class_set_parent_realize(dc, smmu_base_realize,
952527773eeSEric Auger                                     &sbc->parent_realize);
9533c1a7c41SPeter Maydell     rc->phases.hold = smmu_base_reset_hold;
954527773eeSEric Auger }
955527773eeSEric Auger 
956527773eeSEric Auger static const TypeInfo smmu_base_info = {
957527773eeSEric Auger     .name          = TYPE_ARM_SMMU,
958527773eeSEric Auger     .parent        = TYPE_SYS_BUS_DEVICE,
959527773eeSEric Auger     .instance_size = sizeof(SMMUState),
960527773eeSEric Auger     .class_data    = NULL,
961527773eeSEric Auger     .class_size    = sizeof(SMMUBaseClass),
962527773eeSEric Auger     .class_init    = smmu_base_class_init,
963527773eeSEric Auger     .abstract      = true,
964527773eeSEric Auger };
965527773eeSEric Auger 
smmu_base_register_types(void)966527773eeSEric Auger static void smmu_base_register_types(void)
967527773eeSEric Auger {
968527773eeSEric Auger     type_register_static(&smmu_base_info);
969527773eeSEric Auger }
970527773eeSEric Auger 
971527773eeSEric Auger type_init(smmu_base_register_types)
972527773eeSEric Auger 
973