From: Mostafa Saleh <smostafa@google.com>
To: linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev,
iommu@lists.linux.dev
Cc: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org,
oliver.upton@linux.dev, joey.gouly@arm.com,
suzuki.poulose@arm.com, yuzenghui@huawei.com, joro@8bytes.org,
jgg@ziepe.ca, mark.rutland@arm.com, qperret@google.com,
tabba@google.com, vdonnefort@google.com,
sebastianene@google.com, keirf@google.com,
Mostafa Saleh <smostafa@google.com>
Subject: [PATCH v8 23/25] iommu/arm-smmu-v3-kvm: Invalidate the SMMU TLBs
Date: Tue, 22 Sep 2026 13:12:56 +0000 [thread overview]
Message-ID: <20260922131259.2975334-24-smostafa@google.com> (raw)
In-Reply-To: <20260922131259.2975334-1-smostafa@google.com>
Implement the io-pgtable flush ops, which invalidate the shadow stage-2
in every SMMU.
Range invalidation algorithm is based on Robin suggestion on the list
for the upstream kernel driver, where a 2 overlapping commands can
invalidate any range.
Invalidation needs the command queue, so track whether it is enabled in
cmdq_active. It is updated with hw_lock held once the CR0 write has been
acknowledged, so it cannot change under a concurrent invalidation. For
the same reason the host is not allowed to disable the command queue
while the SMMU is enabled, as the hypervisor would no longer be able to
invalidate. As invalidation is skipped while the command queue is off,
flush the TLBs every time the SMMU is enabled.
Signed-off-by: Mostafa Saleh <smostafa@google.com>
---
Jason is currently reworking the TLB invalidation on the mailing
list it might be possible to reuse some of the code once landed.
---
.../arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h | 2 +
.../iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c | 177 +++++++++++++++++-
2 files changed, 176 insertions(+), 3 deletions(-)
diff --git a/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h
index ffc57182e57e..534d329661cf 100644
--- a/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h
+++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3-hyp.h
@@ -29,6 +29,7 @@
* @cmdq_host Host view of the CMDQ, only q_base and llq used.
* @cmdq_max_shift Max shift for the CMDQ probed from HW.
* @cr0 Last value of CR0
+ * @cmdq_active Is SMMU HW cmdq usable, protected by hw_lock
* @host_ste_cfg Host stream table config
* @host_ste_base Host stream table base
* @strtab_cfg Stream table as seen by HW
@@ -57,6 +58,7 @@ struct hyp_arm_smmu_v3_device {
struct arm_smmu_queue cmdq_host;
u32 cmdq_max_shift;
u32 cr0;
+ bool cmdq_active;
dma_addr_t strtab_dma;
size_t strtab_size;
u64 host_ste_cfg;
diff --git a/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c
index 6f0ea3a4e48d..5417a5c2bf58 100644
--- a/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/pkvm/arm-smmu-v3.c
@@ -207,7 +207,6 @@ static int smmu_sync_cmd(struct hyp_arm_smmu_v3_device *smmu)
smmu_cmdq_empty(&smmu->cmdq));
}
-__maybe_unused
static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,
struct arm_smmu_cmd *cmd)
{
@@ -221,17 +220,139 @@ static int smmu_send_cmd(struct hyp_arm_smmu_v3_device *smmu,
return smmu_sync_cmd(smmu);
}
+static int smmu_tlb_inv_vmid(struct hyp_arm_smmu_v3_device *smmu)
+{
+ struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_TLBI_S12_VMALL);
+
+ return smmu_send_cmd(smmu, &cmd);
+}
+
+static int smmu_tlb_range_inv_cmd(struct hyp_arm_smmu_v3_device *smmu,
+ struct arm_smmu_cmd *cmd,
+ unsigned long start, unsigned int num,
+ unsigned int scale, u8 ttl, bool leaf)
+{
+ size_t tg = __ffs(idmap_pgtable->cfg.pgsize_bitmap);
+ u8 tg_enc = arm_smmu_tlb_inv_tg_enc(tg);
+
+ cmd->data[0] |= arm_smmu_tlb_inv_range_enc(num, scale);
+ cmd->data[1] = arm_smmu_tlb_inv_addr(start, leaf, ttl, tg_enc);
+ return smmu_add_cmd(smmu, cmd);
+}
+
+static int __smmu_tlb_inv_range(struct hyp_arm_smmu_v3_device *smmu,
+ struct arm_smmu_cmd *cmd,
+ unsigned long iova, size_t size, size_t granule,
+ bool leaf)
+{
+ size_t tg = __ffs(idmap_pgtable->cfg.pgsize_bitmap);
+ unsigned long n = size >> tg;
+ unsigned long second_start;
+ u64 data0 = cmd->data[0];
+ unsigned int num, scale;
+ u8 ttl;
+ int ret;
+
+ /* Only leaf invalidations know the level, non-leaf must use TTL=0. */
+ ttl = leaf ? arm_smmu_tlb_inv_ttl(granule, tg) : 0;
+ scale = fls64((n - 1) / 32);
+ /* Scale is up to 5 bits. */
+ if (scale > 31)
+ return smmu_tlb_inv_vmid(smmu);
+
+ num = n >> scale;
+ ret = smmu_tlb_range_inv_cmd(smmu, cmd, iova, num, scale, ttl, leaf);
+ if (ret)
+ return ret;
+
+ n -= (unsigned long)num << scale;
+ if (n) {
+ scale = fls64((n - 1) / 32);
+ num = DIV_ROUND_UP(n, 1UL << scale);
+ second_start = iova + size - ((unsigned long)num << (scale + tg));
+
+ cmd->data[0] = data0;
+ ret = smmu_tlb_range_inv_cmd(smmu, cmd, second_start, num, scale, 0, leaf);
+ if (ret)
+ return ret;
+ }
+
+ return smmu_sync_cmd(smmu);
+}
+
+static int __smmu_tlb_inv_range_pages(struct hyp_arm_smmu_v3_device *smmu,
+ struct arm_smmu_cmd *cmd,
+ unsigned long iova, size_t size, size_t granule,
+ bool leaf)
+{
+ unsigned long end = iova + size;
+ int ret;
+
+ /* See arm_smmu_inv_size_too_big() */
+ if (size >= (1UL << (ilog2(granule) - 3)) * granule)
+ return smmu_tlb_inv_vmid(smmu);
+
+ for (; iova < end; iova += granule) {
+ cmd->data[1] = arm_smmu_tlb_inv_addr(iova, leaf, 0, 0);
+ ret = smmu_add_cmd(smmu, cmd);
+ if (ret)
+ return ret;
+ }
+
+ return smmu_sync_cmd(smmu);
+}
+
+static int smmu_tlb_inv_range_smmu(struct hyp_arm_smmu_v3_device *smmu,
+ unsigned long iova, size_t size, size_t granule,
+ bool leaf)
+{
+ struct arm_smmu_cmd cmd_s1 = arm_smmu_make_cmd_op(CMDQ_OP_TLBI_NH_ALL);
+ struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_TLBI_S2_IPA);
+ int ret;
+
+ if (smmu->features & ARM_SMMU_FEAT_RANGE_INV)
+ ret = __smmu_tlb_inv_range(smmu, &cmd, iova, size, granule, leaf);
+ else
+ ret = __smmu_tlb_inv_range_pages(smmu, &cmd, iova, size, granule, leaf);
+ if (ret)
+ return ret;
+
+ return smmu_send_cmd(smmu, &cmd_s1);
+}
+
+static void smmu_tlb_inv_range(unsigned long iova, size_t size, size_t granule,
+ bool leaf)
+{
+ struct hyp_arm_smmu_v3_device *smmu;
+
+ for_each_smmu(smmu) {
+ hyp_spin_lock(&smmu->hw_lock);
+ /*
+ * Don't bother if CMDQ is disabled, this would be useful for the case
+ * when RPM is supported to avoid touching the SMMU MMIO when disabled.
+ * The hypervisor also asserts CMDQEN is enabled before the SMMU is
+ * enabled. As otherwise the host can prevent the hypervisor from doing
+ * TLB invalidations.
+ * When the SMMU is re-enabled the hypervisor cleans the TLBs.
+ */
+ if (smmu->cmdq_active)
+ WARN_ON(smmu_tlb_inv_range_smmu(smmu, iova, size,
+ granule, leaf));
+ hyp_spin_unlock(&smmu->hw_lock);
+ }
+}
+
static void smmu_tlb_flush_walk(unsigned long iova, size_t size,
size_t granule, void *cookie)
{
- /* TBD: Invalidate the range in all the SMMUs. */
+ smmu_tlb_inv_range(iova, size, granule, false);
}
static void smmu_tlb_add_page(struct iommu_iotlb_gather *gather,
unsigned long iova, size_t granule,
void *cookie)
{
- /* TBD: Invalidate the granule in all the SMMUs. */
+ smmu_tlb_inv_range(iova, granule, granule, true);
}
static const struct iommu_flush_ops smmu_tlb_ops = {
@@ -718,6 +839,33 @@ static int smmu_update_ste_shadow(struct hyp_arm_smmu_v3_device *smmu, bool enab
return smmu_unshare_pages(strtab_host_base(smmu), size);
}
+static int smmu_flush_all_tlb(struct hyp_arm_smmu_v3_device *smmu)
+{
+ int ret;
+ u32 cr0;
+ struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_TLBI_NSNH_ALL);
+
+ hyp_spin_lock(&smmu->hw_lock);
+ /*
+ * This must be called when the SMMU is getting enabled.
+ * First enable the cmdq and then invalidate the TLB.
+ */
+ cr0 = readl_relaxed(smmu->base + ARM_SMMU_CR0);
+ if (!(cr0 & CR0_CMDQEN)) {
+ cr0 |= CR0_CMDQEN;
+ writel_relaxed(cr0, smmu->base + ARM_SMMU_CR0);
+ ret = smmu_wait(false, readl_relaxed(smmu->base + ARM_SMMU_CR0ACK) == cr0);
+ if (ret) {
+ hyp_spin_unlock(&smmu->hw_lock);
+ return ret;
+ }
+ }
+
+ ret = smmu_send_cmd(smmu, &cmd);
+ hyp_spin_unlock(&smmu->hw_lock);
+ return ret;
+}
+
static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu)
{
/* Enabling SMMU without CMDQ, means TLB invalidation won't work. */
@@ -725,6 +873,8 @@ static void smmu_emulate_enable(struct hyp_arm_smmu_v3_device *smmu)
return;
WARN_ON(smmu_update_ste_shadow(smmu, true));
+ /* Clean the TLBs each time the SMMU is enabled. */
+ WARN_ON(smmu_flush_all_tlb(smmu));
}
static void smmu_emulate_disable(struct hyp_arm_smmu_v3_device *smmu)
@@ -745,6 +895,13 @@ static void smmu_emulate_cmdq_enable(struct hyp_arm_smmu_v3_device *smmu)
static void smmu_emulate_cmdq_disable(struct hyp_arm_smmu_v3_device *smmu)
{
+ /*
+ * We can not enable the SMMU if the CMDQ is disabled and similarly
+ * we can not disable the CMDQ if the SMMU is enabled, as that can
+ * lead to stale TLBs.
+ */
+ WARN_ON(is_smmu_enabled(smmu));
+
WARN_ON(smmu_unshare_pages(smmu->cmdq_host.base_dma,
cmdq_size(&smmu->cmdq_host)));
}
@@ -1020,6 +1177,20 @@ static bool smmu_dabt_device(struct hyp_arm_smmu_v3_device *smmu,
else
writel_relaxed(val & mask, smmu->base + off);
+ /*
+ * Make sure writes to CR0 are immediately observed, that is important
+ * when synchronizing with TLB invalidation as reading CR0 is not enough
+ * to deduce the SMMU state, and we have to enforce the ack with the
+ * hw_lock acquired.
+ */
+ if (off == ARM_SMMU_CR0) {
+ u32 cr0 = val;
+
+ WARN_ON(smmu_wait(false,
+ readl_relaxed(smmu->base + ARM_SMMU_CR0ACK) == cr0));
+ smmu->cmdq_active = !!(cr0 & CR0_CMDQEN);
+ }
+
hyp_spin_unlock(&smmu->hw_lock);
return true;
}
--
2.55.0.1082.g2b9226bbc0-goog
next prev parent reply other threads:[~2026-09-22 13:13 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-22 13:12 [PATCH v8 00/25] KVM: arm64: SMMUv3 driver for pKVM (trap and emulate) Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 01/25] KVM: arm64: Donate MMIO to the hypervisor Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 02/25] iommu/arm-smmu-v3: Move Queue and STE functions to header Mostafa Saleh
2026-09-22 18:23 ` Nicolin Chen
2026-09-22 13:12 ` [PATCH v8 03/25] iommu/arm-smmu-v3: Introduce RangeInval encoding helpers Mostafa Saleh
2026-09-22 18:45 ` Nicolin Chen
2026-09-22 13:12 ` [PATCH v8 04/25] iommu/arm-smmu-v3: Move IDR parsing to common functions Mostafa Saleh
2026-09-22 19:45 ` Nicolin Chen
2026-09-22 21:48 ` Jason Gunthorpe
2026-09-22 13:12 ` [PATCH v8 05/25] iommu/arm-smmu-v3: Move hitless machinery to common code Mostafa Saleh
2026-09-22 19:59 ` Nicolin Chen
2026-09-22 13:12 ` [PATCH v8 06/25] KVM: arm64: iommu: Introduce IOMMU driver infrastructure Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 07/25] KVM: arm64: iommu: Shadow host stage-2 page table Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 08/25] KVM: arm64: iommu: Add memory pool Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 09/25] KVM: arm64: iommu: Support DABT for IOMMU Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 10/25] iommu/arm-smmu-v3-kvm: Add SMMUv3 driver Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 11/25] iommu/arm-smmu-v3-kvm: Add the kernel driver Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 12/25] iommu/arm-smmu-v3-kvm: Probe SMMU HW Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 13/25] iommu/arm-smmu-v3-kvm: Add MMIO emulation Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 14/25] iommu/arm-smmu-v3-kvm: Shadow the command queue Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 15/25] iommu/arm-smmu-v3-kvm: Add CMDQ functions Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 16/25] iommu/arm-smmu-v3-kvm: Emulate CMDQ for host Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 17/25] iommu/arm-smmu-v3-kvm: Shadow stream table Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 18/25] iommu/arm-smmu-v3-kvm: Shadow STEs Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 19/25] iommu/arm-smmu-v3-kvm: Share other queues Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 20/25] iommu/arm-smmu-v3-kvm: Emulate GBPA Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 21/25] iommu/io-pgtable-arm: Support io-pgtable-arm in the hypervisor Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 22/25] iommu/arm-smmu-v3-kvm: Shadow the CPU stage-2 page table Mostafa Saleh
2026-09-22 13:12 ` Mostafa Saleh [this message]
2026-09-22 13:12 ` [PATCH v8 24/25] iommu/arm-smmu-v3-kvm: Enable nesting Mostafa Saleh
2026-09-22 13:12 ` [PATCH v8 25/25] KVM: arm64: Add documentation for pKVM DMA isolation Mostafa Saleh
2026-09-22 18:07 ` [PATCH v8 00/25] KVM: arm64: SMMUv3 driver for pKVM (trap and emulate) Nicolin Chen
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=20260922131259.2975334-24-smostafa@google.com \
--to=smostafa@google.com \
--cc=catalin.marinas@arm.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joey.gouly@arm.com \
--cc=joro@8bytes.org \
--cc=keirf@google.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=qperret@google.com \
--cc=sebastianene@google.com \
--cc=suzuki.poulose@arm.com \
--cc=tabba@google.com \
--cc=vdonnefort@google.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®