[PATCH 2/6] remoteproc: qcom: Add iommu map_unmap helper function

Mukesh Ojha posted 6 patches 1 month, 3 weeks ago
[PATCH 2/6] remoteproc: qcom: Add iommu map_unmap helper function
Posted by Mukesh Ojha 1 month, 3 weeks ago
From: Komal Bajaj <quic_kbajaj@quicinc.com>

Qualcomm remote processor's IOMMU translation running on Linux KVM host
should be managed by PAS driver and to do this PAS driver need to do map
and unmap remoteproc carveout memory region. Similar map and unmap
private functions for the similar purpose are already available in
qcom_q6v5_adsp.c. So, in motivation to reuse code, introduce common
exported functions like qcom_map_unmap_carveout() such that it can be
used by both qcom_q6v5_adsp and qcom_q6v5_pas.

Signed-off-by: Komal Bajaj <quic_kbajaj@quicinc.com>
Co-Developed-by: Mukesh Ojha <quic_mojha@quicinc.com>
Signed-off-by: Mukesh Ojha <quic_mojha@quicinc.com>
---
 drivers/remoteproc/qcom_common.c | 52 ++++++++++++++++++++++++++++++++
 drivers/remoteproc/qcom_common.h |  3 ++
 2 files changed, 55 insertions(+)

diff --git a/drivers/remoteproc/qcom_common.c b/drivers/remoteproc/qcom_common.c
index 8c8688f99f0a..1c7887dc65b4 100644
--- a/drivers/remoteproc/qcom_common.c
+++ b/drivers/remoteproc/qcom_common.c
@@ -8,6 +8,7 @@
  */
 
 #include <linux/firmware.h>
+#include <linux/iommu.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/notifier.h>
@@ -35,6 +36,8 @@
 #define MINIDUMP_SS_ENCR_DONE		('D' << 24 | 'O' << 16 | 'N' << 8 | 'E' << 0)
 #define MINIDUMP_SS_ENABLED		('E' << 24 | 'N' << 16 | 'B' << 8 | 'L' << 0)
 
+#define SID_MASK_DEFAULT	0xfUL
+
 /**
  * struct minidump_region - Minidump region
  * @name		: Name of the region to be dumped
@@ -606,5 +609,54 @@ void qcom_remove_pdm_subdev(struct rproc *rproc, struct qcom_rproc_pdm *pdm)
 }
 EXPORT_SYMBOL_GPL(qcom_remove_pdm_subdev);
 
+/**
+ * qcom_map_unmap_carveout() - iommu map and unmap carveout region
+ *
+ * @rproc:	rproc handle
+ * @mem_phys:	starting physical address of carveout region
+ * @mem_size:	size of carveout region
+ * @map:	if true, map otherwise, unmap
+ * @use_sid:	decision to append sid to iova
+ * @sid:	SID value
+ */
+int qcom_map_unmap_carveout(struct rproc *rproc, phys_addr_t mem_phys, size_t mem_size,
+			    bool map, bool use_sid, unsigned long sid)
+{
+	unsigned long iova = mem_phys;
+	unsigned long sid_def_val;
+	int ret;
+
+	if (!rproc->has_iommu)
+		return 0;
+
+	if (!rproc->domain)
+		return -EINVAL;
+
+	/*
+	 * Remote processor like ADSP supports upto 36 bit device
+	 * address space and some of its clients like fastrpc uses
+	 * upper 32-35 bits to keep lower 4 bits of its SID to use
+	 * larger address space. To keep this consistent across other
+	 * use cases add remoteproc SID configuration for firmware
+	 * to IOMMU for carveouts.
+	 */
+	if (use_sid && sid) {
+		sid_def_val = sid & SID_MASK_DEFAULT;
+		iova |= (sid_def_val << 32);
+	}
+
+	if (map)
+		ret = iommu_map(rproc->domain, iova, mem_phys, mem_size, IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);
+	else
+		ret = iommu_unmap(rproc->domain, iova, mem_size);
+
+	if (ret)
+		dev_err(&rproc->dev, "Unable to %s IOVA Memory, ret: %d\n",
+			map ? "map" : "unmap", ret);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(qcom_map_unmap_carveout);
+
 MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
 MODULE_LICENSE("GPL v2");
diff --git a/drivers/remoteproc/qcom_common.h b/drivers/remoteproc/qcom_common.h
index b07fbaa091a0..bbc41054e1ea 100644
--- a/drivers/remoteproc/qcom_common.h
+++ b/drivers/remoteproc/qcom_common.h
@@ -59,6 +59,9 @@ void qcom_add_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr,
 			 const char *ssr_name);
 void qcom_remove_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr);
 
