iommu/arm-smmu-v3: Implement Runtime/System Sleep ops
Subsystems (4)
v2 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 in some fashion. The proposed design tries to make use of this fact for implementing the suspend and resume ops.
1. Suspend / Resume
The idea for the "suspend" op is to wait till all the queues are flushed before disabling the SMMU through CR0. In order to avoid mis-use or spurious transactions (b/w SMMU disable -> power-down), the GBPA register is configured to abort all transactions.
The resume operation uses the arm_smmu_device_reset function which re-initializes the HW using the SW-copies maintained by the driver. For example, prod/cons for queues, base addresses for queues & tables. The arm_smmu_device_reset also clears the TLBs and config caches.
2. Interrupt Re-config
a. Wired irqs: The series refactors the arm_smmu_setup_irqs to be able to enable/disable irqs and install their handlers separately to help with the re-initialization of the interrupts correctly.
b. MSIs: The series relies on caching the msi_msg and retrieving it through a newly introduced helper arm_smmu_resume_msis() which re-configures the *_IRQ_CFGn registers via writing back cached msi_msgs.
3. Handling user-space attachments
This version of the series relies on the user-space paths invoking the iommu_*_claim_dma_owner API. The existing IOMMU API iommu_group_dma_owner_claimed() checks if DMA ownership for a group has been claimed. However, it does NOT distinguish between kernel clients and user-space clients. This version of the series introduce a new helper iommu_group_dma_owner_user() which allows the callers to specifically determine if a iommu_group is owned by the user-space.
The function checks the group->owner field which is exclusively managed by the iommu_group_claim_dma_owner() OR iommu_device_claim_dma_owner(), instead of relying on the iommu_group->owner_cnt. Both the *_claim_dma_owner APIs are ONLY called by vfio and iommufd at the moment indicating user-space's DMA ownership for the said iommu_group.
Based on this new API, the arm-smmu-v3 driver tracks all the non-trusted attachments in a list and keeps deferring suspend till the list of non-trusted devices is empty, i.e. no non-trusted attachment exists.
SVA: The binding driver would either be in user-space or kernel-space. For the latter, devlinks would suffice and for the former, we won't suspend until all user-space attachments are removed.
4. Invoking runtime_pm_get/put
Given that most of the configuration done by arm-smmu-v3 is stored in memory, the idea in this version of the series is to elide all TLBIs and CFGIs if the SMMU is suspended and only go ahead with ATC invalidations.
Thus, for most calls, the SMMU driver would make the required changes to the in-memory data structures, but elide all TLBIs, CFGIs and prefetches This is done by introducing another runtime PM helper based on pm_runtime_get_if_active.
Only places where the driver does a pm_runtime_resume_and_get is where touching the HW is unavoidable and important commands like ATC_INV and other commands resuming stalled transactions.
Future Work / Potential Improvements
Draining queues could be slightly optimized based on the feature-set supported by the smmu, for e.g. if STALL isn't supported or ATS is disabled, we could avoid draining evtq and priq respectivey.
Additionally, for tracking insecure attachments, an rbtee/hashtable could be used to improve search times, subject to finding a way to generate IDs / keys for pointers.
Call for review
Any insights/comments on the proposed changes are appreciated, especially in areas related to locking, atomic contexts, early resume, PCIe-related considerations etc. or any other potential optimizations.
Note: The series isn't tested with MSIs and weakly tested for PCIe clients. The same holds true for tegra241_cmdv changes. Any help in reviewing and testing these parts is appreciated.
Changelog:
[v2]
- 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]/
Pranjal Shrivastava (10):
iommu/arm-smmu-v3: Refactor arm_smmu_setup_irqs iommu/arm-smmu-v3: Add a helper to drain all queues iommu/tegra241-cmdqv: Add a helper to drain VCMDQs iommu/arm-smmu-v3: Cache and restore MSI config iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
iommu: Add a helper to check for user ownership iommu/arm-smmu-v3: Track masters with user-owned groups iommu/arm-smmu-v3: Avoid suspend when user owns DMA iommu/arm-smmu-v3: Enable pm_runtime and setup devlinks iommu/arm-smmu-v3: Invoke pm_runtime before hw access
.../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 11 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 468 ++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 19 + .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 28 ++ drivers/iommu/iommu.c | 19 + include/linux/iommu.h | 1 + 6 files changed, 513 insertions(+), 33 deletions(-)
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH v2 1/10 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 | 54 ++++++++++++++------- 1 file changed, 37 insertions(+), 17 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +37 -17
@@ -4009,12 +4009,24 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu) } } -static int arm_smmu_setup_irqs (struct arm_smmu_device *smmu) +static void arm_smmu_enable_irqs (struct arm_smmu_device *smmu) { -int ret, irq; +int ret;u32 irqen_flags =IRQ_CTRL_EVTQ_IRQEN |IRQ_CTRL_GERROR_IRQEN ; -/* Disable IRQs first */ +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) +{ +int ret; + ret =arm_smmu_write_reg_sync (smmu,0 ,ARM_SMMU_IRQ_CTRL ,ARM_SMMU_IRQ_CTRLACK );if (ret) {
@@ -4022,6 +4034,19 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)return ret; } +return 0 ; +} + +static int arm_smmu_setup_irqs (struct arm_smmu_device *smmu) +{ +int ret, irq; + +/* Disable IRQs first */ + ret =arm_smmu_disable_irqs (smmu); + +if (ret) +return ret; + irq = smmu->combined_irq;if (irq) {/*
@@ -4038,15 +4063,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 ; }
@@ -4189,11 +4205,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 );
@@ -4778,6 +4791,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.49.0.805.g082f7c87e0-goog
Re: [RFC PATCH v2 01/10] iommu/arm-smmu-v3: Refactor arm_smmu_setup_irqs
Quoted 7 lines · Pranjal Shrivastava
Refactor
arm_smmu_setup_irqsby 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.Signed-off-by Pranjal Shrivastava <[email protected]>
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu) · line 4047
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +8
-0+static int arm_smmu_setup_irqs (struct arm_smmu_device *smmu) +{ +int ret, irq; + +/* Disable IRQs first */ + ret =arm_smmu_disable_irqs (smmu); + +if (ret)
Could remove the extra line?
RFC PATCH v2 2/10 iommu/arm-smmu-v3: Add a helper to drain all queues
In reply to Pranjal ShrivastavaBefore we suspend SMMU, we want to ensure that all commands, events and paging requests have been flushed by all the queues i.e. the queues are empty. Add a helper function that waits till the queues are dained.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +46 -0
@@ -981,6 +981,52 @@ static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, cmds->num, true); } +static 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); + llq->val =READ_ONCE (q->llq.val); +do { +if (queue_empty (llq)) +break ; + + ret =queue_poll (&qp); + llq->cons =readl (q->cons_reg); + }while (!ret); + +return ret; +} + +static int arm_smmu_drain_queues (struct arm_smmu_device *smmu) +{ +int ret; + +/* cmdq */ +arm_smmu_cmdq_shared_lock (&smmu->cmdq); + ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->cmdq.q); +arm_smmu_cmdq_shared_unlock (&smmu->cmdq); +if (ret) +return ret; + +/* evtq */ + ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->evtq.q); +if (ret) +return ret; + +/* priq */ +if ((smmu->features &ARM_SMMU_FEAT_PRI )) { + ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->priq.q); +if (ret) +return ret; + } + +return 0 ; +} +static void arm_smmu_page_response (struct device *dev,struct iopf_fault *unused,struct iommu_page_response *resp) {
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Re: [RFC PATCH v2 02/10] iommu/arm-smmu-v3: Add a helper to drain all queues
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 992
Quoted 9 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9
-0+static 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); + llq->val =READ_ONCE (q->llq.val);
llq points to q->llq, but here it overwrites the value from itself?
Nicolin
Re: [RFC PATCH v2 02/10] iommu/arm-smmu-v3: Add a helper to drain all queues
Quoted 12 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 992
Quoted 9 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9
-0+static 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); + llq->val =READ_ONCE (q->llq.val);llq points to
q->llq, but here it overwrites the value from itself?
Ack. This seems redundant, will remove.
Quoted 2 lines
Nicolin
Thanks
Praan
Re: [RFC PATCH v2 02/10] iommu/arm-smmu-v3: Add a helper to drain all queues
In reply to Pranjal ShrivastavaQuoted 5 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +5
-0+static 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;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 998
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+int ret =0 ; + +queue_poll_init (smmu, &qp); + llq->val =READ_ONCE (q->llq.val); + do { + if (queue_empty(llq)) + break; + + ret = queue_poll(&qp); + llq->cons = readl(q->cons_reg);
A readl_relaxed() is probably sufficient. Also, in other places, they
use WRITE_ONCE to write to llq->cons. Compare with
WRITE_ONCE(cmdq->q.llq.cons, readl_relaxed(cmdq->q.cons_reg));
in arm_smmu_cmdq_poll_until_not_full.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 1009
Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +11
-0+ }while (!ret); + +return ret; +} + +static int arm_smmu_drain_queues (struct arm_smmu_device *smmu) +{ +int ret; + +/* cmdq */ +arm_smmu_cmdq_shared_lock (&smmu->cmdq);
I'm afraid this might not be a correct usage arm_smmu_cmdq_shared_lock. I understand that acquiring the shared lock means that you want to prevent any other thread from updating cmdq->q.llq.cons, which I believe is not what you're looking for here. If you want to participate in this locking protocol, I believe you need to acquire the exclusive lock.
This being said, I believe you need some other mechanism to ensure that no new commands are submitted to the command queue. Otherwise, the command queue could become non-empty after you return from arm_smmu_queue_poll_until_empty.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 1013
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+ ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->cmdq.q); +arm_smmu_cmdq_shared_unlock (&smmu->cmdq); +if (ret) +return ret;
This is basically a command queue timeout. Consider printing an error message similar to "CMD_SYNC timeout at [...]" in arm_smmu_cmdq_issue_cmdlist.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 1016
Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3
-0+ +/* evtq */ + ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->evtq.q);
I'm not sure if you can use your arm_smmu_queue_poll_until_empty function on the event queue. I understand that in the case of the event queue where the driver is the consumer, the driver's version of llq->cons is always more up-to-date than the SMMU_EVTQ_CONS register. In fact, I'm afraid you might move the consumer index backwards with "llq->cons = readl(q->cons_reg);", because llq->cons is more current than SMMU_EVTQ_CONS.
I'm thinking about the following proposal:
* Drain only the command queue. Use a local copy for llq->cons. Don't
write to cmdq->q.cons_reg
* Don't bother draining the other queues
* Backup consumer and producer indices *after* disabling the queues
in SMMU_CR0. This would involve writing to cmdq->q.cons_reg
Re: [RFC PATCH v2 02/10] iommu/arm-smmu-v3: Add a helper to drain all queues
Quoted 10 lines · Daniel Mentz
Quoted 9 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +9
-0+static 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); + llq->val =READ_ONCE (q->llq.val);
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 998
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +6
-0+ do { + if (queue_empty(llq)) + break; + + ret = queue_poll(&qp); + llq->cons = readl(q->cons_reg);A
readl_relaxed()is probably sufficient.
Ack. I referred to __arm_smmu_cmdq_poll_until_consumed() which uses readl, but I agree that this can be _relaxed as for the former we had to handle a case where multiple threads are polling on their own CMD_SYNCs.
Quoted 7 lines
Also, in other places, they
useWRITE_ONCEto write tollq->cons. Compare with
WRITE_ONCE(cmdq->q.llq.cons, readl_relaxed(cmdq->q.cons_reg));in
arm_smmu_cmdq_poll_until_not_full.
Yes, I believe that's to avoid store-tearing and re-ordering when we have multiple threads potentially polling on the llq.cons
However, I don't think that's going to be the case in the suspend callback as we'd atmost have the page_response responding to a stall event and issuing a CMD_RESUME (without a sync) which won't poll.
Although, if we're looking to use this function in other context apart from suspend, I guess there's not much of a harm in using WRITE_ONCE()..
Quoted 10 lines
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10
-0+ }while (!ret); + +return ret; +} + +static int arm_smmu_drain_queues (struct arm_smmu_device *smmu) +{ +int ret; + +/* cmdq */
Quoted 9 lines
Quoted 1 line
+
arm_smmu_cmdq_shared_lock(&smmu->cmdq);I'm afraid this might not be a correct usage
arm_smmu_cmdq_shared_lock. I understand that acquiring the shared lock means that you want to prevent any other thread from updatingcmdq->q.llq.cons, which I believe is not what you're looking for here. If you want to participate in this locking protocol, I believe you need to acquire the exclusive lock.
Ack.
However, I believe there's no such need to acquire any lock here given the fact that arm_smmu_drain_queues isn't expected to be called from anywhere else except the suspend callback... What do you think ?
The only case that I can think of that could possibly contend with this is the evtq_thread handling a stalled transaction with a CMD_RESUME which could be handled by first draining the evtq.
The callers which plan to use arm_smmu_cmdq_poll_until_empty apart from this could use the appropriate locking, I can add a comment about that.
Quoted 5 lines
This being said, I believe you need some other mechanism to ensure that no new commands are submitted to the command queue. Otherwise, the command queue could become non-empty after you return from
arm_smmu_queue_poll_until_empty.
I think the fact that we're calling this from the suspend callback itself ensures that there aren't any clients actively issuing commands?
The only exception to this could be the other two queues having events which require to be responded with either CMD_RESUME or CMD_PRI_RESP. For example: a stall event that requires us to respond with a CMD_RESUME.
We could drain the other queues before the cmdq in that case.
Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 1013
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+ ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->cmdq.q); +arm_smmu_cmdq_shared_unlock (&smmu->cmdq); +if (ret) +return ret;This is basically a command queue timeout. Consider printing an error message similar to "
CMD_SYNCtimeout at [...]" inarm_smmu_cmdq_issue_cmdlist.
We're already printing that in the arm_smmu_runtime_suspend:
dev_err(smmu->dev, "Draining queues timed-out.. retry later\n");
Are you suggesting to print it specifically for cmdq with the prod/cons?
Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, · line 1016
Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3
-0+ +/* evtq */ + ret =arm_smmu_queue_poll_until_empty (smmu, &smmu->evtq.q);
Quoted 8 lines
I'm not sure if you can use your
arm_smmu_queue_poll_until_emptyfunction on the event queue. I understand that in the case of the event queue where the driver is the consumer, the driver's version ofllq->consis always more up-to-date than theSMMU_EVTQ_CONSregister. In fact, I'm afraid you might move the consumer index backwards with "llq->cons=readl(q->cons_reg);", becausellq->consis more current thanSMMU_EVTQ_CONS.
That's a good catch! Thanks! This also true for priq, I think we can have a dedicated function for draining the cmdq as above and another one for draining priq and evtq.
Quoted 3 lines
I'm thinking about the following proposal:
* Drain only the command queue. Use a local copy forllq->cons. Don't
write tocmdq->q.cons_reg
We're not writing to cmdq->q.cons_reg anywhere? And we can't rely on the llq->cons copy since it's ONLY updated when the last command was issued with a "sync" which isn't guaranteed to be the case always.
Quoted 1 line
* Don't bother draining the other queues
I don't think that would be appropriate, we can't leave a stalled transaction before suspending and should handle all Paging requests via PRI. In fact in the v1, Robin mentioned[1] quite the opposite.
Quoted 2 lines
* Backup consumer and producer indices *after* disabling the queues
inSMMU_CR0. This would involve writing tocmdq->q.cons_reg
So... basically handle the pending stalls and paging requests when we resume later?
Also, why would we write to the cmdq->q.cons_reg if it's going to be lost with the power-down? If at all, we'd have to read the prod/cons_reg values back into the SW copies. Am I missing something here?
Thanks,
Praan
[1]
https://lore.kernel.org/all/[email protected]/T/#m6f479d70d666252a657fb3ff57ff1764267a892d
RFC PATCH v2 3/10 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 | 10 +++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 4 +++ .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -3
@@ -981,8 +981,8 @@ static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, cmds->num, true); } -static int arm_smmu_queue_poll_until_empty (struct arm_smmu_device *smmu, -struct arm_smmu_queue *q) +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;
@@ -1024,7 +1024,11 @@ static int arm_smmu_drain_queues(struct arm_smmu_device *smmu)return ret; } -return 0 ; +/* Drain all implementation-specific queues */ +if (smmu->impl_ops && smmu->impl_ops->drain_queues) + ret = smmu->impl_ops->drain_queues (smmu); + +return ret; } static void arm_smmu_page_response(struct device *dev, struct iopf_fault *unused,
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +4 -0
@@ -720,6 +720,7 @@ struct arm_smmu_impl_ops {int (*init_structures)(struct arm_smmu_device *smmu);struct arm_smmu_cmdq *(*get_secondary_cmdq)(struct arm_smmu_device *smmu,struct arm_smmu_cmdq_ent *ent); +int (*drain_queues)(struct arm_smmu_device *smmu); };/* An SMMUv3 instance */
@@ -965,6 +966,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 &
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +28 -0
@@ -349,6 +349,33 @@ tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu,return &vcmdq->cmdq; } +static int tegra241_cmdqv_drain_hyp_vcmdqs (struct arm_smmu_device *smmu) +{ +struct tegra241_cmdqv *cmdqv = + container_of(smmu, struct tegra241_cmdqv, smmu); +struct tegra241_vintf *vintf = cmdqv->vintfs[0 ]; +struct tegra241_vcmdq *vcmdq; +int ret =0 ; +u16 i; + +/* Kernel only uses VINTF0 return if it's disabled */ +if (!READ_ONCE (vintf->enabled)) +return 0 ; + +for (i =0 ; i < cmdqv->num_lvcmdqs_per_vintf; i++) { + vcmdq = vintf->lvcmdqs[i]; + +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 */ static void tegra241_vcmdq_hw_deinit (struct tegra241_vcmdq *vcmdq)
@@ -681,6 +708,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_hyp_vcmdqs, };/* Probe Functions */
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Re: [RFC PATCH v2 03/10] iommu/tegra241-cmdqv: Add a helper to drain VCMDQs
Quoted 9 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_queuesSigned-off-by Pranjal Shrivastava <[email protected]>
Some nitpickings below. No need to reply unless you disagree :)
Quoted 19 lines
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 10 +++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 4 +++ .../iommu/arm/arm-smmu-v3/tegra241-cmdqv.c | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-)drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2
-2@@ -981,8 +981,8 @@ static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu, cmds->num, true); } -static int arm_smmu_queue_poll_until_empty (struct arm_smmu_device *smmu, -struct arm_smmu_queue *q) +int arm_smmu_queue_poll_until_empty (struct arm_smmu_device *smmu, +struct arm_smmu_queue *q)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, · line 971
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3
-0@@ -965,6 +966,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); +
I'd personally prefer these to stay in the change that introduces the function, since it knew that a following patch wants it to be shared in the header. But either way should be fine.
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu, · line 352
Quoted 12 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0
-0static inline bool arm_smmu_master_canwbs (struct arm_smmu_master *master) { return dev_iommu_fwspec_get(master->dev)->flags &drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +1
-0@@ -349,6 +349,33 @@ tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu,return &vcmdq->cmdq; } +static int tegra241_cmdqv_drain_hyp_vcmdqs (struct arm_smmu_device *smmu)
tegra241_cmdqv_drain_vintf0_lvcmdqs
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu, · line 357
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +5
-0+{ +struct tegra241_cmdqv *cmdqv = + container_of(smmu, struct tegra241_cmdqv, smmu); +struct tegra241_vintf *vintf = cmdqv->vintfs[0 ]; +struct tegra241_vcmdq *vcmdq;
Let's move into the for loop.
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu, · line 359
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +2
-0+int ret =0 ; +u16 i;
u16 lidx;
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu, · line 361
Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +2
-0+ +/* Kernel only uses VINTF0 return if it's disabled */
/* Kernel only uses VINTF0. Return if it's disabled */
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c tegra241_cmdqv_get_cmdq(struct arm_smmu_device *smmu, · line 366
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +5
-0+if (!READ_ONCE (vintf->enabled)) +return 0 ; + +for (i =0 ; i < cmdqv->num_lvcmdqs_per_vintf; i++) { + vcmdq = vintf->lvcmdqs[i];
for (lidx = 0; lidx < cmdqv->num_lvcmdqs_per_vintf; lidx++) {
struct tegra241_vcmdq *vcmdq = vintf->lvcmdqs[i];RFC PATCH v2 4/10 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 | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +41 -0
@@ -3968,6 +3968,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 ;
@@ -3976,6 +3979,44 @@ 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_msis (struct arm_smmu_device *smmu) +{ +struct msi_desc *desc; +struct msi_msg msg; + +if (!(smmu->features &ARM_SMMU_FEAT_MSI )) +return ; + +if (!smmu->dev->msi.domain) +return ; + + desc =irq_get_msi_desc (smmu->evtq.q.irq); +if (desc) { +get_cached_msi_msg (smmu->evtq.q.irq, &msg); +arm_smmu_write_msi_msg (desc, &msg); + }else { +dev_err (smmu->dev,"Failed to resume evtq msi" ); + } + + desc =irq_get_msi_desc (smmu->gerr_irq); +if (desc) { +get_cached_msi_msg (smmu->gerr_irq, &msg); +arm_smmu_write_msi_msg (desc, &msg); + }else { +dev_err (smmu->dev,"Failed to resume gerror msi" ); + } + +if (smmu->features &ARM_SMMU_FEAT_PRI ) { + desc =irq_get_msi_desc (smmu->priq.q.irq); +if (desc) { +get_cached_msi_msg (smmu->priq.q.irq, &msg); +arm_smmu_write_msi_msg (desc, &msg); + }else { +dev_err (smmu->dev,"Failed to resume priq msi" ); + } + } +} +static void arm_smmu_setup_msis (struct arm_smmu_device *smmu) {int ret, nvec =ARM_SMMU_MAX_MSIS ;
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Re: [RFC PATCH v2 04/10] iommu/arm-smmu-v3: Cache and restore MSI config
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) · line 3999
Quoted 7 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7
-0+ desc =irq_get_msi_desc (smmu->evtq.q.irq); +if (desc) { +get_cached_msi_msg (smmu->evtq.q.irq, &msg); +arm_smmu_write_msi_msg (desc, &msg); + }else { +dev_err (smmu->dev,"Failed to resume evtq msi" ); + }
Do we need to check if (smmu->evtq.q.irq) at the top?
And same question to the other irqs.
Thanks
Nicolin
Re: [RFC PATCH v2 04/10] iommu/arm-smmu-v3: Cache and restore MSI config
Quoted 11 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) · line 3999
Quoted 7 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7
-0+ desc =irq_get_msi_desc (smmu->evtq.q.irq); +if (desc) { +get_cached_msi_msg (smmu->evtq.q.irq, &msg); +arm_smmu_write_msi_msg (desc, &msg); + }else { +dev_err (smmu->dev,"Failed to resume evtq msi" ); + }Do we need to check if (
smmu->evtq.q.irq) at the top?
Quoted 2 lines
And same question to the other irqs.
Yes, that's right, we should check if the irqs are assigned. Thanks!
Quoted 2 lines
Thanks
Nicolin
Praan
RFC PATCH v2 5/10 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 smmu is disabled as part of the suspend callbacks after ensuring the completion of all pending commands and is configured to abort any transactions that happen after disabling the smmu. The smmu shall be reinitialized in the resume callback by invoking the arm_smmu_device_reset helper.
The MSIs are freed as part of the suspend and are re-allocated during the resume operation.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 93 +++++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 + 2 files changed, 95 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +93 -0
@@ -26,6 +26,7 @@#include <linux/pci.h> #include <linux/pci-ats.h> #include <linux/platform_device.h> +#include <linux/pm_runtime.h> #include <linux/string_choices.h> #include <kunit/visibility.h> #include <uapi/linux/iommufd.h>
@@ -108,6 +109,41 @@ static const char * const event_class_str[] = {static int arm_smmu_alloc_cd_tables (struct arm_smmu_master *master); +/* Runtime PM helpers */ +static int arm_smmu_rpm_get (struct arm_smmu_device *smmu) +{ +int ret; + +if (pm_runtime_enabled (smmu->dev)) { + ret =pm_runtime_resume_and_get (smmu->dev); +if (ret <0 ) { +dev_err (smmu->dev,"Failed to resume device: %d\n" , ret); +return ret; + } + } + +return 0 ; +} + +static int arm_smmu_rpm_get_if_active (struct arm_smmu_device *smmu) +{ +if (pm_runtime_enabled (smmu->dev)) +return pm_runtime_get_if_in_use (smmu->dev); + +return 0 ; +} + +static void arm_smmu_rpm_put (struct arm_smmu_device *smmu) +{ +int ret; + +if (pm_runtime_enabled (smmu->dev)) { + ret =pm_runtime_put_autosuspend (smmu->dev); +if (ret <0 ) +dev_err (smmu->dev,"Failed to suspend device: %d\n" , ret); + } +} +static void parse_driver_options (struct arm_smmu_device *smmu) {int i =0 ;
@@ -4935,6 +4971,62 @@ 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); +int ret; + +/* +* Since suspend is invoked when all clients have been suspended, +* we don't expect more cmds or events to be added to the queues. +* Wait for all queues to be drained. +*/ + ret =arm_smmu_drain_queues (smmu); +if (ret) { +dev_err (smmu->dev,"Draining queues timed-out.. retry later\n" ); +return -EAGAIN ; + } + +/* Disable all queues */ +arm_smmu_device_disable (smmu); + +/* Abort all transactions to avoid spurious bypass */ +arm_smmu_update_gbpa (smmu,GBPA_ABORT ,0 ); + +dev_dbg (dev,"Suspending smmu\n" ); +return 0 ; +} + +static int __maybe_unused arm_smmu_runtime_resume (struct device *dev) +{ +int ret; +struct arm_smmu_device *smmu =dev_get_drvdata (dev); + +dev_dbg (dev,"Resuming device\n" ); + +/* Re-configure MSIs */ +arm_smmu_resume_msis (smmu); + +/* +* The reset will re-initialize all the base addresses, queues, +* prod and cons maintained within struct arm_smmu_device as well as +* re-enable the interrupts. +*/ + ret =arm_smmu_device_reset (smmu); + +if (ret) +dev_err (dev,"Failed to reset during resume operation: %d\n" , ret); + +return ret; +} + +static const struct dev_pm_ops arm_smmu_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, + pm_runtime_force_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" , }, { },
@@ -4951,6 +5043,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 +2 -0
@@ -506,6 +506,8 @@ static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid)#define MSI_IOVA_BASE 0x8000000#define MSI_IOVA_LENGTH 0x100000 +#define RPM_AUTOSUSPEND_DELAY_MS 15 +enum pri_resp {PRI_RESP_DENY =0 ,PRI_RESP_FAIL =1 ,
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Re: [RFC PATCH v2 05/10] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static const char * const event_class_str[] = { · line 131
Quoted 4 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+static int arm_smmu_rpm_get_if_active (struct arm_smmu_device *smmu) +{ +if (pm_runtime_enabled (smmu->dev)) +return pm_runtime_get_if_in_use (smmu->dev);
You named this function arm_smmu_rpm_get_if_active but then call into pm_runtime_get_if_in_use (There's also a pm_runtime_get_if_active). Shouldn't this function be called arm_smmu_rpm_get_if_in_use?
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid) · line 509
Quoted 6 lines
+++
b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.hdrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-0@@ -506,6 +506,8 @@ static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid)#define MSI_IOVA_BASE 0x8000000#define MSI_IOVA_LENGTH 0x100000 +#define RPM_AUTOSUSPEND_DELAY_MS 15
Consider moving this to "iommu/arm-smmu-v3: Enable pm_runtime and setup devlinks" where it's actually used for the first time.
Re: [RFC PATCH v2 05/10] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops
Quoted 10 lines · Daniel Mentz
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static const char * const event_class_str[] = { · line 131
Quoted 4 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +4
-0+static int arm_smmu_rpm_get_if_active (struct arm_smmu_device *smmu) +{ +if (pm_runtime_enabled (smmu->dev)) +return pm_runtime_get_if_in_use (smmu->dev);You named this function
arm_smmu_rpm_get_if_activebut then call intopm_runtime_get_if_in_use(There's also apm_runtime_get_if_active). Shouldn't this function be calledarm_smmu_rpm_get_if_in_use?
Ack, I'll rename the function to arm_smmu_rpm_get_if_in_use to be more clear.
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid) · line 509
Quoted 6 lines
+++
b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.hdrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-0@@ -506,6 +506,8 @@ static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid)#define MSI_IOVA_BASE 0x8000000#define MSI_IOVA_LENGTH 0x100000 +#define RPM_AUTOSUSPEND_DELAY_MS 15Consider moving this to "iommu/arm-smmu-v3: Enable
pm_runtimeand setup devlinks" where it's actually used for the first time.
Ack.
Thanks
Praan
RFC PATCH v2 6/10 iommu: Add a helper to check for user ownership
In reply to Pranjal ShrivastavaThe existing function iommu_group_dma_owner_claimed() checks if DMA ownership for a group has been claimed, but it cannot distinguish between kernel clients and user-space clients like VFIO. This is because its underlying mechanism (based on group->owner_cnt) can reflect claims from both user-space (e.g. iommu_group_claim_dma_owner()) and kernel clients like iommu_device_use_default_domain() during really_probe.
Introduce a new helper iommu_group_dma_owner_user() which allows the callers to specifically determine if a iommu_group is owned by the user-space.
The function checks the group->owner field which is exclusively managed by the iommu_group_claim_dma_owner() OR iommu_device_claim_dma_owner(), instead of relying on the iommu_group->owner_cnt. Both the *_claim_dma_owner APIs are ONLY called by vfio and iommufd, indicating user-space's DMA ownership for the said iommu_group.
--- drivers/iommu/iommu.c | 19 +++++++++++++++++++ include/linux/iommu.h | 1 + 2 files changed, 20 insertions(+)
drivers/iommu/iommu.c +19 -0
@@ -3348,6 +3348,25 @@ bool iommu_group_dma_owner_claimed(struct iommu_group *group) }EXPORT_SYMBOL_GPL (iommu_group_dma_owner_claimed); +/** +* iommu_group_dma_owner_kernel() - Query if kernel owns the group dma +* @group: The group. +* +* This provides status query on a given group. It is racy and only for +* non-binding status reporting. +*/ +bool iommu_group_dma_owner_user (struct iommu_group *group) +{ +void *user; + +mutex_lock (&group->mutex); + user = group->owner; +mutex_unlock (&group->mutex); + +return user ? true : false; +} +EXPORT_SYMBOL_GPL (iommu_group_dma_owner_user); +static void iommu_remove_dev_pasid (struct device *dev,ioasid_t pasid,struct iommu_domain *domain) {
include/linux/iommu.h +1 -0
@@ -1132,6 +1132,7 @@ void iommu_device_unuse_default_domain(struct device *dev);int iommu_group_claim_dma_owner (struct iommu_group *group,void *owner);void iommu_group_release_dma_owner (struct iommu_group *group);bool iommu_group_dma_owner_claimed (struct iommu_group *group); +bool iommu_group_dma_owner_user (struct iommu_group *group);int iommu_device_claim_dma_owner (struct device *dev,void *owner);void iommu_device_release_dma_owner (struct device *dev);
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
kernel test robot
build botRe: [RFC PATCH v2 06/10] iommu: Add a helper to check for user ownership
kernel test robot noticed the following build warnings:
Hi Pranjal,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on arm-perf/for-next/perf]
[also build test WARNING on linus/master v6.15-rc2 next-20250417] [cannot apply to joro-iommu/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Pranjal-Shrivastava/iommu-arm-smmu-v3-Refactor-arm_smmu_setup_irqs/20250419-073729 base: https://git.kernel.org/pub/scm/linux/kernel/git/will/linux.git for-next/perf patch link: https://lore.kernel.org/r/20250418233409.3926715-7-praan%40google.com patch subject: [RFC PATCH v2 06/10] iommu: Add a helper to check for user ownership config: csky-randconfig-002-20250419 (https://download.01.org/0day-ci/archive/20250419/[email protected]/config) compiler: csky-linux-gcc (GCC) 11.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250419/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/iommu/iommu.c:3355: warning: expecting prototype for iommu_group_dma_owner_kernel(). Prototype was for iommu_group_dma_owner_user() instead
vim +3355 drivers/iommu/iommu.c
3346
3347 /**
3348 * iommu_group_dma_owner_kernel() - Query if kernel owns the group dma
3349 * @group: The group.
3350 *
3351 * This provides status query on a given group. It is racy and only for
3352 * non-binding status reporting.
3353 */
3354 bool iommu_group_dma_owner_user(struct iommu_group *group)
> 3355 { 3356 void *user;
3357
3358 mutex_lock(&group->mutex);
3359 user = group->owner;
3360 mutex_unlock(&group->mutex);
3361
3362 return user ? true : false;
3363 }
3364 EXPORT_SYMBOL_GPL(iommu_group_dma_owner_user);
3365
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH v2 7/10 iommu/arm-smmu-v3: Track masters with user-owned groups
In reply to Pranjal ShrivastavaTrack smmu masters (client devices) which belong to user-owned IOMMU groups as determined by iommu_group_dma_owner_user. This allows the driver to identify attachments where DMA is controlled by user-space.
This tracking provides the necessary infrastructure for setting up power management policies to help decide if it's safe to power-down the smmu if an attachment belongs to a user-space controlled group.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 69 +++++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 10 +++ 2 files changed, 79 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +69 -0
@@ -3018,6 +3018,62 @@ void arm_smmu_attach_commit(struct arm_smmu_attach_state *state)arm_smmu_remove_master_domain (master, state->old_domain, state->ssid); } +static int arm_smmu_insert_insecure_master (struct arm_smmu_device *smmu, +struct arm_smmu_master *master) +{ +struct arm_smmu_unsafe_master *umaster; +unsigned long flags; + + umaster =kzalloc (sizeof (umaster),GFP_KERNEL ); +if (!umaster) +return -ENOMEM ; + umaster->master = master; + +spin_lock_irqsave (&smmu->attach_lock, flags); +list_add_tail (&umaster->masters, &smmu->insecure_attachments); +spin_unlock_irqrestore (&smmu->attach_lock, flags); + +return 0 ; +} + +static struct arm_smmu_master * +arm_smmu_find_insecure_master (struct arm_smmu_device *smmu, +struct arm_smmu_master *master) +{ +struct arm_smmu_unsafe_master *iter, *umaster =NULL ; +unsigned long flags; + +spin_lock_irqsave (&smmu->attach_lock, flags); +list_for_each_entry (iter, &smmu->insecure_attachments, masters) { +if (iter->master == master) { + umaster = iter; +break ; + } + } +spin_unlock_irqrestore (&smmu->attach_lock, flags); + +return umaster ? umaster->master :NULL ; +} + +static void arm_smmu_remove_insecure_master (struct arm_smmu_device *smmu, +struct arm_smmu_master *master) +{ +struct arm_smmu_unsafe_master *iter, *tmp, *node_to_free =NULL ; +unsigned long flags; + +spin_lock_irqsave (&smmu->attach_lock, flags); + +list_for_each_entry_safe (iter, tmp, &smmu->insecure_attachments, masters) { +if (iter->master == master) { +list_del (&iter->masters); + node_to_free = iter; +break ; + } + } +spin_unlock_irqrestore (&smmu->attach_lock, flags); +kfree (node_to_free); +} +static int arm_smmu_attach_dev (struct iommu_domain *domain,struct device *dev) {int ret =0 ;
@@ -3041,6 +3097,16 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)if (smmu_domain->smmu != smmu)return ret; +/* Check if the master is owned by user */ +if (iommu_group_dma_owner_user (dev->iommu_group)) { +if (!arm_smmu_find_insecure_master (smmu, master)) +arm_smmu_insert_insecure_master (smmu, master); + + }else if (arm_smmu_find_insecure_master (smmu, master)) { +/* Remove if the master isn't user-owned anymore */ +arm_smmu_remove_insecure_master (smmu, master); + } +if (smmu_domain->stage ==ARM_SMMU_DOMAIN_S1 ) { cdptr =arm_smmu_alloc_cd_ptr (master,IOMMU_NO_PASID );if (!cdptr)
@@ -3944,6 +4010,9 @@ static int arm_smmu_init_structures(struct arm_smmu_device *smmu)mutex_init (&smmu->streams_mutex); smmu->streams =RB_ROOT ; +INIT_LIST_HEAD (&smmu->insecure_attachments); +spin_lock_init (&smmu->attach_lock); + ret =arm_smmu_init_queues (smmu);if (ret)return ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +10 -0
@@ -795,6 +795,11 @@ struct arm_smmu_device {struct rb_root streams;struct mutex streams_mutex; + +/* Insecure Attach handles */ +struct list_head insecure_attachments; +/* Lock for the list */ +spinlock_t attach_lock; };struct arm_smmu_stream {
@@ -803,6 +808,11 @@ struct arm_smmu_stream {struct rb_node node; }; +struct arm_smmu_unsafe_master { +struct list_head masters; +struct arm_smmu_master *master; +}; +struct arm_smmu_vmaster {struct arm_vsmmu *vsmmu;unsigned long vsid;
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH v2 8/10 iommu/arm-smmu-v3: Avoid suspend when user owns DMA
In reply to Pranjal ShrivastavaSuspending the smmu is unsafe if any attached devices belong to iommu groups claimed by user-space (via VFIO etc.), as a DMA may be ongoing or initiated at any time. The smmu must remain powered ON to handle potential DMA requests originating from the user-space context.
Introduce a helper arm_smmu_suspend_is_safe() using the previously added insecure_attachments list. If the check fails (list is not empty), defer the suspend ensuring the SMMU remains active while it's potentially needed by user-space for DMA.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 18 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +18 -0
@@ -5040,11 +5040,29 @@ static void arm_smmu_device_shutdown(struct platform_device *pdev)arm_smmu_device_disable (smmu); } +static bool arm_smmu_suspend_is_safe (struct arm_smmu_device *smmu) +{ +unsigned long flags; +bool is_empty; + +spin_lock_irqsave (&smmu->attach_lock, flags); + is_empty =list_empty (&smmu->insecure_attachments); +spin_unlock_irqrestore (&smmu->attach_lock, flags); + +return is_empty; +} +static int __maybe_unused arm_smmu_runtime_suspend (struct device *dev) {struct arm_smmu_device *smmu =dev_get_drvdata (dev);int ret; +/* Refuse to suspend if user-space controls DMA */ +if (!arm_smmu_suspend_is_safe (smmu)) { +dev_err (smmu->dev,"Suspend deferred due to unsafe DMA owners\n" ); +return -EAGAIN ; + } +/* * Since suspend is invoked when all clients have been suspended, * we don't expect more cmds or events to be added to the queues.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -1
@@ -796,7 +796,7 @@ struct arm_smmu_device {struct rb_root streams;struct mutex streams_mutex; -/* Insecure Attach handles */ +/* Insecure attach handles */ struct list_head insecure_attachments;/* Lock for the list */ spinlock_t attach_lock;
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Re: [RFC PATCH v2 08/10] iommu/arm-smmu-v3: Avoid suspend when user owns DMA
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_device { · line 799
Quoted 7 lines · Pranjal Shrivastava
+++
b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.hdrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1@@ -796,7 +796,7 @@ struct arm_smmu_device {struct rb_root streams;struct mutex streams_mutex; -/* Insecure Attach handles */ +/* Insecure attach handles */
Move this to the original commit that introduced this comment.
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0
-1struct list_head insecure_attachments;/* Lock for the list */ spinlock_t attach_lock; --2.49.0.805.g082f7c87e0-goog
Re: [RFC PATCH v2 08/10] iommu/arm-smmu-v3: Avoid suspend when user owns DMA
Quoted 11 lines · Daniel Mentz
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h struct arm_smmu_device { · line 799
Quoted 7 lines · Pranjal Shrivastava
+++
b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.hdrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1
-1@@ -796,7 +796,7 @@ struct arm_smmu_device {struct rb_root streams;struct mutex streams_mutex; -/* Insecure Attach handles */ +/* Insecure attach handles */ Move this to the original commit that introduced this comment.
Ack.
Quoted 8 lines
Quoted 8 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0
-1struct list_head insecure_attachments;/* Lock for the list */ spinlock_t attach_lock; --2.49.0.805.g082f7c87e0-goog
Thanks
Praan
RFC PATCH v2 9/10 iommu/arm-smmu-v3: Enable pm_runtime and setup devlinks
In reply to Pranjal ShrivastavaEnable PM runtime for SMMUs having a power-domain during smmu probe. Add a devlink between the clients and SMMU device. The absence of a power domain effectively disables runtime power management.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +13 -1
@@ -3640,6 +3640,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:
@@ -4999,11 +5002,18 @@ static int arm_smmu_device_probe(struct platform_device *pdev)if (ret) goto err_disable; +if (dev->pm_domain) { +pm_runtime_set_active (dev); +pm_runtime_use_autosuspend (dev); +pm_runtime_set_autosuspend_delay (dev,RPM_AUTOSUSPEND_DELAY_MS ); +pm_runtime_enable (dev); + } +/* And we're up. Go go go! */ ret =iommu_device_sysfs_add (&smmu->iommu, dev,NULL ,"smmu3.%pa" , &ioaddr);if (ret) - goto err_disable; + goto err_rpm_disable; ret =iommu_device_register (&smmu->iommu, &arm_smmu_ops, dev);if (ret) {
@@ -5015,6 +5025,8 @@ static int arm_smmu_device_probe(struct platform_device *pdev) err_free_sysfs:iommu_device_sysfs_remove (&smmu->iommu); +err_rpm_disable: +pm_runtime_disable (dev); err_disable:arm_smmu_device_disable (smmu); err_free_iopf:
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
RFC PATCH v2 10/10 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. Elide all TLB and CFG invalidations if the smmu is suspended but not ATC invalidations.
--- .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 11 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 135 +++++++++++++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 + 3 files changed, 131 insertions(+), 18 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +10 -1
@@ -13,10 +13,17 @@ void *arm_smmu_hw_info(struct device *dev, u32 *length, u32 *type)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 ); info =kzalloc (sizeof (*info),GFP_KERNEL ); -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++)
@@ -27,6 +34,7 @@ void *arm_smmu_hw_info(struct device *dev, u32 *length, u32 *type) *length =sizeof (*info); *type =IOMMU_HW_INFO_TYPE_ARM_SMMUV3 ; +arm_smmu_rpm_put (master->smmu);return info; }
@@ -139,6 +147,7 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, .old_domain = iommu_get_domain_for_dev(dev), .ssid = IOMMU_NO_PASID, }; +struct arm_smmu_device *smmu = master->smmu;struct arm_smmu_ste ste;int ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +118 -17
387 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +3 -0
@@ -1009,6 +1009,9 @@ int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu, struct arm_smmu_cmdq *cmdq, u64 *cmds, int n, bool sync); +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);bool arm_smmu_master_sva_supported (struct arm_smmu_master *master);
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
kernel test robot
build botRe: [RFC PATCH v2 10/10] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
kernel test robot noticed the following build warnings:
Hi Pranjal,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on arm-perf/for-next/perf]
[also build test WARNING on linus/master v6.15-rc2 next-20250417] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Pranjal-Shrivastava/iommu-arm-smmu-v3-Refactor-arm_smmu_setup_irqs/20250419-073729 base: https://git.kernel.org/pub/scm/linux/kernel/git/will/linux.git for-next/perf patch link: https://lore.kernel.org/r/20250418233409.3926715-11-praan%40google.com patch subject: [RFC PATCH v2 10/10] iommu/arm-smmu-v3: Invoke pm_runtime before hw access config: arm64-randconfig-001-20250419 (https://download.01.org/0day-ci/archive/20250419/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 7.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250419/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c: In function 'arm_smmu_attach_dev_nested':
>> drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c:150:26: warning: unused variable 'smmu' [-Wunused-variable]
struct arm_smmu_device *smmu = master->smmu;
^~~~vim +/smmu +150 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c
138
139 static int arm_smmu_attach_dev_nested(struct iommu_domain *domain,
140 struct device *dev)
141 {
142 struct arm_smmu_nested_domain *nested_domain =
143 to_smmu_nested_domain(domain);
144 struct arm_smmu_master *master = dev_iommu_priv_get(dev);
145 struct arm_smmu_attach_state state = {
146 .master = master,
147 .old_domain = iommu_get_domain_for_dev(dev),
148 .ssid = IOMMU_NO_PASID,
149 };
> 150 struct arm_smmu_device *smmu = master->smmu;
151 struct arm_smmu_ste ste;
152 int ret;
153
154 if (nested_domain->vsmmu->smmu != master->smmu)
155 return -EINVAL;
156 if (arm_smmu_ssids_in_use(&master->cd_table))
157 return -EBUSY;
158
159 mutex_lock(&arm_smmu_asid_lock);
160 /*
161 * The VM has to control the actual ATS state at the PCI device because
162 * we forward the invalidations directly from the VM. If the VM doesn't
163 * think ATS is on it will not generate ATC flushes and the ATC will
164 * become incoherent. Since we can't access the actual virtual PCI ATS
165 * config bit here base this off the EATS value in the STE. If the EATS
166 * is set then the VM must generate ATC flushes.
167 */
168 state.disable_ats = !nested_domain->enable_ats;
169 ret = arm_smmu_attach_prepare(&state, domain);
170 if (ret) {
171 mutex_unlock(&arm_smmu_asid_lock);
172 return ret;
173 }
174
175 arm_smmu_make_nested_domain_ste(&ste, master, nested_domain,
176 state.ats_enabled);
177 arm_smmu_install_ste_for_dev(master, &ste);
178 arm_smmu_attach_commit(&state);
179 mutex_unlock(&arm_smmu_asid_lock);
180 return 0;
181 }
182
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [RFC PATCH v2 10/10] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
In reply to Pranjal Shrivastavadrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, · line 150
Quoted 5 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1
-0@@ -139,6 +147,7 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, .old_domain = iommu_get_domain_for_dev(dev), .ssid = IOMMU_NO_PASID, }; +struct arm_smmu_device *smmu = master->smmu;
Appears unused.
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_rpm_get_if_active(struct arm_smmu_device *smmu) · line 133
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-1@@ -130,10 +130,10 @@ static int arm_smmu_rpm_get_if_active(struct arm_smmu_device *smmu)if (pm_runtime_enabled (smmu->dev))return pm_runtime_get_if_in_use (smmu->dev); -return 0 ; +return 1 ;
Can we fix this in the commit that added this function?
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd, · line 2440
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0@@ -2365,6 +2437,10 @@ static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd,if (!size)return ; +/* No need to invalidate TLBs if smmu is suspended */
The code doesn't check if the SMMU is suspended but rather whether the usage count is zero which is different. I'm concerned about the following sequence of events:
* usage count drops to 0
* TLB invalidations are elided, stale TLB entries remain
* usage count becomes non-zero
This would result in stale TLB entries staying permanently. I propose adding a new member like "is_suspended" to struct arm_smmu_device that stores whether the SMMU device is suspended. If this value reads true (i.e. SMMU is suspended) then you can rely on the driver issuing TLBI_NSNH_ALL before re-enabling SMMU. Use proper acquire and release semantics when accessing this variable.
Re: [RFC PATCH v2 10/10] iommu/arm-smmu-v3: Invoke pm_runtime before hw access
Quoted 9 lines · Daniel Mentz
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, · line 150
Quoted 5 lines · Pranjal Shrivastava
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1
-0@@ -139,6 +147,7 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, .old_domain = iommu_get_domain_for_dev(dev), .ssid = IOMMU_NO_PASID, }; +struct arm_smmu_device *smmu = master->smmu;Appears unused.
Yes, left it by mistake, the kernel test robot complains about it too, will remove this.
Quoted 9 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_rpm_get_if_active(struct arm_smmu_device *smmu) · line 133
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-1@@ -130,10 +130,10 @@ static int arm_smmu_rpm_get_if_active(struct arm_smmu_device *smmu)if (pm_runtime_enabled (smmu->dev))return pm_runtime_get_if_in_use (smmu->dev); -return 0 ; +return 1 ;Can we fix this in the commit that added this function?
Ugh, yes, my bad. Will move it to the appropriate commit.
Quoted 14 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd, · line 2440
Quoted 5 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1
-0@@ -2365,6 +2437,10 @@ static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd,if (!size)return ; +/* No need to invalidate TLBs if smmu is suspended */ The code doesn't check if the SMMU is suspended but rather whether the usage count is zero which is different. I'm concerned about the following sequence of events:
* usage count drops to 0
* TLB invalidations are elided, stale TLB entries remain
* usage count becomes non-zero
Quoted 6 lines
This would result in stale TLB entries staying permanently. I propose adding a new member like "
is_suspended" to structarm_smmu_devicethat stores whether the SMMU device is suspended. If this value reads true (i.e. SMMU is suspended) then you can rely on the driver issuingTLBI_NSNH_ALLbefore re-enabling SMMU. Use proper acquire and release semantics when accessing this variable.
Hmm.. we aren't relying on the refcount alone but the rpm state as well. Pasting the following lines from pm_runtime_get_if_in_use's doc strings:
* Increment the runtime PM usage counter of @dev if its runtime PM
* status is %RPM_ACTIVE and its runtime PM usage counter is greater
* than 0, in which case it returns 1.
Thus, we aren't only eliding the invalidations when the refcount == 0 but also when rpm state != RPM_ACTIVE which is checked first[1].
I believe you're suggesting a case where the rpm_state == RPM_ACTIVE but the refcount bounces between 0 & 1 without changing the rpm state?
Skimming through the core runtime pm code[2], it seems it does nothing to resume if current_state and/or last_state were RPM_ACTIVE.
Let me dig deeper into the core runtime code to see if we're missing something here.. otherwise, I agree with your suggestion to have a bool to check if the smmu is really suspended.
Thanks,
Praan
[1]
https://elixir.bootlin.com/linux/v6.15-rc4/source/drivers/base/power/runtime.c#L1217
[2]
https://elixir.bootlin.com/linux/v6.15-rc4/source/drivers/base/power/runtime.c#L784