Subject: Re: [PATCH v7 02/11] iommu/arm-smmu-v3: Add a helper to drain cmd queues

From: Pranjal Shrivastava <[email-masked]>
Date: Fri, 29 May 2026 14:32:22 +0000

On Thu, May 28, 2026 at 03:09:03PM -0700, Nicolin Chen wrote:
> On Thu, May 28, 2026 at 10:34:42AM +0000, Pranjal Shrivastava wrote:
> > On Wed, May 27, 2026 at 06:35:56PM -0700, Nicolin Chen wrote:
> > > On Wed, May 27, 2026 at 10:13:58PM +0000, Pranjal Shrivastava wrote:
> > I'm planning to rename it to arm_smmu_drain_cmdq(), sounds good?
> 
> That sounds okay.
> 
> Though my series is still at the early review stage, I would like
> to merge those two functions at some point

Makes sense.

> 
> Do you know if my implementation can work for cmdq too? Seems that
> only the WFE would be different, which can be a bool?

I think we can definitely unify these. I had a similar approach in my v2. 
I've reviewed your PRI series and I think it would be best to use a common 
helper for both use cases. 

Maybe you include this patch in your series to combine the draining logic
It leverages the helpers you factored out in your Patch 2 and adds a 
toggle for the queue_empty requirement of Runtime PM. It also checks for 
WFE appropriately:

Subject: [PATCH] iommu/arm-smmu-v3: Add a unified arm_smmu_drain_queue() helper

Introduce a unified arm_smmu_drain_queue() helper to handle both 
snapshot-based cohort draining (for IOPF) and polling until queue_empty 
(required for Runtime PM).

The helper dynamically checks for the WFE requirement when targeting the 
CMDQ and uses cond_resched() to avoid starving the threaded IRQ handlers 
responsible for consuming entries from other queues.

Signed-off-by: Pranjal Shrivastava <[email-masked]>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 32 +++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index 866c711b118b..cf41b3cf5985f 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -894,6 +894,38 @@ static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu,
 					   cmds->num, true);
 }
 
+static int arm_smmu_drain_queue(struct arm_smmu_device *smmu,
+				struct arm_smmu_queue *q,
+				bool until_empty)
+{
+	struct arm_smmu_queue_poll qp;
+	u32 snapshot_prod, cons;
+	int ret = 0;
+
+	queue_poll_init(smmu, &qp);
+
+	/* Secondary CMDQs don't support WFE*/
+	qp.wfe &= (q == &smmu->cmdq.q);
+
+	snapshot_prod = readl_relaxed(q->prod_reg);
+
+	do {
+		u32 target_prod;
+
+		cons = readl_relaxed(q->cons_reg);
+		target_prod = until_empty ? readl_relaxed(q->prod_reg) : snapshot_prod;
+
+		if (__queue_empty(&q->llq, cons, target_prod) ||
+		    __queue_consumed(&q->llq, cons, target_prod))
+			return 0;
+
+		cond_resched();
+	} while (!(ret = queue_poll(&qp)));
+
+	dev_warn_ratelimited(smmu->dev,
+			     "queue drain timed out at prod=0x%x cons=0x%x\n",
+			     readl_relaxed(q->prod_reg), cons);
+	return ret;
+}
+
 static void arm_smmu_page_response(struct device *dev, struct iopf_fault *unused,
 				   struct iommu_page_response *resp)
 {
--

Thanks,
Praan