Re: [PATCH v2 17/17] iommu/arm-smmu: Add context init implementation hook

2019-08-20 Thread Robin Murphy

On 20/08/2019 11:15, Vivek Gautam wrote:
[...]

Hi Robin,

Sorry for responding late to this series. I have couple of doubts here 
that I wanted to discuss.


Are we standardizing these implementation specific ops? Each vendor 
implementations will have something peculiar to take care. Things are 
good right now with 'reset', 'cfg_probe', and 'init_context' hooks.
But, on top of vendor implementation details, there can be SoC specific 
errata changes that need to be added.


The idea behind the impl hooks is to try to have them in logical places 
which should be suitable for multiple different workarounds (e.g. 
init_context is arranged to allow replacing smmu_domain->tlb_ops) - I 
want to avoid proliferating dozens of them that end up each being 
specific to individual workarounds, but that's not to say that the 
design we're starting with here is by any means complete or final. We're 
almost certainly going to evolve more hooks (and possibly adjust the 
current ones) in future.


Moreover, adding implementation data based on __model__ may not suffice 
for long. Do you suggest adding any other data variable in the 
ARM_SMMU_MATCH_DATA?


As commented in the code, the setup for the existing quirks works out to 
be deceptively simple, and it can and will change. Ultimately I'm fully 
expecting to end up with complex logic hanging off arm_smmu_impl_init() 
to detect the integration details and compose sets of impl hooks in 
various ways as appropriate - I doubt that it's going to be practical or 
even possible to have static data for *every* possibility. The 
fundamental shape of the code is based on "model" quirks being more 
general than "integration" quirks, such that the latter should be in a 
position to dynamically inherit (or statically replace, in simple cases) 
the hooks of the former.


To show SoC specific needs, I have the change attached in this email to 
take care of the SDM845 'wait-for-safe' sequence.

Please take a look.


Other than that introducing QCOM_SMMU500 seems to be redundant, and 
whether it also needs ACPI-based detection, that certainly seems fairly 
reasonable for a simple isolated workaround. However, is the firmware 
call really a one-off probe-time thing? If the firmware does take care 
of preserving the wait-for-safe state across 
suspend/resume/hibernate/etc. then fair enough, but I would have assumed 
that the reset hook would be the more likely place to put it.


Robin.
___
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu


Re: [PATCH v2 17/17] iommu/arm-smmu: Add context init implementation hook

2019-08-20 Thread Vivek Gautam



On 8/16/2019 12:07 AM, Robin Murphy wrote:

Allocating and initialising a context for a domain is another point
where certain implementations are known to want special behaviour.
Currently the other half of the Cavium workaround comes into play here,
so let's finish the job to get the whole thing right out of the way.

Signed-off-by: Robin Murphy 
---
  drivers/iommu/arm-smmu-impl.c | 42 ++---
  drivers/iommu/arm-smmu.c  | 51 +++
  drivers/iommu/arm-smmu.h  | 42 +++--
  3 files changed, 87 insertions(+), 48 deletions(-)

diff --git a/drivers/iommu/arm-smmu-impl.c b/drivers/iommu/arm-smmu-impl.c
index 4dc8b1c4befb..e22e9004f449 100644
--- a/drivers/iommu/arm-smmu-impl.c
+++ b/drivers/iommu/arm-smmu-impl.c
@@ -48,25 +48,60 @@ const struct arm_smmu_impl calxeda_impl = {
  };
  
  
+struct cavium_smmu {

+   struct arm_smmu_device smmu;
+   u32 id_base;
+};
+
  static int cavium_cfg_probe(struct arm_smmu_device *smmu)
  {
static atomic_t context_count = ATOMIC_INIT(0);
+   struct cavium_smmu *cs = container_of(smmu, struct cavium_smmu, smmu);
/*
 * Cavium CN88xx erratum #27704.
 * Ensure ASID and VMID allocation is unique across all SMMUs in
 * the system.
 */
-   smmu->cavium_id_base = atomic_fetch_add(smmu->num_context_banks,
-  _count);
+   cs->id_base = atomic_fetch_add(smmu->num_context_banks, _count);
dev_notice(smmu->dev, "\tenabling workaround for Cavium erratum 
27704\n");
  
  	return 0;

  }
  
+int cavium_init_context(struct arm_smmu_domain *smmu_domain)

+{
+   struct cavium_smmu *cs = container_of(smmu_domain->smmu,
+ struct cavium_smmu, smmu);
+
+   if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
+   smmu_domain->cfg.vmid += cs->id_base;
+   else
+   smmu_domain->cfg.asid += cs->id_base;
+
+   return 0;
+}
+
  const struct arm_smmu_impl cavium_impl = {
.cfg_probe = cavium_cfg_probe,
+   .init_context = cavium_init_context,
  };
  
+struct arm_smmu_device *cavium_smmu_impl_init(struct arm_smmu_device *smmu)

+{
+   struct cavium_smmu *cs;
+
+   cs = devm_kzalloc(smmu->dev, sizeof(*cs), GFP_KERNEL);
+   if (!cs)
+   return ERR_PTR(-ENOMEM);
+
+   cs->smmu = *smmu;
+   cs->smmu.impl = _impl;
+
+   devm_kfree(smmu->dev, smmu);
+
+   return >smmu;
+}
+
  
  #define ARM_MMU500_ACTLR_CPRE		(1 << 1)
  
@@ -126,8 +161,7 @@ struct arm_smmu_device *arm_smmu_impl_init(struct arm_smmu_device *smmu)

smmu->impl = _mmu500_impl;
break;
case CAVIUM_SMMUV2:
-   smmu->impl = _impl;
-   break;
+   return cavium_smmu_impl_init(smmu);
default:
break;
}
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index fc98992d120d..b8628e2ab579 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -27,7 +27,6 @@
  #include 
  #include 
  #include 
-#include 
  #include 
  #include 
  #include 
@@ -111,44 +110,6 @@ struct arm_smmu_master_cfg {
  #define for_each_cfg_sme(fw, i, idx) \
for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
  
-enum arm_smmu_context_fmt {

-   ARM_SMMU_CTX_FMT_NONE,
-   ARM_SMMU_CTX_FMT_AARCH64,
-   ARM_SMMU_CTX_FMT_AARCH32_L,
-   ARM_SMMU_CTX_FMT_AARCH32_S,
-};
-
-struct arm_smmu_cfg {
-   u8  cbndx;
-   u8  irptndx;
-   union {
-   u16 asid;
-   u16 vmid;
-   };
-   enum arm_smmu_cbar_type cbar;
-   enum arm_smmu_context_fmt   fmt;
-};
-#define INVALID_IRPTNDX0xff
-
-enum arm_smmu_domain_stage {
-   ARM_SMMU_DOMAIN_S1 = 0,
-   ARM_SMMU_DOMAIN_S2,
-   ARM_SMMU_DOMAIN_NESTED,
-   ARM_SMMU_DOMAIN_BYPASS,
-};
-
-struct arm_smmu_domain {
-   struct arm_smmu_device  *smmu;
-   struct io_pgtable_ops   *pgtbl_ops;
-   const struct iommu_gather_ops   *tlb_ops;
-   struct arm_smmu_cfg cfg;
-   enum arm_smmu_domain_stage  stage;
-   boolnon_strict;
-   struct mutexinit_mutex; /* Protects smmu pointer */
-   spinlock_t  cb_lock; /* Serialises ATS1* ops and 
TLB syncs */
-   struct iommu_domain domain;
-};
-
  static bool using_legacy_binding, using_generic_binding;
  
  static inline int arm_smmu_rpm_get(struct arm_smmu_device *smmu)

@@ -749,9 +710,16 @@ static int arm_smmu_init_domain_context(struct 
iommu_domain *domain,
}
  
  	if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)

-   cfg->vmid = cfg->cbndx + 1 + smmu->cavium_id_base;
+   

[PATCH v2 17/17] iommu/arm-smmu: Add context init implementation hook

2019-08-15 Thread Robin Murphy
Allocating and initialising a context for a domain is another point
where certain implementations are known to want special behaviour.
Currently the other half of the Cavium workaround comes into play here,
so let's finish the job to get the whole thing right out of the way.

Signed-off-by: Robin Murphy 
---
 drivers/iommu/arm-smmu-impl.c | 42 ++---
 drivers/iommu/arm-smmu.c  | 51 +++
 drivers/iommu/arm-smmu.h  | 42 +++--
 3 files changed, 87 insertions(+), 48 deletions(-)

diff --git a/drivers/iommu/arm-smmu-impl.c b/drivers/iommu/arm-smmu-impl.c
index 4dc8b1c4befb..e22e9004f449 100644
--- a/drivers/iommu/arm-smmu-impl.c
+++ b/drivers/iommu/arm-smmu-impl.c
@@ -48,25 +48,60 @@ const struct arm_smmu_impl calxeda_impl = {
 };
 
 
+struct cavium_smmu {
+   struct arm_smmu_device smmu;
+   u32 id_base;
+};
+
 static int cavium_cfg_probe(struct arm_smmu_device *smmu)
 {
static atomic_t context_count = ATOMIC_INIT(0);
+   struct cavium_smmu *cs = container_of(smmu, struct cavium_smmu, smmu);
/*
 * Cavium CN88xx erratum #27704.
 * Ensure ASID and VMID allocation is unique across all SMMUs in
 * the system.
 */
-   smmu->cavium_id_base = atomic_fetch_add(smmu->num_context_banks,
-  _count);
+   cs->id_base = atomic_fetch_add(smmu->num_context_banks, _count);
dev_notice(smmu->dev, "\tenabling workaround for Cavium erratum 
27704\n");
 
return 0;
 }
 
+int cavium_init_context(struct arm_smmu_domain *smmu_domain)
+{
+   struct cavium_smmu *cs = container_of(smmu_domain->smmu,
+ struct cavium_smmu, smmu);
+
+   if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
+   smmu_domain->cfg.vmid += cs->id_base;
+   else
+   smmu_domain->cfg.asid += cs->id_base;
+
+   return 0;
+}
+
 const struct arm_smmu_impl cavium_impl = {
.cfg_probe = cavium_cfg_probe,
+   .init_context = cavium_init_context,
 };
 
+struct arm_smmu_device *cavium_smmu_impl_init(struct arm_smmu_device *smmu)
+{
+   struct cavium_smmu *cs;
+
+   cs = devm_kzalloc(smmu->dev, sizeof(*cs), GFP_KERNEL);
+   if (!cs)
+   return ERR_PTR(-ENOMEM);
+
+   cs->smmu = *smmu;
+   cs->smmu.impl = _impl;
+
+   devm_kfree(smmu->dev, smmu);
+
+   return >smmu;
+}
+
 
 #define ARM_MMU500_ACTLR_CPRE  (1 << 1)
 
@@ -126,8 +161,7 @@ struct arm_smmu_device *arm_smmu_impl_init(struct 
arm_smmu_device *smmu)
smmu->impl = _mmu500_impl;
break;
case CAVIUM_SMMUV2:
-   smmu->impl = _impl;
-   break;
+   return cavium_smmu_impl_init(smmu);
default:
break;
}
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index fc98992d120d..b8628e2ab579 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -27,7 +27,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -111,44 +110,6 @@ struct arm_smmu_master_cfg {
 #define for_each_cfg_sme(fw, i, idx) \
for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
 
-enum arm_smmu_context_fmt {
-   ARM_SMMU_CTX_FMT_NONE,
-   ARM_SMMU_CTX_FMT_AARCH64,
-   ARM_SMMU_CTX_FMT_AARCH32_L,
-   ARM_SMMU_CTX_FMT_AARCH32_S,
-};
-
-struct arm_smmu_cfg {
-   u8  cbndx;
-   u8  irptndx;
-   union {
-   u16 asid;
-   u16 vmid;
-   };
-   enum arm_smmu_cbar_type cbar;
-   enum arm_smmu_context_fmt   fmt;
-};
-#define INVALID_IRPTNDX0xff
-
-enum arm_smmu_domain_stage {
-   ARM_SMMU_DOMAIN_S1 = 0,
-   ARM_SMMU_DOMAIN_S2,
-   ARM_SMMU_DOMAIN_NESTED,
-   ARM_SMMU_DOMAIN_BYPASS,
-};
-
-struct arm_smmu_domain {
-   struct arm_smmu_device  *smmu;
-   struct io_pgtable_ops   *pgtbl_ops;
-   const struct iommu_gather_ops   *tlb_ops;
-   struct arm_smmu_cfg cfg;
-   enum arm_smmu_domain_stage  stage;
-   boolnon_strict;
-   struct mutexinit_mutex; /* Protects smmu pointer */
-   spinlock_t  cb_lock; /* Serialises ATS1* ops and 
TLB syncs */
-   struct iommu_domain domain;
-};
-
 static bool using_legacy_binding, using_generic_binding;
 
 static inline int arm_smmu_rpm_get(struct arm_smmu_device *smmu)
@@ -749,9 +710,16 @@ static int arm_smmu_init_domain_context(struct 
iommu_domain *domain,
}
 
if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
-   cfg->vmid = cfg->cbndx + 1 + smmu->cavium_id_base;
+   cfg->vmid = cfg->cbndx + 1;
else
-