+int qcom_map_unmap_carveout(struct rproc *rproc, phys_addr_t mem_phys, size_t mem_size,
+			    bool map, bool use_sid, unsigned long sid);
+
 void qcom_add_pdm_subdev(struct rproc *rproc, struct qcom_rproc_pdm *pdm);
 void qcom_remove_pdm_subdev(struct rproc *rproc, struct qcom_rproc_pdm *pdm);
 
-- 
2.34.1
Re: [PATCH 2/6] remoteproc: qcom: Add iommu map_unmap helper function
Posted by kernel test robot 1 month, 3 weeks ago
Hi Mukesh,

kernel test robot noticed the following build warnings:

[auto build test WARNING on remoteproc/rproc-next]
[also build test WARNING on robh/for-next linus/master v6.12-rc1 next-20241004]
[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/Mukesh-Ojha/dt-bindings-remoteproc-qcom-pas-common-Introduce-iommus-and-qcom-devmem-property/20241005-052733
base:   https://git.kernel.org/pub/scm/linux/kernel/git/remoteproc/linux.git rproc-next
patch link:    https://lore.kernel.org/r/20241004212359.2263502-3-quic_mojha%40quicinc.com
patch subject: [PATCH 2/6] remoteproc: qcom: Add iommu map_unmap helper function
config: arm-allmodconfig (https://download.01.org/0day-ci/archive/20241006/202410061256.KV3EbD7H-lkp@intel.com/config)
compiler: arm-linux-gnueabi-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241006/202410061256.KV3EbD7H-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410061256.KV3EbD7H-lkp@intel.com/

All warnings (new ones prefixed by >>):

   drivers/remoteproc/qcom_common.c: In function 'qcom_map_unmap_carveout':
>> drivers/remoteproc/qcom_common.c:645:38: warning: left shift count >= width of type [-Wshift-count-overflow]
     645 |                 iova |= (sid_def_val << 32);
         |                                      ^~

Kconfig warnings: (for reference only)
   WARNING: unmet direct dependencies detected for GET_FREE_REGION
   Depends on [n]: SPARSEMEM [=n]
   Selected by [m]:
   - RESOURCE_KUNIT_TEST [=m] && RUNTIME_TESTING_MENU [=y] && KUNIT [=m]


vim +645 drivers/remoteproc/qcom_common.c

   611	
   612	/**
   613	 * qcom_map_unmap_carveout() - iommu map and unmap carveout region
   614	 *
   615	 * @rproc:	rproc handle
   616	 * @mem_phys:	starting physical address of carveout region
   617	 * @mem_size:	size of carveout region
   618	 * @map:	if true, map otherwise, unmap
   619	 * @use_sid:	decision to append sid to iova
   620	 * @sid:	SID value
   621	 */
   622	int qcom_map_unmap_carveout(struct rproc *rproc, phys_addr_t mem_phys, size_t mem_size,
   623				    bool map, bool use_sid, unsigned long sid)
   624	{
   625		unsigned long iova = mem_phys;
   626		unsigned long sid_def_val;
   627		int ret;
   628	
   629		if (!rproc->has_iommu)
   630			return 0;
   631	
   632		if (!rproc->domain)
   633			return -EINVAL;
   634	
   635		/*
   636		 * Remote processor like ADSP supports upto 36 bit device
   637		 * address space and some of its clients like fastrpc uses
   638		 * upper 32-35 bits to keep lower 4 bits of its SID to use
   639		 * larger address space. To keep this consistent across other
   640		 * use cases add remoteproc SID configuration for firmware
   641		 * to IOMMU for carveouts.
   642		 */
   643		if (use_sid && sid) {
   644			sid_def_val = sid & SID_MASK_DEFAULT;
 > 645			iova |= (sid_def_val << 32);
   646		}
   647	
   648		if (map)
   649			ret = iommu_map(rproc->domain, iova, mem_phys, mem_size, IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);
   650		else
   651			ret = iommu_unmap(rproc->domain, iova, mem_size);
   652	
   653		if (ret)
   654			dev_err(&rproc->dev, "Unable to %s IOVA Memory, ret: %d\n",
   655				map ? "map" : "unmap", ret);
   656	
   657		return ret;
   658	}
   659	EXPORT_SYMBOL_GPL(qcom_map_unmap_carveout);
   660	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 2/6] remoteproc: qcom: Add iommu map_unmap helper function
Posted by kernel test robot 1 month, 3 weeks ago
Hi Mukesh,

kernel test robot noticed the following build warnings:

[auto build test WARNING on remoteproc/rproc-next]
[also build test WARNING on robh/for-next linus/master v6.12-rc1 next-20241004]
[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/Mukesh-Ojha/dt-bindings-remoteproc-qcom-pas-common-Introduce-iommus-and-qcom-devmem-property/20241005-052733
base:   https://git.kernel.org/pub/scm/linux/kernel/git/remoteproc/linux.git rproc-next
patch link:    https://lore.kernel.org/r/20241004212359.2263502-3-quic_mojha%40quicinc.com
patch subject: [PATCH 2/6] remoteproc: qcom: Add iommu map_unmap helper function
config: arm-defconfig (https://download.01.org/0day-ci/archive/20241006/202410060943.UAdM2uKn-lkp@intel.com/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241006/202410060943.UAdM2uKn-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410060943.UAdM2uKn-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/remoteproc/qcom_common.c:645:24: warning: shift count >= width of type [-Wshift-count-overflow]
                   iova |= (sid_def_val << 32);
                                        ^  ~~
   1 warning generated.


vim +645 drivers/remoteproc/qcom_common.c

   611	
   612	/**
   613	 * qcom_map_unmap_carveout() - iommu map and unmap carveout region
   614	 *
   615	 * @rproc:	rproc handle
   616	 * @mem_phys:	starting physical address of carveout region
   617	 * @mem_size:	size of carveout region
   618	 * @map:	if true, map otherwise, unmap
   619	 * @use_sid:	decision to append sid to iova
   620	 * @sid:	SID value
   621	 */
   622	int qcom_map_unmap_carveout(struct rproc *rproc, phys_addr_t mem_phys, size_t mem_size,
   623				    bool map, bool use_sid, unsigned long sid)
   624	{
   625		unsigned long iova = mem_phys;
   626		unsigned long sid_def_val;
   627		int ret;
   628	
   629		if (!rproc->has_iommu)
   630			return 0;
   631	
   632		if (!rproc->domain)
   633			return -EINVAL;
   634	
   635		/*
   636		 * Remote processor like ADSP supports upto 36 bit device
   637		 * address space and some of its clients like fastrpc uses
   638		 * upper 32-35 bits to keep lower 4 bits of its SID to use
   639		 * larger address space. To keep this consistent across other
   640		 * use cases add remoteproc SID configuration for firmware
   641		 * to IOMMU for carveouts.
   642		 */
   643		if (use_sid && sid) {
   644			sid_def_val = sid & SID_MASK_DEFAULT;
 > 645			iova |= (sid_def_val << 32);
   646		}
   647	
   648		if (map)
   649			ret = iommu_map(rproc->domain, iova, mem_phys, mem_size, IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);
   650		else
   651			ret = iommu_unmap(rproc->domain, iova, mem_size);
   652	
   653		if (ret)
   654			dev_err(&rproc->dev, "Unable to %s IOVA Memory, ret: %d\n",
   655				map ? "map" : "unmap", ret);
   656	
   657		return ret;
   658	}
   659	EXPORT_SYMBOL_GPL(qcom_map_unmap_carveout);
   660	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki