diff mbox series

[v3,1/8] iommu/arm-smmu: Refactor context bank allocation

Message ID 20200904155513.282067-2-bjorn.andersson@linaro.org
State New
Headers show
Series iommu/arm-smmu: Support maintaining bootloader mappings | expand

Commit Message

Bjorn Andersson Sept. 4, 2020, 3:55 p.m. UTC
Extract the conditional invocation of the platform defined
alloc_context_bank() to a separate function to keep
arm_smmu_init_domain_context() cleaner.

Instead pass a reference to the arm_smmu_device as parameter to the
call. Also remove the count parameter, as this can be read from the
newly passed object.

This allows us to not assign smmu_domain->smmu before attempting to
allocate the context bank and as such we don't need to roll back this
assignment on failure.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>

---

Note that this series applies ontop of:
https://lore.kernel.org/linux-arm-msm/20200901164707.2645413-1-robdclark@gmail.com/

This could either go on its own, or be squashed with "[PATCH v16 14/20]
iommu/arm-smmu: Prepare for the adreno-smmu implementation" from Rob's series.

Changes since v2:
- New patch

 drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c |  6 ++++--
 drivers/iommu/arm/arm-smmu/arm-smmu.c      | 23 ++++++++++++----------
 drivers/iommu/arm/arm-smmu/arm-smmu.h      |  3 ++-
 3 files changed, 19 insertions(+), 13 deletions(-)

-- 
2.28.0

Comments

Jordan Crouse Sept. 8, 2020, 6:42 p.m. UTC | #1
On Fri, Sep 04, 2020 at 03:55:06PM +0000, Bjorn Andersson wrote:
> Extract the conditional invocation of the platform defined

> alloc_context_bank() to a separate function to keep

> arm_smmu_init_domain_context() cleaner.

> 

> Instead pass a reference to the arm_smmu_device as parameter to the

> call. Also remove the count parameter, as this can be read from the

> newly passed object.

> 

> This allows us to not assign smmu_domain->smmu before attempting to

> allocate the context bank and as such we don't need to roll back this

> assignment on failure.


Much nicer.

Reviewed-by: Jordan Crouse <jcrouse@codeaurora.org>


> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>

> ---

> 

> Note that this series applies ontop of:

> https://lore.kernel.org/linux-arm-msm/20200901164707.2645413-1-robdclark@gmail.com/

> 

> This could either go on its own, or be squashed with "[PATCH v16 14/20]

> iommu/arm-smmu: Prepare for the adreno-smmu implementation" from Rob's series.

> 

> Changes since v2:

> - New patch

> 

>  drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c |  6 ++++--

>  drivers/iommu/arm/arm-smmu/arm-smmu.c      | 23 ++++++++++++----------

>  drivers/iommu/arm/arm-smmu/arm-smmu.h      |  3 ++-

>  3 files changed, 19 insertions(+), 13 deletions(-)

> 

> diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c

> index 2aa6249050ff..0663d7d26908 100644

> --- a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c

> +++ b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c

> @@ -91,9 +91,10 @@ static int qcom_adreno_smmu_set_ttbr0_cfg(const void *cookie,

>  }

>  

>  static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,

> -		struct device *dev, int start, int count)

> +					       struct arm_smmu_device *smmu,

> +					       struct device *dev, int start)

>  {

> -	struct arm_smmu_device *smmu = smmu_domain->smmu;

> +	int count;

>  

>  	/*

>  	 * Assign context bank 0 to the GPU device so the GPU hardware can

> @@ -104,6 +105,7 @@ static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_doma

>  		count = 1;

>  	} else {

>  		start = 1;

> +		count = smmu->num_context_banks;

>  	}

>  

>  	return __arm_smmu_alloc_bitmap(smmu->context_map, start, count);

> diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c

> index bbec5793faf8..e19d7bdc7674 100644

> --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c

> +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c

> @@ -623,6 +623,16 @@ void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)

>  	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);

>  }

>  

> +static int arm_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,

> +				       struct arm_smmu_device *smmu,

> +				       struct device *dev, unsigned int start)

> +{

> +	if (smmu->impl && smmu->impl->alloc_context_bank)

> +		return smmu->impl->alloc_context_bank(smmu_domain, smmu, dev, start);

> +

> +	return __arm_smmu_alloc_bitmap(smmu->context_map, start, smmu->num_context_banks);

> +}

> +

>  static int arm_smmu_init_domain_context(struct iommu_domain *domain,

>  					struct arm_smmu_device *smmu,

>  					struct device *dev)

> @@ -741,20 +751,13 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,

>  		goto out_unlock;

>  	}

>  

> -	smmu_domain->smmu = smmu;

> -

> -	if (smmu->impl && smmu->impl->alloc_context_bank)

> -		ret = smmu->impl->alloc_context_bank(smmu_domain, dev,

> -				start, smmu->num_context_banks);

> -	else

> -		ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,

> -				      smmu->num_context_banks);

> -

> +	ret = arm_smmu_alloc_context_bank(smmu_domain, smmu, dev, start);

>  	if (ret < 0) {

> -		smmu_domain->smmu = NULL;

>  		goto out_unlock;

>  	}

>  

> +	smmu_domain->smmu = smmu;

> +

>  	cfg->cbndx = ret;

>  	if (smmu->version < ARM_SMMU_V2) {

>  		cfg->irptndx = atomic_inc_return(&smmu->irptndx);

> diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.h b/drivers/iommu/arm/arm-smmu/arm-smmu.h

> index 2df3a70a8a41..ddf2ca4c923d 100644

> --- a/drivers/iommu/arm/arm-smmu/arm-smmu.h

> +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.h

> @@ -437,7 +437,8 @@ struct arm_smmu_impl {

>  	irqreturn_t (*global_fault)(int irq, void *dev);

>  	irqreturn_t (*context_fault)(int irq, void *dev);

>  	int (*alloc_context_bank)(struct arm_smmu_domain *smmu_domain,

> -			struct device *dev, int start, int max);

> +				  struct arm_smmu_device *smmu,

> +				  struct device *dev, int start);

>  };

>  

>  #define INVALID_SMENDX			-1

> -- 

> 2.28.0

> 


-- 
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Jordan Crouse Sept. 8, 2020, 6:46 p.m. UTC | #2
On Tue, Sep 08, 2020 at 06:42:06PM +0000, Jordan Crouse wrote:
> On Fri, Sep 04, 2020 at 03:55:06PM +0000, Bjorn Andersson wrote:

> > Extract the conditional invocation of the platform defined

> > alloc_context_bank() to a separate function to keep

> > arm_smmu_init_domain_context() cleaner.

> > 

> > Instead pass a reference to the arm_smmu_device as parameter to the

> > call. Also remove the count parameter, as this can be read from the

> > newly passed object.

> > 

> > This allows us to not assign smmu_domain->smmu before attempting to

> > allocate the context bank and as such we don't need to roll back this

> > assignment on failure.

> 

> Much nicer.

> 

> Reviewed-by: Jordan Crouse <jcrouse@codeaurora.org>


I didn't notice that Rob had grabbed this one for his stack. That's fine too.

> > Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>

> > ---

> > 

> > Note that this series applies ontop of:

> > https://lore.kernel.org/linux-arm-msm/20200901164707.2645413-1-robdclark@gmail.com/

> > 

> > This could either go on its own, or be squashed with "[PATCH v16 14/20]

> > iommu/arm-smmu: Prepare for the adreno-smmu implementation" from Rob's series.

> > 

> > Changes since v2:

> > - New patch

> > 

> >  drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c |  6 ++++--

> >  drivers/iommu/arm/arm-smmu/arm-smmu.c      | 23 ++++++++++++----------

> >  drivers/iommu/arm/arm-smmu/arm-smmu.h      |  3 ++-

> >  3 files changed, 19 insertions(+), 13 deletions(-)

> > 

> > diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c

> > index 2aa6249050ff..0663d7d26908 100644

> > --- a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c

> > +++ b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c

> > @@ -91,9 +91,10 @@ static int qcom_adreno_smmu_set_ttbr0_cfg(const void *cookie,

> >  }

> >  

> >  static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,

> > -		struct device *dev, int start, int count)

> > +					       struct arm_smmu_device *smmu,

> > +					       struct device *dev, int start)

> >  {

> > -	struct arm_smmu_device *smmu = smmu_domain->smmu;

> > +	int count;

> >  

> >  	/*

> >  	 * Assign context bank 0 to the GPU device so the GPU hardware can

> > @@ -104,6 +105,7 @@ static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_doma

> >  		count = 1;

> >  	} else {

> >  		start = 1;

> > +		count = smmu->num_context_banks;

> >  	}

> >  

> >  	return __arm_smmu_alloc_bitmap(smmu->context_map, start, count);

> > diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c

> > index bbec5793faf8..e19d7bdc7674 100644

> > --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c

> > +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c

> > @@ -623,6 +623,16 @@ void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)

> >  	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);

> >  }

> >  

> > +static int arm_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,

> > +				       struct arm_smmu_device *smmu,

> > +				       struct device *dev, unsigned int start)

> > +{

> > +	if (smmu->impl && smmu->impl->alloc_context_bank)

> > +		return smmu->impl->alloc_context_bank(smmu_domain, smmu, dev, start);

> > +

> > +	return __arm_smmu_alloc_bitmap(smmu->context_map, start, smmu->num_context_banks);

> > +}

> > +

> >  static int arm_smmu_init_domain_context(struct iommu_domain *domain,

> >  					struct arm_smmu_device *smmu,

> >  					struct device *dev)

> > @@ -741,20 +751,13 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,

> >  		goto out_unlock;

> >  	}

> >  

> > -	smmu_domain->smmu = smmu;

> > -

> > -	if (smmu->impl && smmu->impl->alloc_context_bank)

> > -		ret = smmu->impl->alloc_context_bank(smmu_domain, dev,

> > -				start, smmu->num_context_banks);

> > -	else

> > -		ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,

> > -				      smmu->num_context_banks);

> > -

> > +	ret = arm_smmu_alloc_context_bank(smmu_domain, smmu, dev, start);

> >  	if (ret < 0) {

> > -		smmu_domain->smmu = NULL;

> >  		goto out_unlock;

> >  	}

> >  

> > +	smmu_domain->smmu = smmu;

> > +

> >  	cfg->cbndx = ret;

> >  	if (smmu->version < ARM_SMMU_V2) {

> >  		cfg->irptndx = atomic_inc_return(&smmu->irptndx);

> > diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.h b/drivers/iommu/arm/arm-smmu/arm-smmu.h

> > index 2df3a70a8a41..ddf2ca4c923d 100644

> > --- a/drivers/iommu/arm/arm-smmu/arm-smmu.h

> > +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.h

> > @@ -437,7 +437,8 @@ struct arm_smmu_impl {

> >  	irqreturn_t (*global_fault)(int irq, void *dev);

> >  	irqreturn_t (*context_fault)(int irq, void *dev);

> >  	int (*alloc_context_bank)(struct arm_smmu_domain *smmu_domain,

> > -			struct device *dev, int start, int max);

> > +				  struct arm_smmu_device *smmu,

> > +				  struct device *dev, int start);

> >  };

> >  

> >  #define INVALID_SMENDX			-1

> > -- 

> > 2.28.0

> > 

> 

> -- 

> The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,

> a Linux Foundation Collaborative Project

> _______________________________________________

> iommu mailing list

> iommu@lists.linux-foundation.org

> https://lists.linuxfoundation.org/mailman/listinfo/iommu


-- 
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Sai Prakash Ranjan Sept. 11, 2020, 8:18 a.m. UTC | #3
On 2020-09-04 21:25, Bjorn Andersson wrote:
> Extract the conditional invocation of the platform defined

> alloc_context_bank() to a separate function to keep

> arm_smmu_init_domain_context() cleaner.

> 

> Instead pass a reference to the arm_smmu_device as parameter to the

> call. Also remove the count parameter, as this can be read from the

> newly passed object.

> 

> This allows us to not assign smmu_domain->smmu before attempting to

> allocate the context bank and as such we don't need to roll back this

> assignment on failure.

> 

> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>

> ---


Reviewed-by: Sai Prakash Ranjan <saiprakash.ranjan@codeaurora.org>

Tested-by: Sai Prakash Ranjan <saiprakash.ranjan@codeaurora.org>


-- 
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a 
member
of Code Aurora Forum, hosted by The Linux Foundation
diff mbox series

Patch

diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c
index 2aa6249050ff..0663d7d26908 100644
--- a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c
+++ b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c
@@ -91,9 +91,10 @@  static int qcom_adreno_smmu_set_ttbr0_cfg(const void *cookie,
 }
 
 static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,
-		struct device *dev, int start, int count)
+					       struct arm_smmu_device *smmu,
+					       struct device *dev, int start)
 {
-	struct arm_smmu_device *smmu = smmu_domain->smmu;
+	int count;
 
 	/*
 	 * Assign context bank 0 to the GPU device so the GPU hardware can
@@ -104,6 +105,7 @@  static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_doma
 		count = 1;
 	} else {
 		start = 1;
+		count = smmu->num_context_banks;
 	}
 
 	return __arm_smmu_alloc_bitmap(smmu->context_map, start, count);
diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c
index bbec5793faf8..e19d7bdc7674 100644
--- a/drivers/iommu/arm/arm-smmu/arm-smmu.c
+++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c
@@ -623,6 +623,16 @@  void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)
 	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
 }
 
+static int arm_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,
+				       struct arm_smmu_device *smmu,
+				       struct device *dev, unsigned int start)
+{
+	if (smmu->impl && smmu->impl->alloc_context_bank)
+		return smmu->impl->alloc_context_bank(smmu_domain, smmu, dev, start);
+
+	return __arm_smmu_alloc_bitmap(smmu->context_map, start, smmu->num_context_banks);
+}
+
 static int arm_smmu_init_domain_context(struct iommu_domain *domain,
 					struct arm_smmu_device *smmu,
 					struct device *dev)
@@ -741,20 +751,13 @@  static int arm_smmu_init_domain_context(struct iommu_domain *domain,
 		goto out_unlock;
 	}
 
-	smmu_domain->smmu = smmu;
-
-	if (smmu->impl && smmu->impl->alloc_context_bank)
-		ret = smmu->impl->alloc_context_bank(smmu_domain, dev,
-				start, smmu->num_context_banks);
-	else
-		ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
-				      smmu->num_context_banks);
-
+	ret = arm_smmu_alloc_context_bank(smmu_domain, smmu, dev, start);
 	if (ret < 0) {
-		smmu_domain->smmu = NULL;
 		goto out_unlock;
 	}
 
+	smmu_domain->smmu = smmu;
+
 	cfg->cbndx = ret;
 	if (smmu->version < ARM_SMMU_V2) {
 		cfg->irptndx = atomic_inc_return(&smmu->irptndx);
diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.h b/drivers/iommu/arm/arm-smmu/arm-smmu.h
index 2df3a70a8a41..ddf2ca4c923d 100644
--- a/drivers/iommu/arm/arm-smmu/arm-smmu.h
+++ b/drivers/iommu/arm/arm-smmu/arm-smmu.h
@@ -437,7 +437,8 @@  struct arm_smmu_impl {
 	irqreturn_t (*global_fault)(int irq, void *dev);
 	irqreturn_t (*context_fault)(int irq, void *dev);
 	int (*alloc_context_bank)(struct arm_smmu_domain *smmu_domain,
-			struct device *dev, int start, int max);
+				  struct arm_smmu_device *smmu,
+				  struct device *dev, int start);
 };
 
 #define INVALID_SMENDX			-1