[RFC PATCH v2 1/9] iommu/arm-smmu-v3: group attached devices by smmu

From: Michael Shavit
Date: Tue Aug 22 2023 - 06:57:52 EST


Always insert a new master in the devices_list besides other masters
that belong to the same smmu.
This allows code to batch commands by SMMU when iterating over masters
that a domain is attached to.

Signed-off-by: Michael Shavit <mshavit@xxxxxxxxxx>
---

Changes in v2:
- New commit

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 22 ++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index f17704c35858d..37b9223c145ba 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2382,6 +2382,24 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master)
arm_smmu_write_ctx_desc(master, 0, NULL);
}

+static void arm_smmu_domain_device_list_add(struct arm_smmu_domain *smmu_domain,
+ struct arm_smmu_master *master)
+{
+ struct arm_smmu_master *iter;
+ unsigned long flags;
+
+ spin_lock_irqsave(&smmu_domain->devices_lock, flags);
+ if (list_empty(&smmu_domain->devices))
+ list_add(&master->domain_head, &smmu_domain->devices);
+ else {
+ list_for_each_entry(iter, &smmu_domain->devices, domain_head)
+ if (iter->smmu == master->smmu)
+ break;
+ list_add(&master->domain_head, &iter->domain_head);
+ }
+ spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+}
+
static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
{
int ret = 0;
@@ -2435,9 +2453,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
if (smmu_domain->stage != ARM_SMMU_DOMAIN_BYPASS)
master->ats_enabled = arm_smmu_ats_supported(master);

- spin_lock_irqsave(&smmu_domain->devices_lock, flags);
- list_add(&master->domain_head, &smmu_domain->devices);
- spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+ arm_smmu_domain_device_list_add(smmu_domain, master);

if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
if (!master->cd_table.cdtab) {
--
2.42.0.rc1.204.g551eb34607-goog