diff mbox series

[2/2] crypto: caam - OP-TEE firmware support

Message ID 20230302062055.1564514-3-meenakshi.aggarwal@nxp.com
State Superseded
Headers show
Series No access to CAAM page 0 registers | expand

Commit Message

Meenakshi Aggarwal March 2, 2023, 6:20 a.m. UTC
From: Horia Geantă <horia.geanta@nxp.com>

caam driver needs to be aware of OP-TEE f/w presence, since some things
are done differently:

1. there is no access to controller's register page (note however that
some registers are aliased in job rings' register pages)

2 Due to this, MCFGR[PS] cannot be read and driver assumes
MCFGR[PS] = b'0 - engine using 32-bit address pointers.

This is in sync with the fact that:
-all i.MX SoCs currently use MCFGR[PS] = b'0
-only i.MX OP-TEE use cases don't allow access to controller register page

Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
Signed-off-by: Meenakshi Aggarwal <meenakshi.aggarwal@nxp.com>
---
 drivers/crypto/caam/ctrl.c    | 24 ++++++++++++++++++++++--
 drivers/crypto/caam/debugfs.c |  3 +++
 drivers/crypto/caam/intern.h  |  3 ++-
 3 files changed, 27 insertions(+), 3 deletions(-)

Comments

Gaurav Jain April 4, 2023, 6:08 a.m. UTC | #1
Hi

