From: Mostafa Saleh <smostafa@google.com>
To: Tao Tang <tangtao1634@phytium.com.cn>
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
"Eric Auger" <eric.auger@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>,
qemu-devel@nongnu.org, qemu-arm@nongnu.org,
"Pierrick Bouvier" <pierrick.bouvier@oss.qualcomm.com>,
"Chao Liu" <chao.liu@processmission.com>,
"Chen Baozi" <chenbaozi@phytium.com.cn>,
"Jim MacArthur" <jim.macarthur@linaro.org>
Subject: Re: [PATCH v6 17/35] hw/arm/smmuv3: Scope IOTLB invalidation by SEC_SID
Date: Thu, 3 Dec 2026 15:55:52 +0000 [thread overview]
Message-ID: <axGRCCBW8IGlWca1@google.com> (raw)
In-Reply-To: <20261114161753.3645568-8-tangtao1634@phytium.com.cn>
On Sun, Nov 15, 2026 at 12:17:51AM +0800, Tao Tang wrote:
> IOTLB invalidation must select the security state represented in the
> cache key. Add SEC_SID matching to the invalidation helpers and propagate
> the command queue or command-selected state through TLBI handling.
>
> Select stage-2 capability from the issuing command queue when deciding
> whether to match VMID, including SEL2 for the Secure command queue.
> Preserve full-cache invalidation for operations that require it and
> include SEC_SID in the affected invalidation traces.
>
> Signed-off-by: Tao Tang <tangtao1634@phytium.com.cn>
> ---
> include/hw/arm/smmu-common.h | 1 +
> hw/arm/smmu-common.c | 18 +++++++++++
> hw/arm/smmuv3.c | 62 +++++++++++++++++++++++++++++-------
> hw/arm/trace-events | 3 +-
> 4 files changed, 71 insertions(+), 13 deletions(-)
>
> diff --git a/include/hw/arm/smmu-common.h b/include/hw/arm/smmu-common.h
> index 49b86cede47..a21c6061808 100644
> --- a/include/hw/arm/smmu-common.h
> +++ b/include/hw/arm/smmu-common.h
> @@ -269,6 +269,7 @@ SMMUIOTLBKey smmu_get_iotlb_key(int asid, int vmid, uint64_t iova,
> uint8_t tg, uint8_t level, SMMUSecSID sec_sid);
> SMMUConfigKey smmu_get_config_key(SMMUDevice *sdev, SMMUSecSID sec_sid);
> void smmu_iotlb_inv_all(SMMUState *s);
> +void smmu_iotlb_inv_by_sec_sid(SMMUState *s, SMMUSecSID sec_sid);
> void smmu_iotlb_inv_asid_vmid(SMMUState *s, int asid, int vmid,
> SMMUSecSID sec_sid);
> void smmu_iotlb_inv_vmid(SMMUState *s, int vmid, SMMUSecSID sec_sid);
> diff --git a/hw/arm/smmu-common.c b/hw/arm/smmu-common.c
> index f6ad89208ac..0408fd25b3a 100644
> --- a/hw/arm/smmu-common.c
> +++ b/hw/arm/smmu-common.c
> @@ -222,12 +222,30 @@ void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *new,
> g_hash_table_insert(bs->iotlb, key, new);
> }
>
> +static gboolean smmu_hash_remove_by_sec_sid(gpointer key, gpointer value,
> + gpointer user_data)
> +{
> + SMMUIOTLBKey *iotlb_key = (SMMUIOTLBKey *)key;
> + SMMUSecSID *sec_sid = (SMMUSecSID *)user_data;
> +
> + return SMMU_IOTLB_SEC_SID(*iotlb_key) == *sec_sid;
> +}
> +
> void smmu_iotlb_inv_all(SMMUState *s)
> {
> trace_smmu_iotlb_inv_all();
> g_hash_table_remove_all(s->iotlb);
> }
>
> +void smmu_iotlb_inv_by_sec_sid(SMMUState *s, SMMUSecSID sec_sid)
> +{
> + g_assert(sec_sid < SMMU_SEC_SID_NUM);
> +
> + trace_smmu_iotlb_inv_by_sec_sid(sec_sid);
> + g_hash_table_foreach_remove(s->iotlb, smmu_hash_remove_by_sec_sid,
> + &sec_sid);
> +}
> +
> static gboolean smmu_hash_remove_by_asid_vmid(gpointer key, gpointer value,
> gpointer user_data)
> {
> diff --git a/hw/arm/smmuv3.c b/hw/arm/smmuv3.c
> index 41fa7271d8d..fc5cd46d1a8 100644
> --- a/hw/arm/smmuv3.c
> +++ b/hw/arm/smmuv3.c
> @@ -1340,7 +1340,7 @@ static void smmuv3_inv_notifiers_all(SMMUState *s, SMMUSecSID sec_sid)
> }
>
> static void smmuv3_range_inval(SMMUState *s, Cmd *cmd, SMMUStage stage,
> - SMMUSecSID sec_sid)
> + SMMUSecSID sec_sid, bool use_vmid)
> {
> dma_addr_t end, addr = CMD_ADDR(cmd);
> uint8_t type = CMD_TYPE(cmd);
> @@ -1353,10 +1353,8 @@ static void smmuv3_range_inval(SMMUState *s, Cmd *cmd, SMMUStage stage,
> uint64_t num_pages;
> uint8_t granule;
> int asid = -1;
> - SMMUv3State *smmuv3 = ARM_SMMUV3(s);
>
> - /* Only consider VMID if stage-2 is supported. */
> - if (STAGE2_SUPPORTED(smmuv3)) {
> + if (use_vmid) {
> vmid = CMD_VMID(cmd);
> }
>
> @@ -1403,6 +1401,26 @@ static void smmuv3_range_inval(SMMUState *s, Cmd *cmd, SMMUStage stage,
> }
> }
>
> +static inline bool
> +smmu_cmdq_stage2_supported(SMMUv3State *s, SMMUSecSID sec_sid)
> +{
> + /* IDR0.S2P: Stage 2 translation supported */
> + bool s2p = STAGE2_SUPPORTED(s);
> + if (!s2p) {
> + return false;
> + }
> +
> + /*
> + * For Secure Command queue, Secure stage 2 is additionally gated by SEL2
> + * (SEL2 is 0 if S2P is 0).
> + */
> + if (sec_sid == SMMU_SEC_SID_S) {
> + return FIELD_EX32(s->bank[SMMU_SEC_SID_S].idr[1], S_IDR1, SEL2);
> + }
This is never true, I'd suggest to drop it for now and make this helper
simpler, but no strong opinion.
> +
> + return true;
> +}
> +
> static int smmuv3_cmdq_consume(SMMUv3State *s, Error **errp, SMMUSecSID sec_sid)
> {
> SMMUState *bs = ARM_SMMU(s);
> @@ -1412,6 +1430,7 @@ static int smmuv3_cmdq_consume(SMMUv3State *s, Error **errp, SMMUSecSID sec_sid)
> SMMUCommandType type = 0;
> MemTxAttrs attrs = smmu_get_txattrs(sec_sid);
> AddressSpace *as = smmu_get_address_space(bs, sec_sid);
> + bool queue_stage2_supported = smmu_cmdq_stage2_supported(s, sec_sid);
>
> if (!smmuv3_cmdq_enabled(s, sec_sid)) {
> return 0;
> @@ -1542,7 +1561,7 @@ static int smmuv3_cmdq_consume(SMMUv3State *s, Error **errp, SMMUSecSID sec_sid)
> * VMID is only matched when stage 2 is supported, otherwise set it
> * to -1 as the value used for stage-1 only VMIDs.
> */
> - if (STAGE2_SUPPORTED(s)) {
> + if (queue_stage2_supported) {
> vmid = CMD_VMID(&cmd);
> }
>
> @@ -1568,30 +1587,49 @@ static int smmuv3_cmdq_consume(SMMUv3State *s, Error **errp, SMMUSecSID sec_sid)
> * If stage-2 is supported, invalidate for this VMID only, otherwise
> * invalidate the whole thing.
> */
> - if (STAGE2_SUPPORTED(s)) {
> + if (queue_stage2_supported) {
> vmid = CMD_VMID(&cmd);
> trace_smmuv3_cmdq_tlbi_nh(sec_sid, vmid);
> smmu_iotlb_inv_vmid_s1(bs, vmid, sec_sid);
> break;
> }
> - QEMU_FALLTHROUGH;
> + trace_smmuv3_cmdq_tlbi_nh(sec_sid, vmid);
> + smmuv3_inv_notifiers_all(bs, sec_sid);
> + smmu_iotlb_inv_by_sec_sid(bs, sec_sid);
> + if (!smmuv3_accel_issue_inv_cmd(s, &cmd, NULL, errp)) {
> + cmd_error = SMMU_CERROR_ILL;
> + break;
> + }
> + break;
> }
> case SMMU_CMD_TLBI_NSNH_ALL:
> - trace_smmuv3_cmdq_tlbi_nsnh();
> - smmuv3_inv_notifiers_all(bs, SMMU_SEC_SID_NS);
> - smmu_iotlb_inv_all(bs);
> + {
> + /*
> + * CMD_TLBI_NSNH_ALL targets Non-secure entries when issued from the
> + * Non-secure or Secure Command queue, but Realm entries when issued
> + * from the Realm Command queue.
> + * (IHI 0070G.b) 4.4.4.1 CMD_TLBI_NSNH_ALL, Page 194
> + */
> + SMMUSecSID target_sec_sid = sec_sid > SMMU_SEC_SID_S ?
> + sec_sid : SMMU_SEC_SID_NS;
I think the realm comments are confusing, and target_sec_sid is always
NS now, so I'd suggest just to remove this for now until realms are
supported and keep the switch case as it was.
Thanks,
Mostafa
> +
> + trace_smmuv3_cmdq_tlbi_nsnh(sec_sid, target_sec_sid);
> + smmuv3_inv_notifiers_all(bs, target_sec_sid);
> + smmu_iotlb_inv_by_sec_sid(bs, target_sec_sid);
> if (!smmuv3_accel_issue_inv_cmd(s, &cmd, NULL, errp)) {
> cmd_error = SMMU_CERROR_ILL;
> break;
> }
> break;
> + }
> case SMMU_CMD_TLBI_NH_VAA:
> case SMMU_CMD_TLBI_NH_VA:
> if (!STAGE1_SUPPORTED(s)) {
> cmd_error = SMMU_CERROR_ILL;
> break;
> }
> - smmuv3_range_inval(bs, &cmd, SMMU_STAGE_1, SMMU_SEC_SID_NS);
> + smmuv3_range_inval(bs, &cmd, SMMU_STAGE_1, sec_sid,
> + queue_stage2_supported);
> if (!smmuv3_accel_issue_inv_cmd(s, &cmd, NULL, errp)) {
> cmd_error = SMMU_CERROR_ILL;
> break;
> @@ -1620,7 +1658,7 @@ static int smmuv3_cmdq_consume(SMMUv3State *s, Error **errp, SMMUSecSID sec_sid)
> * As currently only either s1 or s2 are supported
> * we can reuse same function for s2.
> */
> - smmuv3_range_inval(bs, &cmd, SMMU_STAGE_2, SMMU_SEC_SID_NS);
> + smmuv3_range_inval(bs, &cmd, SMMU_STAGE_2, SMMU_SEC_SID_NS, true);
> break;
> case SMMU_CMD_ATC_INV:
> {
> diff --git a/hw/arm/trace-events b/hw/arm/trace-events
> index ea525a81b3f..4130d16fad5 100644
> --- a/hw/arm/trace-events
> +++ b/hw/arm/trace-events
> @@ -19,6 +19,7 @@ smmu_ptw_page_pte(int stage, int level, uint64_t iova, uint64_t baseaddr, uint6
> 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_inv_all(void) "IOTLB invalidate all"
> +smmu_iotlb_inv_by_sec_sid(int sec_sid) "IOTLB invalidate sec_sid=%d"
> smmu_iotlb_inv_asid_vmid(int sec_sid, int asid, int vmid) "IOTLB invalidate sec_sid=%d asid=%d vmid=%d"
> smmu_iotlb_inv_vmid(int sec_sid, int vmid) "IOTLB invalidate sec_sid=%d vmid=%d"
> smmu_iotlb_inv_vmid_s1(int sec_sid, int vmid) "IOTLB invalidate S1 sec_sid=%d vmid=%d"
> @@ -60,7 +61,7 @@ smmuv3_config_cache_hit(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t p
> smmuv3_config_cache_miss(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache MISS for sid=0x%x (hits=%d, misses=%d, hit rate=%d)"
> smmuv3_range_inval(int sec_sid, int vmid, int asid, uint64_t addr, uint8_t tg, uint64_t num_pages, uint8_t ttl, bool leaf, int stage) "sec_sid=%d vmid=%d asid=%d addr=0x%"PRIx64" tg=%d num_pages=0x%"PRIx64" ttl=%d leaf=%d stage=%d"
> smmuv3_cmdq_tlbi_nh(int sec_sid, int vmid) "sec_sid=%d vmid=%d"
> -smmuv3_cmdq_tlbi_nsnh(void) ""
> +smmuv3_cmdq_tlbi_nsnh(int sec_sid, int target_sec_sid) "cmdq_sec_sid=%d target_sec_sid=%d"
> smmuv3_cmdq_tlbi_nh_asid(int sec_sid, int asid) "sec_sid=%d asid=%d"
> smmuv3_cmdq_tlbi_s12_vmid(int vmid) "vmid=%d"
> smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s"
> --
> 2.34.1
>
next prev parent reply other threads:[~2026-12-03 15:56 UTC|newest]
Thread overview: 71+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-14 15:46 [PATCH v6 00/35] hw/arm/smmuv3: Support Secure state for SMMUv3 Tao Tang
2026-11-14 16:05 ` [PATCH v6 27/35] hw/arm/smmuv3: Route IRQ and GERROR handling by SEC_SID Tao Tang
2026-12-03 21:06 ` Mostafa Saleh
2026-11-14 16:07 ` [PATCH v6 30/35] hw/pci: Add sec-sid property to PCIDevice Tao Tang
2026-11-14 16:07 ` [PATCH v6 31/35] hw/arm/smmuv3: Select sec-sid from PCI property and validate SECURE_IMPL Tao Tang
2026-11-14 16:07 ` [PATCH v6 34/35] hw/arm/smmuv3: Add the secure-impl property Tao Tang
2026-11-14 16:13 ` [PATCH v6 01/35] hw/arm/smmuv3: Introduce Secure registers Tao Tang
2026-11-14 16:13 ` [PATCH v6 02/35] hw/arm/smmuv3: Introduce banked registers for SMMUv3 state Tao Tang
2026-11-14 16:13 ` [PATCH v6 03/35] hw/arm/smmuv3: Thread SEC_SID through helper APIs Tao Tang
2026-11-14 16:13 ` [PATCH v6 04/35] hw/arm/smmuv3: Track SEC_SID in configs and events Tao Tang
2026-11-14 16:13 ` [PATCH v6 05/35] hw/arm/smmu-common: Add security-aware address space selector Tao Tang
2026-11-14 16:16 ` [PATCH v6 06/35] hw/arm/smmuv3: Plumb transaction attributes into config helpers Tao Tang
2026-12-03 15:47 ` Mostafa Saleh
2026-12-04 8:20 ` Eric Auger
2026-11-14 16:16 ` [PATCH v6 07/35] hw/arm/smmuv3: Reject Secure STEs with stage-2 enabled Tao Tang
2026-12-03 15:47 ` Mostafa Saleh
2026-12-04 8:24 ` Eric Auger
2026-11-14 16:16 ` [PATCH v6 08/35] hw/arm/smmu-common: Key configuration cache on SMMUDevice and SEC_SID Tao Tang
2026-11-14 16:16 ` [PATCH v6 09/35] hw/arm/smmu: Add PTE NS/NSTable helpers Tao Tang
2026-11-14 16:17 ` [PATCH v6 10/35] hw/arm/smmuv3: Store CD NSCFG in TT info Tao Tang
2026-11-14 16:17 ` [PATCH v6 11/35] hw/arm/smmu-common: Implement Secure state handling in ptw Tao Tang
2026-12-04 13:52 ` Eric Auger
2026-11-14 16:17 ` [PATCH v6 12/35] hw/arm/smmuv3: Tag IOTLB cache keys with SEC_SID Tao Tang
2026-12-03 15:48 ` Mostafa Saleh
2026-11-14 16:17 ` [PATCH v6 13/35] hw/arm/smmuv3: Pass sec_sid into cmdq consume path Tao Tang
2026-12-03 15:51 ` Mostafa Saleh
2026-11-14 16:17 ` [PATCH v6 14/35] hw/arm/smmuv3: Make evtq producer use SEC_SID Tao Tang
2026-11-14 16:17 ` [PATCH v6 15/35] hw/arm/smmuv3: Limit IOMMU notifications to Non-secure targets Tao Tang
2026-12-01 10:46 ` Jim MacArthur
2026-12-03 15:52 ` Mostafa Saleh
2026-12-04 14:43 ` Eric Auger
2026-11-14 16:17 ` [PATCH v6 16/35] hw/arm/smmuv3: Scope configuration invalidation by SEC_SID Tao Tang
2026-12-01 12:32 ` Jim MacArthur
2026-12-03 15:53 ` Mostafa Saleh
2026-12-04 15:02 ` Eric Auger
2026-12-04 15:09 ` Eric Auger
2026-11-14 16:17 ` [PATCH v6 17/35] hw/arm/smmuv3: Scope IOTLB " Tao Tang
2026-12-01 13:15 ` Jim MacArthur
2026-12-03 15:55 ` Mostafa Saleh [this message]
2026-12-04 17:15 ` Eric Auger
2026-12-04 17:43 ` Eric Auger
2026-11-14 16:17 ` [PATCH v6 18/35] hw/arm/smmuv3: Fix VMID tagging for Secure stage 1 Tao Tang
2026-12-01 13:20 ` Jim MacArthur
2026-12-03 15:56 ` Mostafa Saleh
2026-11-14 16:17 ` [PATCH v6 19/35] hw/arm/smmuv3: Keep accelerated invalidation Non-secure-only Tao Tang
2026-12-01 14:14 ` Jim MacArthur
2026-12-03 15:57 ` Mostafa Saleh
2026-11-14 16:19 ` [PATCH v6 20/35] hw/arm/smmuv3: Fix ATC invalidation command checks Tao Tang
2026-12-01 18:30 ` Jim MacArthur
2026-12-03 15:58 ` Mostafa Saleh
2026-11-14 16:19 ` [PATCH v6 21/35] hw/arm/smmuv3: Make GERROR_IRQ_CFG registers RES0 without MSI Tao Tang
2026-12-03 15:58 ` Mostafa Saleh
2026-11-14 16:19 ` [PATCH v6 22/35] hw/arm/smmuv3: Ignore CR2 writes while the SMMU is enabled Tao Tang
2026-12-03 15:58 ` Mostafa Saleh
2026-11-14 16:19 ` [PATCH v6 23/35] hw/arm/smmuv3: Guard stream table register writes Tao Tang
2026-12-03 20:09 ` Mostafa Saleh
2026-11-14 16:19 ` [PATCH v6 24/35] hw/arm/smmuv3: Guard command queue " Tao Tang
2026-11-14 16:19 ` [PATCH v6 25/35] hw/arm/smmuv3: Guard event " Tao Tang
2026-11-14 16:19 ` [PATCH v6 26/35] hw/arm/smmuv3: Determine register bank from MMIO offset Tao Tang
2026-12-03 21:05 ` Mostafa Saleh
2026-11-14 16:19 ` [PATCH v6 28/35] hw/arm/smmuv3: Implement SMMU_S_INIT register Tao Tang
2026-12-03 21:07 ` Mostafa Saleh
2026-11-14 16:19 ` [PATCH v6 29/35] hw/arm/smmuv3: Harden security checks in MMIO handlers Tao Tang
2026-12-03 21:08 ` Mostafa Saleh
2026-11-14 16:20 ` [PATCH v6 32/35] hw/arm/smmuv3: Reject IOMMU notifiers for non-NS devices Tao Tang
2026-12-03 21:09 ` Mostafa Saleh
2026-11-14 16:20 ` [PATCH v6 33/35] hw/arm/smmuv3: Initialize the Secure register bank Tao Tang
2026-12-03 21:10 ` Mostafa Saleh
2026-11-14 16:20 ` [PATCH v6 35/35] hw/arm/smmuv3: Migrate " Tao Tang
2026-12-03 9:24 ` [PATCH v6 00/35] hw/arm/smmuv3: Support Secure state for SMMUv3 Jim MacArthur
2026-12-03 9:37 ` Eric Auger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axGRCCBW8IGlWca1@google.com \
--to=smostafa@google.com \
--cc=chao.liu@processmission.com \
--cc=chenbaozi@phytium.com.cn \
--cc=eric.auger@redhat.com \
--cc=jim.macarthur@linaro.org \
--cc=peter.maydell@linaro.org \
--cc=philmd@oss.qualcomm.com \
--cc=pierrick.bouvier@oss.qualcomm.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=tangtao1634@phytium.com.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.