iommu/arm-smmu-v3: Implement Runtime/System Sleep ops
Subsystems (4)
v7 cover iommu/arm-smmu-v3: Implement Runtime/System Sleep ops
As arm-smmu-v3 rapidly finds its way into SoCs designed for hand-held devices, power management capabilities, similar to its predecessors, are crucial for these applications. This series introduces power management support for the arm-smmu-v3 driver.
Design
======
The arm-smmu-v3 primarily operates with in-memory data structures through HW registers pointing to these data structures. The proposed design makes use of this fact for implementing suspend and resume ops, centered around a software gate embedded in the command queue.
1. CMDQ Gate (CMDQ_PROD_STOP_FLAG)
To safely manage runtime PM without regressing performance on high core count servers or systems not opting for runtime power management, this series introduces a CMDQ_PROD_STOP_FLAG (bit 30) in the command queue's producer index. The flag acts as a Point of Commitment in the cmpxchg loop of arm_smmu_cmdq_issue_cmdlist(), ensuring no new indices are reserved once suspension begins.
2. Suspend / Resume Flow
The "suspend" operation follows a multi-stage quiesce sequence:
a. Stop Traffic: Sets SMMUEN=0 and GBPA=Abort to halt new transactions. b. Gate CMDQ: Sets the CMDQ_PROD_STOP_FLAG to block new submissions. c. Command Flush: Waits for any in-flight "owner" threads to commit
their reserved indices to hardware.
d. HW Drain: Polls the CMDQ until all committed commands are consumed. e. SW Quiesce: Waits for all concurrent threads to release the shared
cmdq->lock, ensuring no CPUs are left polling CONS register.
Entering the suspend sequence implies that the device has no active clients Any racing command submissions or failure to quiesce at this stage indicate a bug in the Runtime PM or device link dependencies. Such races should not happen in practice, which justifies the non-failing nature of the suspend sequence in favor of memory safety.
The "resume" operation clears the STOP_FLAG & performs a full device reset via arm_smmu_device_reset(), which re-initializes the HW using the SW-copies maintained by the driver and clears all cached configurations.
3. Guarding Hardware Access and Elision
The driver ensures the SMMU is active before hardware access via arm_smmu_rpm_get() and arm_smmu_rpm_put() helpers. Commands are elided if the SMMU is suspended by observing the CMDQ_PROD_STOP_FLAG (via arm_smmu_can_elide()). For ATC invalidations, devlinks must guarantee the SMMU is active if the endpoint is active, a WARN_ON_ONCE() catches inconsistencies. Elision is safe because hardware reset on resume invalidates caches, and PCIe device links handle power dependencies for ATC.
4. Interrupt Re-config
a. Wired irqs: The series refactors arm_smmu_setup_irqs to allow separate installation of handlers, aiding in correct re-initialization.
b. MSIs: The series caches the msi_msg and restores it during resume via a new arm_smmu_resume_msis() helper.
c. GERROR: Late-breaking global errors are captured and handled immediately after SMMU disablement during suspend to ensure no diagnostic information is lost.
Scalability and Performance
===========================
A key design goal of this series is to ensure that high-performance systems (typically servers) that do not enable runtime PM are not penalized. By embedding a stop flag in the command queue's producer index and designing RPM helpers to perform only read-only checks when RPM is disabled, command submission on these systems incurs negligible overhead. Power-managed systems only utilize runtime PM atomics as necessary, ensuring that scalability is maintained across all hardware classes.
Call for review
===============
Any insights/comments on the proposed changes are appreciated, especially regarding the synchronization & elision logic.
[v7]
- Rebased on the latest arm/smmu/updates branch (which has the removal of struct arm_smmu_cmdq_ent merged)
- Converted manual cmpxchg loops in suspend/resume to use atomics
- Re-worked to elide invalidations solely based on the CMDQ_PROD_STOP_FLAG via arm_smmu_can_elide(), dropping any need for pm_runtime_get_if_active
- Added an smp_mb() fence in the reset sequence to ensure that the SMMU acquires all RAM updates made by newly un-gated threads before SMMUEN=1
- Implemented bitwise masking for the PROD register to prevent software metadata (STOP_FLAG) bits from being written to physical hardware.
- Introduced a KUnit test suite to verify the CMDQ gating algorithm
[v6]
- https://lore.kernel.org/all/[email protected]/
- Replaced the atomic nr_cmdq_users counter with CMDQ_PROD_STOP_FLAG to eliminate atomic overhead on high-core count servers.
- Implemented a 5-step quiesce sequence in runtime_suspend including pipeline flushes and software completion barriers.
- Introduced arm_smmu_rpm_get_if_active() to elide TLB/CFG/ATC invalidations when the SMMU is suspended.
- Added WARN_ON_ONCE() in invalidation paths to detect inconsistent power states for active endpoints.
- Refined batch submission in __arm_smmu_domain_inv_range() to ensure clean state when dropping batches.
- Refactored GERROR handling for better integration with suspend.
- Added Suggested-by tags for Daniel Mentz.
[v5]
- https://lore.kernel.org/all/[email protected]/
- Refactored GERROR handling into a helper function and invoked it during runtime suspend after disabling the SMMU to capture any late-breaking gerrors as suggested by Jason.
- Updated arm_smmu_page_response to be power-state aware and drop page faults received while suspended.
- Included a patch from Ashish to correctly restore PROD and CONS indices for tegra241-cmdqv after a hardware reset.
- Collected Reviewed-bys from Mostafa and Nicolin.
[v4]
- https://lore.kernel.org/all/[email protected]/
- Dropped the pm_runtime_get_if_not_suspended() API in favor of a simpler, driver-specific biased counter (nr_cmdq_users) to manage runtime PM state.
- Reworked the suspend callback to poll on the biased counter before disabling the SMMU.
- Addressed comments for the MSI refactor.
[v3]
- https://lore.kernel.org/all/[email protected]/
- Introduced pm_runtime_get_if_not_suspended API to avoid races due to bouncing RPM states while eliding TLBIs as pointed out by Daniel.
- Addressed Nicolin's comments regarding msi_resume and CMDQV flush
- Addressed Daniel's comments about CMDQ locking and draining
- Addressed issues related to draining the evtq and priq
- Dropped the code to identify and track user-space attachments
[v2]
- https://lore.kernel.org/all/[email protected]/
- Introduced arm_smmu_rpm_get_if_active for eliding TLBIs & CFGIs
- Updated the rpm helper invocation strategy.
- Drained all queues in suspend callback (including tegra241-cmdv)
- Cache and restore msi_msg instead of free-ing realloc-ing on resume
- Added support to identify and track user-space attachments
- Fixed the setup_irqs as per Nicolin & Mostafa's suggestions
- Used force_runtime_suspend/resume instead as per Mostafa's suggestion.
- Added "Reviewed-by" line from Mostafa on an unchanged patch
[v1]
- https://lore.kernel.org/all/[email protected]/
Ashish Mhetre (1):
iommu/tegra241-cmdqv: Restore PROD and CONS after resume
Pranjal Shrivastava (10):
iommu/arm-smmu-v3: Refactor arm_smmu_setup_irqs iommu/arm-smmu-v3: Add a helper to drain cmd queues iommu/tegra241-cmdqv: Add a helper to drain VCMDQs iommu/arm-smmu-v3: Cache and restore MSI config iommu/arm-smmu-v3: Handle gerror during suspend iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops iommu/arm-smmu-v3: Enable pm_runtime and setup devlinks iommu/arm-smmu-v3: Invoke pm_runtime before hw access iommu/arm-smmu-v3: Add KUnit unit tests for Runtime PM
.../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 15 +- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 150 ++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 445 ++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 27 ++ .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 29 ++ 5 files changed, 636 insertions(+), 30 deletions(-)
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 1/11 iommu/arm-smmu-v3: Refactor arm_smmu_setup_irqs
Refactor arm_smmu_setup_irqs by splitting it into two parts, one for registering interrupt handlers and the other one for enabling interrupt generation in the hardware. This refactor helps in re-initialization of hardware interrupts as part of a subsequent patch that enables runtime power management for the arm-smmu-v3 driver.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 47 +++++++++++++-------- 1 file changed, 30 insertions(+), 17 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +30 -17
@@ -4601,14 +4601,32 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu) } } +static void arm_smmu_enable_irqs (struct arm_smmu_device *smmu) +{ +int ret; +u32 irqen_flags =IRQ_CTRL_EVTQ_IRQEN |IRQ_CTRL_GERROR_IRQEN ; + +if (smmu->features &ARM_SMMU_FEAT_PRI ) + irqen_flags |=IRQ_CTRL_PRIQ_IRQEN ; + + ret =arm_smmu_write_reg_sync (smmu, irqen_flags, +ARM_SMMU_IRQ_CTRL ,ARM_SMMU_IRQ_CTRLACK ); +if (ret) +dev_warn (smmu->dev,"failed to enable irqs\n" ); +} + +static int arm_smmu_disable_irqs (struct arm_smmu_device *smmu) +{ +return arm_smmu_write_reg_sync (smmu,0 ,ARM_SMMU_IRQ_CTRL , +ARM_SMMU_IRQ_CTRLACK ); +} +static int arm_smmu_setup_irqs (struct arm_smmu_device *smmu) {int ret, irq; -u32 irqen_flags =IRQ_CTRL_EVTQ_IRQEN |IRQ_CTRL_GERROR_IRQEN ;/* Disable IRQs first */ - ret =arm_smmu_write_reg_sync (smmu,0 ,ARM_SMMU_IRQ_CTRL , -ARM_SMMU_IRQ_CTRLACK ); + ret =arm_smmu_disable_irqs (smmu);if (ret) {dev_err (smmu->dev,"failed to disable irqs\n" );return ret;
@@ -4630,15 +4648,6 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu) }else arm_smmu_setup_unique_irqs (smmu); -if (smmu->features &ARM_SMMU_FEAT_PRI ) - irqen_flags |=IRQ_CTRL_PRIQ_IRQEN ; - -/* Enable interrupt generation on the SMMU */ - ret =arm_smmu_write_reg_sync (smmu, irqen_flags, -ARM_SMMU_IRQ_CTRL ,ARM_SMMU_IRQ_CTRLACK ); -if (ret) -dev_warn (smmu->dev,"failed to enable irqs\n" ); -return 0 ; }
@@ -4779,11 +4788,8 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu) } } - ret =arm_smmu_setup_irqs (smmu); -if (ret) { -dev_err (smmu->dev,"failed to setup irqs\n" ); -return ret; - } +/* Enable interrupt generation on the SMMU */ +arm_smmu_enable_irqs (smmu);if (is_kdump_kernel ()) enables &= ~(CR0_EVTQEN |CR0_PRIQEN );
@@ -5417,6 +5423,13 @@ static int arm_smmu_device_probe(struct platform_device *pdev)/* Check for RMRs and install bypass STEs if any */ arm_smmu_rmr_install_bypass_ste (smmu); +/* Setup interrupt handlers */ + ret =arm_smmu_setup_irqs (smmu); +if (ret) { +dev_err (smmu->dev,"failed to setup irqs\n" ); + goto err_free_iopf; + } +/* Reset the device */ ret =arm_smmu_device_reset (smmu);if (ret)
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 2/11 iommu/arm-smmu-v3: Add a helper to drain cmd queues
In reply to Pranjal ShrivastavaBefore we suspend SMMU, we want to ensure that all commands (especially ATC_INV) have been flushed by the CMDQ, i.e. the CMDQs are empty. Add a helper function that polls till the queues are dained.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 34 +++++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 ++ 2 files changed, 37 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +34 -0
@@ -884,6 +884,40 @@ static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, cmds->num, true); } +int arm_smmu_queue_poll_until_empty (struct arm_smmu_device *smmu, +struct arm_smmu_queue *q) +{ +struct arm_smmu_queue_poll qp; +struct arm_smmu_ll_queue *llq = &q->llq; +int ret =0 ; + +queue_poll_init (smmu, &qp); +do { +if (queue_empty (llq)) +break ; + + ret =queue_poll (&qp); +WRITE_ONCE (llq->cons,readl_relaxed (q->cons_reg)); + + }while (!ret); + +return ret; +} + +static int arm_smmu_drain_queues (struct arm_smmu_device *smmu) +{ +int ret; + +/* +* Since this is only called from the suspend callback where +* exclusive access is ensured as CMDQ_PROD_STOP_FLAG blocks new +* command submissions to the cmdq, we can skip the cmdq locking. +*/ + ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->cmdq.q); + +return ret; +} +static void arm_smmu_page_response (struct device *dev,struct iopf_fault *unused,struct iommu_page_response *resp) {
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +3 -0
@@ -1165,6 +1165,9 @@ int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,int arm_smmu_cmdq_init (struct arm_smmu_device *smmu,struct arm_smmu_cmdq *cmdq); +int arm_smmu_queue_poll_until_empty (struct arm_smmu_device *smmu, +struct arm_smmu_queue *q); +static inline bool arm_smmu_master_canwbs (struct arm_smmu_master *master) { return dev_iommu_fwspec_get(master->dev)->flags &
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 02/11] iommu/arm-smmu-v3: Add a helper to drain cmd queues
Quoted 1 line · Pranjal Shrivastava
+static int
arm_smmu_drain_queues(struct arm_smmu_device *smmu)
So, this function has a very generic name for queues but its use case is limited to cmdq in a suspend context, and might not work for other queues.
I am adding another helper for in-flight evetq and priq:
- Drain a queue for a detaching master; other attached masters might still advance the prod, so queue wouldn't be empty.
- evtq and priq sets wfe=false.
So, there is a naming conflict here.
Maybe:
arm_smmu_drain_queue_for_cmdq()
arm_smmu_drain_queue_for_iopf()
?
Nicolin
Re: [PATCH v7 02/11] iommu/arm-smmu-v3: Add a helper to drain cmd queues
Quoted 12 lines · Nicolin Chen
Quoted 1 line · Pranjal Shrivastava
+static int
arm_smmu_drain_queues(struct arm_smmu_device *smmu)So, this function has a very generic name for queues but its use case is limited to cmdq in a suspend context, and might not work for other queues.
I am adding another helper for in-flight evetq and priq:
- Drain a queue for a detaching master; other attached masters might still advance the prod, so queue wouldn't be empty.
- evtq and priq sets wfe=false.
Quoted 6 lines
So, there is a naming conflict here.
Maybe:
arm_smmu_drain_queue_for_cmdq()
arm_smmu_drain_queue_for_iopf()
?
Ack. Sorry about that, the earlier versions were draining the other queues too but that has changed from the last few versions.
I'm planning to rename it to arm_smmu_drain_cmdq(), sounds good?
Thanks,
Praan
Re: [PATCH v7 02/11] iommu/arm-smmu-v3: Add a helper to drain cmd queues
Quoted 20 lines · Pranjal Shrivastava
Quoted 12 lines · Nicolin Chen
Quoted 1 line · Pranjal Shrivastava
+static int
arm_smmu_drain_queues(struct arm_smmu_device *smmu)So, this function has a very generic name for queues but its use case is limited to cmdq in a suspend context, and might not work for other queues.
I am adding another helper for in-flight evetq and priq:
- Drain a queue for a detaching master; other attached masters might still advance the prod, so queue wouldn't be empty.
- evtq and priq sets wfe=false.Quoted 6 lines
So, there is a naming conflict here.
Maybe:
arm_smmu_drain_queue_for_cmdq()
arm_smmu_drain_queue_for_iopf()
?
Quoted 4 lines
Ack. Sorry about that, the earlier versions were draining the other queues too but that has changed from the last few versions.
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
Do you know if my implementation can work for cmdq too? Seems that only the WFE would be different, which can be a bool?
Nicolin
Re: [PATCH v7 02/11] iommu/arm-smmu-v3: Add a helper to drain cmd queues
Quoted 9 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
Quoted 1 line · Nicolin Chen
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.
Quoted 3 lines
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.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +34 -0
@@ -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
PATCH v7 3/11 iommu/tegra241-cmdqv: Add a helper to drain VCMDQs
In reply to Pranjal ShrivastavaThe tegra241-cmdqv driver supports vCMDQs which need to be drained before suspending the SMMU. The current driver implementation only uses VINTF0 for vCMDQs owned by the kernel which need to be drained. Add a helper that drains all the enabled vCMDQs under VINTF0.
Add another function ptr to arm_smmu_impl_ops to drain implementation specified queues and call it within arm_smmu_drain_queues.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 7 +++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 27 +++++++++++++++++++ 3 files changed, 35 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -0
@@ -915,6 +915,13 @@ static int arm_smmu_drain_queues(struct arm_smmu_device *smmu)*/ ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->cmdq.q); +if (ret) + goto out; + +/* Drain all implementation-specific queues */ +if (smmu->impl_ops && smmu->impl_ops->drain_queues) + ret = smmu->impl_ops->drain_queues (smmu); +out:return ret; }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -0
@@ -885,6 +885,7 @@ struct arm_smmu_impl_ops {size_t (*get_viommu_size)(enum iommu_viommu_type viommu_type);int (*vsmmu_init)(struct arm_vsmmu *vsmmu,const struct iommu_user_data *user_data); +int (*drain_queues)(struct arm_smmu_device *smmu); };/* An SMMUv3 instance */
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +27 -0
@@ -414,6 +414,32 @@ tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu,return &vcmdq->cmdq; } +static int tegra241_cmdqv_drain_vintf0_lvcmdqs (struct arm_smmu_device *smmu) +{ +struct tegra241_cmdqv *cmdqv = + container_of(smmu, struct tegra241_cmdqv, smmu); +struct tegra241_vintf *vintf = cmdqv->vintfs[0 ]; +int ret =0 ; +u16 lidx; + +/* Kernel only uses VINTF0. Return if it's disabled */ +if (!READ_ONCE (vintf->enabled)) +return 0 ; + +for (lidx =0 ; lidx < cmdqv->num_lvcmdqs_per_vintf; lidx++) { +struct tegra241_vcmdq *vcmdq = vintf->lvcmdqs[lidx]; + +if (!vcmdq || !READ_ONCE (vcmdq->enabled)) +continue ; + + ret =arm_smmu_queue_poll_until_empty (smmu, &vcmdq->cmdq.q); +if (ret) +break ; + } + +return ret; +} +/* HW Reset Functions */ /*
@@ -845,6 +871,7 @@ static struct arm_smmu_impl_ops tegra241_cmdqv_impl_ops = { .get_secondary_cmdq = tegra241_cmdqv_get_cmdq, .device_reset = tegra241_cmdqv_hw_reset, .device_remove = tegra241_cmdqv_remove, + .drain_queues = tegra241_cmdqv_drain_vintf0_lvcmdqs,/* For user-space use */ .hw_info = tegra241_cmdqv_hw_info, .get_viommu_size = tegra241_cmdqv_get_vintf_size,
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 4/11 iommu/tegra241-cmdqv: Restore PROD and CONS after resume
In reply to Pranjal ShrivastavaFrom: Ashish Mhetre <[email protected]>
PROD and CONS indices for vcmdqs are getting set to 0 after resume. Because of this the vcmdq is not consuming commands after resume. Fix this by restoring PROD and CONS indices after resume from saved pointers.
--- drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +2 -0
@@ -511,6 +511,8 @@ static int tegra241_vcmdq_hw_init(struct tegra241_vcmdq *vcmdq)/* Configure and enable VCMDQ */ writeq_relaxed (vcmdq->cmdq.q.q_base,REG_VCMDQ_PAGE1 (vcmdq,BASE )); +writel_relaxed (vcmdq->cmdq.q.llq.prod,REG_VCMDQ_PAGE0 (vcmdq,PROD )); +writel_relaxed (vcmdq->cmdq.q.llq.cons,REG_VCMDQ_PAGE0 (vcmdq,CONS )); ret =vcmdq_write_config (vcmdq,VCMDQ_EN );if (ret) {
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 04/11] iommu/tegra241-cmdqv: Restore PROD and CONS after resume
Quoted 9 lines · Pranjal Shrivastava
From: Ashish Mhetre <[email protected]>
PROD and CONS indices for vcmdqs are getting set to 0 after resume. Because of this the vcmdq is not consuming commands after resume. Fix this by restoring PROD and CONS indices after resume from saved pointers.
Signed-off-by Ashish Mhetre <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>
PATCH v7 5/11 iommu/arm-smmu-v3: Cache and restore MSI config
In reply to Pranjal ShrivastavaThe SMMU's MSI configuration registers (*_IRQ_CFGn) containing target address, data and memory attributes lose their state when the SMMU is powered down. We'll need to cache and restore their contents to ensure that MSIs work after the system resumes.
To address this, cache the original msi_msg within the msi_desc when the configuration is first written by arm_smmu_write_msi_msg. This primarily includes the target address and data since the memory attributes are fixed.
Introduce a new helper arm_smmu_resume_msis which will later be called during the driver's resume callback. The helper would retrieve the cached MSI message for each relevant interrupt (evtq, gerr, priq) via get_cached_msi_msg & re-config the registers via arm_smmu_write_msi_msg.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +37 -0
@@ -4551,6 +4551,9 @@ static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)struct arm_smmu_device *smmu =dev_get_drvdata (dev);phys_addr_t *cfg = arm_smmu_msi_cfg[desc->msi_index]; +/* Cache the msi_msg for resume */ + desc->msg = *msg; + doorbell = (((u64 )msg->address_hi) <<32 ) | msg->address_lo; doorbell &=MSI_CFG0_ADDR_MASK ;
@@ -4559,6 +4562,40 @@ static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)writel_relaxed (ARM_SMMU_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2 ]); } +static void arm_smmu_resume_msi (struct arm_smmu_device *smmu, +unsigned int irq,const char *name) +{ +struct msi_desc *desc; +struct msi_msg msg; + +if (!irq) +return ; + + desc =irq_get_msi_desc (irq); +if (!desc) { +dev_err (smmu->dev,"Failed to resume msi: %s" , name); +return ; + } + +get_cached_msi_msg (irq, &msg); +arm_smmu_write_msi_msg (desc, &msg); +} + +static void arm_smmu_resume_msis (struct arm_smmu_device *smmu) +{ +if (!(smmu->features &ARM_SMMU_FEAT_MSI )) +return ; + +if (!dev_get_msi_domain (smmu->dev)) +return ; + +arm_smmu_resume_msi (smmu, smmu->gerr_irq,"gerror" ); +arm_smmu_resume_msi (smmu, smmu->evtq.q.irq,"evtq" ); + +if (smmu->features &ARM_SMMU_FEAT_PRI ) +arm_smmu_resume_msi (smmu, smmu->priq.q.irq,"priq" ); +} +static void arm_smmu_setup_msis (struct arm_smmu_device *smmu) {int ret, nvec =ARM_SMMU_MAX_MSIS ;
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 05/11] iommu/arm-smmu-v3: Cache and restore MSI config
Quoted 10 lines · Pranjal Shrivastava
The SMMU's MSI configuration registers (
*_IRQ_CFGn) containing target address, data and memory attributes lose their state when the SMMU is powered down. We'll need to cache and restore their contents to ensure that MSIs work after the system resumes.To address this, cache the original
msi_msgwithin themsi_descwhen the configuration is first written byarm_smmu_write_msi_msg. This primarily includes the target address and data since the memory attributes are fixed.
Quoted 6 lines
Introduce a new helper
arm_smmu_resume_msiswhich will later be called during the driver's resume callback. The helper would retrieve the cached MSI message for each relevant interrupt (evtq, gerr, priq) viaget_cached_msi_msg& re-config the registers viaarm_smmu_write_msi_msg.Signed-off-by Pranjal Shrivastava <[email protected]>
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+static void arm_smmu_resume_msi (struct arm_smmu_device *smmu, +unsigned int irq,const char *name) +{ +struct msi_desc *desc; +struct msi_msg msg; +
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) · line 4580
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+if (!irq) +return ; + + desc =irq_get_msi_desc (irq); +if (!desc) { +dev_err (smmu->dev,"Failed to resume msi: %s" , name); +return ; + } + +get_cached_msi_msg (irq, &msg);
I wonder if we could use __get_cached_msi_msg() since we got desc?
Re: [PATCH v7 05/11] iommu/arm-smmu-v3: Cache and restore MSI config
Quoted 30 lines · Nicolin Chen
Quoted 10 lines · Pranjal Shrivastava
The SMMU's MSI configuration registers (
*_IRQ_CFGn) containing target address, data and memory attributes lose their state when the SMMU is powered down. We'll need to cache and restore their contents to ensure that MSIs work after the system resumes.To address this, cache the original
msi_msgwithin themsi_descwhen the configuration is first written byarm_smmu_write_msi_msg. This primarily includes the target address and data since the memory attributes are fixed.Quoted 6 lines
Introduce a new helper
arm_smmu_resume_msiswhich will later be called during the driver's resume callback. The helper would retrieve the cached MSI message for each relevant interrupt (evtq, gerr, priq) viaget_cached_msi_msg& re-config the registers viaarm_smmu_write_msi_msg.Signed-off-by Pranjal Shrivastava <[email protected]>Reviewed-by Nicolin Chen <[email protected]>Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+static void arm_smmu_resume_msi (struct arm_smmu_device *smmu, +unsigned int irq,const char *name) +{ +struct msi_desc *desc; +struct msi_msg msg; + +if (!irq) +return ; + + desc =irq_get_msi_desc (irq);
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) · line 4580
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+if (!desc) { +dev_err (smmu->dev,"Failed to resume msi: %s" , name); +return ; + } + +get_cached_msi_msg (irq, &msg);I wonder if we could use
__get_cached_msi_msg()since we got desc?
We could've (and that's a nice suggestion), but unfortunately it (__get_cached_msi_msg) is not exported via EXPORT_SYMBOL_GPL() :( We could break modular builds as CONFIG_ARM_SMMU_V3 is tristate.
Let me know if you have a strong preference about exporting it, we can add a patch in the next versions.
Thanks,
Praan
Re: [PATCH v7 05/11] iommu/arm-smmu-v3: Cache and restore MSI config
Quoted 6 lines · Pranjal Shrivastava
Quoted 4 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) · line 4580
Quoted 1 line · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0+get_cached_msi_msg (irq, &msg);I wonder if we could use
__get_cached_msi_msg()since we got desc?
Quoted 6 lines
We could've (and that's a nice suggestion), but unfortunately it (
__get_cached_msi_msg) is not exported viaEXPORT_SYMBOL_GPL():( We could break modular builds asCONFIG_ARM_SMMU_V3is tristate.Let me know if you have a strong preference about exporting it, we can add a patch in the next versions.
Not necessary. I see all drivers use get_cached_msi_msg() too.
Nicolin
PATCH v7 6/11 iommu/arm-smmu-v3: Handle gerror during suspend
In reply to Pranjal ShrivastavaThe GERROR register's state might be lost when the SMMU is powered down during runtime suspend. Handle any pending errors before suspending.
Refactor the gerror handling logic into a helper function and invoke it from the runtime suspend callback after disabling the SMMU. This ensures that any late-breaking gerrors are logged and ack'ed before the hardware state is lost.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9 -2
@@ -2257,10 +2257,9 @@ static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)static int arm_smmu_device_disable (struct arm_smmu_device *smmu); -static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) +static irqreturn_t arm_smmu_handle_gerror (struct arm_smmu_device *smmu) {u32 gerror, gerrorn, active; -struct arm_smmu_device *smmu = dev; gerror =readl_relaxed (smmu->base +ARM_SMMU_GERROR ); gerrorn =readl_relaxed (smmu->base +ARM_SMMU_GERRORN );
@@ -2300,9 +2299,17 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)arm_smmu_cmdq_skip_err (smmu);writel (gerror, smmu->base +ARM_SMMU_GERRORN ); +return IRQ_HANDLED ; } +static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) +{ +struct arm_smmu_device *smmu = dev; + +return arm_smmu_handle_gerror (smmu); +} +static irqreturn_t arm_smmu_combined_irq_thread (int irq,void *dev) {struct arm_smmu_device *smmu = dev;
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 06/11] iommu/arm-smmu-v3: Handle gerror during suspend
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) · line 2260
Quoted 10 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-1@@ -2257,10 +2257,9 @@ static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)static int arm_smmu_device_disable (struct arm_smmu_device *smmu); -static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) +static irqreturn_t arm_smmu_handle_gerror (struct arm_smmu_device *smmu)
We don't need a lock because the new caller would disable the IRQ.
Yet, maybe add a line of comments:
/* Lockless; must ensure that there are no concurrent callers */
Otherwise,
Re: [PATCH v7 06/11] iommu/arm-smmu-v3: Handle gerror during suspend
Quoted 9 lines · Nicolin Chen
Quoted 8 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0@@ -2257,10 +2257,9 @@ static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)static int arm_smmu_device_disable (struct arm_smmu_device *smmu);
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) · line 2260
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-1-static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) +static irqreturn_t arm_smmu_handle_gerror (struct arm_smmu_device *smmu)We don't need a lock because the new caller would disable the IRQ.
Yet, maybe add a line of comments:
/* Lockless; must ensure that there are no concurrent callers */
Ack, I'll add the comment.
Quoted 3 lines
Otherwise,
Reviewed-by Nicolin Chen <[email protected]>
Thanks!
Praan
PATCH v7 7/11 iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
In reply to Pranjal ShrivastavaIntroduce a new bit flag, CMDQ_PROD_STOP_FLAG (bit 30), in the command queue's producer index to safely gate command submissions during device suspension.
The flag embeds the suspend state directly into the existing global state The flag checked in the compxchg loop in arm_smmu_cmdq_issue_cmdlist(), which acts as a Point of Commitment, ensuring that no indices are reserved or committed once the SMMU begins suspending.
This prevents a situation of "abandoned batches" where indices are incremented but commands are never written, which would otherwise lead to timeout during the drain poll.
Update queue_inc_prod_n() to preserve this flag during index calculations, ensuring that any in-flight commands that successfully passed the point of commitment can proceed to completion while the flag remains set.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 20 +++++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 ++ 2 files changed, 21 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +19 -1
@@ -210,7 +210,8 @@ static int queue_sync_prod_in(struct arm_smmu_queue *q)static u32 queue_inc_prod_n (struct arm_smmu_ll_queue *q,int n) {u32 prod = (Q_WRP (q, q->prod) |Q_IDX (q, q->prod)) + n; -return Q_OVF (q->prod) |Q_WRP (q, prod) |Q_IDX (q, prod); + +return Q_OVF (q->prod) |Q_STOP (q->prod) |Q_WRP (q, prod) |Q_IDX (q, prod); } static void queue_poll_init(struct arm_smmu_device *smmu,
@@ -718,8 +719,25 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, do { u64 old; + /* + * If the SMMU is suspended/suspending, any new CMDs are elided. + * This loop is the Point of Commitment. If we haven't cmpxchg'd + * our new indices yet, we can safely bail. Once the indices are + * committed, we MUST write valid commands to those slots to + * avoid indefinite polling in the drain function. + */ + if (Q_STOP(llq.prod)) { + local_irq_restore(flags); + return 0; + } + while (!queue_has_space(&llq, n + sync)) { local_irq_restore(flags); + + /* Avoid waiting for space if the SMMU is suspending */ + if (Q_STOP(READ_ONCE(cmdq->q.llq.prod))) + return 0; + if (arm_smmu_cmdq_poll_until_not_full(smmu, cmdq, &llq)) dev_err_ratelimited(smmu->dev, "CMDQ timeout\n"); local_irq_save(flags);
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +2 -0
@@ -389,6 +389,8 @@ static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid)#define CMDQ_ERR_CERROR_ATC_INV_IDX 3#define CMDQ_PROD_OWNED_FLAG Q_OVERFLOW_FLAG +#define CMDQ_PROD_STOP_FLAG (1U << 30) +#define Q_STOP (p) ((p) & CMDQ_PROD_STOP_FLAG)struct arm_smmu_cmd {u64 data[CMDQ_ENT_DWORDS ];
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 07/11] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
Quoted 6 lines · Pranjal Shrivastava
Introduce a new bit flag,
CMDQ_PROD_STOP_FLAG(bit 30), in the command queue's producer index to safely gate command submissions during device suspension.The flag embeds the suspend state directly into the existing global state The flag checked in the compxchg loop in
arm_smmu_cmdq_issue_cmdlist(),
s/compxchg/cmpxchg
Quoted 7 lines
which acts as a Point of Commitment, ensuring that no indices are reserved or committed once the SMMU begins suspending.
This prevents a situation of "abandoned batches" where indices are incremented but commands are never written, which would otherwise lead to timeout during the drain poll.
Quoted 7 lines
Update
queue_inc_prod_n()to preserve this flag during index calculations, ensuring that any in-flight commands that successfully passed the point of commitment can proceed to completion while the flag remains set.Suggested-by Daniel Mentz <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>
Re: [PATCH v7 07/11] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
Quoted 9 lines · Nicolin Chen
Quoted 6 lines · Pranjal Shrivastava
Introduce a new bit flag,
CMDQ_PROD_STOP_FLAG(bit 30), in the command queue's producer index to safely gate command submissions during device suspension.The flag embeds the suspend state directly into the existing global state The flag checked in the compxchg loop in
arm_smmu_cmdq_issue_cmdlist(),s/compxchg/cmpxchg
Ack. I'll fix it.
Quoted 13 lines
Quoted 13 lines
which acts as a Point of Commitment, ensuring that no indices are reserved or committed once the SMMU begins suspending.
This prevents a situation of "abandoned batches" where indices are incremented but commands are never written, which would otherwise lead to timeout during the drain poll.
Update
queue_inc_prod_n()to preserve this flag during index calculations, ensuring that any in-flight commands that successfully passed the point of commitment can proceed to completion while the flag remains set.
Quoted 4 lines
Quoted 2 lines
Suggested-by Daniel Mentz <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>Reviewed-by Nicolin Chen <[email protected]>
Thanks!
Praan
PATCH v7 8/11 iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Pranjal ShrivastavaImplement pm_runtime and system sleep ops for arm-smmu-v3.
The suspend callback configures the SMMU to abort new transactions, disables the main translation unit and then drains the command queue to ensure completion of any in-flight commands. A software gate (STOP_FLAG) and synchronization barriers are used to quiesce the command submission pipeline and ensure state consistency before power-off.
To prevent software metadata flags from leaking into physical registers or polluting the tracking pointer, a newly introduced bitmask (CMDQ_PROD_IDX_MASK) is applied to all fast-path register writes and tracking updates, avoiding post-resume deadlocks.
The resume callback restores the MSI configuration and performs a full device reset via arm_smmu_device_reset to bring the SMMU back to an operational state. The MSIs are cached during the msi_write and are restored during the resume operation by using the helper. The STOP_FLAG is cleared only after the CMDQ is enabled in hardware.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 165 +++++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 18 +++ 2 files changed, 181 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +163 -2
@@ -28,6 +28,7 @@#include <linux/platform_device.h> #include <linux/sort.h> #include <linux/string_choices.h> +#include <linux/pm_runtime.h> #include <kunit/visibility.h> #include <uapi/linux/iommufd.h>
@@ -110,6 +111,38 @@ static const char * const event_class_str[] = {static int arm_smmu_alloc_cd_tables (struct arm_smmu_master *master);static bool arm_smmu_ats_supported (struct arm_smmu_master *master); +/* Runtime PM helpers */ +__maybe_unused static int arm_smmu_rpm_get (struct arm_smmu_device *smmu) +{ +int ret; + +if (pm_runtime_enabled (smmu->dev)) { + ret =pm_runtime_resume_and_get (smmu->dev); +if (ret <0 ) { +dev_err (smmu->dev,"failed to resume device: %d\n" , ret); +return ret; + } + } + +return 0 ; +} + +__maybe_unused static void arm_smmu_rpm_put (struct arm_smmu_device *smmu) +{ +int ret; + +if (pm_runtime_enabled (smmu->dev)) { + ret =pm_runtime_put_autosuspend (smmu->dev); +if (ret <0 ) +dev_err (smmu->dev,"failed to suspend device: %d\n" , ret); + } +} + +static inline u32 arm_smmu_cmdq_owner_prod_idx (struct arm_smmu_cmdq *cmdq) +{ +return atomic_read (&cmdq->owner_prod) &CMDQ_PROD_IDX_MASK ; +} +static void parse_driver_options (struct arm_smmu_device *smmu) {int i =0 ;
@@ -789,7 +822,8 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,/* b. Stop gathering work by clearing the owned flag */ prod =atomic_fetch_andnot_relaxed (CMDQ_PROD_OWNED_FLAG , &cmdq->q.llq.atomic.prod); - prod &= ~CMDQ_PROD_OWNED_FLAG ; +/* Strip all metadata flags */ + prod &=CMDQ_PROD_IDX_MASK ;/* * c. Wait for any gathered work to be written to the queue.
@@ -4827,7 +4861,8 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)/* Command queue */ writeq_relaxed (smmu->cmdq.q.q_base, smmu->base +ARM_SMMU_CMDQ_BASE ); -writel_relaxed (smmu->cmdq.q.llq.prod, smmu->base +ARM_SMMU_CMDQ_PROD ); +writel_relaxed (smmu->cmdq.q.llq.prod &CMDQ_PROD_IDX_MASK , + smmu->base +ARM_SMMU_CMDQ_PROD );writel_relaxed (smmu->cmdq.q.llq.cons, smmu->base +ARM_SMMU_CMDQ_CONS ); enables =CR0_CMDQEN ;
@@ -4838,6 +4873,10 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)return ret; } +/* Clear any flags from the previous life */ +atomic_andnot (CMDQ_PROD_STOP_FLAG , &smmu->cmdq.owner_prod); +atomic_andnot (CMDQ_PROD_STOP_FLAG , &smmu->cmdq.q.llq.atomic.prod); +/* Invalidate any cached configuration */ arm_smmu_cmdq_issue_cmd_with_sync (smmu,arm_smmu_make_cmd_cfgi_all ());
@@ -4897,6 +4936,21 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)if (is_kdump_kernel ()) enables &= ~(CR0_EVTQEN |CR0_PRIQEN ); +/* +* While the SMMU was suspended, concurrent CPU threads may have +* updated in-memory structures (such as STEs, CDs, and PTEs). +* Any invalidations corresponding to those updates were safely +* elided because the command queue was stopped (STOP_FLAG == 1). +* +* Since the reset invalidate-all commands above have fully cleared +* the HW TLBs and config caches, the SMMU will fetch these descriptors +* directly from RAM as soon as translation is enabled. +* +* Add a memory barrier to collect all prior RAM writes to ensure the +* SMMU sees a consistent view of memory before translation is enabled. +*/ +smp_mb (); +/* Enable the SMMU interface */ enables |=CR0_SMMUEN ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
@@ -5579,6 +5633,112 @@ static void arm_smmu_device_shutdown(struct platform_device *pdev)arm_smmu_device_disable (smmu); } +static int __maybe_unused arm_smmu_runtime_suspend (struct device *dev) +{ +struct arm_smmu_device *smmu =dev_get_drvdata (dev); +struct arm_smmu_cmdq *cmdq = &smmu->cmdq; +int timeout =ARM_SMMU_SUSPEND_TIMEOUT_US ; +u32 enables, target; +int ret; + +/* Abort all transactions before disable to avoid spurious bypass */ +arm_smmu_update_gbpa (smmu,GBPA_ABORT ,0 ); + +/* Disable the SMMU via CR0.EN and all queues except CMDQ */ + enables =CR0_CMDQEN ; + ret =arm_smmu_write_reg_sync (smmu, enables,ARM_SMMU_CR0 ,ARM_SMMU_CR0ACK ); +if (ret) { +dev_err (smmu->dev,"failed to disable SMMU\n" ); +return ret; + } + +/* +* At this point the SMMU is completely disabled and won't access +* any translation/config structures, even speculative accesses +* aren't performed as per the IHI0070 spec (section 6.3.9.6). +*/ + +/* Mark the CMDQ to stop and get the target index before the stop */ + target =atomic_fetch_or_relaxed (CMDQ_PROD_STOP_FLAG , &cmdq->q.llq.atomic.prod); + target &=CMDQ_PROD_IDX_MASK ; + + +/* Wait for the last committed owner to reach the hardware */ +while ((arm_smmu_cmdq_owner_prod_idx (cmdq) != target) && --timeout) +udelay (1 ); + +/* +* Entering suspend implies no active clients. A timeout here +* indicates a fatal CMDQ lockup or hardware stall. We proceed +* anyway to prioritize memory safety (avoiding stale TLBs) +*/ +if (!timeout) +dev_err (smmu->dev,"cmdq owner wait timeout, (check runtime PM + devlinks)\n" ); + +/* Drain the CMDQs */ + ret =arm_smmu_drain_queues (smmu); +if (ret) +dev_warn (smmu->dev,"failed to drain queues, forcing suspend\n" ); + +/* Wait for cmdq->lock == 0 to ensure last CMDQ_CONS_REG is written */ + timeout =ARM_SMMU_SUSPEND_TIMEOUT_US ; +while (atomic_read (&cmdq->lock) !=0 && --timeout) +udelay (1 ); + +/* Timing out here implies misconfigured Runtime PM or broken devlinks */ +if (!timeout) +dev_err (smmu->dev,"cmdq lock != 0, forcing suspend. Polling CPUs may fault.\n" ); + +/* Disable everything */ +arm_smmu_device_disable (smmu); + +/* Handle any pending gerrors before powering down */ +arm_smmu_handle_gerror (smmu); + +dev_dbg (dev,"suspended smmu\n" ); + +return 0 ; +} + +static int __maybe_unused arm_smmu_runtime_resume (struct device *dev) +{ +struct arm_smmu_device *smmu =dev_get_drvdata (dev); +int ret; + +/* Re-configure MSIs */ +arm_smmu_resume_msis (smmu); + + ret =arm_smmu_device_reset (smmu); +if (ret) +dev_err (dev,"failed to reset during resume operation: %d\n" , ret); + +dev_dbg (dev,"resumed smmu\n" ); + +return ret; +} + +static int __maybe_unused arm_smmu_pm_suspend (struct device *dev) +{ +if (pm_runtime_suspended (dev)) +return 0 ; + +return arm_smmu_runtime_suspend (dev); +} + +static int __maybe_unused arm_smmu_pm_resume (struct device *dev) +{ +if (pm_runtime_suspended (dev)) +return 0 ; + +return arm_smmu_runtime_resume (dev); +} + +static const struct dev_pm_ops arm_smmu_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend, arm_smmu_pm_resume) +SET_RUNTIME_PM_OPS (arm_smmu_runtime_suspend, + arm_smmu_runtime_resume,NULL ) +}; +static const struct of_device_id arm_smmu_of_match[] = { { .compatible ="arm,smmu-v3" , }, { },
@@ -5595,6 +5755,7 @@ static struct platform_driver arm_smmu_driver = { .driver = { .name = "arm-smmu-v3", .of_match_table = arm_smmu_of_match, + .pm = &arm_smmu_pm_ops, .suppress_bind_attrs = true, }, .probe = arm_smmu_device_probe,
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +18 -0
@@ -392,6 +392,9 @@ static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid)#define CMDQ_PROD_STOP_FLAG (1U << 30)#define Q_STOP (p) ((p) & CMDQ_PROD_STOP_FLAG) +/* Mask out software-only metadata flags to get the pure queue index/wrap bits */ +#define CMDQ_PROD_IDX_MASK ~(CMDQ_PROD_STOP_FLAG | CMDQ_PROD_OWNED_FLAG) +struct arm_smmu_cmd {u64 data[CMDQ_ENT_DWORDS ]; };
@@ -655,11 +658,14 @@ arm_smmu_make_cmd_tlbi(enum arm_smmu_cmdq_opcode op, u16 asid, u16 vmid)/* High-level queue structures */ #define ARM_SMMU_POLL_TIMEOUT_US 1000000/* 1s! */ +#define ARM_SMMU_SUSPEND_TIMEOUT_US 1000000/* 1s! */ #define ARM_SMMU_POLL_SPIN_COUNT 10#define MSI_IOVA_BASE 0x8000000#define MSI_IOVA_LENGTH 0x100000 +#define RPM_AUTOSUSPEND_DELAY_MS 15 + struct arm_smmu_ll_queue { union { u64 val;
@@ -1217,6 +1223,18 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, struct arm_smmu_cmd *cmds, int n, bool sync); +/* +* Lockless pre-check to elide invalidations if SMMU is suspended. +* Races with concurrent suspend are benign: the cmpxchg loop in +* arm_smmu_cmdq_issue_cmdlist() acts as the true commit point. +* If we lose the race, that loop observes Q_STOP == 1 and safely +* drops the command. If we win, the suspend thread waits for us. +*/ +static inline bool arm_smmu_can_elide (struct arm_smmu_device *smmu) +{ +return !!Q_STOP (READ_ONCE (smmu->cmdq.q.llq.prod)); +} +#ifdef CONFIG_ARM_SMMU_V3_SVA bool arm_smmu_sva_supported (struct arm_smmu_device *smmu);void arm_smmu_sva_notifier_synchronize (void );
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 08/11] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
Quoted 4 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+/* Runtime PM helpers */ +__maybe_unused static int arm_smmu_rpm_get (struct arm_smmu_device *smmu) +{ +int ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static const char * const event_class_str[] = { · line 127
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+ +if (pm_runtime_enabled (smmu->dev)) { + ret =pm_runtime_resume_and_get (smmu->dev); +if (ret <0 ) { +dev_err (smmu->dev,"failed to resume device: %d\n" , ret); +return ret; + } + } + +return 0 ;
Nit: ret is used within the first if.
Yet, I wouldn't like it to move. So, maybe do an early return:
if (!pm_runtime_enabled(smmu->dev)) return 0;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static const char * const event_class_str[] = { · line 139
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+__maybe_unused static void arm_smmu_rpm_put (struct arm_smmu_device *smmu) +{ +int ret; + +if (pm_runtime_enabled (smmu->dev)) { + ret =pm_runtime_put_autosuspend (smmu->dev); +if (ret <0 ) +dev_err (smmu->dev,"failed to suspend device: %d\n" , ret); + } +}
Ditto
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+ +static inline u32 arm_smmu_cmdq_owner_prod_idx (struct arm_smmu_cmdq *cmdq) +{ +return atomic_read (&cmdq->owner_prod) &CMDQ_PROD_IDX_MASK ; +} +static void parse_driver_options (struct arm_smmu_device *smmu) {int i =0 ;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 826
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-1@@ -789,7 +822,8 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,/* b. Stop gathering work by clearing the owned flag */ prod =atomic_fetch_andnot_relaxed (CMDQ_PROD_OWNED_FLAG , &cmdq->q.llq.atomic.prod); - prod &= ~CMDQ_PROD_OWNED_FLAG ; +/* Strip all metadata flags */ + prod &=CMDQ_PROD_IDX_MASK ;
Should its prior atomic_fetch_andnot_relaxed() call do something about the CMDQ_PROD_STOP_FLAG as well?
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 1236
Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +11
-0+/* +* Lockless pre-check to elide invalidations if SMMU is suspended. +* Races with concurrent suspend are benign: the cmpxchg loop in +* arm_smmu_cmdq_issue_cmdlist() acts as the true commit point. +* If we lose the race, that loop observes Q_STOP == 1 and safely +* drops the command. If we win, the suspend thread waits for us. +*/ +static inline bool arm_smmu_can_elide (struct arm_smmu_device *smmu) +{ +return !!Q_STOP (READ_ONCE (smmu->cmdq.q.llq.prod)); +}
arm_smmu_cmdq_can_elide()
Should it handle a secondary_cmdq?
Nicolin
Re: [PATCH v7 08/11] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
Quoted 18 lines · Nicolin Chen
Quoted 4 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+/* Runtime PM helpers */ +__maybe_unused static int arm_smmu_rpm_get (struct arm_smmu_device *smmu) +{ +int ret;drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static const char * const event_class_str[] = { · line 127
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+ +if (pm_runtime_enabled (smmu->dev)) { + ret =pm_runtime_resume_and_get (smmu->dev); +if (ret <0 ) { +dev_err (smmu->dev,"failed to resume device: %d\n" , ret); +return ret; + } + } + +return 0 ;Nit: ret is used within the first if.
Quoted 5 lines
Yet, I wouldn't like it to move. So, maybe do an early return:
if (!pm_runtime_enabled(smmu->dev)) return 0;
Ack. I'll add an early return.
Quoted 4 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+__maybe_unused static void arm_smmu_rpm_put (struct arm_smmu_device *smmu) +{ +int ret; +
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static const char * const event_class_str[] = { · line 139
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+if (pm_runtime_enabled (smmu->dev)) { + ret =pm_runtime_put_autosuspend (smmu->dev); +if (ret <0 ) +dev_err (smmu->dev,"failed to suspend device: %d\n" , ret); + } +}Ditto
Same here.
Quoted 11 lines
Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+ +static inline u32 arm_smmu_cmdq_owner_prod_idx (struct arm_smmu_cmdq *cmdq) +{ +return atomic_read (&cmdq->owner_prod) &CMDQ_PROD_IDX_MASK ; +} +static void parse_driver_options (struct arm_smmu_device *smmu) {int i =0 ;@@ -789,7 +822,8 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,/* b. Stop gathering work by clearing the owned flag */
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 826
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-1prod =atomic_fetch_andnot_relaxed (CMDQ_PROD_OWNED_FLAG , &cmdq->q.llq.atomic.prod); - prod &= ~CMDQ_PROD_OWNED_FLAG ; +/* Strip all metadata flags */ + prod &=CMDQ_PROD_IDX_MASK ;Should its prior
atomic_fetch_andnot_relaxed()call do something about theCMDQ_PROD_STOP_FLAGas well?
Umm.. No, the atomic_fetch_andnot_relaxed() call must leave the STOP_FLAG. This block is the owner-publish phase, which occurs *after* the Point of Commitment. If a submission successfully reserved its indices before the gate closed, it shall be allowed to finish.
If the owner thread cleared the STOP_FLAG here in the global memory, it would prematurely re-open the gate, allowing new racing submissions to leak in during the suspend sequence.
The algorithm works mainly because the RPM callbacks are the only ones that are allowed to manipulate this flag.
Quoted 14 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 1236
Quoted 12 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +11
-0+/* +* Lockless pre-check to elide invalidations if SMMU is suspended. +* Races with concurrent suspend are benign: the cmpxchg loop in +* arm_smmu_cmdq_issue_cmdlist() acts as the true commit point. +* If we lose the race, that loop observes Q_STOP == 1 and safely +* drops the command. If we win, the suspend thread waits for us. +*/ +static inline bool arm_smmu_can_elide (struct arm_smmu_device *smmu) +{ +return !!Q_STOP (READ_ONCE (smmu->cmdq.q.llq.prod)); +}
arm_smmu_cmdq_can_elide()
Nice name! I'll update it.
Quoted 2 lines
Should it handle a
secondary_cmdq?
No, I don't think we need to check secondary queues here. The STOP_FLAG being set on the primary CMDQ during the suspend should suffice to indicate the entire SMMU's power state. That's why the function takes in the smmu ptr and not the cmdq.
Altough, thanks for discussing this, I noticed that while rebasing I missed the elision check at one place: arm_smmu_write_ste() I'll take add a check in that too in the next version.
Did you have another scenario / use-case in mind where this might not work?
Thanks,
Praan
Re: [PATCH v7 08/11] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
Quoted 18 lines · Pranjal Shrivastava
Quoted 11 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 826
Quoted 7 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-1@@ -789,7 +822,8 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,/* b. Stop gathering work by clearing the owned flag */ prod =atomic_fetch_andnot_relaxed (CMDQ_PROD_OWNED_FLAG , &cmdq->q.llq.atomic.prod); - prod &= ~CMDQ_PROD_OWNED_FLAG ; +/* Strip all metadata flags */ + prod &=CMDQ_PROD_IDX_MASK ;Should its prior
atomic_fetch_andnot_relaxed()call do something about theCMDQ_PROD_STOP_FLAGas well?Umm.. No, the
atomic_fetch_andnot_relaxed()call must leave theSTOP_FLAG. This block is the owner-publish phase, which occurs *after* the Point of Commitment. If a submission successfully reserved its indices before the gate closed, it shall be allowed to finish.
Quoted 6 lines
If the owner thread cleared the
STOP_FLAGhere in the global memory, it would prematurely re-open the gate, allowing new racing submissions to leak in during the suspend sequence.The algorithm works mainly because the RPM callbacks are the only ones that are allowed to manipulate this flag.
That reads plausible to me. It would be nicer to leave a brief note inline.
Quoted 6 lines
Quoted 1 line
Should it handle a
secondary_cmdq?No, I don't think we need to check secondary queues here. The
STOP_FLAGbeing set on the primary CMDQ during the suspend should suffice to indicate the entire SMMU's power state. That's why the function takes in the smmu ptr and not the cmdq.
But if we don't set STOP flag to the secondary_cmdq, wouldn't the drain() timeout on the secondary_cmdq that does not elide?
Nicolin
Re: [PATCH v7 08/11] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
Quoted 23 lines · Nicolin Chen
Quoted 22 lines · Pranjal Shrivastava
Quoted 11 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 826
Quoted 7 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-1@@ -789,7 +822,8 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,/* b. Stop gathering work by clearing the owned flag */ prod =atomic_fetch_andnot_relaxed (CMDQ_PROD_OWNED_FLAG , &cmdq->q.llq.atomic.prod); - prod &= ~CMDQ_PROD_OWNED_FLAG ; +/* Strip all metadata flags */ + prod &=CMDQ_PROD_IDX_MASK ;Should its prior
atomic_fetch_andnot_relaxed()call do something about theCMDQ_PROD_STOP_FLAGas well?Umm.. No, the
atomic_fetch_andnot_relaxed()call must leave theSTOP_FLAG. This block is the owner-publish phase, which occurs *after* the Point of Commitment. If a submission successfully reserved its indices before the gate closed, it shall be allowed to finish.If the owner thread cleared the
STOP_FLAGhere in the global memory, it would prematurely re-open the gate, allowing new racing submissions to leak in during the suspend sequence.
Quoted 5 lines
Quoted 2 lines
The algorithm works mainly because the RPM callbacks are the only ones that are allowed to manipulate this flag.
That reads plausible to me. It would be nicer to leave a brief note inline.
Ack.
Quoted 10 lines
Quoted 7 lines
Quoted 2 lines
Should it handle a
secondary_cmdq?No, I don't think we need to check secondary queues here. The
STOP_FLAGbeing set on the primary CMDQ during the suspend should suffice to indicate the entire SMMU's power state. That's why the function takes in the smmu ptr and not the cmdq.But if we don't set STOP flag to the
secondary_cmdq, wouldn't thedrain()timeout on thesecondary_cmdqthat does not elide?
I believe you're right..
I was banking on the fact that the ellision would work based on the primary queue alone as we check the call-sites *before* the cmdq is chosen. However, I see the need for setting the STOP_FLAG to secondary CMDQs too (especially when we remove the check from the call-sites).
I'll add setting the FLAG on secondary queues too!
Thanks,
Praan
PATCH v7 9/11 iommu/arm-smmu-v3: Enable pm_runtime and setup devlinks
In reply to Pranjal ShrivastavaEnable PM runtime for SMMUs having a power-domain during smmu probe. Add a devlink between the clients and SMMU device. The absence of a power domain effectively disables runtime power management.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13 -1
@@ -4240,6 +4240,9 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev)pci_prepare_ats (to_pci_dev (dev), stu); } +device_link_add (dev, smmu->dev, +DL_FLAG_PM_RUNTIME |DL_FLAG_AUTOREMOVE_SUPPLIER ); +return &smmu->iommu; err_free_master:
@@ -5592,11 +5595,18 @@ static int arm_smmu_device_probe(struct platform_device *pdev)if (ret) goto err_disable; +if (dev->pm_domain) { +pm_runtime_set_active (dev); +pm_runtime_use_autosuspend (dev); +pm_runtime_set_autosuspend_delay (dev,RPM_AUTOSUSPEND_DELAY_MS ); +pm_runtime_enable (dev); + } +/* And we're up. Go go go! */ ret =iommu_device_sysfs_add (&smmu->iommu, dev,NULL ,"smmu3.%pa" , &ioaddr);if (ret) - goto err_disable; + goto err_rpm_disable; ret =iommu_device_register (&smmu->iommu, &arm_smmu_ops, dev);if (ret) {
@@ -5608,6 +5618,8 @@ static int arm_smmu_device_probe(struct platform_device *pdev) err_free_sysfs:iommu_device_sysfs_remove (&smmu->iommu); +err_rpm_disable: +pm_runtime_disable (dev); err_disable:arm_smmu_device_disable (smmu); err_free_iopf:
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 09/11] iommu/arm-smmu-v3: Enable pm_runtime and setup devlinks
Quoted 10 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0@@ -5592,11 +5595,18 @@ static int arm_smmu_device_probe(struct platform_device *pdev)if (ret) goto err_disable; +if (dev->pm_domain) { +pm_runtime_set_active (dev); +pm_runtime_use_autosuspend (dev); +pm_runtime_set_autosuspend_delay (dev,RPM_AUTOSUSPEND_DELAY_MS ); +pm_runtime_enable (dev); + }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_probe(struct platform_device *pdev) · line 5612
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-1+/* And we're up. Go go go! */ ret =iommu_device_sysfs_add (&smmu->iommu, dev,NULL ,"smmu3.%pa" , &ioaddr);if (ret) - goto err_disable; + goto err_rpm_disable; ret =iommu_device_register (&smmu->iommu, &arm_smmu_ops, dev);if (ret) {
Can we move the entire pm_runtime_* piece at the end of this probe function, like arm-smmu v2 driver does?
It is no-fail, so we wouldn't need to add the revert path.
Otherwise,
Re: [PATCH v7 09/11] iommu/arm-smmu-v3: Enable pm_runtime and setup devlinks
Quoted 19 lines · Nicolin Chen
Quoted 18 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +8
-1@@ -5592,11 +5595,18 @@ static int arm_smmu_device_probe(struct platform_device *pdev)if (ret) goto err_disable; +if (dev->pm_domain) { +pm_runtime_set_active (dev); +pm_runtime_use_autosuspend (dev); +pm_runtime_set_autosuspend_delay (dev,RPM_AUTOSUSPEND_DELAY_MS ); +pm_runtime_enable (dev); + } +/* And we're up. Go go go! */ ret =iommu_device_sysfs_add (&smmu->iommu, dev,NULL ,"smmu3.%pa" , &ioaddr);if (ret) - goto err_disable; + goto err_rpm_disable;
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_probe(struct platform_device *pdev) · line 5612
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0ret =iommu_device_register (&smmu->iommu, &arm_smmu_ops, dev);if (ret) {Can we move the entire pm_runtime_* piece at the end of this probe function, like arm-smmu v2 driver does?
It is no-fail, so we wouldn't need to add the revert path.
Hmm.. I kept it earlier since I thought iommu_device_register starts the whole chain of probing masters, def_domain attach etc. but I guess it could work, the client devices can attach during the initial probe as pm_runtime remains disabled until registration is complete.
I'll just check if there are some weird edge cases here, just to be sure
Quoted 3 lines
Otherwise,
Reviewed-by Nicolin Chen <[email protected]>
Thanks!
Praan
PATCH v7 10/11 iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Pranjal ShrivastavaInvoke the pm_runtime helpers at all places before accessing the hw. The idea is to invoke runtime_pm helpers at common points which are used by exposed ops or interrupt handlers. TLB and CFG invalidations are elided if the SMMU is suspended by observing the CMDQ_PROD_STOP_FLAG via the arm_smmu_can_elide() helper.
--- .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 15 ++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 115 ++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 + 3 files changed, 123 insertions(+), 10 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +12 -3
@@ -15,6 +15,11 @@ void *arm_smmu_hw_info(struct device *dev, u32 *length,struct iommu_hw_info_arm_smmuv3 *info;u32 __iomem *base_idr;unsigned int i; +int ret; + + ret =arm_smmu_rpm_get (master->smmu); +if (ret <0 ) +return ERR_PTR (-EIO );if (*type !=IOMMU_HW_INFO_TYPE_DEFAULT && *type !=IOMMU_HW_INFO_TYPE_ARM_SMMUV3 ) {
@@ -24,8 +29,10 @@ void *arm_smmu_hw_info(struct device *dev, u32 *length, } info =kzalloc_obj (*info); -if (!info) +if (!info) { +arm_smmu_rpm_put (master->smmu);return ERR_PTR (-ENOMEM ); + } base_idr = master->smmu->base +ARM_SMMU_IDR0 ;for (i =0 ; i <=5 ; i++)
@@ -36,6 +43,7 @@ void *arm_smmu_hw_info(struct device *dev, u32 *length, *length =sizeof (*info); *type =IOMMU_HW_INFO_TYPE_ARM_SMMUV3 ; +arm_smmu_rpm_put (master->smmu);return info; }
@@ -386,8 +394,9 @@ int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,continue ;/* FIXME always uses the main cmdq rather than trying to group by type */ - ret =arm_smmu_cmdq_issue_cmdlist (smmu, &smmu->cmdq, &last->cmd, - cur - last, true); +if (!arm_smmu_can_elide (smmu)) + ret =arm_smmu_cmdq_issue_cmdlist (smmu, &smmu->cmdq, &last->cmd, + cur - last, true);if (ret) { cur--; goto out;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +108 -7
238 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +3 -0
@@ -1235,6 +1235,9 @@ static inline bool arm_smmu_can_elide(struct arm_smmu_device *smmu)return !!Q_STOP (READ_ONCE (smmu->cmdq.q.llq.prod)); } +int arm_smmu_rpm_get (struct arm_smmu_device *smmu); +void arm_smmu_rpm_put (struct arm_smmu_device *smmu); +#ifdef CONFIG_ARM_SMMU_V3_SVA bool arm_smmu_sva_supported (struct arm_smmu_device *smmu);void arm_smmu_sva_notifier_synchronize (void );
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.
All the arm_smmu_can_elide() call sites here would eventually elide the commands in arm_smmu_cmdq_issue_cmdlist() that is already gated by CMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?
Nicolin
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 8 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.All the
arm_smmu_can_elide()call sites here would eventually elide the commands inarm_smmu_cmdq_issue_cmdlist()that is already gated byCMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?
While issue_cmdlist() would eventually elide these commands, the can_elide() check is necessary to return early during suspension.
This avoids unnecessary stack allocation, cmd building, and spinlock contention on the cmdq->lock for threads that are anyway about to be elided.
By dropping these requests immediately, we significantly reduce cacheline bouncing and contention during unmap storms. Furthermore, the early check also allows us to specifically trigger the WARN_ON_ONCE() for broken devlinks.
Thanks,
Praan
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 10 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.All the
arm_smmu_can_elide()call sites here would eventually elide the commands inarm_smmu_cmdq_issue_cmdlist()that is already gated byCMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?
Quoted 6 lines
While
issue_cmdlist()would eventually elide these commands, thecan_elide()check is necessary to return early during suspension.This avoids unnecessary stack allocation, cmd building, and spinlock contention on the
cmdq->lockfor threads that are anyway about to be elided.
We aren't in the perf sensitive path.. most of those aren't going to be that bad.
arm_smmu_cmdq_shared_lock() on the other hand is taken at step 2, and the STOP flag in the same function is gated at step 1?
Quoted 2 lines
By dropping these requests immediately, we significantly reduce cacheline bouncing and contention during unmap storms.
How significantly, so as to justify invading every command issue() call site, which would be difficult to maintain? If we really need an early return, it would be nicer to have a common place at least.
Quoted 3 lines
Furthermore, the early check
also allows us to specifically trigger theWARN_ON_ONCE()for broken devlinks.
This doesn't sound very appealing to me.. And even if there is a very plausible reason, it should be noted in the commit message.
Nicolin
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 18 lines · Nicolin Chen
Quoted 10 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.All the
arm_smmu_can_elide()call sites here would eventually elide the commands inarm_smmu_cmdq_issue_cmdlist()that is already gated byCMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?Quoted 6 lines
While
issue_cmdlist()would eventually elide these commands, thecan_elide()check is necessary to return early during suspension.This avoids unnecessary stack allocation, cmd building, and spinlock contention on the
cmdq->lockfor threads that are anyway about to be elided.
Quoted 5 lines
We aren't in the perf sensitive path.. most of those aren't going to be that bad.
arm_smmu_cmdq_shared_lock()on the other hand is taken at step 2, and the STOP flag in the same function is gated at step 1?
DMA unmaps frequently occur from atomic contexts, interrupt handlers etc Thee Step 1 check in issue_cmdlist() happens under local_irq_save(). We may argue that it doesn't happen for long though..
Quoted 7 lines
Quoted 3 lines
By dropping these requests immediately, we significantly reduce cacheline bouncing and contention during unmap storms.
How significantly, so as to justify invading every command
issue()call site, which would be difficult to maintain? If we really need an early return, it would be nicer to have a common place at least.
Eliding early is more of an early-exit from the DMA unmap paths really.. If maintaining these high-level elision checks at 4 or 5 call sites is a maintenance burden, maybe we could move the logic into the issue_cmd macros?
Thanks,
Praan
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 22 lines · Pranjal Shrivastava
Quoted 21 lines · Nicolin Chen
Quoted 10 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.All the
arm_smmu_can_elide()call sites here would eventually elide the commands inarm_smmu_cmdq_issue_cmdlist()that is already gated byCMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?Quoted 6 lines
While
issue_cmdlist()would eventually elide these commands, thecan_elide()check is necessary to return early during suspension.This avoids unnecessary stack allocation, cmd building, and spinlock contention on the
cmdq->lockfor threads that are anyway about to be elided.We aren't in the perf sensitive path.. most of those aren't going to be that bad.
Quoted 6 lines
Quoted 2 lines
arm_smmu_cmdq_shared_lock()on the other hand is taken at step 2, and the STOP flag in the same function is gated at step 1?DMA unmaps frequently occur from atomic contexts, interrupt handlers etc Thee Step 1 check in
issue_cmdlist()happens underlocal_irq_save(). We may argue that it doesn't happen for long though..
It shouldn't IMHO. At least most of the call sites in this patch are right before calling issue() functions, so they are merely a few cycles away from the STOP gate in issue_cmdlist()?
The only place that might be slightly longer is the inv_range(), if the domain->invs is really long (e.g. nesting parent for VM), in which case, it might be plausible to add a gate. And even with that being said, it should be add to the top of the iteration (on invs->has_ats) rather than before submit()?
Quoted 11 lines
Quoted 6 lines
Quoted 2 lines
By dropping these requests immediately, we significantly reduce cacheline bouncing and contention during unmap storms.
How significantly, so as to justify invading every command
issue()call site, which would be difficult to maintain? If we really need an early return, it would be nicer to have a common place at least.Eliding early is more of an early-exit from the DMA unmap paths really.. If maintaining these high-level elision checks at 4 or 5 call sites is a maintenance burden, maybe we could move the logic into the
issue_cmdmacros?
What kinda of macro? Again, if it is added to just a few cycles right before issue_cmdlist(), it still wouldn't seem necessary.
Nicolin
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 26 lines · Nicolin Chen
Quoted 25 lines · Pranjal Shrivastava
Quoted 23 lines · Nicolin Chen
Quoted 10 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.All the
arm_smmu_can_elide()call sites here would eventually elide the commands inarm_smmu_cmdq_issue_cmdlist()that is already gated byCMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?Quoted 6 lines
While
issue_cmdlist()would eventually elide these commands, thecan_elide()check is necessary to return early during suspension.This avoids unnecessary stack allocation, cmd building, and spinlock contention on the
cmdq->lockfor threads that are anyway about to be elided.We aren't in the perf sensitive path.. most of those aren't going to be that bad.
arm_smmu_cmdq_shared_lock()on the other hand is taken at step 2, and the STOP flag in the same function is gated at step 1?
Quoted 8 lines
Quoted 3 lines
DMA unmaps frequently occur from atomic contexts, interrupt handlers etc Thee Step 1 check in
issue_cmdlist()happens underlocal_irq_save(). We may argue that it doesn't happen for long though..It shouldn't IMHO. At least most of the call sites in this patch are right before calling
issue()functions, so they are merely a few cycles away from the STOP gate inissue_cmdlist()?
I agree that eliding right before calling issue_cmdlist() might seem like an over-optimization. I guess we had this earlier because we didn't have ellision in the CMDQ. I'll think more about it (just in case we're missing some scenario) and try to perf it to confirm there's no big diff
Otherwise, I guess I'll drop the "early-exit" in v8..
Quoted 6 lines
The only place that might be slightly longer is the
inv_range(), if thedomain->invsis really long (e.g. nesting parent for VM), in which case, it might be plausible to add a gate. And even with that being said, it should be add to the top of the iteration (oninvs->has_ats) rather than beforesubmit()?
I agree.. but I'm thinking if we plan to remove the early exits, does it make sense to keep this one? Ideally, we shouldn't be dealing with a long domain->invs if we are in VMs (IOMMUFD & VFIO both get a pm_ref). So, I guess if we're dropping elisions from everywhere it would be fine
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
By dropping these requests immediately, we significantly reduce cacheline bouncing and contention during unmap storms.
How significantly, so as to justify invading every command
issue()call site, which would be difficult to maintain? If we really need an early return, it would be nicer to have a common place at least.
Quoted 7 lines
Quoted 4 lines
Eliding early is more of an early-exit from the DMA unmap paths really.. If maintaining these high-level elision checks at 4 or 5 call sites is a maintenance burden, maybe we could move the logic into the
issue_cmdmacros?What kinda of macro? Again, if it is added to just a few cycles right before
issue_cmdlist(), it still wouldn't seem necessary.
I was referring to the arm_smmu_cmdq_issue_cmd* macros here. But I suppose you're right..
Thanks,
Praan
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Pranjal ShrivastavaQuoted 14 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.All the
arm_smmu_can_elide()call sites here would eventually elide the commands inarm_smmu_cmdq_issue_cmdlist()that is already gated byCMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?While
issue_cmdlist()would eventually elide these commands, thecan_elide()check is necessary to return early during suspension.
Quoted 8 lines
This avoids unnecessary stack allocation, cmd building, and spinlock contention on the
cmdq->lockfor threads that are anyway about to be elided.By dropping these requests immediately, we significantly reduce cacheline bouncing and contention during unmap storms. Furthermore, the early check also allows us to specifically trigger the
WARN_ON_ONCE()for broken devlinks.
Hi Pranjal,
Have you observed unmap storms in a real-world use case, or is this a preemptive optimization? I would not expect a high rate of map/unmap operations while the SMMU is suspended. If a client device calls iommu_map/iommu_unmap (directly or indirectly), it suggests the client device is RPM_ACTIVE, meaning the SMMU should be active as well.
I am in favor of removing arm_smmu_can_elide().
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 26 lines · Daniel Mentz
Quoted 14 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.All the
arm_smmu_can_elide()call sites here would eventually elide the commands inarm_smmu_cmdq_issue_cmdlist()that is already gated byCMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?While
issue_cmdlist()would eventually elide these commands, thecan_elide()check is necessary to return early during suspension.Quoted 8 lines
This avoids unnecessary stack allocation, cmd building, and spinlock contention on the
cmdq->lockfor threads that are anyway about to be elided.By dropping these requests immediately, we significantly reduce cacheline bouncing and contention during unmap storms. Furthermore, the early check also allows us to specifically trigger the
WARN_ON_ONCE()for broken devlinks.Hi Pranjal,
Quoted 7 lines
Have you observed unmap storms in a real-world use case, or is this a preemptive optimization? I would not expect a high rate of map/unmap operations while the SMMU is suspended. If a client device calls iommu_map/iommu_unmap (directly or indirectly), it suggests the client device is
RPM_ACTIVE, meaning the SMMU should be active as well.I am in favor of removing
arm_smmu_can_elide().
I saw some with DMA_FQ (fq_timer does batched async invalidations) but the early ellision doesn't really help with perf which I agreed to in my reply to Nicolin as well.
The early checks were dropped in v8 (except for invs_array and for the WARN_ON in inv_master).
Thanks,
Praan
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 34 lines · Pranjal Shrivastava
Quoted 32 lines · Daniel Mentz
Quoted 14 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.All the
arm_smmu_can_elide()call sites here would eventually elide the commands inarm_smmu_cmdq_issue_cmdlist()that is already gated byCMDQ_PROD_STOP_FLAG? It doesn't seem necessary to gate again?While
issue_cmdlist()would eventually elide these commands, thecan_elide()check is necessary to return early during suspension.Quoted 8 lines
This avoids unnecessary stack allocation, cmd building, and spinlock contention on the
cmdq->lockfor threads that are anyway about to be elided.By dropping these requests immediately, we significantly reduce cacheline bouncing and contention during unmap storms. Furthermore, the early check also allows us to specifically trigger the
WARN_ON_ONCE()for broken devlinks.Hi Pranjal,
Have you observed unmap storms in a real-world use case, or is this a preemptive optimization? I would not expect a high rate of map/unmap operations while the SMMU is suspended. If a client device calls iommu_map/iommu_unmap (directly or indirectly), it suggests the client device is
RPM_ACTIVE, meaning the SMMU should be active as well.
Quoted 8 lines
Quoted 1 line
I am in favor of removing
arm_smmu_can_elide().I saw some with
DMA_FQ(fq_timerdoes batched async invalidations) but the early ellision doesn't really help with perf which I agreed to in my reply to Nicolin as well.The early checks were dropped in v8 (except for
invs_arrayand for theWARN_ONininv_master).
Hi Pranjal,
Please correct me if I'm wrong, but in v8, I can see a call to arm_smmu_cmdq_can_elide() at the very beginning of arm_smmu_domain_inv_range(). Isn't this the path every iommu_unmap goes through?
Re: [PATCH v7 10/11] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 11 lines · Daniel Mentz
Quoted 10 lines · Pranjal Shrivastava
Quoted 8 lines · Daniel Mentz
Quoted 7 lines · Pranjal Shrivastava
Quoted 5 lines · Nicolin Chen
Quoted 3 lines · Pranjal Shrivastava
TLB and CFG invalidations are
elided if the SMMU is suspended by observing theCMDQ_PROD_STOP_FLAGvia thearm_smmu_can_elide()helper.
[..]
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
Hi Pranjal,
Have you observed unmap storms in a real-world use case, or is this a preemptive optimization? I would not expect a high rate of map/unmap operations while the SMMU is suspended. If a client device calls iommu_map/iommu_unmap (directly or indirectly), it suggests the client device is
RPM_ACTIVE, meaning the SMMU should be active as well.Quoted 8 lines
Quoted 1 line
I am in favor of removing
arm_smmu_can_elide().I saw some with
DMA_FQ(fq_timerdoes batched async invalidations) but the early ellision doesn't really help with perf which I agreed to in my reply to Nicolin as well.The early checks were dropped in v8 (except for
invs_arrayand for theWARN_ONininv_master).
Quoted 6 lines
Hi Pranjal,
Please correct me if I'm wrong, but in v8, I can see a call to
arm_smmu_cmdq_can_elide()at the very beginning ofarm_smmu_domain_inv_range(). Isn't this the path everyiommu_unmapgoes through?
That's correct. In v7 there were 4-5 callsites for these checks, most of which were dropped. This one (for invs array) was retained in v8 based on this discussion with Nicolin [1].
Thanks,
Praan
https://lore.kernel.org/all/ahjNYqe8hjPUQIQD@Asurada-Nvidia/
PATCH v7 11/11 iommu/arm-smmu-v3: Add KUnit unit tests for Runtime PM
In reply to Pranjal ShrivastavaIntroduce a kunit selftests to verify the Runtime PM elision gating, post-suspend elisions and progress on resumption under active invalidation load. Simulate concurrent HW suspension using a timer. Mock all HW registers and CMDQ buffers by allocating them on RAM. Make the mock CMDQ self-consuming to avoid hitting queue_full scenarios.
--- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 150 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 1 + 2 files changed, 151 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +150 -0
@@ -3,6 +3,10 @@* Copyright 2024 Google LLC. */ #include <kunit/test.h> +#include <linux/delay.h> +#include <linux/kthread.h> +#include <linux/sched.h> +#include <linux/timer.h> #include <linux/io-pgtable.h> #include "arm-smmu-v3.h"
@@ -771,7 +775,153 @@ static void arm_smmu_v3_invs_test(struct kunit *test)kfree (test_b); } +struct arm_smmu_test_pm_context { +u32 *mock_prod_reg; +u64 *mock_base; +unsigned long *mock_valid_map; +}; + +/* +* Shared helper to allocate and map a fully functional, self-consuming mock +* CMDQ. By redirecting both prod_reg and cons_reg to the same memory location, +* every producer write is instantly reflected as a consumer completion. +*/ +static struct arm_smmu_test_pm_context * +arm_smmu_v3_test_init_mock_cmdq (struct kunit *test,struct arm_smmu_device *smmu) +{ +struct arm_smmu_test_pm_context *ctx; +struct arm_smmu_cmdq *cmdq = &smmu->cmdq; + + ctx =kunit_kzalloc (test,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, ctx); + +/* Use a standard 1024-entry queue for safe word bitmask alignment */ + ctx->mock_prod_reg =kunit_kzalloc (test,sizeof (u32),GFP_KERNEL ); + ctx->mock_base =kunit_kzalloc (test,1024 *16 ,GFP_KERNEL ); + ctx->mock_valid_map =kunit_kzalloc (test,BITS_TO_LONGS (1024 ) *sizeof (long ),GFP_KERNEL ); + +KUNIT_ASSERT_NOT_NULL (test, ctx->mock_prod_reg); +KUNIT_ASSERT_NOT_NULL (test, ctx->mock_base); +KUNIT_ASSERT_NOT_NULL (test, ctx->mock_valid_map); + + smmu->features =0 ; +/* 1024 entries */ + cmdq->q.llq.max_n_shift =10 ; +/* SMMUv3 commands are 2 dwords (16 bytes) */ + cmdq->q.ent_dwords =2 ; + cmdq->q.base = (__le64 *)ctx->mock_base; + cmdq->valid_map = (atomic_long_t *)ctx->mock_valid_map; + +/* Self-Consuming, prod == cons always ensures queue empty */ + cmdq->q.prod_reg = (__force u32 __iomem *)ctx->mock_prod_reg; + cmdq->q.cons_reg = (__force u32 __iomem *)ctx->mock_prod_reg; + +/* Initialize memory indices */ +atomic_set (&cmdq->q.llq.atomic.prod,0 ); +atomic_set (&cmdq->q.llq.atomic.cons,0 ); +atomic_set (&cmdq->owner_prod,0 ); + *ctx->mock_prod_reg =0 ; + +return ctx; +} + +struct arm_smmu_test_timer_context { +struct kunit *test; +struct arm_smmu_device *smmu; +struct arm_smmu_test_pm_context *pm_ctx; +struct timer_list timer; +bool stopped; +}; + +/* Asynchronous timer callback running in softirq context */ +static void arm_smmu_v3_test_rpm_timer_callback (struct timer_list *t) +{ +struct arm_smmu_test_timer_context *ctx = +timer_container_of (ctx, t, timer); +struct arm_smmu_cmdq *cmdq = &ctx->smmu->cmdq; + +/* +* Asynchronously set the STOP_FLAG (Close the gate). +* Simulate a concurrent runtime suspend event interrupting +* the invalidations happening under active load. +*/ +atomic_or (CMDQ_PROD_STOP_FLAG , &cmdq->q.llq.atomic.prod); + +/* Signal to the main storm loop that suspend has triggered */ +WRITE_ONCE (ctx->stopped, true); +} + +/* +* Verify SMMU PM Runtime gating, elision, and post-suspend resumption +* safety sequentially under active stress. +*/ +static void arm_smmu_v3_test_rpm_stress_race (struct kunit *test) +{ +struct arm_smmu_device *smmu =kunit_kzalloc (test,sizeof (*smmu),GFP_KERNEL ); +struct arm_smmu_test_pm_context *pm_ctx; +struct arm_smmu_test_timer_context *timer_ctx; +struct arm_smmu_cmdq *cmdq = &smmu->cmdq; +struct arm_smmu_cmd cmd =arm_smmu_make_cmd_cfgi_all (); +u32 stopped_prod; +int i, ret; + +KUNIT_ASSERT_NOT_NULL (test, smmu); + pm_ctx =arm_smmu_v3_test_init_mock_cmdq (test, smmu); + + timer_ctx =kunit_kmalloc (test,sizeof (*timer_ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, timer_ctx); + timer_ctx->test = test; + timer_ctx->smmu = smmu; + timer_ctx->pm_ctx = pm_ctx; + timer_ctx->stopped = false; + +/* Setup the kernel software timer */ +timer_setup (&timer_ctx->timer, arm_smmu_v3_test_rpm_timer_callback,0 ); + +/* Start the timer to fire in 10 jiffies */ +mod_timer (&timer_ctx->timer, jiffies +msecs_to_jiffies (10 )); + +/* Execute the unmap storm until the timer triggers */ +while (!READ_ONCE (timer_ctx->stopped)) { + ret =arm_smmu_cmdq_issue_cmdlist (smmu, cmdq, &cmd,1 , false); +if (ret !=0 ) { +KUNIT_FAIL (test,"Unmap Storm loop failed to submit cmd list: %d\n" , ret); +break ; + } + +usleep_range (50 ,100 ); + } + +timer_delete_sync (&timer_ctx->timer); + +/* Establish the globally stable, post-storm register baseline index */ + stopped_prod = *pm_ctx->mock_prod_reg; + +/* +* Attempt multiple unmaps while the SMMU is disabled (STOP_GATE is set) +* Every single invalidation must be instantly elided, returning 0 +* immediately and leaving the mock MMIO register completely frozen. +*/ +for (i =0 ; i <1000 ; i++) { + ret =arm_smmu_cmdq_issue_cmdlist (smmu, cmdq, &cmd,1 , false); +if (ret !=0 ) { +KUNIT_FAIL (test,"Gated storm loop failed at iteration %d: %d\n" , i, ret); +break ; + } + } +KUNIT_EXPECT_EQ (test, stopped_prod, *pm_ctx->mock_prod_reg); + +/* +* Clear the STOP_FLAG (resume SMMU). A new invalidation must now +* successfully commit, moving the prod_reg by exactly 1 slot +*/ +atomic_andnot (CMDQ_PROD_STOP_FLAG , &cmdq->q.llq.atomic.prod); +KUNIT_EXPECT_EQ (test,0 ,arm_smmu_cmdq_issue_cmdlist (smmu, cmdq, &cmd,1 , false)); +KUNIT_EXPECT_EQ (test, stopped_prod +1 , *pm_ctx->mock_prod_reg); +} +static struct kunit_case arm_smmu_v3_test_cases[] = { +KUNIT_CASE (arm_smmu_v3_test_rpm_stress_race),KUNIT_CASE (arm_smmu_v3_write_ste_test_bypass_to_abort),KUNIT_CASE (arm_smmu_v3_write_ste_test_abort_to_bypass),KUNIT_CASE (arm_smmu_v3_write_ste_test_cdtable_to_abort),
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -0
@@ -871,6 +871,7 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,local_irq_restore (flags);return ret; } +EXPORT_SYMBOL_IF_KUNIT (arm_smmu_cmdq_issue_cmdlist);static int arm_smmu_cmdq_issue_cmd_p (struct arm_smmu_device *smmu,struct arm_smmu_cmd *cmd,bool sync)
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Re: [PATCH v7 11/11] iommu/arm-smmu-v3: Add KUnit unit tests for Runtime PM
Quoted 7 lines · Pranjal Shrivastava
Introduce a kunit selftests to verify the Runtime PM elision gating, post-suspend elisions and progress on resumption under active invalidation load. Simulate concurrent HW suspension using a timer. Mock all HW registers and CMDQ buffers by allocating them on RAM. Make the mock CMDQ self-consuming to avoid hitting
queue_fullscenarios.Signed-off-by Pranjal Shrivastava <[email protected]>
Maybe I am wrong, yet this reads like an entirely AI-generated code. So, should it have an Assisted-by?
Documentation/process/submitting-patches.rst:637:Using Assisted-by:
Documentation/process/submitting-patches.rst:641:you need to acknowledge that use by adding an Assisted-by tag. Failure to Documentation/process/coding-assistants.rst:44:Contributions should include an Assisted-by tag in the following format::Documentation/process/coding-assistants.rst:46: Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2] Documentation/process/coding-assistants.rst:59: Assisted-by: Claude:claude-3-opus coccinelle sparse
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 782
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +5
-0+struct arm_smmu_test_pm_context { +u32 *mock_prod_reg; +u64 *mock_base; +unsigned long *mock_valid_map; +};
It seems all about cmdq only, so maybe:
struct arm_smmu_mock_cmdq
?
Quoted 2 lines
+/*
+ * Shared helper to allocate and map a fully functional, self-consuming mock
Is it really a "Shared" helper? I see only one caller..
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 796
Quoted 11 lines
+ * CMDQ. By redirecting both
prod_regandcons_regto the same memory location,
+ * every producer write is instantly reflected as a consumer completion.
+ */drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +8
-0+static struct arm_smmu_test_pm_context * +arm_smmu_v3_test_init_mock_cmdq (struct kunit *test,struct arm_smmu_device *smmu) +{ +struct arm_smmu_test_pm_context *ctx; +struct arm_smmu_cmdq *cmdq = &smmu->cmdq; + + ctx =kunit_kzalloc (test,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, ctx);
This ctx struct isn't that large. Maybe the test function (caller) can put on the stack and pass its point int than allocation?
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 799
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +2
-0+/* Use a standard 1024-entry queue for safe word bitmask alignment */ + ctx->mock_prod_reg =kunit_kzalloc (test,sizeof (u32),GFP_KERNEL );
Is a u32 allocation necessary?
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 800
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +1
-0+ ctx->mock_base =kunit_kzalloc (test,1024 *16 ,GFP_KERNEL );
s/16/sizeof(struct arm_smmu_cmd)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 811
Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +11
-0+ ctx->mock_valid_map =kunit_kzalloc (test,BITS_TO_LONGS (1024 ) *sizeof (long ),GFP_KERNEL ); + +KUNIT_ASSERT_NOT_NULL (test, ctx->mock_prod_reg); +KUNIT_ASSERT_NOT_NULL (test, ctx->mock_base); +KUNIT_ASSERT_NOT_NULL (test, ctx->mock_valid_map); + + smmu->features =0 ; +/* 1024 entries */ + cmdq->q.llq.max_n_shift =10 ; +/* SMMUv3 commands are 2 dwords (16 bytes) */ + cmdq->q.ent_dwords =2 ;
CMDQ_ENT_DWORDS?
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 812
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +1
-0+ cmdq->q.base = (__le64 *)ctx->mock_base;
ctx->mock_base doesn't seem to have any other use? Why does it have to be in the ctx structure?
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 813
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +1
-0+ cmdq->valid_map = (atomic_long_t *)ctx->mock_valid_map;
Same questions.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 823
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +9
-0+/* Self-Consuming, prod == cons always ensures queue empty */ + cmdq->q.prod_reg = (__force u32 __iomem *)ctx->mock_prod_reg; + cmdq->q.cons_reg = (__force u32 __iomem *)ctx->mock_prod_reg; + +/* Initialize memory indices */ +atomic_set (&cmdq->q.llq.atomic.prod,0 ); +atomic_set (&cmdq->q.llq.atomic.cons,0 ); +atomic_set (&cmdq->owner_prod,0 ); + *ctx->mock_prod_reg =0 ;
Arguably, mock_prod_reg is only used to test against a prod snapshot so it doesn't seem necessary to be in a structure.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 831
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +7
-0+return ctx; +} + +struct arm_smmu_test_timer_context { +struct kunit *test; +struct arm_smmu_device *smmu; +struct arm_smmu_test_pm_context *pm_ctx;
pm_ctx doesn't seem to be used by the timer callback function?
Is it necessary in this structure? Even "test" isn't used.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 836
Quoted 5 lines
. + struct
timer_listtimer;drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +4
-0+bool stopped; +}; + +/* Asynchronous timer callback running in softirq context */
Would be nicer to replace this with the inline comments:
/* Simulate a concurrent runtime suspend event interrupting the invalidations */
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +5
-0+static void arm_smmu_v3_test_rpm_timer_callback (struct timer_list *t) +{ +struct arm_smmu_test_timer_context *ctx = +timer_container_of (ctx, t, timer); +struct arm_smmu_cmdq *cmdq = &ctx->smmu->cmdq;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 851
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +10
-0+ +/* +* Asynchronously set the STOP_FLAG (Close the gate). +* Simulate a concurrent runtime suspend event interrupting +* the invalidations happening under active load. +*/ +atomic_or (CMDQ_PROD_STOP_FLAG , &cmdq->q.llq.atomic.prod); + +/* Signal to the main storm loop that suspend has triggered */ +WRITE_ONCE (ctx->stopped, true);
Then, it should be named "suspended".
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 860
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +9
-0+} + +/* +* Verify SMMU PM Runtime gating, elision, and post-suspend resumption +* safety sequentially under active stress. +*/ +static void arm_smmu_v3_test_rpm_stress_race (struct kunit *test) +{ +struct arm_smmu_device *smmu =kunit_kzalloc (test,sizeof (*smmu),GFP_KERNEL );
arm-smmu-v3-test has a global arm_smmu_device to use.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 864
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +4
-0+struct arm_smmu_test_pm_context *pm_ctx; +struct arm_smmu_test_timer_context *timer_ctx; +struct arm_smmu_cmdq *cmdq = &smmu->cmdq; +struct arm_smmu_cmd cmd =arm_smmu_make_cmd_cfgi_all ();
Can we organize this a bit? Maybe in an inverted xmas tree?
Also, it would be nicer to run git-clang-format.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 872
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +8
-0+u32 stopped_prod; +int i, ret; + +KUNIT_ASSERT_NOT_NULL (test, smmu); + pm_ctx =arm_smmu_v3_test_init_mock_cmdq (test, smmu); + + timer_ctx =kunit_kmalloc (test,sizeof (*timer_ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_NULL (test, timer_ctx);
The allocation doesn't seem necessary either. We use kunit_kzalloc in this file only when the memory (usually an array) is too large.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 876
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +1
-0+ timer_ctx->stopped = false;
Then, should have been kunit_kzalloc() at least. Or " = {0};", if keep it on the stack.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 882
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +5
-0+/* Setup the kernel software timer */ +timer_setup (&timer_ctx->timer, arm_smmu_v3_test_rpm_timer_callback,0 ); + +/* Start the timer to fire in 10 jiffies */ +mod_timer (&timer_ctx->timer, jiffies +msecs_to_jiffies (10 ));
Both lines reads pretty clear without that verbose comments...
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 890
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +8
-0+ +/* Execute the unmap storm until the timer triggers */ +while (!READ_ONCE (timer_ctx->stopped)) { + ret =arm_smmu_cmdq_issue_cmdlist (smmu, cmdq, &cmd,1 , false); +if (ret !=0 ) { +KUNIT_FAIL (test,"Unmap Storm loop failed to submit cmd list: %d\n" , ret); +break ; + }
As of now, arm_smmu_cmdq_issue_cmdlist() fails only on -ETIMEDOUT and spits a print already. So, maybe simplify it:
if (arm_smmu_cmdq_issue_cmdlist()) break;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c static void arm_smmu_v3_invs_test(struct kunit *test) · line 927
Quoted 5 lines
static struct
kunit_casearm_smmu_v3_test_cases[] = {drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +1
-0+KUNIT_CASE (arm_smmu_v3_test_rpm_stress_race),KUNIT_CASE (arm_smmu_v3_write_ste_test_bypass_to_abort),KUNIT_CASE (arm_smmu_v3_write_ste_test_abort_to_bypass), KUNIT_CASE(arm_smmu_v3_write_ste_test_cdtable_to_abort),
Usually we append to the end. And following the naming convention:
arm_smmu_v3_rpm_test_stress_race
Nicolin
Re: [PATCH v7 11/11] iommu/arm-smmu-v3: Add KUnit unit tests for Runtime PM
Quoted 7 lines · Nicolin Chen
Quoted 6 lines · Pranjal Shrivastava
Introduce a kunit selftests to verify the Runtime PM elision gating, post-suspend elisions and progress on resumption under active invalidation load. Simulate concurrent HW suspension using a timer. Mock all HW registers and CMDQ buffers by allocating them on RAM. Make the mock CMDQ self-consuming to avoid hitting
queue_fullscenarios.
Quoted 5 lines
Quoted 1 line
Signed-off-by Pranjal Shrivastava <[email protected]>Maybe I am wrong, yet this reads like an entirely AI-generated code. So, should it have an Assisted-by?
Agh, It appears I made an error during the final send and mistakenly posted an old WIP version of this patch! I created a patch file before committing the final changes. This has remnants of older experiments and lacks severals other cleanups!
I'm aware of the AI usage guidelines. However, this is not an entirely AI generated patch, it's a pre-cleanup snapshot of a WIP version where I had multiple rpm tests (an attempt to use kthreads to simulate races) that were using shared functions like the mock_cmdq etc. remnants of which are seen in this patch. *FACEPALM*.
I'll send out a v8 soon!
Thanks!
Praan
Re: [PATCH v7 11/11] iommu/arm-smmu-v3: Add KUnit unit tests for Runtime PM
Quoted 19 lines · Pranjal Shrivastava
Quoted 7 lines · Nicolin Chen
Quoted 6 lines · Pranjal Shrivastava
Introduce a kunit selftests to verify the Runtime PM elision gating, post-suspend elisions and progress on resumption under active invalidation load. Simulate concurrent HW suspension using a timer. Mock all HW registers and CMDQ buffers by allocating them on RAM. Make the mock CMDQ self-consuming to avoid hitting
queue_fullscenarios.Quoted 5 lines
Quoted 1 line
Signed-off-by Pranjal Shrivastava <[email protected]>Maybe I am wrong, yet this reads like an entirely AI-generated code. So, should it have an Assisted-by?
Agh, It appears I made an error during the final send and mistakenly posted an old WIP version of this patch! I created a patch file before committing the final changes. This has remnants of older experiments and lacks severals other cleanups!
Quoted 5 lines
I'm aware of the AI usage guidelines. However, this is not an entirely AI generated patch, it's a pre-cleanup snapshot of a WIP version where I had multiple rpm tests (an attempt to use kthreads to simulate races) that were using shared functions like the
mock_cmdqetc. remnants of which are seen in this patch. *FACEPALM*.
It'd be still very nice to have a KUNIT coverage.
Nicolin
Re: [PATCH v7 11/11] iommu/arm-smmu-v3: Add KUnit unit tests for Runtime PM
Quoted 20 lines · Nicolin Chen
Quoted 19 lines · Pranjal Shrivastava
Quoted 7 lines · Nicolin Chen
Quoted 6 lines · Pranjal Shrivastava
Introduce a kunit selftests to verify the Runtime PM elision gating, post-suspend elisions and progress on resumption under active invalidation load. Simulate concurrent HW suspension using a timer. Mock all HW registers and CMDQ buffers by allocating them on RAM. Make the mock CMDQ self-consuming to avoid hitting
queue_fullscenarios.Quoted 5 lines
Quoted 1 line
Signed-off-by Pranjal Shrivastava <[email protected]>Maybe I am wrong, yet this reads like an entirely AI-generated code. So, should it have an Assisted-by?
Agh, It appears I made an error during the final send and mistakenly posted an old WIP version of this patch! I created a patch file before committing the final changes. This has remnants of older experiments and lacks severals other cleanups!
Quoted 7 lines
Quoted 5 lines
I'm aware of the AI usage guidelines. However, this is not an entirely AI generated patch, it's a pre-cleanup snapshot of a WIP version where I had multiple rpm tests (an attempt to use kthreads to simulate races) that were using shared functions like the
mock_cmdqetc. remnants of which are seen in this patch. *FACEPALM*.It'd be still very nice to have a KUNIT coverage.
Yes, I plan to add a few RPM tests to the KUNIT.
This however was a WIP version that went into the v7 patches *before* I commited the final version of this. Basically git format-patch *before* committing the final version..
Thanks,
Praan
Quoted 3 lines · Pranjal Shrivastava
[v7]
- Rebased on the latest arm/smmu/updates branch (which has the removal of structarm_smmu_cmdq_entmerged)
Sashiko failed to apply:
https://sashiko.dev/#/patchset/20260527221407.1756491-1-praan%40google.com
Let's add a base-commit to the series and see if it would work?
Nicolin