> -----Original Message-----
> From: Meenakshi Aggarwal <meenakshi.aggarwal@nxp.com>
> Sent: Wednesday, March 22, 2023 11:47 AM
> To: Horia Geanta <horia.geanta@nxp.com>; Varun Sethi <V.Sethi@nxp.com>;
> Pankaj Gupta <pankaj.gupta@nxp.com>; Gaurav Jain <gaurav.jain@nxp.com>;
> herbert@gondor.apana.org.au; davem@davemloft.net; linux-
> crypto@vger.kernel.org; linux-kernel@vger.kernel.org
> Cc: Meenakshi Aggarwal <meenakshi.aggarwal@nxp.com>
> Subject: [PATCH v2 2/2] crypto: caam - OP-TEE firmware support
> 
> From: Horia GeantA <horia.geanta@nxp.com>
> 
> caam driver needs to be aware of OP-TEE f/w presence, since some things are
> done differently:
> 
> 1. there is no access to controller's register page (note however that some
> registers are aliased in job rings' register pages)
> 
> 2 Due to this, MCFGR[PS] cannot be read and driver assumes MCFGR[PS] = b'0 -
> engine using 32-bit address pointers.
> 
> This is in sync with the fact that:
> -all i.MX SoCs currently use MCFGR[PS] = b'0 -only i.MX OP-TEE use cases don't
> allow access to controller register page
> 
> Signed-off-by: Horia GeantA <horia.geanta@nxp.com>
> Signed-off-by: Meenakshi Aggarwal <meenakshi.aggarwal@nxp.com>
> ---
>  drivers/crypto/caam/ctrl.c    | 23 ++++++++++++++++++++++-
>  drivers/crypto/caam/debugfs.c |  3 +++
>  drivers/crypto/caam/intern.h  |  1 +
>  3 files changed, 26 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c index
> ae07c1e5fd38..d96c81540957 100644
> --- a/drivers/crypto/caam/ctrl.c
> +++ b/drivers/crypto/caam/ctrl.c
> @@ -633,6 +633,7 @@ static int caam_probe(struct platform_device *pdev)
>  	int pg_size;
>  	int BLOCK_OFFSET = 0;
>  	bool pr_support = false;
> +	bool reg_access = true;
> 
>  	ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
>  	if (!ctrlpriv)
> @@ -646,6 +647,17 @@ static int caam_probe(struct platform_device *pdev)
>  	caam_imx = (bool)imx_soc_match;
> 
>  	if (imx_soc_match) {
> +		/*
> +		 * Until Layerscape and i.MX OP-TEE get in sync,
> +		 * only i.MX OP-TEE use cases disallow access to
> +		 * caam page 0 (controller) registers.
> +		 */
> +		np = of_find_compatible_node(NULL, NULL, "linaro,optee-tz");
> +		ctrlpriv->optee_en = !!np;
> +		of_node_put(np);
> +
> +		reg_access = ctrlpriv->optee_en;
When optee is enabled in imx, we need to skip the operations done by optee.
Update this to reg_access = !ctrlpriv->optee_en;

Regards
Gaurav Jain

> +
>  		if (!imx_soc_match->data) {
>  			dev_err(dev, "No clock data provided for i.MX SoC");
>  			return -EINVAL;
> @@ -696,7 +708,8 @@ static int caam_probe(struct platform_device *pdev)
>  	caam_little_end = !(bool)(rd_reg32(&perfmon->status) &
>  				  (CSTA_PLEND | CSTA_ALT_PLEND));
>  	comp_params = rd_reg32(&perfmon->comp_parms_ms);
> -	if (comp_params & CTPR_MS_PS && rd_reg32(&ctrl->mcr) &
> MCFGR_LONG_PTR)
> +	if (reg_access && comp_params & CTPR_MS_PS &&
> +	    rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR)
>  		caam_ptr_sz = sizeof(u64);
>  	else
>  		caam_ptr_sz = sizeof(u32);
> @@ -761,6 +774,9 @@ static int caam_probe(struct platform_device *pdev)
>  	}
>  #endif
> 
> +	if (!reg_access)
> +		goto set_dma_mask;
> +
>  	/*
>  	 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
>  	 * long pointers in master configuration register.
> @@ -800,6 +816,7 @@ static int caam_probe(struct platform_device *pdev)
>  			      JRSTART_JR1_START | JRSTART_JR2_START |
>  			      JRSTART_JR3_START);
> 
> +set_dma_mask:
>  	ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev));
>  	if (ret) {
>  		dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
> @@ -842,6 +859,9 @@ static int caam_probe(struct platform_device *pdev)
>  		return -ENOMEM;
>  	}
> 
> +	if (!reg_access)
> +		goto report_live;
> +
>  	comp_params = rd_reg32(&perfmon->comp_parms_ls);
>  	ctrlpriv->blob_present = !!(comp_params & CTPR_LS_BLOB);
> 
> @@ -943,6 +963,7 @@ static int caam_probe(struct platform_device *pdev)
>  		clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
>  	}
> 
> +report_live:
>  	/* NOTE: RTIC detection ought to go here, around Si time */
> 
>  	caam_id = (u64)rd_reg32(&perfmon->caam_id_ms) << 32 | diff --git
> a/drivers/crypto/caam/debugfs.c b/drivers/crypto/caam/debugfs.c index
> 798ba989a8a0..cec93498836d 100644
> --- a/drivers/crypto/caam/debugfs.c
> +++ b/drivers/crypto/caam/debugfs.c
> @@ -77,6 +77,9 @@ void caam_debugfs_init(struct caam_drv_private *ctrlpriv,
>  	debugfs_create_file("fault_status", 0444, ctrlpriv->ctl,
>  			    &perfmon->status, &caam_fops_u32_ro);
> 
> +	if (ctrlpriv->optee_en)
> +		return;
> +
>  	/* Internal covering keys (useful in non-secure mode only) */
>  	ctrlpriv->ctl_kek_wrap.data = (__force void *)&ctrlpriv->ctrl->kek[0];
>  	ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32); diff --git
> a/drivers/crypto/caam/intern.h b/drivers/crypto/caam/intern.h index
> 572cf66c887a..86ed1b91c22d 100644
> --- a/drivers/crypto/caam/intern.h
> +++ b/drivers/crypto/caam/intern.h
> @@ -94,6 +94,7 @@ struct caam_drv_private {
>  	u8 qi_present;		/* Nonzero if QI present in device */
>  	u8 blob_present;	/* Nonzero if BLOB support present in device */
>  	u8 mc_en;		/* Nonzero if MC f/w is active */
> +	u8 optee_en;		/* Nonzero if OP-TEE f/w is active */
>  	int secvio_irq;		/* Security violation interrupt number */
>  	int virt_en;		/* Virtualization enabled in CAAM */
>  	int era;		/* CAAM Era (internal HW revision) */
> --
> 2.25.1
diff mbox series

Patch

diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c
index be9724d05d01..5a6cd63742a4 100644
--- a/drivers/crypto/caam/ctrl.c
+++ b/drivers/crypto/caam/ctrl.c
@@ -633,6 +633,7 @@  static int caam_probe(struct platform_device *pdev)
 	int pg_size;
 	int BLOCK_OFFSET = 0;
 	bool pr_support = false;
+	bool reg_access = true;
 
 	ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
 	if (!ctrlpriv)
@@ -646,6 +647,17 @@  static int caam_probe(struct platform_device *pdev)
 	caam_imx = (bool)imx_soc_match;
 
 	if (imx_soc_match) {
+		/*
+		 * Until Layerscape and i.MX OP-TEE get in sync,
+		 * only i.MX OP-TEE use cases disallow access to
+		 * caam page 0 (controller) registers.
+		 */
+		np = of_find_compatible_node(NULL, NULL, "linaro,optee-tz");
+		ctrlpriv->optee_en = !!np;
+		of_node_put(np);
+
+		reg_access = ctrlpriv->optee_en;
+
 		if (!imx_soc_match->data) {
 			dev_err(dev, "No clock data provided for i.MX SoC");
 			return -EINVAL;
@@ -696,7 +708,8 @@  static int caam_probe(struct platform_device *pdev)
 	caam_little_end = !(bool)(rd_reg32(&perfmon->status) &
 				  (CSTA_PLEND | CSTA_ALT_PLEND));
 	comp_params = rd_reg32(&perfmon->comp_parms_ms);
-	if (comp_params & CTPR_MS_PS && rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR)
+	if (reg_access && comp_params & CTPR_MS_PS &&
+	    rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR)
 		caam_ptr_sz = sizeof(u64);
 	else
 		caam_ptr_sz = sizeof(u32);
@@ -761,6 +774,9 @@  static int caam_probe(struct platform_device *pdev)
 	}
 #endif
 
+	if (!reg_access)
+		goto set_dma_mask;
+
 	/*
 	 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
 	 * long pointers in master configuration register.
@@ -799,7 +815,7 @@  static int caam_probe(struct platform_device *pdev)
 		clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
 			      JRSTART_JR1_START | JRSTART_JR2_START |
 			      JRSTART_JR3_START);
-
+set_dma_mask:
 	ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev));
 	if (ret) {
 		dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
@@ -842,6 +858,9 @@  static int caam_probe(struct platform_device *pdev)
 		return -ENOMEM;
 	}
 
+	if (!reg_access)
+		goto report_live;
+
 	comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ls);
 	ctrlpriv->blob_present = !!(comp_params & CTPR_LS_BLOB);
 
@@ -945,6 +964,7 @@  static int caam_probe(struct platform_device *pdev)
 
 	/* NOTE: RTIC detection ought to go here, around Si time */
 
+report_live:
 	caam_id = (u64)rd_reg32(&perfmon->caam_id_ms) << 32 |
 		  (u64)rd_reg32(&perfmon->caam_id_ls);
 
diff --git a/drivers/crypto/caam/debugfs.c b/drivers/crypto/caam/debugfs.c
index 73fe8530c0d6..42081224f568 100644
--- a/drivers/crypto/caam/debugfs.c
+++ b/drivers/crypto/caam/debugfs.c
@@ -76,6 +76,9 @@  void caam_debugfs_init(struct caam_drv_private *ctrlpriv,
 	debugfs_create_file("fault_status", 0444, ctrlpriv->ctl,
 			    &perfmon->status, &caam_fops_u32_ro);
 
+	if (ctrlpriv->optee_en)
+		return;
+
 	/* Internal covering keys (useful in non-secure mode only) */
 	ctrlpriv->ctl_kek_wrap.data = (__force void *)&ctrlpriv->ctrl->kek[0];
 	ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
diff --git a/drivers/crypto/caam/intern.h b/drivers/crypto/caam/intern.h
index 572cf66c887a..efe2bc2f1103 100644
--- a/drivers/crypto/caam/intern.h
+++ b/drivers/crypto/caam/intern.h
@@ -4,7 +4,7 @@ 
  * Private/internal definitions between modules
  *
  * Copyright 2008-2011 Freescale Semiconductor, Inc.
- * Copyright 2019 NXP
+ * Copyright 2019, 2023 NXP
  */
 
 #ifndef INTERN_H
@@ -94,6 +94,7 @@  struct caam_drv_private {
 	u8 qi_present;		/* Nonzero if QI present in device */
 	u8 blob_present;	/* Nonzero if BLOB support present in device */
 	u8 mc_en;		/* Nonzero if MC f/w is active */
+	u8 optee_en;		/* Nonzero if OP-TEE f/w is active */
 	int secvio_irq;		/* Security violation interrupt number */
 	int virt_en;		/* Virtualization enabled in CAAM */
 	int era;		/* CAAM Era (internal HW revision) */