iommu/arm-smmu-v3: Implement Runtime/System Sleep ops
Subsystems (4)
v8 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. In v8, a surgical state-correction is added to runtime_suspend: if draining fails, the software trackers (cons and owner_prod) are forced to align with the masked producer index. This voids any stale commands for the next session and prevents deadlocks or spurious executions upon resumption.
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. To maintain code clarity, invalidation call-sites no longer perform manual elision checks; instead, elision is authoritative within the CMDQ submission path.
The renamed helper, arm_smmu_cmdq_can_elide(), is preserved only for critical diagnostic (ATC), early-drop (Page Response), and performance (massive Domain Range) short-circuits. For ATC invalidations, devlinks must guarantee the SMMU is active if the endpoint is active, a WARN_ON_ONCE() catches inconsistencies.
4. Implementation-Specific Quiescing (Tegra VCMDQ)
In v8, implementation-specific gating and draining are consolidated into a unified quiesce_and_drain_queues callback. The tegra241-cmdqv driver is updated to gate all active local virtual queues (VINTF0) before draining.
5. 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.
[v8]
- Centralized elision logic: dropped redundant checks from invalidation call-sites; elision is now authoritative within the CMDQ layer.
- Renamed elision helper to arm_smmu_cmdq_can_elide() and preserved it only for diagnostic/safety paths (ATC, GERROR, Page Response).
- Consolidated implementation-specific gating and draining into a unified quiesce_and_drain_queues callback.
- Updated tegra241-cmdqv to gate virtual queues before draining, addressing non-deterministic timeouts from guest-side submissions.
- Re-ordered probe sequence to enable pm_runtime only at the end, aligning with SMMUv2 and simplifying error paths.
- Refactored KUnit tests for better, addressed Nicolin's comments & added a multi-threaded kthread race test.
- Refactored RPM helpers to use early-return patterns for improved clarity.
- Collected R-bs from Nicolin.
[v7]
- https://lore.kernel.org/all/[email protected]/
- 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 (11):
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/tegra241-cmdqv: Add a helper to quiesce VCMDQs 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 | 10 +- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 169 +++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 435 +++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 27 ++ .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 57 +++ 5 files changed, 672 insertions(+), 26 deletions(-)
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
PATCH v8 1/12 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.1013.g208068f2d8-goog
PATCH v8 2/12 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.1013.g208068f2d8-goog
PATCH v8 3/12 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.1013.g208068f2d8-goog
PATCH v8 4/12 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.1013.g208068f2d8-goog
PATCH v8 5/12 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.1013.g208068f2d8-goog
PATCH v8 6/12 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 | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10 -2
@@ -2257,10 +2257,10 @@ 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) +/* Lockless; must ensure that there are no concurrent callers */ +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 +2300,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.1013.g208068f2d8-goog
PATCH v8 7/12 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 cmpxchg 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.1013.g208068f2d8-goog
PATCH v8 8/12 iommu/tegra241-cmdqv: Add a helper to quiesce VCMDQs
In reply to Pranjal ShrivastavaThe tegra241-cmdqv driver supports vCMDQs which need to be quiesced using the STOP_FLAG as well. The current driver implementation only uses VINTF0 for vCMDQs owned by the kernel which need to be stopped. Add a helper that sets the CMDQ_PROD_STOP_FLAG on the vCMDQs as well.
Consolidate this logic by renaming the implementation hook to quiesce_and_drain_queues and ensuring that the tegra241-cmdqv driver gates all active local virtual queues before starting the drain loop. Additionally, clear the STOP_FLAG in tegra241_vcmdq_hw_init() as a part of tegra241_cmdqv_hw_reset.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 +-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 32 +++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -2
@@ -937,8 +937,8 @@ static int arm_smmu_drain_queues(struct arm_smmu_device *smmu) goto out;/* Drain all implementation-specific queues */ -if (smmu->impl_ops && smmu->impl_ops->drain_queues) - ret = smmu->impl_ops->drain_queues (smmu); +if (smmu->impl_ops && smmu->impl_ops->quiesce_and_drain_queues) + ret = smmu->impl_ops->quiesce_and_drain_queues (smmu); out:return ret; }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -1
@@ -887,7 +887,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); +int (*quiesce_and_drain_queues)(struct arm_smmu_device *smmu); };/* An SMMUv3 instance */
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +30 -2
@@ -414,6 +414,28 @@ tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu,return &vcmdq->cmdq; } +static void tegra241_cmdqv_quiesce_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 ]; +u16 lidx; + +if (!READ_ONCE (vintf->enabled)) +return ; + +for (lidx =0 ; lidx < cmdqv->num_lvcmdqs_per_vintf; lidx++) { +struct tegra241_vcmdq *vcmdq = vintf->lvcmdqs[lidx]; + +if (!vcmdq || !READ_ONCE (vcmdq->enabled)) +continue ; + +atomic_or (CMDQ_PROD_STOP_FLAG , &vcmdq->cmdq.q.llq.atomic.prod); + } + +return ; +} +static int tegra241_cmdqv_drain_vintf0_lvcmdqs (struct arm_smmu_device *smmu) { struct tegra241_cmdqv *cmdqv =
@@ -426,6 +448,8 @@ static int tegra241_cmdqv_drain_vintf0_lvcmdqs(struct arm_smmu_device *smmu)if (!READ_ONCE (vintf->enabled))return 0 ; +tegra241_cmdqv_quiesce_vintf0_lvcmdqs (smmu); +for (lidx =0 ; lidx < cmdqv->num_lvcmdqs_per_vintf; lidx++) {struct tegra241_vcmdq *vcmdq = vintf->lvcmdqs[lidx];
@@ -511,9 +535,13 @@ 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.prod &CMDQ_PROD_IDX_MASK , +REG_VCMDQ_PAGE0 (vcmdq,PROD ));writel_relaxed (vcmdq->cmdq.q.llq.cons,REG_VCMDQ_PAGE0 (vcmdq,CONS )); +/* Clear the CMDQ_PROD_STOP_FLAG */ +atomic_andnot (CMDQ_PROD_STOP_FLAG , &vcmdq->cmdq.q.llq.atomic.prod); + ret =vcmdq_write_config (vcmdq,VCMDQ_EN );if (ret) { dev_err(vcmdq->cmdqv->dev,
@@ -873,7 +901,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, + .quiesce_and_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.1013.g208068f2d8-goog
PATCH v8 9/12 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 register writes and tracking updates.
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 | 172 +++++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 18 ++ 2 files changed, 188 insertions(+), 2 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +170 -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,40 @@ 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)) +return 0 ; + + 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)) +return ; + + 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 +824,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.
@@ -4828,7 +4864,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 ;
@@ -4839,6 +4876,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 ());
@@ -4898,6 +4939,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,
@@ -5580,6 +5636,117 @@ 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); + +/* Avoid consuming stale commands if we timed-out to drain the queues */ +if (ret || !timeout) + cmdq->q.llq.cons = cmdq->q.llq.prod &CMDQ_PROD_IDX_MASK ; + +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); + +/* Clears the CMDQ_PROD_STOP_FLAG as well */ + 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" , }, { },
@@ -5596,6 +5763,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.1013.g208068f2d8-goog
PATCH v8 10/12 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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10 -0
@@ -4243,6 +4243,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:
@@ -5607,6 +5610,13 @@ static int arm_smmu_device_probe(struct platform_device *pdev) goto err_free_sysfs; } +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); + } +return 0 ; err_free_sysfs:
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
PATCH v8 11/12 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.
--- .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 10 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 101 ++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 +- 3 files changed, 108 insertions(+), 8 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +9 -1
@@ -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; }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +95 -6
219 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +4 -1
@@ -1230,11 +1230,14 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,* 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) +static inline bool arm_smmu_cmdq_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.1013.g208068f2d8-goog
PATCH v8 12/12 iommu/arm-smmu-v3: Add KUnit unit tests for Runtime PM
In reply to Pranjal ShrivastavaIntroduce 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 | 169 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 1 + 2 files changed, 170 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +169 -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,6 +775,169 @@ static void arm_smmu_v3_invs_test(struct kunit *test)kfree (test_b); } +struct arm_smmu_mock_cmdq { +u32 mock_prod_reg; +}; + +/* Helper to allocate a self-consuming mock cmdq */ +static void arm_smmu_v3_test_init_mock_cmdq (struct kunit *test, +struct arm_smmu_device *smmu, +struct arm_smmu_mock_cmdq *mock) +{ +struct arm_smmu_cmdq *cmdq = &smmu->cmdq; +unsigned long *mock_valid_map; +u64 *mock_base; + + mock_base =kunit_kzalloc (test,1024 *sizeof (struct arm_smmu_cmd ),GFP_KERNEL ); + mock_valid_map =kunit_kzalloc (test,BITS_TO_LONGS (1024 ) *sizeof (long ),GFP_KERNEL ); + +KUNIT_ASSERT_NOT_NULL (test, mock_base); +KUNIT_ASSERT_NOT_NULL (test, mock_valid_map); + + smmu->features =0 ; +/* 1024 entries */ + cmdq->q.llq.max_n_shift =10 ; + cmdq->q.ent_dwords =CMDQ_ENT_DWORDS ; + cmdq->q.base = (__le64 *)mock_base; + cmdq->valid_map = (atomic_long_t *)mock_valid_map; + +/* Self-Consuming, prod == cons always ensures queue empty */ + cmdq->q.prod_reg = (__force u32 __iomem *)&mock->mock_prod_reg; + cmdq->q.cons_reg = (__force u32 __iomem *)&mock->mock_prod_reg; + +atomic_set (&cmdq->q.llq.atomic.prod,0 ); +atomic_set (&cmdq->q.llq.atomic.cons,0 ); +atomic_set (&cmdq->owner_prod,0 ); + mock->mock_prod_reg =0 ; +} + +struct arm_smmu_test_timer_context { +struct arm_smmu_device *smmu; +struct timer_list timer; +bool suspended; +}; + +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; + +/* Simulate a concurrent suspend event interrupting the invalidations */ +atomic_or (CMDQ_PROD_STOP_FLAG , &cmdq->q.llq.atomic.prod); +WRITE_ONCE (ctx->suspended, true); +} + +/* +* Verify SMMU PM Runtime gating, elision, and post-suspend resumption +* safety sequentially under active stress. +*/ +static void arm_smmu_v3_rpm_test_stress_race (struct kunit *test) +{ +struct arm_smmu_cmd cmd =arm_smmu_make_cmd_cfgi_all (); +struct arm_smmu_test_timer_context timer_ctx = {0 }; +struct arm_smmu_device mock_smmu = smmu; +struct arm_smmu_cmdq *cmdq = &mock_smmu.cmdq; +struct arm_smmu_mock_cmdq mock = {0 }; +u32 stopped_prod; +int i; + +arm_smmu_v3_test_init_mock_cmdq (test, &mock_smmu, &mock); + + timer_ctx.smmu = &mock_smmu; + +timer_setup (&timer_ctx.timer, arm_smmu_v3_test_rpm_timer_callback,0 ); +mod_timer (&timer_ctx.timer, jiffies +msecs_to_jiffies (10 )); + +/* Execute the unmap storm until the timer triggers */ +while (!READ_ONCE (timer_ctx.suspended)) { +if (arm_smmu_cmdq_issue_cmdlist (&mock_smmu, cmdq, &cmd,1 , false)) +break ; +usleep_range (50 ,100 ); + } + +timer_delete_sync (&timer_ctx.timer); + +/* Establish the post-storm prod_reg index */ + stopped_prod = mock.mock_prod_reg; + +/* +* Attempt multiple unmaps while the SMMU is disabled (STOP_GATE is set) +* Every single invalidation must get elided and return 0. The prod_reg +* shall remain completely frozen after all of these submissions. +*/ +for (i =0 ; i <1000 ; i++) { +if (arm_smmu_cmdq_issue_cmdlist (&mock_smmu, cmdq, &cmd,1 , false)) +break ; + } +KUNIT_EXPECT_EQ (test, stopped_prod, mock.mock_prod_reg); + +/* +* Clear the STOP_FLAG (resume the SMMU). A new invalidation must +* now successfully commit prod_idx & move the prod_reg by exactly 1. +*/ +atomic_andnot (CMDQ_PROD_STOP_FLAG , &cmdq->q.llq.atomic.prod); +KUNIT_EXPECT_EQ (test,0 ,arm_smmu_cmdq_issue_cmdlist (&mock_smmu, cmdq, &cmd,1 , false)); +KUNIT_EXPECT_EQ (test, stopped_prod +1 , mock.mock_prod_reg); +} + +struct arm_smmu_test_kthread_context { +struct arm_smmu_device *smmu; +int error; +}; + +static int arm_smmu_v3_test_kthread_worker (void *data) +{ +struct arm_smmu_cmd cmd =arm_smmu_make_cmd_cfgi_all (); +struct arm_smmu_test_kthread_context *ctx = data; +struct arm_smmu_cmdq *cmdq = &ctx->smmu->cmdq; + +while (!kthread_should_stop ()) { +if (arm_smmu_cmdq_issue_cmdlist (ctx->smmu, cmdq, &cmd,1 , false)) +WRITE_ONCE (ctx->error,1 ); +usleep_range (50 ,100 ); + } +return 0 ; +} + +static void arm_smmu_v3_rpm_test_kthread_race (struct kunit *test) +{ +struct arm_smmu_test_kthread_context ctx = {0 }; +struct arm_smmu_device mock_smmu = smmu; +struct arm_smmu_mock_cmdq mock = {0 }; +struct task_struct *thread1, *thread2; +u32 stopped_prod; + + ctx.smmu = &mock_smmu; +arm_smmu_v3_test_init_mock_cmdq (test, &mock_smmu, &mock); + + thread1 =kthread_run (arm_smmu_v3_test_kthread_worker, &ctx,"smmu_w1" ); + thread2 =kthread_run (arm_smmu_v3_test_kthread_worker, &ctx,"smmu_w2" ); +if (IS_ERR (thread1) ||IS_ERR (thread2)) { +if (!IS_ERR (thread1))kthread_stop (thread1); +if (!IS_ERR (thread2))kthread_stop (thread2); +return ; + } + +usleep_range (1000 ,2000 ); + +/* Gate the CMDQ */ +atomic_or (CMDQ_PROD_STOP_FLAG , &mock_smmu.cmdq.q.llq.atomic.prod); + stopped_prod = mock.mock_prod_reg; + +usleep_range (1000 ,2000 ); +KUNIT_EXPECT_EQ (test, stopped_prod, mock.mock_prod_reg); + +/* Open the gate */ +atomic_andnot (CMDQ_PROD_STOP_FLAG , &mock_smmu.cmdq.q.llq.atomic.prod); +usleep_range (1000 ,2000 ); +KUNIT_EXPECT_NE (test, stopped_prod, mock.mock_prod_reg); + +kthread_stop (thread1); +kthread_stop (thread2); +KUNIT_EXPECT_EQ (test,0 , ctx.error); +} +static struct kunit_case arm_smmu_v3_test_cases[] = {KUNIT_CASE (arm_smmu_v3_write_ste_test_bypass_to_abort),KUNIT_CASE (arm_smmu_v3_write_ste_test_abort_to_bypass),
@@ -797,6 +964,8 @@ static struct kunit_case arm_smmu_v3_test_cases[] = {KUNIT_CASE (arm_smmu_v3_write_cd_test_sva_clear),KUNIT_CASE (arm_smmu_v3_write_cd_test_sva_release),KUNIT_CASE (arm_smmu_v3_invs_test), +KUNIT_CASE (arm_smmu_v3_rpm_test_stress_race), + KUNIT_CASE(arm_smmu_v3_rpm_test_kthread_race), {}, };
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -0
@@ -873,6 +873,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.1013.g208068f2d8-goog
Re: [PATCH v8 02/12] iommu/arm-smmu-v3: Add a helper to drain cmd queues
In reply to Pranjal ShrivastavaQuoted 9 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9
-0+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 {
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 905
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+if (queue_empty (llq)) +break ; + + ret =queue_poll (&qp); +WRITE_ONCE (llq->cons,readl_relaxed (q->cons_reg)); + + }while (!ret); + +return ret; +}
Hmm, this is not the unified arm_smmu_drain_queue() that you sent me lately? Do you want me to change this function to that version in my series? I thought it could go in with yours.
Nicolin
Re: [PATCH v8 08/12] iommu/tegra241-cmdqv: Add a helper to quiesce VCMDQs
In reply to Pranjal Shrivastavadrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_impl_ops { · line 890
Quoted 6 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1@@ -887,7 +887,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); +int (*quiesce_and_drain_queues)(struct arm_smmu_device *smmu);
Hmm? What's the point in PATCH-3 adding drain_queues then?
Nicolin
Re: [PATCH v8 06/12] iommu/arm-smmu-v3: Handle gerror during suspend
In reply to Pranjal Shrivastavadrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) · line 2305
Quoted 7 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0@@ -2300,9 +2300,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 ; }
This seems unnecessary.
Nicolin
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Pranjal Shrivastavadrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 1236
Quoted 5 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1-static inline bool arm_smmu_can_elide (struct arm_smmu_device *smmu) +static inline bool arm_smmu_cmdq_can_elide (struct arm_smmu_device *smmu) {return !!Q_STOP (READ_ONCE (smmu->cmdq.q.llq.prod)); }
Can it be named perfectly at the beginning?
And again, should it check Q_STOP in vcmdq?
Nicolin
Re: [PATCH v8 02/12] iommu/arm-smmu-v3: Add a helper to drain cmd queues
In reply to Nicolin ChenQuoted 16 lines · Nicolin Chen
Quoted 15 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +15
-0+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)); +
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 905
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+ }while (!ret); + +return ret; +}Hmm, this is not the unified
arm_smmu_drain_queue()that you sent me lately? Do you want me to change this function to that version in my series? I thought it could go in with yours.
Yes.. I thought it could go with your series
If I were to include the full unified version here, I'd essentially have to include changes from the PRI support, which is still in its early stages. I'm keen to keep this RPM series standalone to avoid coupling its progress to the PRI review cycle.
Anyway it's just a clean up?
Praan
Re: [PATCH v8 06/12] iommu/arm-smmu-v3: Handle gerror during suspend
In reply to Nicolin ChenQuoted 10 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) · line 2305
Quoted 7 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0@@ -2300,9 +2300,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 ; }This seems unnecessary.
Will remove,
Thanks,
Praan
Re: [PATCH v8 08/12] iommu/tegra241-cmdqv: Add a helper to quiesce VCMDQs
In reply to Nicolin ChenQuoted 9 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_impl_ops { · line 890
Quoted 6 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1@@ -887,7 +887,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); +int (*quiesce_and_drain_queues)(struct arm_smmu_device *smmu);Hmm? What's the point in PATCH-3 adding
drain_queuesthen?
Well, you suggested in the previous version that STOP_FLAG needs to be set for the secondary CMDQs too
All we're doing here is renaming this function as it also sets the STOP_FLAG on impl-specific CMDQs now. We can still call it drain_queues if you prefer not renaming this.
On the other hand, we can add a dedicated queisce_queues() op too that's called before draining queues. I found that to be unnecessary as I'd rather have one impl-specific op handle everything for impl-specific queues.
Thanks,
Praan
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Nicolin ChenQuoted 8 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 1236
Quoted 5 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1-static inline bool arm_smmu_can_elide (struct arm_smmu_device *smmu) +static inline bool arm_smmu_cmdq_can_elide (struct arm_smmu_device *smmu) {return !!Q_STOP (READ_ONCE (smmu->cmdq.q.llq.prod)); }Can it be named perfectly at the beginning?
Ack.
Quoted 2 lines
And again, should it check
Q_STOPin vcmdq?
I don't think so.. the primary CMDQ's STOP_FLAG should act as a proxy for the SMMU's global power state. Since all queues (primary and secondary) are gated synchronously during suspend after SMMUEN=0, and ungated before SMMUEN=1 (i.e.before the caching can begin). Checking the primary queue should suffice..
We can also decide to drop the can_elide checks from invs_array and atc_inv_master altogether, leaving gerror & page_response the only remaining call-sites, both of which don't use secondary queues.
Praan
Re: [PATCH v8 02/12] iommu/arm-smmu-v3: Add a helper to drain cmd queues
In reply to Pranjal Shrivastavadrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 915
Quoted 9 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9
-0+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. +*/
From what I understand, the purpose of this lock is to ensure that only one thread updates smmu->cmdq.q.cons. The flag CMDQ_PROD_STOP_FLAG might block new threads from submitting commands to the command queue. However, other threads might have already passed that point and are about to update smmu->cmdq.q.cons. Hence, this assumption might not be safe.
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+ ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->cmdq.q); + +return ret; +}
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Pranjal Shrivastavadrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5684
Quoted 4 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+/* Drain the CMDQs */ + ret =arm_smmu_drain_queues (smmu); +if (ret) +dev_warn (smmu->dev,"failed to drain queues, forcing suspend\n" );
Can arm_smmu_drain_queues() be called after cmdq->lock is 0? I think that way we can be sure that no other thread updates smmu->cmdq.q.cons
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9
-0+ +/* 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" );
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Pranjal ShrivastavaQuoted 13 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 1236
Quoted 5 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1-static inline bool arm_smmu_can_elide (struct arm_smmu_device *smmu) +static inline bool arm_smmu_cmdq_can_elide (struct arm_smmu_device *smmu) {return !!Q_STOP (READ_ONCE (smmu->cmdq.q.llq.prod)); }Can it be named perfectly at the beginning?
Ack.
Quoted 1 line
Quoted 7 lines
Quoted 1 line
And again, should it check
Q_STOPin vcmdq?I don't think so.. the primary CMDQ's
STOP_FLAGshould act as a proxy for the SMMU's global power state. Since all queues (primary and secondary) are gated synchronously during suspend after SMMUEN=0, and ungated before SMMUEN=1 (i.e.before the caching can begin). Checking the primary queue should suffice..
Should leave a note inline to elaborate why we only check primary cmdq only.
[...]
Quoted 3 lines
We can also decide to drop the
can_elidechecks frominvs_arrayandatc_inv_masteraltogether, leaving gerror &page_responsethe only remaining call-sites, both of which don't use secondary queues.
That's not true.. In the host mode, any command will be issued to vcmdq. IOW, primary queue might not be used at all.
Nicolin
Re: [PATCH v8 08/12] iommu/tegra241-cmdqv: Add a helper to quiesce VCMDQs
In reply to Pranjal ShrivastavaQuoted 14 lines · Pranjal Shrivastava
Quoted 9 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_impl_ops { · line 890
Quoted 6 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1@@ -887,7 +887,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); +int (*quiesce_and_drain_queues)(struct arm_smmu_device *smmu);Hmm? What's the point in PATCH-3 adding
drain_queuesthen?Well, you suggested in the previous version that
STOP_FLAGneeds to be set for the secondary CMDQs too
Quoted 7 lines
All we're doing here is renaming this function as it also sets the
STOP_FLAGon impl-specific CMDQs now. We can still call itdrain_queuesif you prefer not renaming this.On the other hand, we can add a dedicated
queisce_queues()op too that's called before draining queues. I found that to be unnecessary as I'd rather have one impl-specific op handle everything for impl-specific queues.
The renaming is very confusing... I'd leave it as drain_queues.
Arguably,
atomic_or(CMDQ_PROD_STOP_FLAG, &vcmdq->cmdq.q.llq.atomic.prod);
could be even stuffed into tegra241_cmdqv_drain_vintf0_lvcmdqs(), where there is a for loop already.
Nicolin
Quoted 7 lines · Pranjal Shrivastava
[v8]
- Centralized elision logic: dropped redundant checks from invalidation call-sites; elision is now authoritative within the CMDQ layer.
- Renamed elision helper toarm_smmu_cmdq_can_elide()and preserved it only for diagnostic/safety paths (ATC, GERROR, Page Response).
- Consolidated implementation-specific gating and draining into a unifiedquiesce_and_drain_queuescallback.
Quoted 8 lines
- Updated tegra241-cmdqv to gate virtual queues before draining, addressing non-deterministic timeouts from guest-side submissions.
- Re-ordered probe sequence to enablepm_runtimeonly at the end, aligning with SMMUv2 and simplifying error paths.
- Refactored KUnit tests for better, addressed Nicolin's comments & added a multi-threaded kthread race test.
- Refactored RPM helpers to use early-return patterns for improved clarity.
- Collected R-bs from Nicolin.
FWIW, v8 is still missing a base-commit and Sashiko fails to review:
https://sashiko.dev/#/patchset/20260601215909.3958732-1-praan%40google.com
Please make sure v9 patches are formatted with "--base=" and a public commit.
Nicolin
Re: [PATCH v8 08/12] iommu/tegra241-cmdqv: Add a helper to quiesce VCMDQs
In reply to Nicolin ChenQuoted 19 lines · Nicolin Chen
Quoted 18 lines · Pranjal Shrivastava
Quoted 9 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_impl_ops { · line 890
Quoted 6 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1@@ -887,7 +887,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); +int (*quiesce_and_drain_queues)(struct arm_smmu_device *smmu);Hmm? What's the point in PATCH-3 adding
drain_queuesthen?Well, you suggested in the previous version that
STOP_FLAGneeds to be set for the secondary CMDQs tooAll we're doing here is renaming this function as it also sets the
STOP_FLAGon impl-specific CMDQs now. We can still call itdrain_queuesif you prefer not renaming this.
Quoted 5 lines
Quoted 3 lines
On the other hand, we can add a dedicated
queisce_queues()op too that's called before draining queues. I found that to be unnecessary as I'd rather have one impl-specific op handle everything for impl-specific queues.The renaming is very confusing... I'd leave it as
drain_queues.
Ack.
Quoted 5 lines
Arguably,
atomic_or(CMDQ_PROD_STOP_FLAG, &vcmdq->cmdq.q.llq.atomic.prod);could be even stuffed into
tegra241_cmdqv_drain_vintf0_lvcmdqs(), where there is a for loop already.
That would be slow IMO, because we'd set the the STOP_FLAG on ONE queue, then wait for it to drain and repeat this pattern for all vCMDQs. Ideally, we should set the STOP_FLAG on all vCMDQs first and then wait for them to drain.
Thanks,
Praan
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Nicolin ChenQuoted 16 lines · Nicolin Chen
Quoted 15 lines · Pranjal Shrivastava
Quoted 8 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 1236
Quoted 5 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1-static inline bool arm_smmu_can_elide (struct arm_smmu_device *smmu) +static inline bool arm_smmu_cmdq_can_elide (struct arm_smmu_device *smmu) {return !!Q_STOP (READ_ONCE (smmu->cmdq.q.llq.prod)); }Can it be named perfectly at the beginning?
Ack.
Quoted 2 lines
And again, should it check
Q_STOPin vcmdq?
Quoted 8 lines
Quoted 5 lines
I don't think so.. the primary CMDQ's
STOP_FLAGshould act as a proxy for the SMMU's global power state. Since all queues (primary and secondary) are gated synchronously during suspend after SMMUEN=0, and ungated before SMMUEN=1 (i.e.before the caching can begin). Checking the primary queue should suffice..Should leave a note inline to elaborate why we only check primary cmdq only.
Ack.
Quoted 9 lines
[...]
Quoted 3 lines
We can also decide to drop the
can_elidechecks frominvs_arrayandatc_inv_masteraltogether, leaving gerror &page_responsethe only remaining call-sites, both of which don't use secondary queues.That's not true.. In the host mode, any command will be issued to vcmdq. IOW, primary queue might not be used at all.
Alright, I see the supports_cmd is a Guest-only thing. I'll add not to the can_elide helper.
Praan
Re: [PATCH v8 08/12] iommu/tegra241-cmdqv: Add a helper to quiesce VCMDQs
In reply to Pranjal ShrivastavaQuoted 8 lines · Pranjal Shrivastava
Quoted 6 lines · Nicolin Chen
Quoted 1 line · Pranjal Shrivastava
On Mon, Jun 01, 2026 at 05:14:24PM -0700, Nicolin Chen wrote:
Arguably,
atomic_or(CMDQ_PROD_STOP_FLAG, &vcmdq->cmdq.q.llq.atomic.prod);could be even stuffed into
tegra241_cmdqv_drain_vintf0_lvcmdqs(), where there is a for loop already.
Quoted 4 lines
That would be slow IMO, because we'd set the the
STOP_FLAGon ONE queue, then wait for it to drain and repeat this pattern for all vCMDQs. Ideally, we should set theSTOP_FLAGon all vCMDQs first and then wait for them to drain.
That sounds plausible. Please leave a note for the double loops.
Nicolin
Quoted 17 lines · Nicolin Chen
Quoted 7 lines · Pranjal Shrivastava
[v8]
- Centralized elision logic: dropped redundant checks from invalidation call-sites; elision is now authoritative within the CMDQ layer.
- Renamed elision helper toarm_smmu_cmdq_can_elide()and preserved it only for diagnostic/safety paths (ATC, GERROR, Page Response).
- Consolidated implementation-specific gating and draining into a unifiedquiesce_and_drain_queuescallback.Quoted 8 lines
- Updated tegra241-cmdqv to gate virtual queues before draining, addressing non-deterministic timeouts from guest-side submissions.
- Re-ordered probe sequence to enablepm_runtimeonly at the end, aligning with SMMUv2 and simplifying error paths.
- Refactored KUnit tests for better, addressed Nicolin's comments & added a multi-threaded kthread race test.
- Refactored RPM helpers to use early-return patterns for improved clarity.
- Collected R-bs from Nicolin.
Quoted 5 lines
FWIW, v8 is still missing a base-commit and Sashiko fails to review:
https://sashiko.dev/#/patchset/20260601215909.3958732-1-praan%40google.comPlease make sure v9 patches are formatted with "--base=" and a public commit.
Ah :/ that's a shame.. I gave --base=74fa4c177ad0 to format patch which worked for a different series [1] but not this one for some reason?
I'll ensure to add it in v9.
[1] https://lore.kernel.org/all/[email protected]/
Thanks,
Praan
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Daniel MentzQuoted 10 lines · Daniel Mentz
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5684
Quoted 4 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+/* Drain the CMDQs */ + ret =arm_smmu_drain_queues (smmu); +if (ret) +dev_warn (smmu->dev,"failed to drain queues, forcing suspend\n" );Can
arm_smmu_drain_queues()be called aftercmdq->lockis 0? I think that way we can be sure that no other thread updatessmmu->cmdq.q.cons
Hmm.. that wouldn't work for impl-specific CMDQs as we STOP and drain them together in a single function.. maybe I'll factor out setting the STOP_FLAG on impl-specific queues as another impl_op, gate the queues first, wait for lock == 0 and then drain_queues?
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5693
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9
-0+ +/* 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" );
Thanks,
Praan
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Pranjal Shrivastavadrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static const char * const event_class_str[] = { · line 145
Quoted 3 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3
-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 ;
Is this masking necessary? Can't we just use atomic_read(&cmdq->owner_prod) as is? The only place in arm_smmu_cmdq_issue_cmdlist() that writes to cmdq->owner_prod already applied CMDQ_PROD_IDX_MASK.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4880
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+} +@@ -4839,6 +4876,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);
Same. I believe CMDQ_PROD_STOP_FLAG will never be set in smmu->cmdq.owner_prod. Hence, clearing it shouldn't be necessary.
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0+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 ());@@ -4898,6 +4939,21 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Pranjal ShrivastavaQuoted 17 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13
-0@@ -2361,8 +2394,33 @@ static irqreturn_t arm_smmu_handle_gerror(struct arm_smmu_device *smmu)static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) {struct arm_smmu_device *smmu = dev; +irqreturn_t ret; + +/* +* Global Errors are only processed if the SMMU is active. +* +* If the STOP_FLAG is set (can_elide == true), the hardware is +* either already disabled or in the process of being disabled. +* Any errors captured during the quiesce/drain phase will be +* handled by the explicit arm_smmu_handle_gerror() call at the +* end of arm_smmu_runtime_suspend() callback. On resume, the +* STOP_FLAG is cleared before interrupts are re-enabled, ensuring +* no valid errors are missed. +*
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static irqreturn_t arm_smmu_handle_gerror(struct arm_smmu_device *smmu) · line 2419
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+* A lockless check is favoured here over a dynamic PM core check +* since the runtime_pm_get_if_active would return false during +* transient states like RPM_RESUMING & ignore level-triggered +* interrupts. +*/ +if (arm_smmu_cmdq_can_elide (smmu)) { +dev_err (smmu->dev, +"Ignoring gerror interrupt because the SMMU is suspended\n" ); +return IRQ_NONE ; + }
Have you considered using arm_smmu_rpm_get() here instead? I can see two issues with the currenlty proposal:
* Returning IRQ_NONE when an interrupt is indeed active and needs to
be handled. This might be interpreted as a spurious interrupt
* Nothing is preventing the suspend handler from running whilearm_smmu_gerror_handler is in the middle of handling an interrupt
I understand that using arm_smmu_rpm_get() also has downsides, including an unnecessary resume operation when the SMMU is already in RPM_SUSPENDING state. However, using arm_smmu_rpm_get() would make it easier to ensure correctness.
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3
-1+ + ret =arm_smmu_handle_gerror (smmu); -return arm_smmu_handle_gerror (smmu); +return ret; }
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 36 lines · Daniel Mentz
Quoted 17 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13
-0@@ -2361,8 +2394,33 @@ static irqreturn_t arm_smmu_handle_gerror(struct arm_smmu_device *smmu)static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) {struct arm_smmu_device *smmu = dev; +irqreturn_t ret; + +/* +* Global Errors are only processed if the SMMU is active. +* +* If the STOP_FLAG is set (can_elide == true), the hardware is +* either already disabled or in the process of being disabled. +* Any errors captured during the quiesce/drain phase will be +* handled by the explicit arm_smmu_handle_gerror() call at the +* end of arm_smmu_runtime_suspend() callback. On resume, the +* STOP_FLAG is cleared before interrupts are re-enabled, ensuring +* no valid errors are missed. +* drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static irqreturn_t arm_smmu_handle_gerror(struct arm_smmu_device *smmu) · line 2419
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+* A lockless check is favoured here over a dynamic PM core check +* since the runtime_pm_get_if_active would return false during +* transient states like RPM_RESUMING & ignore level-triggered +* interrupts. +*/ +if (arm_smmu_cmdq_can_elide (smmu)) { +dev_err (smmu->dev, +"Ignoring gerror interrupt because the SMMU is suspended\n" ); +return IRQ_NONE ; + }Have you considered using
arm_smmu_rpm_get()here instead? I can see two issues with the currenlty proposal:
* ReturningIRQ_NONEwhen an interrupt is indeed active and needs to
be handled. This might be interpreted as a spurious interrupt
* Nothing is preventing the suspend handler from running whilearm_smmu_gerror_handleris in the middle of handling an interrupt
Quoted 5 lines
I understand that using
arm_smmu_rpm_get()also has downsides, including an unnecessary resume operation when the SMMU is already inRPM_SUSPENDINGstate. However, usingarm_smmu_rpm_get()would make it easier to ensure correctness.
I don't think using arm_smmu_rpm_get() here is possible..
GERROR is registered as a hard IRQ handler, so calling rpm_get (which can sleep) would be wrong.
Regarding the race, the STOP_FLAG is set at the very beginning of the suspend sequence. If an IRQ fires after that, we return IRQ_NONE and let the explicit arm_smmu_handle_gerror() call at the end of runtime_suspend catch and clear it. After CMDQEN, PRIQEN, EVTQEN & SMMUEN are all cleared, getting a Gerror should be treated as spurious
That said, I understand your concerns about a real IRQ being interpreted as a spurious one, and creating an IRQ storm since the gerror register isn't really written. I have 2 ideas here:
1. We could have a "suspended" flag and check it with can_elide here:arm_smmu_cmdq_can_elide() && is_suspended() to correctly return IRQ_NONE
2. We could explicitly disable Gerror in IRQ_CTRL write after setting the CMDQ_STOP_FLAG. Even if there are Gerrors during the CMDQ drain, we'll catcup to those at the end of our suspend callback.
I'm more inclined towards 2 as it prevents potential races (execution of an IRQ handler with handle_gerror calls at the end of the suspend).
WDYT?
Thanks.
Praan
Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
In reply to Pranjal ShrivastavaQuoted 45 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 cmpxchg 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.Suggested-by Daniel Mentz <[email protected]>Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>--- 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 +2
-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;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 731
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+/* +* 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 ;
On second thought, I no longer believe that this is safe. I understand that READ_ONCE(cmdq->q.llq.val) implies no ordering guarantees with respect to any writes to translation tables. In the non-stopped case, we can rely on the call to dma_wmb() further down in this function. However, for the stopped case, I can't identify any barriers that would ensure that the STOP flag is checked only after the writes are visible to SMMU. Here is an example: Let's assume the following program order:
* Write to invalidate PTE
* Read from cmdq->q.llq.val, determine STOP flag is set, elide TLBI
What prevents the CPU from reordering these operations as follows?
* Read from cmdq->q.llq.val, determine STOP flag is set, elide TLBI
* Write to invalidate PTE
Can the following situation occur?
* Read from cmdq->q.llq.val, determine STOP flag is set, elide TLBI
* (Different CPU resumes SMMU)
* SMMU loads old PTE value into TLB
* Write to invalidate PTE
* (stale PTE remains in TLB)
I propose the following: If you find the STOP flag set, run dma_mb() and check again. I'm afraid that running dma_mb() unconditionally might incur too much of a performance penalty.
I have the same concerns with arm_smmu_cmdq_can_elide(): That READ_ONCE in arm_smmu_cmdq_can_elide() provides no guarantees in regards to ordering relative to PTE writes.
Quoted 13 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7
-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);
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Pranjal ShrivastavaQuoted 8 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0@@ -4898,6 +4939,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
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4955
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+* 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 ();
I'm not convinced that this is necessary. I understand that the write to smmu->cmdq.q.llq.atomic.prod needs to be ordered before setting CR0_SMMUEN in ARM_SMMU_CR0. However, this ordering requirement appears to already be met by the dma_wmb() in arm_smmu_cmdq_issue_cmdlist. Could you provide an example of a scenario that might fail if this smp_mb() were removed?
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0+/* Enable the SMMU interface */ enables |=CR0_SMMUEN ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Pranjal ShrivastavaQuoted 44 lines · Pranjal Shrivastava
Quoted 36 lines · Daniel Mentz
Quoted 17 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13
-0@@ -2361,8 +2394,33 @@ static irqreturn_t arm_smmu_handle_gerror(struct arm_smmu_device *smmu)static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) {struct arm_smmu_device *smmu = dev; +irqreturn_t ret; + +/* +* Global Errors are only processed if the SMMU is active. +* +* If the STOP_FLAG is set (can_elide == true), the hardware is +* either already disabled or in the process of being disabled. +* Any errors captured during the quiesce/drain phase will be +* handled by the explicit arm_smmu_handle_gerror() call at the +* end of arm_smmu_runtime_suspend() callback. On resume, the +* STOP_FLAG is cleared before interrupts are re-enabled, ensuring +* no valid errors are missed. +* drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static irqreturn_t arm_smmu_handle_gerror(struct arm_smmu_device *smmu) · line 2419
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+* A lockless check is favoured here over a dynamic PM core check +* since the runtime_pm_get_if_active would return false during +* transient states like RPM_RESUMING & ignore level-triggered +* interrupts. +*/ +if (arm_smmu_cmdq_can_elide (smmu)) { +dev_err (smmu->dev, +"Ignoring gerror interrupt because the SMMU is suspended\n" ); +return IRQ_NONE ; + }Have you considered using
arm_smmu_rpm_get()here instead? I can see two issues with the currenlty proposal:
* ReturningIRQ_NONEwhen an interrupt is indeed active and needs to
be handled. This might be interpreted as a spurious interrupt
* Nothing is preventing the suspend handler from running whilearm_smmu_gerror_handleris in the middle of handling an interruptQuoted 5 lines
I understand that using
arm_smmu_rpm_get()also has downsides, including an unnecessary resume operation when the SMMU is already inRPM_SUSPENDINGstate. However, usingarm_smmu_rpm_get()would make it easier to ensure correctness.
Quoted 4 lines
I don't think using
arm_smmu_rpm_get()here is possible..GERROR is registered as a hard IRQ handler, so calling
rpm_get(which can sleep) would be wrong.
You're right. Sorry, I missed that arm_smmu_gerror_handler is registered as a hard irq handler.
Quoted 13 lines
Regarding the race, the
STOP_FLAGis set at the very beginning of the suspend sequence. If an IRQ fires after that, we returnIRQ_NONEand let the explicitarm_smmu_handle_gerror()call at the end ofruntime_suspendcatch and clear it. After CMDQEN, PRIQEN, EVTQEN & SMMUEN are all cleared, getting a Gerror should be treated as spuriousThat said, I understand your concerns about a real IRQ being interpreted as a spurious one, and creating an IRQ storm since the gerror register isn't really written. I have 2 ideas here:
1. We could have a "suspended" flag and check it with
can_elidehere:arm_smmu_cmdq_can_elide()&&is_suspended()to correctly returnIRQ_NONE
Quoted 8 lines
2. We could explicitly disable Gerror in
IRQ_CTRLwrite after setting theCMDQ_STOP_FLAG. Even if there are Gerrors during the CMDQ drain, we'll catcup to those at the end of our suspend callback.I'm more inclined towards 2 as it prevents potential races (execution of an IRQ handler with
handle_gerrorcalls at the end of the suspend).WDYT?
I'm not sure if I have a good suggestion here. Have you considered the
following: Do not call arm_smmu_handle_gerror() from arm_smmu_runtime_suspend(). Instead, call disable_irq() at the end of the suspend handler (and enable_irq() at the beginning of the resume handler)?
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Pranjal ShrivastavaQuoted 17 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +17
-0+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;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5665
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+ } + +/* +* 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);
I'm wondering if we need the non-relaxed version of atomic_fetch_or() here to benefit from the barrier guarantees. Otherwise, how do you ensure that CMDQ_PROD_STOP_FLAG isn't set before SMMUEN is cleared?
Quoted 1 line
+ target &=
CMDQ_PROD_IDX_MASK;
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Pranjal ShrivastavaQuoted 9 lines · Pranjal Shrivastava
Quoted 7 lines · Daniel Mentz
Quoted 4 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+/* Drain the CMDQs */ + ret =arm_smmu_drain_queues (smmu); +if (ret) +dev_warn (smmu->dev,"failed to drain queues, forcing suspend\n" );
Quoted 8 lines
Quoted 3 lines
Can
arm_smmu_drain_queues()be called aftercmdq->lockis 0? I think that way we can be sure that no other thread updatessmmu->cmdq.q.consHmm.. that wouldn't work for impl-specific CMDQs as we STOP and drain them together in a single function.. maybe I'll factor out setting the
STOP_FLAGon impl-specific queues as anotherimpl_op, gate the queues first, wait for lock == 0 and thendrain_queues?
I'm not familiar with impl-specific CMDQs, but what you are suggesting sounds plausible to me.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9
-0+ +/* 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" );
Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
In reply to Daniel MentzQuoted 19 lines · Daniel Mentz
Quoted 18 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 cmpxchg 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.
[...]
Quoted 29 lines
Quoted 6 lines
static void queue_poll_init(struct
arm_smmu_device*smmu,drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0@@ -718,8 +719,25 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, do { u64 old;drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 731
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+/* +* 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 ;On second thought, I no longer believe that this is safe. I understand that
READ_ONCE(cmdq->q.llq.val)implies no ordering guarantees with respect to any writes to translation tables. In the non-stopped case, we can rely on the call todma_wmb()further down in this function. However, for the stopped case, I can't identify any barriers that would ensure that the STOP flag is checked only after the writes are visible to SMMU. Here is an example: Let's assume the following program order:* Write to invalidate PTE
* Read fromcmdq->q.llq.val, determine STOP flag is set, elide TLBI
Quoted 6 lines
What prevents the CPU from reordering these operations as follows?
* Read from
cmdq->q.llq.val, determine STOP flag is set, elide TLBI
* Write to invalidate PTECan the following situation occur?
Not really because:
Quoted 3 lines
* Read from
cmdq->q.llq.val, determine STOP flag is set, elide TLBI
* (Different CPU resumes SMMU)
We do a full smp_mb() here to ensure all writes are seen before SMMUEN=1 (note that the STOP_FLAG is already unset at that point, so we stop eliding commands much before the SMMU is physically able to access any config/xlation structures, I've explained everything below).
Quoted 7 lines
* SMMU loads old PTE value into TLB
* Write to invalidate PTE
* (stale PTE remains in TLB)I propose the following: If you find the STOP flag set, run
dma_mb()and check again. I'm afraid that runningdma_mb()unconditionally might incur too much of a performance penalty.
IMHO, I think this might be overcomplicating things here.. Here's why the current version works according to me:
Till SMMUEN=0, the SMMU is spec-guaranteed to never access translation or configuration structures (Section 6.3.9.6).
Our runtime_suspend callback sets SMMUEN=0 before setting the STOP_FLAG.
Even if the worker CPU reorders the PTE write after the STOP_FLAG check, it is benign because the SMMU is incapable of fetching that (or any) PTE while the gate is closed. Because GATE_CLOSED == SMMUEN = 0, implying no access to any HW structures whatsoever.
The real synchronization happens in the Resume Path:
1. arm_smmu_device_reset() clears all caches / TLBs. (None of these can have entries before SMMUEN=1)
2. We execute a full smp_mb() before setting SMMUEN=1. (that's why we need smp_mb before SMMUEN=1). This barrier ensures that any PTE writes made by any thread—including those that were elided while the gate was closed, are globally visible before the SMMU hardware starts fetching into TLBs again. (This is why Jason suggested this in v6 [1])
Adding a dma_mb() to the elision path would be redundant given the SMMU can't access any structures and the resume barrier. We'd be needlessly injecting dma_mbs when no entity is actually accessing those structures while the STOP_FLAG is set.
Quoted 5 lines
I have the same concerns with
arm_smmu_cmdq_can_elide(): ThatREAD_ONCEinarm_smmu_cmdq_can_elide()provides no guarantees in regards to ordering relative to PTE writes.
The same as above, No structures are accessed during SMMUEN=0 (spec) and during resume before setting SMMUEN=1, we do a full smp_mb() to ensure all concurrent PTE writes are acquired in our resume thread before we enable SMMUEN=1
Thanks,
Praan
Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
Quoted 26 lines · Pranjal Shrivastava
Quoted 26 lines
Quoted 6 lines
static void queue_poll_init(struct
arm_smmu_device*smmu,drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0@@ -718,8 +719,25 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, do { u64 old;drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 731
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+/* +* 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 ;On second thought, I no longer believe that this is safe. I understand that
READ_ONCE(cmdq->q.llq.val)implies no ordering guarantees with respect to any writes to translation tables. In the non-stopped case, we can rely on the call todma_wmb()further down in this function. However, for the stopped case, I can't identify any barriers that would ensure that the STOP flag is checked only after the writes are visible to SMMU. Here is an example: Let's assume the following program order:
Quoted 7 lines
Quoted 7 lines
* Write to invalidate PTE
* Read fromcmdq->q.llq.val, determine STOP flag is set, elide TLBIWhat prevents the CPU from reordering these operations as follows?
* Read from
cmdq->q.llq.val, determine STOP flag is set, elide TLBI
* Write to invalidate PTE
Now, I believe that the smp_mb() call in arm_smmu_domain_inv_range() actually prevents this (if we remove arm_smmu_cmdq_can_elide or move it to after the smp_mb() call)
Quoted 22 lines
Quoted 2 lines
Can the following situation occur?
Not really because:
Quoted 3 lines
* Read from
cmdq->q.llq.val, determine STOP flag is set, elide TLBI
* (Different CPU resumes SMMU)We do a full
smp_mb()here to ensure all writes are seen before SMMUEN=1 (note that theSTOP_FLAGis already unset at that point, so we stop eliding commands much before the SMMU is physically able to access any config/xlation structures, I've explained everything below).Quoted 7 lines
* SMMU loads old PTE value into TLB
* Write to invalidate PTE
* (stale PTE remains in TLB)I propose the following: If you find the STOP flag set, run
dma_mb()and check again. I'm afraid that runningdma_mb()unconditionally might incur too much of a performance penalty.
Quoted 7 lines
IMHO, I think this might be overcomplicating things here.. Here's why the current version works according to me:
Till SMMUEN=0, the SMMU is spec-guaranteed to never access translation or configuration structures (Section 6.3.9.6).
Our
runtime_suspendcallback sets SMMUEN=0 before setting theSTOP_FLAG.
Does "before" refer to program order or the order in which other agents can observe the STOP_FLAG relative to when SMMUEN=0 is set? Your program might read "Set SMMUEN=0 then set STOP_FLAG". However, my question is whether other observers might see the STOP_FLAG before SMMUEN is cleared. I couldn't identify any barriers in arm_smmu_runtime_suspend() that would prevent this type of re-ordering.
Quoted 7 lines
Even if the worker CPU reorders the PTE write after the
STOP_FLAGcheck, it is benign because the SMMU is incapable of fetching that (or any) PTE while the gate is closed. BecauseGATE_CLOSED== SMMUEN = 0, implying no access to any HW structures whatsoever.The real synchronization happens in the Resume Path:
Quoted 8 lines
1.
arm_smmu_device_reset()clears all caches / TLBs. (None of these can have entries before SMMUEN=1)2. We execute a full
smp_mb()before setting SMMUEN=1. (that's why we needsmp_mbbefore SMMUEN=1). This barrier ensures that any PTE writes made by any thread—including those that were elided while the gate was closed, are globally visible before the SMMU hardware starts fetching into TLBs again. (This is why Jason suggested this in v6 [1])
A barrier on one CPU has no bearing on whether writes by any other CPU can be observed by any particular agent in the system.
Let's compare this with the long comment in arm_smmu_domain_inv_range() which is what I believe Jason was referring to. In that example, you see smp_mb() in the code path on CPU0 and dma_wmb() in the code path on CPU1. Hence, barriers exist on both sides. If you compare the runtime PM design with arm_smmu_domain_inv_range(), then smp_mb() belongs in the CPU thread that performs the translation table updates not the one that performs the suspend/resume operation.
Actually, now that I think of it: We could just piggy pack on the existing smp_mb() in arm_smmu_domain_inv_range(). For your arm_smmu_cmdq_can_elide() to work correctly, though, you would need to move it to *after* the smp_mb() call. This being said, I would prefer to remove arm_smmu_cmdq_can_elide() as I wrote on the other thread. If we do take advantage of the existing smp_mb() in arm_smmu_domain_inv_range(), then we should probably update that comment to state that the runtime PM implementation now also depends on it.
Quoted 11 lines
Adding a
dma_mb()to the elision path would be redundant given the SMMU can't access any structures and the resume barrier. We'd be needlessly injectingdma_mbswhen no entity is actually accessing those structures while theSTOP_FLAGis set.Quoted 5 lines
I have the same concerns with
arm_smmu_cmdq_can_elide(): ThatREAD_ONCEinarm_smmu_cmdq_can_elide()provides no guarantees in regards to ordering relative to PTE writes.
Quoted 4 lines
The same as above, No structures are accessed during SMMUEN=0 (spec) and during resume before setting SMMUEN=1, we do a full
smp_mb()to ensure all concurrent PTE writes are acquired in our resume thread before we enable SMMUEN=1
I think the definition of "all concurrent PTE writes" is a bit vague. I don't think the architecture allows you to wait for writes performed by other CPUs to be observable by all other agents. The only thing you can do is to ensure that other agents can observe that the STOP_FLAG has been cleared *before* SMMUEN is set. However, this is already achieved by the existing dma_wmb() in arm_smmu_cmdq_issue_cmdlist(). Hence, the extra smp_mb() is not required.
Quoted 5 lines
Thanks,
Praan
Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
Quoted 69 lines · Daniel Mentz
Quoted 26 lines · Pranjal Shrivastava
Quoted 26 lines
Quoted 6 lines
static void queue_poll_init(struct
arm_smmu_device*smmu,drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0@@ -718,8 +719,25 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, do { u64 old;drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, · line 731
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+/* +* 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 ;On second thought, I no longer believe that this is safe. I understand that
READ_ONCE(cmdq->q.llq.val)implies no ordering guarantees with respect to any writes to translation tables. In the non-stopped case, we can rely on the call todma_wmb()further down in this function. However, for the stopped case, I can't identify any barriers that would ensure that the STOP flag is checked only after the writes are visible to SMMU. Here is an example: Let's assume the following program order:Quoted 7 lines
Quoted 7 lines
* Write to invalidate PTE
* Read fromcmdq->q.llq.val, determine STOP flag is set, elide TLBIWhat prevents the CPU from reordering these operations as follows?
* Read from
cmdq->q.llq.val, determine STOP flag is set, elide TLBI
* Write to invalidate PTENow, I believe that the
smp_mb()call inarm_smmu_domain_inv_range()actually prevents this (if we removearm_smmu_cmdq_can_elideor move it to after thesmp_mb()call)Quoted 22 lines
Quoted 2 lines
Can the following situation occur?
Not really because:
Quoted 3 lines
* Read from
cmdq->q.llq.val, determine STOP flag is set, elide TLBI
* (Different CPU resumes SMMU)We do a full
smp_mb()here to ensure all writes are seen before SMMUEN=1 (note that theSTOP_FLAGis already unset at that point, so we stop eliding commands much before the SMMU is physically able to access any config/xlation structures, I've explained everything below).Quoted 7 lines
* SMMU loads old PTE value into TLB
* Write to invalidate PTE
* (stale PTE remains in TLB)I propose the following: If you find the STOP flag set, run
dma_mb()and check again. I'm afraid that runningdma_mb()unconditionally might incur too much of a performance penalty.Quoted 7 lines
IMHO, I think this might be overcomplicating things here.. Here's why the current version works according to me:
Till SMMUEN=0, the SMMU is spec-guaranteed to never access translation or configuration structures (Section 6.3.9.6).
Our
runtime_suspendcallback sets SMMUEN=0 before setting theSTOP_FLAG.
Quoted 7 lines
Does "before" refer to program order or the order in which other agents can observe the
STOP_FLAGrelative to when SMMUEN=0 is set? Your program might read "Set SMMUEN=0 then setSTOP_FLAG". However, my question is whether other observers might see theSTOP_FLAGbefore SMMUEN is cleared. I couldn't identify any barriers inarm_smmu_runtime_suspend()that would prevent this type of re-ordering.
Hmm.. that's right. I guess I was wrongly relying on the fact that _relaxed() variants are guaranteed to be ordered amongst themselves on the same CPU but reading the barriers documentation [1] I see:
These are similar to readX() and writeX(),
[...] but they are still guaranteed to be ordered with respect to other accesses from the same CPU thread to the same peripheral when operating on __iomem pointers mapped with the default I/O attributes.
Hence, it seems it is possible for RAM writes to be re-ordered before the writeX_relaxed(MMIO).
In that case, I guess we could just use the non-relaxed version to set the STOP_FLAG...
Quoted 20 lines
Quoted 8 lines
Even if the worker CPU reorders the PTE write after the
STOP_FLAGcheck, it is benign because the SMMU is incapable of fetching that (or any) PTE while the gate is closed. BecauseGATE_CLOSED== SMMUEN = 0, implying no access to any HW structures whatsoever.The real synchronization happens in the Resume Path:
Quoted 8 lines
1.
arm_smmu_device_reset()clears all caches / TLBs. (None of these can have entries before SMMUEN=1)2. We execute a full
smp_mb()before setting SMMUEN=1. (that's why we needsmp_mbbefore SMMUEN=1). This barrier ensures that any PTE writes made by any thread—including those that were elided while the gate was closed, are globally visible before the SMMU hardware starts fetching into TLBs again. (This is why Jason suggested this in v6 [1])A barrier on one CPU has no bearing on whether writes by any other CPU can be observed by any particular agent in the system.
Quoted 9 lines
Let's compare this with the long comment in
arm_smmu_domain_inv_range()which is what I believe Jason was referring to. In that example, you seesmp_mb()in the code path on CPU0 anddma_wmb()in the code path on CPU1. Hence, barriers exist on both sides. If you compare the runtime PM design witharm_smmu_domain_inv_range(), thensmp_mb()belongs in the CPU thread that performs the translation table updates not the one that performs the suspend/resume operation.
I might be missing something here, so please bear with me. My understanding it that's needed because the IOMMU is live & actively caching, which is not true for our case.
[Assuming we use non-relaxed semantics & ordering for the STOP flag, i.e. set STOP_FLAG + barrier & clear STOP_FLAG (implicit dma_wmb())]
In our case, during the resume op, we first clear the STOP_FLAG before setting SMMUEN=1 in program order. Thus, any PTE invalidations occurring before SMMUEN=1 are executed, i.e. EVEN when the SMMU is guaranteed not to access any structures, we've resumed invalidations.
Let's consider a few examples:
1. SUSPEND (say CPU0 is suspending)
[CPU0] SMMUEN = 0 ==> SMMU stops accessing HW structures (ABORT NOT set)
HW structures not accessed means no TLB / CFG cache accesses as well according to the spec.
[CPU1] ==> PTE update => Invalidate => Succeeds (although SMMUEN = 0)
[CPU0] GBPA.Abort set ==> Txns are blocked
[CPU2] => PTE update => Invalidate => Succeeds [Txns blocked + SMMUEN=0]
[CPU0] ==> SET STOP_FLAG ==> Elision begins
[CPU3] ==> PTE update ==> Invalidation ==> Elided [Txns blocked + SMMUEN=0]
Hence, the races in the suspend sequence are handled correctly.
2. RESUME (say CPU0 is resuming)
[CPU1] ==> Update PTE ==> Invalidate ==> Elided [Txns blocked + SMMUEN=0]
[CPU0] ==> Clear STOP_FLAGs [Txns still blocked + SMMUEN=0]
[CPU2] ==> Update PTE ==> Invalidate ==> Succeeds [Txns blocked + SMMUEN=0]
[CPU0] ==> Invalidate all TLB ==> Succeeds [Txns still blocked + SMMUEN=0] [CPU0] ==> Invalidate all CFG ==> Succeeds [Txns still blocked + SMMUEN=0]
[CPU2] ==> Update PTE ==> Invalidate ==> Succeeds [Txns still blocked + SMMUEN=0]
[CPU0] ==> Set SMMUEN = 1 [SMMU can now access in memory structures]
However, the TLBs and CFG caches are clean because everything until this point couldn't have cached anything anyway.
Hence, right after clearing the STOP_FLAG, we're taking in invalidations as normal in the resume, much before the real caching can begin.
Thus, by resuming invalidations before SMMUEN=1, we guarantee a consistent state before the very first translation is performed.
Apart from this, I guess I'll drop the can_elide check from all invalidation paths.
Does that sound fine?
Quoted 26 lines
Actually, now that I think of it: We could just piggy pack on the existing
smp_mb()inarm_smmu_domain_inv_range(). For yourarm_smmu_cmdq_can_elide()to work correctly, though, you would need to move it to *after* thesmp_mb()call. This being said, I would prefer to removearm_smmu_cmdq_can_elide()as I wrote on the other thread. If we do take advantage of the existingsmp_mb()inarm_smmu_domain_inv_range(), then we should probably update that comment to state that the runtime PM implementation now also depends on it.Quoted 11 lines
Adding a
dma_mb()to the elision path would be redundant given the SMMU can't access any structures and the resume barrier. We'd be needlessly injectingdma_mbswhen no entity is actually accessing those structures while theSTOP_FLAGis set.Quoted 5 lines
I have the same concerns with
arm_smmu_cmdq_can_elide(): ThatREAD_ONCEinarm_smmu_cmdq_can_elide()provides no guarantees in regards to ordering relative to PTE writes.Quoted 4 lines
The same as above, No structures are accessed during SMMUEN=0 (spec) and during resume before setting SMMUEN=1, we do a full
smp_mb()to ensure all concurrent PTE writes are acquired in our resume thread before we enable SMMUEN=1
Quoted 8 lines
I think the definition of "all concurrent PTE writes" is a bit vague. I don't think the architecture allows you to wait for writes performed by other CPUs to be observable by all other agents. The only thing you can do is to ensure that other agents can observe that the
STOP_FLAGhas been cleared *before* SMMUEN is set. However, this is already achieved by the existingdma_wmb()inarm_smmu_cmdq_issue_cmdlist(). Hence, the extrasmp_mb()is not required.
Right, I meant that the smp_mb was needed for the STOP_FLAG is visible to other CPUs performing PTE writes.. but I agree that the first CFGI invalidation's dma_wmb() would ensure the STOP_FLAG is visible to all other CPUs, I'll drop this and add a comment explaining this.
Thanks,
Praan
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Daniel MentzQuoted 24 lines · Daniel Mentz
Quoted 23 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +23
-0+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).
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5665
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+*/ + +/* 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);I'm wondering if we need the non-relaxed version of
atomic_fetch_or()here to benefit from the barrier guarantees. Otherwise, how do you ensure thatCMDQ_PROD_STOP_FLAGisn't set before SMMUEN is cleared?
Ack. I agree we need a non-relaxed version, I missed that the STOP_FLAG is purely RAM & was focused on the _relaxed variants to keep things ordered as all were MMIO. I'll drop the relaxed semantics with the STOP_FLAG.
Quoted 2 lines
Quoted 2 lines
+ target &=
CMDQ_PROD_IDX_MASK;
Thanks,
Praan
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Daniel MentzQuoted 18 lines · Daniel Mentz
Quoted 17 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13
-0@@ -4898,6 +4939,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. +*/
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4955
Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0+smp_mb ();I'm not convinced that this is necessary. I understand that the write to
smmu->cmdq.q.llq.atomic.prodneeds to be ordered before settingCR0_SMMUENinARM_SMMU_CR0. However, this ordering requirement appears to already be met by thedma_wmb()inarm_smmu_cmdq_issue_cmdlist. Could you provide an example of a scenario that might fail if thissmp_mb()were removed?
Agreed. The first dma_wmb() in the issue_cmdlist will handle this. We don't need this smp_mb(); I'll add a note as specified in [1]
Thanks,
Praan
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Daniel MentzQuoted 9 lines · Daniel Mentz
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static const char * const event_class_str[] = { · line 145
Quoted 3 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3
-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 ;Is this masking necessary? Can't we just use
atomic_read(&cmdq->owner_prod)as is? The only place inarm_smmu_cmdq_issue_cmdlist()that writes tocmdq->owner_prodalready appliedCMDQ_PROD_IDX_MASK.
Ack. I'll drop the masking.
Quoted 8 lines
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0+} +@@ -4839,6 +4876,10 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)return ret; }
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4880
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0+/* Clear any flags from the previous life */ +atomic_andnot (CMDQ_PROD_STOP_FLAG , &smmu->cmdq.owner_prod);Same. I believe
CMDQ_PROD_STOP_FLAGwill never be set insmmu->cmdq.owner_prod. Hence, clearing it shouldn't be necessary.
Ack. I'll drop this.
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0+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 ());@@ -4898,6 +4939,21 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
Thanks,
Praan
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Daniel MentzQuoted 73 lines · Daniel Mentz
Quoted 44 lines · Pranjal Shrivastava
Quoted 36 lines · Daniel Mentz
Quoted 17 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13
-0@@ -2361,8 +2394,33 @@ static irqreturn_t arm_smmu_handle_gerror(struct arm_smmu_device *smmu)static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) {struct arm_smmu_device *smmu = dev; +irqreturn_t ret; + +/* +* Global Errors are only processed if the SMMU is active. +* +* If the STOP_FLAG is set (can_elide == true), the hardware is +* either already disabled or in the process of being disabled. +* Any errors captured during the quiesce/drain phase will be +* handled by the explicit arm_smmu_handle_gerror() call at the +* end of arm_smmu_runtime_suspend() callback. On resume, the +* STOP_FLAG is cleared before interrupts are re-enabled, ensuring +* no valid errors are missed. +* drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static irqreturn_t arm_smmu_handle_gerror(struct arm_smmu_device *smmu) · line 2419
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+* A lockless check is favoured here over a dynamic PM core check +* since the runtime_pm_get_if_active would return false during +* transient states like RPM_RESUMING & ignore level-triggered +* interrupts. +*/ +if (arm_smmu_cmdq_can_elide (smmu)) { +dev_err (smmu->dev, +"Ignoring gerror interrupt because the SMMU is suspended\n" ); +return IRQ_NONE ; + }Have you considered using
arm_smmu_rpm_get()here instead? I can see two issues with the currenlty proposal:
* ReturningIRQ_NONEwhen an interrupt is indeed active and needs to
be handled. This might be interpreted as a spurious interrupt
* Nothing is preventing the suspend handler from running whilearm_smmu_gerror_handleris in the middle of handling an interruptQuoted 5 lines
I understand that using
arm_smmu_rpm_get()also has downsides, including an unnecessary resume operation when the SMMU is already inRPM_SUSPENDINGstate. However, usingarm_smmu_rpm_get()would make it easier to ensure correctness.Quoted 4 lines
I don't think using
arm_smmu_rpm_get()here is possible..GERROR is registered as a hard IRQ handler, so calling
rpm_get(which can sleep) would be wrong.You're right. Sorry, I missed that
arm_smmu_gerror_handleris registered as a hard irq handler.Quoted 20 lines
Regarding the race, the
STOP_FLAGis set at the very beginning of the suspend sequence. If an IRQ fires after that, we returnIRQ_NONEand let the explicitarm_smmu_handle_gerror()call at the end ofruntime_suspendcatch and clear it. After CMDQEN, PRIQEN, EVTQEN & SMMUEN are all cleared, getting a Gerror should be treated as spuriousThat said, I understand your concerns about a real IRQ being interpreted as a spurious one, and creating an IRQ storm since the gerror register isn't really written. I have 2 ideas here:
1. We could have a "suspended" flag and check it with
can_elidehere:arm_smmu_cmdq_can_elide()&&is_suspended()to correctly returnIRQ_NONE2. We could explicitly disable Gerror in
IRQ_CTRLwrite after setting theCMDQ_STOP_FLAG. Even if there are Gerrors during the CMDQ drain, we'll catcup to those at the end of our suspend callback.I'm more inclined towards 2 as it prevents potential races (execution of an IRQ handler with
handle_gerrorcalls at the end of the suspend).
Quoted 7 lines
Quoted 1 line
WDYT?
I'm not sure if I have a good suggestion here. Have you considered the
following: Do not callarm_smmu_handle_gerror()fromarm_smmu_runtime_suspend(). Instead, calldisable_irq()at the end of the suspend handler (andenable_irq()at the beginning of the resume handler)?
I thought about using disable_irq(), but I think doing it at the hardware level (IRQ_CTRL) is better.
By disabling in IRQ_CTRL and keeping the manual arm_smmu_handle_gerror() call at the end of suspend, we ensure that we don't lose any gerror info We catch and handle any errors that occurred during the drain/quiesce phase right before the power-down.
Thanks,
Praan
Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
In reply to Pranjal ShrivastavaQuoted 30 lines · Pranjal Shrivastava
Quoted 20 lines
Quoted 8 lines
Even if the worker CPU reorders the PTE write after the
STOP_FLAGcheck, it is benign because the SMMU is incapable of fetching that (or any) PTE while the gate is closed. BecauseGATE_CLOSED== SMMUEN = 0, implying no access to any HW structures whatsoever.The real synchronization happens in the Resume Path:
Quoted 8 lines
1.
arm_smmu_device_reset()clears all caches / TLBs. (None of these can have entries before SMMUEN=1)2. We execute a full
smp_mb()before setting SMMUEN=1. (that's why we needsmp_mbbefore SMMUEN=1). This barrier ensures that any PTE writes made by any thread—including those that were elided while the gate was closed, are globally visible before the SMMU hardware starts fetching into TLBs again. (This is why Jason suggested this in v6 [1])A barrier on one CPU has no bearing on whether writes by any other CPU can be observed by any particular agent in the system.
Quoted 9 lines
Let's compare this with the long comment in
arm_smmu_domain_inv_range()which is what I believe Jason was referring to. In that example, you seesmp_mb()in the code path on CPU0 anddma_wmb()in the code path on CPU1. Hence, barriers exist on both sides. If you compare the runtime PM design witharm_smmu_domain_inv_range(), thensmp_mb()belongs in the CPU thread that performs the translation table updates not the one that performs the suspend/resume operation.
Quoted 3 lines
I might be missing something here, so please bear with me. My understanding it that's needed because the IOMMU is live & actively caching, which is not true for our case.
I think the "invs" design (Per-domain invalidation array) is more similar than you think! An SMMU being absent from invs is equivalent to the STOP flag, and the STE pointing to TTB0 is roughly the equivalent of SMMEN=1 i.e. the IOMMU is not actively caching a particular translation domain until an STE (or CD) points to it.
Quoted 7 lines
[Assuming we use non-relaxed semantics & ordering for the STOP flag, i.e. set
STOP_FLAG+ barrier & clearSTOP_FLAG(implicitdma_wmb())]In our case, during the resume op, we first clear the
STOP_FLAGbefore setting SMMUEN=1 in program order. Thus, any PTE invalidations occurring before SMMUEN=1 are executed, i.e. EVEN when the SMMU is guaranteed not to access any structures, we've resumed invalidations.
"[...] we first clear the STOP_FLAG before setting SMMUEN=1 in program order." I think this should be modified to "we first clear the STOP_FLAG and ensure that the cleared STOP_FLAG is observable by all other CPUs before setting SMMUEN=1"
Re "Thus, any PTE invalidations occurring before SMMUEN=1 are executed,": I think that "a PTE invalidation occurring" is not clearly defined. Also, it's not clear to me what this statement implies. It's paramount that invalidations are performed when SMMUEN=1. The fact that we perform invalidations before SMMUEN=1 is more of a side effect of our methodology.
I would define a set of invariants:
* If an agent observes the STOP flag, it is guaranteed that SMMUEN=0
(with ABORT set) at the time of observation.
* Any transition from a set STOP flag to SMMUEN=1 involves an
invalidate-all operation prior to setting SMMUEN=1
Hence, if a CPU observes the STOP flag, it is assured that (a) transactions are blocked and (b) if the SMMU is ever re-enabled, an invalidate-all is performed prior to it being enabled.
I would then argue that all operations support these invariants. For example, we need proper barriers in the iommu_unmap path to ensure that the STOP flag is only checked *after* the translation table update is made. Hence, we need a memory barrier.
I look at it this way: Every elided invalidation creates an "invalidation deficit", and this deficit is tolerable for two reasons:
(a) SMMU blocks all transactions while there is a deficit. (b) An invalidate-all eliminates any deficit accrued while the STOP flag was set.
Quoted 5 lines
Let's consider a few examples:
1. SUSPEND (say CPU0 is suspending)
[CPU0] SMMUEN = 0 ==> SMMU stops accessing HW structures (ABORT NOT set)
I thought we never disable the SMMU unless ABORT is set.
Quoted 7 lines
HW structures not accessed means no TLB / CFG cache accesses as well according to the spec.
[CPU1] ==> PTE update => Invalidate => Succeeds (although SMMUEN = 0)
[CPU0] GBPA.Abort set ==> Txns are blocked
Quoted 7 lines
[CPU2] => PTE update => Invalidate => Succeeds [Txns blocked + SMMUEN=0]
[CPU0] ==> SET
STOP_FLAG==> Elision begins[CPU3] ==> PTE update ==> Invalidation ==> Elided [Txns blocked + SMMUEN=0]
Hence, the races in the suspend sequence are handled correctly.
I'm not sure if this description demonstrates that every possible race is handled correctly. If I compare this with Nicolin's presentation in arm_smmu_domain_inv_range, I like that presentation, as it explicitly mentions loads and barriers. For example, it has an smp_mb() followed by "// load the updated invs". I think you should make have something like "smp_mb() ; CHECK STOP_FLAG" in your presentation. Currently, the STOP_FLAG checking is somehow implicit in "Invalidation".
Quoted 9 lines
2. RESUME (say CPU0 is resuming)
[CPU1] ==> Update PTE ==> Invalidate ==> Elided [Txns blocked + SMMUEN=0]
[CPU0] ==> Clear
STOP_FLAGs[Txns still blocked + SMMUEN=0][CPU2] ==> Update PTE ==> Invalidate ==> Succeeds [Txns blocked + SMMUEN=0]
Quoted 8 lines
[CPU0] ==> Invalidate all TLB ==> Succeeds [Txns still blocked + SMMUEN=0] [CPU0] ==> Invalidate all CFG ==> Succeeds [Txns still blocked + SMMUEN=0]
[CPU2] ==> Update PTE ==> Invalidate ==> Succeeds [Txns still blocked + SMMUEN=0]
[CPU0] ==> Set SMMUEN = 1 [SMMU can now access in memory structures]
However, the TLBs and CFG caches are clean because everything until this point couldn't have cached anything anyway.
My concern with this diagram is that it appears sequential, suggesting operations happen in a specific order across CPUs when they, in fact, occur in parallel. I find these diagrams more useful for describing failure cases than for proving that every race is handled correctly.
Quoted 11 lines
Hence, right after clearing the
STOP_FLAG, we're taking in invalidations as normal in the resume, much before the real caching can begin.Thus, by resuming invalidations before SMMUEN=1, we guarantee a consistent state before the very first translation is performed.
Apart from this, I guess I'll drop the
can_elidecheck from all invalidation paths.Does that sound fine?
Dropping can_elide sounds fine. However, if you still use this function, for example in the gerror handler, then you might consider renaming it.
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Pranjal ShrivastavaQuoted 6 lines · Pranjal Shrivastava
Quoted 5 lines
I'm not sure if I have a good suggestion here. Have you considered the
following: Do not callarm_smmu_handle_gerror()fromarm_smmu_runtime_suspend(). Instead, calldisable_irq()at the end of the suspend handler (andenable_irq()at the beginning of the resume handler)?
Quoted 7 lines
I thought about using
disable_irq(), but I think doing it at the hardware level (IRQ_CTRL) is better.By disabling in
IRQ_CTRLand keeping the manualarm_smmu_handle_gerror()call at the end of suspend, we ensure that we don't lose any gerror info We catch and handle any errors that occurred during the drain/quiesce phase right before the power-down.
I think the beauty of disable_irq() is that it synchronizes any potentially running hard IRQ handler, which helps avoid races.
Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
In reply to Daniel MentzQuoted 49 lines · Daniel Mentz
Quoted 30 lines · Pranjal Shrivastava
Quoted 20 lines
Quoted 8 lines
Even if the worker CPU reorders the PTE write after the
STOP_FLAGcheck, it is benign because the SMMU is incapable of fetching that (or any) PTE while the gate is closed. BecauseGATE_CLOSED== SMMUEN = 0, implying no access to any HW structures whatsoever.The real synchronization happens in the Resume Path:
Quoted 8 lines
1.
arm_smmu_device_reset()clears all caches / TLBs. (None of these can have entries before SMMUEN=1)2. We execute a full
smp_mb()before setting SMMUEN=1. (that's why we needsmp_mbbefore SMMUEN=1). This barrier ensures that any PTE writes made by any thread—including those that were elided while the gate was closed, are globally visible before the SMMU hardware starts fetching into TLBs again. (This is why Jason suggested this in v6 [1])A barrier on one CPU has no bearing on whether writes by any other CPU can be observed by any particular agent in the system.
Quoted 9 lines
Let's compare this with the long comment in
arm_smmu_domain_inv_range()which is what I believe Jason was referring to. In that example, you seesmp_mb()in the code path on CPU0 anddma_wmb()in the code path on CPU1. Hence, barriers exist on both sides. If you compare the runtime PM design witharm_smmu_domain_inv_range(), thensmp_mb()belongs in the CPU thread that performs the translation table updates not the one that performs the suspend/resume operation.Quoted 3 lines
I might be missing something here, so please bear with me. My understanding it that's needed because the IOMMU is live & actively caching, which is not true for our case.
I think the "invs" design (Per-domain invalidation array) is more similar than you think! An SMMU being absent from invs is equivalent to the STOP flag, and the STE pointing to TTB0 is roughly the equivalent of SMMEN=1 i.e. the IOMMU is not actively caching a particular translation domain until an STE (or CD) points to it.
Quoted 7 lines
[Assuming we use non-relaxed semantics & ordering for the STOP flag, i.e. set
STOP_FLAG+ barrier & clearSTOP_FLAG(implicitdma_wmb())]In our case, during the resume op, we first clear the
STOP_FLAGbefore setting SMMUEN=1 in program order. Thus, any PTE invalidations occurring before SMMUEN=1 are executed, i.e. EVEN when the SMMU is guaranteed not to access any structures, we've resumed invalidations.
Quoted 5 lines
"[...] we first clear the
STOP_FLAGbefore setting SMMUEN=1 in program order." I think this should be modified to "we first clear theSTOP_FLAGand ensure that the clearedSTOP_FLAGis observable by all other CPUs before setting SMMUEN=1"
Ack. The goal was to explain the algorithm for this thread, I won't be commenting it in code. Are you suggesting I should convert my explaination of the algorithm above into in-line comments and make sure to include the STOP_FLAG observability part?
Quoted 6 lines
Re "Thus, any PTE invalidations occurring before SMMUEN=1 are executed,": I think that "a PTE invalidation occurring" is not clearly defined. Also, it's not clear to me what this statement implies. It's paramount that invalidations are performed when SMMUEN=1. The fact that we perform invalidations before SMMUEN=1 is more of a side effect of our methodology.
By "invalidations occurring before SMMUEN=1", I mean that once the STOP_FLAG is cleared, any concurrent unmaps (PTE updates) will successfully submit invalidation cmds to the cmdq. Since SMMU translation (SMMUEN) is still disabled at this point, these invalidations execute safely without racing against SMMU caching (which was the concern in the previous email).
Quoted 17 lines
I would define a set of invariants:
* If an agent observes the STOP flag, it is guaranteed that SMMUEN=0
(with ABORT set) at the time of observation.
* Any transition from a set STOP flag to SMMUEN=1 involves an
invalidate-all operation prior to setting SMMUEN=1Hence, if a CPU observes the STOP flag, it is assured that (a) transactions are blocked and (b) if the SMMU is ever re-enabled, an invalidate-all is performed prior to it being enabled.
I would then argue that all operations support these invariants. For example, we need proper barriers in the
iommu_unmappath to ensure that the STOP flag is only checked *after* the translation table update is made. Hence, we need a memory barrier.
Quoted 5 lines
I look at it this way: Every elided invalidation creates an "invalidation deficit", and this deficit is tolerable for two reasons:
(a) SMMU blocks all transactions while there is a deficit. (b) An invalidate-all eliminates any deficit accrued while the STOP flag was set.
Ack, which means you agree with the design proposed in my last reply. I'll document these invariants in line if that's what you're suggesting here?
Quoted 8 lines
Quoted 6 lines
Let's consider a few examples:
1. SUSPEND (say CPU0 is suspending)
[CPU0] SMMUEN = 0 ==> SMMU stops accessing HW structures (ABORT NOT set)
I thought we never disable the SMMU unless ABORT is set.
Yes, that is evident from the code [1]. I made a mistake in ordering while trying to explain why & how this works.
Quoted 17 lines
Quoted 9 lines
HW structures not accessed means no TLB / CFG cache accesses as well according to the spec.
[CPU1] ==> PTE update => Invalidate => Succeeds (although SMMUEN = 0)
[CPU0] GBPA.Abort set ==> Txns are blocked
Quoted 7 lines
[CPU2] => PTE update => Invalidate => Succeeds [Txns blocked + SMMUEN=0]
[CPU0] ==> SET
STOP_FLAG==> Elision begins[CPU3] ==> PTE update ==> Invalidation ==> Elided [Txns blocked + SMMUEN=0]
Hence, the races in the suspend sequence are handled correctly.
Quoted 7 lines
I'm not sure if this description demonstrates that every possible race is handled correctly. If I compare this with Nicolin's presentation in
arm_smmu_domain_inv_range, I like that presentation, as it explicitly mentions loads and barriers. For example, it has ansmp_mb()followed by "// load the updated invs". I think you should make have something like "smp_mb(); CHECKSTOP_FLAG" in your presentation. Currently, theSTOP_FLAGchecking is somehow implicit in "Invalidation".
Ack. The goal of this diagram was to explain the working of the design, this is NOT the comment/document I plan to include in code.
I'll add this as an in-line comment if that's what you're suggesting? OR are you also suggesting I should have this in my cover letter?
Quoted 15 lines
Quoted 15 lines
2. RESUME (say CPU0 is resuming)
[CPU1] ==> Update PTE ==> Invalidate ==> Elided [Txns blocked + SMMUEN=0]
[CPU0] ==> Clear
STOP_FLAGs[Txns still blocked + SMMUEN=0][CPU2] ==> Update PTE ==> Invalidate ==> Succeeds [Txns blocked + SMMUEN=0]
[CPU0] ==> Invalidate all TLB ==> Succeeds [Txns still blocked + SMMUEN=0] [CPU0] ==> Invalidate all CFG ==> Succeeds [Txns still blocked + SMMUEN=0]
[CPU2] ==> Update PTE ==> Invalidate ==> Succeeds [Txns still blocked + SMMUEN=0]
Quoted 9 lines
Quoted 3 lines
[CPU0] ==> Set SMMUEN = 1 [SMMU can now access in memory structures]
However, the TLBs and CFG caches are clean because everything until this point couldn't have cached anything anyway.My concern with this diagram is that it appears sequential, suggesting operations happen in a specific order across CPUs when they, in fact, occur in parallel. I find these diagrams more useful for describing failure cases than for proving that every race is handled correctly.
Ack. Again, the goal was to explain it to you and not propose how the comments are gonna look like. I'll use your preferred way to explain in the future.
Quoted 7 lines
Quoted 7 lines
Hence, right after clearing the
STOP_FLAG, we're taking in invalidations as normal in the resume, much before the real caching can begin.Thus, by resuming invalidations before SMMUEN=1, we guarantee a consistent state before the very first translation is performed.
Quoted 8 lines
Quoted 4 lines
Apart from this, I guess I'll drop the
can_elidecheck from all invalidation paths.Does that sound fine?
Dropping
can_elidesounds fine. However, if you still use this function, for example in the gerror handler, then you might consider renaming it.
Ack, I'll name it back to arm_smmu_is_suspended().
Thanks,
Praan
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Daniel MentzQuoted 10 lines · Daniel Mentz
Quoted 9 lines · Pranjal Shrivastava
Quoted 5 lines
I'm not sure if I have a good suggestion here. Have you considered the
following: Do not callarm_smmu_handle_gerror()fromarm_smmu_runtime_suspend(). Instead, calldisable_irq()at the end of the suspend handler (andenable_irq()at the beginning of the resume handler)?I thought about using
disable_irq(), but I think doing it at the hardware level (IRQ_CTRL) is better.
Quoted 7 lines
Quoted 4 lines
By disabling in
IRQ_CTRLand keeping the manualarm_smmu_handle_gerror()call at the end of suspend, we ensure that we don't lose any gerror info We catch and handle any errors that occurred during the drain/quiesce phase right before the power-down.I think the beauty of
disable_irq()is that it synchronizes any potentially running hard IRQ handler, which helps avoid races.
Alright, I guess we could:
1. SMMUEN=0
2. IRQ_CTRL.gerror=0
3. disable_irq();
4. handle_gerror(); at the end of suspend
Thanks,
Praan
Re: [PATCH v8 11/12] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 17 lines · Pranjal Shrivastava
Quoted 15 lines · Daniel Mentz
Quoted 13 lines · Pranjal Shrivastava
Quoted 5 lines
I'm not sure if I have a good suggestion here. Have you considered the
following: Do not callarm_smmu_handle_gerror()fromarm_smmu_runtime_suspend(). Instead, calldisable_irq()at the end of the suspend handler (andenable_irq()at the beginning of the resume handler)?I thought about using
disable_irq(), but I think doing it at the hardware level (IRQ_CTRL) is better.By disabling in
IRQ_CTRLand keeping the manualarm_smmu_handle_gerror()call at the end of suspend, we ensure that we don't lose any gerror info We catch and handle any errors that occurred during the drain/quiesce phase right before the power-down.
Quoted 7 lines
Quoted 2 lines
I think the beauty of
disable_irq()is that it synchronizes any potentially running hard IRQ handler, which helps avoid races.Alright, I guess we could:
1. SMMUEN=0
2. IRQ_CTRL.gerror=0
Should we call arm_smmu_disable_irqs() here?
Quoted 2 lines
3.
disable_irq();
4.handle_gerror(); at the end of suspend
Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Add CMDQ_PROD_STOP_FLAG to gate CMDQ submissions
In reply to Pranjal ShrivastavaQuoted 57 lines · Pranjal Shrivastava
Quoted 49 lines · Daniel Mentz
Quoted 30 lines · Pranjal Shrivastava
Quoted 20 lines
Quoted 8 lines
Even if the worker CPU reorders the PTE write after the
STOP_FLAGcheck, it is benign because the SMMU is incapable of fetching that (or any) PTE while the gate is closed. BecauseGATE_CLOSED== SMMUEN = 0, implying no access to any HW structures whatsoever.The real synchronization happens in the Resume Path:
Quoted 8 lines
1.
arm_smmu_device_reset()clears all caches / TLBs. (None of these can have entries before SMMUEN=1)2. We execute a full
smp_mb()before setting SMMUEN=1. (that's why we needsmp_mbbefore SMMUEN=1). This barrier ensures that any PTE writes made by any thread—including those that were elided while the gate was closed, are globally visible before the SMMU hardware starts fetching into TLBs again. (This is why Jason suggested this in v6 [1])A barrier on one CPU has no bearing on whether writes by any other CPU can be observed by any particular agent in the system.
Quoted 9 lines
Let's compare this with the long comment in
arm_smmu_domain_inv_range()which is what I believe Jason was referring to. In that example, you seesmp_mb()in the code path on CPU0 anddma_wmb()in the code path on CPU1. Hence, barriers exist on both sides. If you compare the runtime PM design witharm_smmu_domain_inv_range(), thensmp_mb()belongs in the CPU thread that performs the translation table updates not the one that performs the suspend/resume operation.Quoted 3 lines
I might be missing something here, so please bear with me. My understanding it that's needed because the IOMMU is live & actively caching, which is not true for our case.
I think the "invs" design (Per-domain invalidation array) is more similar than you think! An SMMU being absent from invs is equivalent to the STOP flag, and the STE pointing to TTB0 is roughly the equivalent of SMMEN=1 i.e. the IOMMU is not actively caching a particular translation domain until an STE (or CD) points to it.
Quoted 7 lines
[Assuming we use non-relaxed semantics & ordering for the STOP flag, i.e. set
STOP_FLAG+ barrier & clearSTOP_FLAG(implicitdma_wmb())]In our case, during the resume op, we first clear the
STOP_FLAGbefore setting SMMUEN=1 in program order. Thus, any PTE invalidations occurring before SMMUEN=1 are executed, i.e. EVEN when the SMMU is guaranteed not to access any structures, we've resumed invalidations.Quoted 5 lines
"[...] we first clear the
STOP_FLAGbefore setting SMMUEN=1 in program order." I think this should be modified to "we first clear theSTOP_FLAGand ensure that the clearedSTOP_FLAGis observable by all other CPUs before setting SMMUEN=1"
Quoted 4 lines
Ack. The goal was to explain the algorithm for this thread, I won't be commenting it in code. Are you suggesting I should convert my explaination of the algorithm above into in-line comments and make sure to include the
STOP_FLAGobservability part?
I believe there would be a benefit to having a comment in the code that states the requirements for the STOP_FLAG observability. Something along the lines of either of the following:
* SMMU must be disabled any time another CPU can observe the STOP_FLAG
* Other CPUs must (a) observe the STOP flag only after the SMMU is disabled, and (b) observe the cleared STOP flag before the SMMU is re-enabled.
Quoted 23 lines
Quoted 17 lines
I would define a set of invariants:
* If an agent observes the STOP flag, it is guaranteed that SMMUEN=0
(with ABORT set) at the time of observation.
* Any transition from a set STOP flag to SMMUEN=1 involves an
invalidate-all operation prior to setting SMMUEN=1Hence, if a CPU observes the STOP flag, it is assured that (a) transactions are blocked and (b) if the SMMU is ever re-enabled, an invalidate-all is performed prior to it being enabled.
I would then argue that all operations support these invariants. For example, we need proper barriers in the
iommu_unmappath to ensure that the STOP flag is only checked *after* the translation table update is made. Hence, we need a memory barrier.Quoted 5 lines
I look at it this way: Every elided invalidation creates an "invalidation deficit", and this deficit is tolerable for two reasons:
(a) SMMU blocks all transactions while there is a deficit. (b) An invalidate-all eliminates any deficit accrued while the STOP flag was set.
Quoted 3 lines
Ack, which means you agree with the design proposed in my last reply. I'll document these invariants in line if that's what you're suggesting here?
Yes, I believe we are in agreement, and yes, I think documenting these invariants would be beneficial.
[...]
Quoted 25 lines
Quoted 17 lines
Quoted 9 lines
HW structures not accessed means no TLB / CFG cache accesses as well according to the spec.
[CPU1] ==> PTE update => Invalidate => Succeeds (although SMMUEN = 0)
[CPU0] GBPA.Abort set ==> Txns are blocked
Quoted 7 lines
[CPU2] => PTE update => Invalidate => Succeeds [Txns blocked + SMMUEN=0]
[CPU0] ==> SET
STOP_FLAG==> Elision begins[CPU3] ==> PTE update ==> Invalidation ==> Elided [Txns blocked + SMMUEN=0]
Hence, the races in the suspend sequence are handled correctly.
Quoted 7 lines
I'm not sure if this description demonstrates that every possible race is handled correctly. If I compare this with Nicolin's presentation in
arm_smmu_domain_inv_range, I like that presentation, as it explicitly mentions loads and barriers. For example, it has ansmp_mb()followed by "// load the updated invs". I think you should make have something like "smp_mb(); CHECKSTOP_FLAG" in your presentation. Currently, theSTOP_FLAGchecking is somehow implicit in "Invalidation".
Quoted 5 lines
Ack. The goal of this diagram was to explain the working of the design, this is NOT the comment/document I plan to include in code.
I'll add this as an in-line comment if that's what you're suggesting? OR are you also suggesting I should have this in my cover letter?
In arm_smmu_domain_inv_range(), there's Nicolin's sequence diagram that describes how installing a new domain is synchronized with PTE updates and subsequent TLBIs. I would recommend adding a similar diagram that shows how suspend and resume are synchronized with PTE updates and TLBIs.
Thanks
Daniel
Re: [PATCH v8 03/12] iommu/tegra241-cmdqv: Add a helper to drain VCMDQs
In reply to Pranjal ShrivastavaQuoted 55 lines · Pranjal Shrivastava
The 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_opsto drain implementation specified queues and call it withinarm_smmu_drain_queues.Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>--- 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 +1
-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)
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu, · line 427
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +10
-0+{ +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 ;
I am not familiar with this driver, but the READ_ONCE() caught my eye, I see that’s already what is the existing code is doing, but it is not clear to me why, it seems to be an attempt to make this path lockless.
However, won’t we need some aquire/release semantics?
For example in tegra241_vintf_hw_deinit() it WRITE_ONCE() cmdq and then vintf and finally writel() with a write memory barrier.
While in tegra241_cmdqv_drain_vintf0_lvcmdqs() (or in tegra241_cmdqv_get_cmdq()) it checks READ_ONCE(vintf->enabled) then READ_ONCE(vcmdq->enabled)
Now it is possible that this executes in any order, due to the lack of barriers,which means you can see:
Thread#1: READ_ONCE(vintf->enabled) => TRUE
Thread#2: Writes both vintf->enabled and vcmdq->enabled to FALSE Thread#1: Still sees vcmdq->enabled as TRUE because it was speculated.
Am I missing something?
Thanks,
Mostafa
Quoted 30 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +16
-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.1013.g208068f2d8-goog
Re: [PATCH v8 04/12] iommu/tegra241-cmdqv: Restore PROD and CONS after resume
In reply to Pranjal ShrivastavaQuoted 6 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.
What commands are exisiting at resume? Won't tegra241_cmdqv_drain_vintf0_lvcmdqs() drain the queues and make the PROD and CONS equal each other anyway?
Thanks,
Mostafa
Quoted 25 lines
Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Ashish Mhetre <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>--- 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.1013.g208068f2d8-goog
Re: [PATCH v8 05/12] iommu/arm-smmu-v3: Cache and restore MSI config
In reply to Pranjal ShrivastavaQuoted 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 7 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.Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>
Thanks,
Mostafa
Quoted 63 lines
--- 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.1013.g208068f2d8-goog
Re: [PATCH v8 04/12] iommu/tegra241-cmdqv: Restore PROD and CONS after resume
In reply to Mostafa SalehQuoted 11 lines · Mostafa Saleh
Quoted 6 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.
What commands are exisiting at resume? Won't
tegra241_cmdqv_drain_vintf0_lvcmdqs()drain the queues and make the PROD and CONS equal each other anyway?
The SW prod / cons are indeed equal because of the queue drain before suspend. However, the HW registers in vCMDQ reset to 0 on a power-cycle
Now, if we resume with the software prod/cons at 100/100, since the HW prod/cons is at 0/0, when the driver then issues a new command and only updates the PROD register to 101, the HW will see (CONS=0, PROD=101).
The arm-smmu-v3 driver restores these indices correctly in device_reset but the Tegra VMDQV driver doesn't. Thus, this patch was added to restore both indices ensuring the HW doesn't de-sync.
This was added after Ashish tested an older version of the series [1] with Tegra,
Thanks,
Praan
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
In reply to Pranjal ShrivastavaQuoted 100 lines · Pranjal Shrivastava
Implement
pm_runtimeand 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 register writes and tracking updates.The resume callback restores the MSI configuration and performs a full device reset via
arm_smmu_device_resetto bring the SMMU back to an operational state. The MSIs are cached during themsi_writeand are restored during the resume operation by using the helper. TheSTOP_FLAGis cleared only after the CMDQ is enabled in hardware.Suggested-by Daniel Mentz <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 172 +++++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 18 ++ 2 files changed, 188 insertions(+), 2 deletions(-)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +39
-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,40 @@ 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)) +return 0 ; + + 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)) +return ; + + 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 +824,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. @@ -4828,7 +4864,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 ;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4881
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3
-0@@ -4839,6 +4876,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);
Should not that be done from the suspend call?
Quoted 12 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +5
-0+/* Invalidate any cached configuration */ arm_smmu_cmdq_issue_cmd_with_sync (smmu,arm_smmu_make_cmd_cfgi_all ());@@ -4898,6 +4939,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
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4955
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+* 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 ();
Should not that be dma_wmb() as this is syncing with the HW?
Quoted 25 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +18
-0+/* Enable the SMMU interface */ enables |=CR0_SMMUEN ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,@@ -5580,6 +5636,117 @@ 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;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5665
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+ } + +/* +* 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);
As Daniel mentioned, I think this shouldn't be relaxed.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5671
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+ 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 );
I think --timeout has an off-by-one.
Thanks,
Mostafa
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
Quoted 24 lines · Mostafa Saleh
Quoted 23 lines · Pranjal Shrivastava
Implement
pm_runtimeand 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 register writes and tracking updates.The resume callback restores the MSI configuration and performs a full device reset via
arm_smmu_device_resetto bring the SMMU back to an operational state. The MSIs are cached during themsi_writeand are restored during the resume operation by using the helper. TheSTOP_FLAGis cleared only after the CMDQ is enabled in hardware.Suggested-by Daniel Mentz <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>---
[...]
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4881
Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3
-0+/* 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);Should not that be done from the suspend call?
I'm not sure if I understand? We're just clearing the flag here? We set the flag in suspend to close the gate and clear it in resume to re-open it. Clearing it at the end of suspend would be wrong as it would allow new submissions while the SMMU is off..
Additionally, I'll remove the redundant operation on owner_prod (since it's never set in owner_prod) if that's what you're saying?
Quoted 9 lines
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0+/* Invalidate any cached configuration */ arm_smmu_cmdq_issue_cmd_with_sync (smmu,arm_smmu_make_cmd_cfgi_all ());@@ -4898,6 +4939,21 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu)if (is_kdump_kernel ()) enables &= ~(CR0_EVTQEN |CR0_PRIQEN );
Quoted 17 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+/* +* 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 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4955
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+* 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 ();Should not that be
dma_wmb()as this is syncing with the HW?
Right.. as discussed with Daniel on the other thread, the dma_wmb() inside the issue_cmdlist() already ensures that PTE writes have reached RAM. I'll update the comments to clarify the barrier design here.
The first CFGI_ALL invalidation we issue on resume uses the CMDQ's standard submission path already includes the necessary dma_wmb(). This ensures that the hardware sees the correct state before we set SMMUEN=1. I'll update the comment to clarify that we are relying on this existing synchronization rather than adding a redundant barrier.
Quoted 29 lines
Quoted 29 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +22
-0+/* Enable the SMMU interface */ enables |=CR0_SMMUEN ; ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,@@ -5580,6 +5636,117 @@ 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
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5665
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+* 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);As Daniel mentioned, I think this shouldn't be relaxed.
Ack. I agree, I mis-read the kdoc about this, I'll fix it.
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_device_shutdown(struct platform_device *pdev) · line 5671
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+ 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 );I think --timeout has an off-by-one.
Good catch, I'll fix this!
Thanks,
Praan
Re: [PATCH v8 09/12] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
Quoted 6 lines · Pranjal Shrivastava
Quoted 4 lines · Mostafa Saleh
Quoted 3 lines · Pranjal Shrivastava
Implement
pm_runtimeand system sleep ops for arm-smmu-v3.+ /* Clear any flags from the previous life */
I'm wondering if this comment can be improved. You are clearing a very specific flag, not just any random one. How about "Resume command queue insertion by clearing STOP_FLAG"?
Quoted 9 lines
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_device_reset(struct arm_smmu_device *smmu) · line 4881
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-0+atomic_andnot (CMDQ_PROD_STOP_FLAG , &smmu->cmdq.owner_prod); +atomic_andnot (CMDQ_PROD_STOP_FLAG , &smmu->cmdq.q.llq.atomic.prod);Should not that be done from the suspend call?
I'm not sure if I understand? We're just clearing the flag here? We set the flag in suspend to close the gate and clear it in resume to re-open it. Clearing it at the end of suspend would be wrong as it would allow new submissions while the SMMU is off..