Skip to contentSkip to conversation

remoteproc: qcom: q6v5: Decouple driver from MDT loader

PATCH In review Bjorn Andersson 3 participants 9 messages Jan 30, 2017 – Feb 16, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/5 remoteproc: qcom: q6v5: Decouple driver from MDT loader

Rather than duplicating half of the MDT loader in the validation step move the entire MDT parser into the q6v5 driver. This allows us to make the shared MDT-loader call the SCM PAS operations directly which simplifies the client code and allows for better reuse of the code.

Cc Avaneesh Kumar Dwivedi <[email protected]>
---
 drivers/remoteproc/Kconfig         |   1 -
 drivers/remoteproc/qcom_q6v5_pil.c | 149 +++++++++++++++++++++++--------------
 2 files changed, 92 insertions(+), 58 deletions(-)
drivers/remoteproc/Kconfig +0 -1
index a5e888043f1f..454fd9a4dd96 100644
@@ -92,7 +92,6 @@ config QCOM_Q6V5_PIL
 	depends on QCOM_SMEM
 	depends on REMOTEPROC
 	select MFD_SYSCON
-	select QCOM_MDT_LOADER
 	select QCOM_SCM
 	help
 	  Say y here to support the Qualcomm Peripherial Image Loader for the
drivers/remoteproc/qcom_q6v5_pil.c +92 -57

202 lines not on this page · raw message

Signature · 1 line
2.11.0
#1 lore raw

PATCH 2/5 remoteproc: qcom: mdt_loader: Don't overwrite firmware object

The "fw" firmware object is passed from the remoteproc core and should not be overwritten, as that results in leaked buffers and a double free of the the last firmware object.

Fixes 051fb70fd4ea ("remoteproc: qcom: Driver for the self-authenticating Hexagon v5")
---
 drivers/remoteproc/qcom_mdt_loader.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/remoteproc/qcom_mdt_loader.c +4 -3
index 2ff18cd6c096..2393398f63ea 100644
@@ -116,6 +116,7 @@ int qcom_mdt_load(struct rproc *rproc,
 	const struct elf32_phdr *phdrs;
 	const struct elf32_phdr *phdr;
 	const struct elf32_hdr *ehdr;
+	const struct firmware *seg_fw;
 	size_t fw_name_len;
 	char *fw_name;
 	void *ptr;
@@ -154,16 +155,16 @@ int qcom_mdt_load(struct rproc *rproc,
 
 		if (phdr->p_filesz) {
 			sprintf(fw_name + fw_name_len - 3, "b%02d", i);
-			ret = request_firmware(&fw, fw_name, &rproc->dev);
+			ret = request_firmware(&seg_fw, fw_name, &rproc->dev);
 			if (ret) {
 				dev_err(&rproc->dev, "failed to load %s\n",
 					fw_name);
 				break;
 			}
 
-			memcpy(ptr, fw->data, fw->size);
+			memcpy(ptr, seg_fw->data, seg_fw->size);
 
-			release_firmware(fw);
+			release_firmware(seg_fw);
 		}
 
 		if (phdr->p_memsz > phdr->p_filesz)
Signature · 1 line
2.11.0
#2 lore raw

PATCH 3/5 remoteproc: qcom: mdt_loader: Refactor MDT loader

In reply to Bjorn Andersson

Pushing the SCM calls into the MDT loader reduces duplication in the callers and allows for non-remoteproc clients to use the helper for parsing and loading MDT files.

---
 drivers/remoteproc/qcom_adsp_pil.c   |  29 +-------
 drivers/remoteproc/qcom_mdt_loader.c | 134 +++++++++++++++++++++++------------
 drivers/remoteproc/qcom_mdt_loader.h |   6 +-
 drivers/remoteproc/qcom_q6v5_pil.c   |   4 +-
 drivers/remoteproc/qcom_wcnss.c      |  29 +-------
 5 files changed, 100 insertions(+), 102 deletions(-)
drivers/remoteproc/qcom_adsp_pil.c +2 -27
index 43a4ed2f346c..e300b8c7e5aa 100644
@@ -65,34 +65,9 @@ struct qcom_adsp {
 static int adsp_load(struct rproc *rproc, const struct firmware *fw)
 {
 	struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
-	phys_addr_t fw_addr;
-	size_t fw_size;
-	bool relocate;
-	int ret;
-
-	ret = qcom_scm_pas_init_image(ADSP_PAS_ID, fw->data, fw->size);
-	if (ret) {
-		dev_err(&rproc->dev, "invalid firmware metadata\n");
-		return ret;
-	}
-
-	ret = qcom_mdt_parse(fw, &fw_addr, &fw_size, &relocate);
-	if (ret) {
-		dev_err(&rproc->dev, "failed to parse mdt header\n");
-		return ret;
-	}
-
-	if (relocate) {
-		adsp->mem_reloc = fw_addr;
-
-		ret = qcom_scm_pas_mem_setup(ADSP_PAS_ID, adsp->mem_phys, fw_size);
-		if (ret) {
-			dev_err(&rproc->dev, "unable to setup memory for image\n");
-			return ret;
-		}
-	}
 
-	return qcom_mdt_load(rproc, fw, rproc->firmware);
+	return qcom_mdt_load(adsp->dev, fw, rproc->firmware, ADSP_PAS_ID,
+			     adsp->mem_region, adsp->mem_phys, adsp->mem_size);
 }
 
 static const struct rproc_fw_ops adsp_fw_ops = {
drivers/remoteproc/qcom_mdt_loader.c +89 -45

213 lines not on this page · raw message

drivers/remoteproc/qcom_mdt_loader.h +4 -2
index c5d7122755b6..261088c8da18 100644
@@ -6,8 +6,10 @@
 #define QCOM_MDT_RELOCATABLE	BIT(27)
 
 struct resource_table * qcom_mdt_find_rsc_table(struct rproc *rproc, const struct firmware *fw, int *tablesz);
-int qcom_mdt_load(struct rproc *rproc, const struct firmware *fw, const char *fw_name);
 
-int qcom_mdt_parse(const struct firmware *fw, phys_addr_t *fw_addr, size_t *fw_size, bool *fw_relocate);
+ssize_t qcom_mdt_get_size(const struct firmware *fw);
+int qcom_mdt_load(struct device *dev, const struct firmware *fw,
+		  const char *fw_name, int pas_id, void *mem_region,
+		  phys_addr_t mem_phys, size_t mem_size);
 
 #endif
drivers/remoteproc/qcom_q6v5_pil.c +3 -1
index db341028f2d1..1afadab4edfd 100644
@@ -496,6 +496,7 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 	const struct firmware *fw;
 	struct elf32_hdr *ehdr;
 	phys_addr_t mpss_reloc;
+	phys_addr_t boot_addr;
 	phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX;
 	phys_addr_t max_addr = 0;
 	bool relocate = false;
@@ -576,7 +577,8 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 
 		size = readl(qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
 		if (!size) {
-			writel(mpss_reloc, qproc->rmb_base + RMB_PMI_CODE_START_REG);
+			boot_addr = relocate ? qproc->mpss_phys : min_addr;
+			writel(boot_addr, qproc->rmb_base + RMB_PMI_CODE_START_REG);
 			writel(RMB_CMD_LOAD_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
 		}
 
drivers/remoteproc/qcom_wcnss.c +2 -27
index ebd61f5d18bb..aa97a61e6a69 100644
@@ -152,34 +152,9 @@ void qcom_wcnss_assign_iris(struct qcom_wcnss *wcnss,
 static int wcnss_load(struct rproc *rproc, const struct firmware *fw)
 {
 	struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
-	phys_addr_t fw_addr;
-	size_t fw_size;
-	bool relocate;
-	int ret;
-
-	ret = qcom_scm_pas_init_image(WCNSS_PAS_ID, fw->data, fw->size);
-	if (ret) {
-		dev_err(&rproc->dev, "invalid firmware metadata\n");
-		return ret;
-	}
-
-	ret = qcom_mdt_parse(fw, &fw_addr, &fw_size, &relocate);
-	if (ret) {
-		dev_err(&rproc->dev, "failed to parse mdt header\n");
-		return ret;
-	}
-
-	if (relocate) {
-		wcnss->mem_reloc = fw_addr;
-
-		ret = qcom_scm_pas_mem_setup(WCNSS_PAS_ID, wcnss->mem_phys, fw_size);
-		if (ret) {
-			dev_err(&rproc->dev, "unable to setup memory for image\n");
-			return ret;
-		}
-	}
 
-	return qcom_mdt_load(rproc, fw, rproc->firmware);
+	return qcom_mdt_load(wcnss->dev, fw, rproc->firmware, WCNSS_PAS_ID,
+			     wcnss->mem_region, wcnss->mem_phys, wcnss->mem_size);
 }
 
 static const struct rproc_fw_ops wcnss_fw_ops = {
Signature · 1 line
2.11.0
#6 lore raw

PATCH 4/5 remoteproc: qcom-common: Extract non-mdt related helper

In reply to Bjorn Andersson

In preparation for moving the mdt loader out of remoteproc let's move the somewhat unrelated resource table dummy helper to a Qualcomm "common" file.

---
 drivers/remoteproc/Kconfig           |  6 +++++
 drivers/remoteproc/Makefile          |  1 +
 drivers/remoteproc/qcom_adsp_pil.c   |  1 +
 drivers/remoteproc/qcom_common.c     | 46 ++++++++++++++++++++++++++++++++++++
 drivers/remoteproc/qcom_common.h     | 11 +++++++++
 drivers/remoteproc/qcom_mdt_loader.c | 19 ---------------
 drivers/remoteproc/qcom_mdt_loader.h |  1 -
 drivers/remoteproc/qcom_q6v5_pil.c   |  1 +
 drivers/remoteproc/qcom_wcnss.c      |  1 +
 9 files changed, 67 insertions(+), 20 deletions(-)
 create mode 100644 drivers/remoteproc/qcom_common.c
 create mode 100644 drivers/remoteproc/qcom_common.h
drivers/remoteproc/Kconfig +6 -0
index 454fd9a4dd96..71ea703190c6 100644
@@ -78,11 +78,15 @@ config QCOM_ADSP_PIL
 	depends on QCOM_SMEM
 	select MFD_SYSCON
 	select QCOM_MDT_LOADER
+	select QCOM_RPROC_COMMON
 	select QCOM_SCM
 	help
 	  Say y here to support the TrustZone based Peripherial Image Loader
 	  for the Qualcomm ADSP remote processors.
 
+config QCOM_RPROC_COMMON
+	tristate
+
 config QCOM_MDT_LOADER
 	tristate
 
@@ -92,6 +96,7 @@ config QCOM_Q6V5_PIL
 	depends on QCOM_SMEM
 	depends on REMOTEPROC
 	select MFD_SYSCON
+	select QCOM_RPROC_COMMON
 	select QCOM_SCM
 	help
 	  Say y here to support the Qualcomm Peripherial Image Loader for the
@@ -104,6 +109,7 @@ config QCOM_WCNSS_PIL
 	depends on QCOM_SMEM
 	depends on REMOTEPROC
 	select QCOM_MDT_LOADER
+	select QCOM_RPROC_COMMON
 	select QCOM_SCM
 	help
 	  Say y here to support the Peripheral Image Loader for the Qualcomm
drivers/remoteproc/Makefile +1 -0
index 0938ea3c41ba..d4f9525a226d 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_WKUP_M3_RPROC)		+= wkup_m3_rproc.o
 obj-$(CONFIG_DA8XX_REMOTEPROC)		+= da8xx_remoteproc.o
 obj-$(CONFIG_QCOM_ADSP_PIL)		+= qcom_adsp_pil.o
 obj-$(CONFIG_QCOM_MDT_LOADER)		+= qcom_mdt_loader.o
+obj-$(CONFIG_QCOM_RPROC_COMMON)		+= qcom_common.o
 obj-$(CONFIG_QCOM_Q6V5_PIL)		+= qcom_q6v5_pil.o
 obj-$(CONFIG_QCOM_WCNSS_PIL)		+= qcom_wcnss_pil.o
 qcom_wcnss_pil-y			+= qcom_wcnss.o
drivers/remoteproc/qcom_adsp_pil.c +1 -0
index e300b8c7e5aa..e2151b017616 100644
@@ -29,6 +29,7 @@
 #include <linux/soc/qcom/smem.h>
 #include <linux/soc/qcom/smem_state.h>
 
+#include "qcom_common.h"
 #include "qcom_mdt_loader.h"
 #include "remoteproc_internal.h"
 
drivers/remoteproc/qcom_common.c new file +46 -0

46 lines not on this page · raw message

drivers/remoteproc/qcom_common.h new file +11 -0
new file mode 100644
index 000000000000..caecf27c4ffa
@@ -0,0 +1,11 @@
+#ifndef __RPROC_QCOM_COMMON_H__
+#define __RPROC_QCOM_COMMON_H__
+
+struct resource_table;
+struct rproc;
+
+struct resource_table *qcom_mdt_find_rsc_table(struct rproc *rproc,
+					       const struct firmware *fw,
+					       int *tablesz);
+
+#endif
drivers/remoteproc/qcom_mdt_loader.c +0 -19
index f239f6fddbb7..b36a47bef056 100644
@@ -27,25 +27,6 @@
 #include "remoteproc_internal.h"
 #include "qcom_mdt_loader.h"
 
-/**
- * qcom_mdt_find_rsc_table() - provide dummy resource table for remoteproc
- * @rproc:	remoteproc handle
- * @fw:		firmware header
- * @tablesz:	outgoing size of the table
- *
- * Returns a dummy table.
- */
-struct resource_table *qcom_mdt_find_rsc_table(struct rproc *rproc,
-					       const struct firmware *fw,
-					       int *tablesz)
-{
-	static struct resource_table table = { .ver = 1, };
-
-	*tablesz = sizeof(table);
-	return &table;
-}
-EXPORT_SYMBOL_GPL(qcom_mdt_find_rsc_table);
-
 static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
 {
 	if (phdr->p_type != PT_LOAD)
drivers/remoteproc/qcom_mdt_loader.h +0 -1
index 261088c8da18..ca9a91709810 100644
@@ -5,7 +5,6 @@
 #define QCOM_MDT_TYPE_HASH	(2 << 24)
 #define QCOM_MDT_RELOCATABLE	BIT(27)
 
-struct resource_table * qcom_mdt_find_rsc_table(struct rproc *rproc, const struct firmware *fw, int *tablesz);
 
 ssize_t qcom_mdt_get_size(const struct firmware *fw);
 int qcom_mdt_load(struct device *dev, const struct firmware *fw,
drivers/remoteproc/qcom_q6v5_pil.c +1 -0
index 1afadab4edfd..d37f7d206b10 100644
@@ -33,6 +33,7 @@
 #include <linux/soc/qcom/smem_state.h>
 
 #include "remoteproc_internal.h"
+#include "qcom_common.h"
 #include "qcom_mdt_loader.h"
 
 #include <linux/qcom_scm.h>
drivers/remoteproc/qcom_wcnss.c +1 -0
index aa97a61e6a69..fbb25ea4ae8a 100644
@@ -32,6 +32,7 @@
 #include <linux/soc/qcom/smem_state.h>
 #include <linux/rpmsg/qcom_smd.h>
 
+#include "qcom_common.h"
 #include "qcom_mdt_loader.h"
 #include "remoteproc_internal.h"
 #include "qcom_wcnss.h"
Signature · 1 line
2.11.0
#7 lore raw

PATCH 5/5 soc/qcom: Move qcom_mdt_loader from remoteproc

In reply to Bjorn Andersson

With the remoteproc parts cleaned out of the MDT loader we can move it to drivers/soc/qcom.

Cc Andy Gross <[email protected]>
Cc Georgi Djakov <[email protected]>
Cc Jordan Crouse <[email protected]>

---

For testing purposes a temporary branch containing these patches can be found here:

https://github.com/andersson/kernel/commits/linux-next/rproc-q6v5

 drivers/remoteproc/Kconfig                                            | 3 ---
 drivers/remoteproc/Makefile                                           | 1 -
 drivers/remoteproc/qcom_adsp_pil.c                                    | 2 +-
 drivers/remoteproc/qcom_q6v5_pil.c                                    | 2 +-
 drivers/remoteproc/qcom_wcnss.c                                       | 2 +-
 drivers/soc/qcom/Kconfig                                              | 4 ++++
 drivers/soc/qcom/Makefile                                             | 1 +
 drivers/{remoteproc/qcom_mdt_loader.c => soc/qcom/mdt_loader.c}       | 4 +---
 .../qcom_mdt_loader.h => include/linux/soc/qcom/mdt_loader.h          | 4 ++++
 9 files changed, 13 insertions(+), 10 deletions(-)
 rename drivers/{remoteproc/qcom_mdt_loader.c => soc/qcom/mdt_loader.c} (98%)
 rename drivers/remoteproc/qcom_mdt_loader.h => include/linux/soc/qcom/mdt_loader.h (87%)
drivers/remoteproc/Kconfig +0 -3
index 71ea703190c6..555dba04b5ae 100644
@@ -87,9 +87,6 @@ config QCOM_ADSP_PIL
 config QCOM_RPROC_COMMON
 	tristate
 
-config QCOM_MDT_LOADER
-	tristate
-
 config QCOM_Q6V5_PIL
 	tristate "Qualcomm Hexagon V5 Peripherial Image Loader"
 	depends on OF && ARCH_QCOM
drivers/remoteproc/Makefile +0 -1
index d4f9525a226d..ffc5e430df27 100644
@@ -12,7 +12,6 @@ obj-$(CONFIG_OMAP_REMOTEPROC)		+= omap_remoteproc.o
 obj-$(CONFIG_WKUP_M3_RPROC)		+= wkup_m3_rproc.o
 obj-$(CONFIG_DA8XX_REMOTEPROC)		+= da8xx_remoteproc.o
 obj-$(CONFIG_QCOM_ADSP_PIL)		+= qcom_adsp_pil.o
-obj-$(CONFIG_QCOM_MDT_LOADER)		+= qcom_mdt_loader.o
 obj-$(CONFIG_QCOM_RPROC_COMMON)		+= qcom_common.o
 obj-$(CONFIG_QCOM_Q6V5_PIL)		+= qcom_q6v5_pil.o
 obj-$(CONFIG_QCOM_WCNSS_PIL)		+= qcom_wcnss_pil.o
drivers/remoteproc/qcom_adsp_pil.c +1 -1
index e2151b017616..ce98818d3526 100644
@@ -26,11 +26,11 @@
 #include <linux/qcom_scm.h>
 #include <linux/regulator/consumer.h>
 #include <linux/remoteproc.h>
+#include <linux/soc/qcom/mdt_loader.h>
 #include <linux/soc/qcom/smem.h>
 #include <linux/soc/qcom/smem_state.h>
 
 #include "qcom_common.h"
-#include "qcom_mdt_loader.h"
 #include "remoteproc_internal.h"
 
 #define ADSP_CRASH_REASON_SMEM		423
drivers/remoteproc/qcom_q6v5_pil.c +1 -1
index d37f7d206b10..a19e7ae5009c 100644
@@ -29,12 +29,12 @@
 #include <linux/regulator/consumer.h>
 #include <linux/remoteproc.h>
 #include <linux/reset.h>
+#include <linux/soc/qcom/mdt_loader.h>
 #include <linux/soc/qcom/smem.h>
 #include <linux/soc/qcom/smem_state.h>
 
 #include "remoteproc_internal.h"
 #include "qcom_common.h"
-#include "qcom_mdt_loader.h"
 
 #include <linux/qcom_scm.h>
 
drivers/remoteproc/qcom_wcnss.c +1 -1
index fbb25ea4ae8a..781211c144c6 100644
@@ -28,12 +28,12 @@
 #include <linux/qcom_scm.h>
 #include <linux/regulator/consumer.h>
 #include <linux/remoteproc.h>
+#include <linux/soc/qcom/mdt_loader.h>
 #include <linux/soc/qcom/smem.h>
 #include <linux/soc/qcom/smem_state.h>
 #include <linux/rpmsg/qcom_smd.h>
 
 #include "qcom_common.h"
-#include "qcom_mdt_loader.h"
 #include "remoteproc_internal.h"
 #include "qcom_wcnss.h"
 
drivers/soc/qcom/Kconfig +4 -0
index 461b387d03cc..78b1bb7bcf20 100644
@@ -10,6 +10,10 @@ config QCOM_GSBI
           functions for connecting the underlying serial UART, SPI, and I2C
           devices to the output pins.
 
+config QCOM_MDT_LOADER
+	tristate
+	select QCOM_SCM
+
 config QCOM_PM
 	bool "Qualcomm Power Management"
 	depends on ARCH_QCOM && !ARM64
drivers/soc/qcom/Makefile +1 -0
index fdd664edf0bd..1f30260b06b8 100644
@@ -1,4 +1,5 @@
 obj-$(CONFIG_QCOM_GSBI)	+=	qcom_gsbi.o
+obj-$(CONFIG_QCOM_MDT_LOADER)	+= mdt_loader.o
 obj-$(CONFIG_QCOM_PM)	+=	spm.o
 obj-$(CONFIG_QCOM_SMD) +=	smd.o
 obj-$(CONFIG_QCOM_SMD_RPM)	+= smd-rpm.o
drivers/soc/qcom/mdt_loader.c renamed from drivers/remoteproc/qcom_mdt_loader.c +1 -3
similarity index 98%
rename from drivers/remoteproc/qcom_mdt_loader.c
rename to drivers/soc/qcom/mdt_loader.c
index b36a47bef056..15cb6e089c42 100644
@@ -23,9 +23,7 @@
 #include <linux/remoteproc.h>
 #include <linux/sizes.h>
 #include <linux/slab.h>
-
-#include "remoteproc_internal.h"
-#include "qcom_mdt_loader.h"
+#include <linux/soc/qcom/mdt_loader.h>
 
 static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
 {
include/linux/soc/qcom/mdt_loader.h renamed from drivers/remoteproc/qcom_mdt_loader.h +4 -0
similarity index 87%
rename from drivers/remoteproc/qcom_mdt_loader.h
rename to include/linux/soc/qcom/mdt_loader.h
index ca9a91709810..f423001db3a9 100644
@@ -1,10 +1,14 @@
 #ifndef __QCOM_MDT_LOADER_H__
 #define __QCOM_MDT_LOADER_H__
 
+#include <linux/types.h>
+
 #define QCOM_MDT_TYPE_MASK	(7 << 24)
 #define QCOM_MDT_TYPE_HASH	(2 << 24)
 #define QCOM_MDT_RELOCATABLE	BIT(27)
 
+struct device;
+struct firmware;
 
 ssize_t qcom_mdt_get_size(const struct firmware *fw);
 int qcom_mdt_load(struct device *dev, const struct firmware *fw,
Signature · 1 line
2.11.0
Acked-by on v1 5/5
#8 lore raw

Re: [PATCH 5/5] soc/qcom: Move qcom_mdt_loader from remoteproc

Quoted 2 lines · Bjorn Andersson

With the remoteproc parts cleaned out of the MDT loader we can move it to drivers/soc/qcom.

<snip>

drivers/soc/qcom/Kconfig config QCOM_GSBI · line 15

Quoted 11 lines
drivers/soc/qcom/Kconfig +3 -0
index 461b387d03cc..78b1bb7bcf20 100644
@@ -10,6 +10,10 @@ config QCOM_GSBI
           functions for connecting the underlying serial UART, SPI, and I2C
           devices to the output pins.
 
+config QCOM_MDT_LOADER
+	tristate
+	select QCOM_SCM

Do we need a help description here?

drivers/soc/qcom/Kconfig config QCOM_GSBI · line 19

Quoted 4 lines
drivers/soc/qcom/Kconfig +1 -0
+
 config QCOM_PM
 	bool "Qualcomm Power Management"
 	depends on ARCH_QCOM && !ARM64

Add the description on the next set and add my ack as well.

#3 lore raw

Re: [PATCH 3/5] remoteproc: qcom: mdt_loader: Refactor MDT loader

In reply to Bjorn Andersson

Hi Bjorn,

Thanks for the patch!

Quoted 14 lines · Bjorn Andersson

Pushing the SCM calls into the MDT loader reduces duplication in the callers and allows for non-remoteproc clients to use the helper for parsing and loading MDT files.

---
 drivers/remoteproc/qcom_adsp_pil.c   |  29 +-------
 drivers/remoteproc/qcom_mdt_loader.c | 134 +++++++++++++++++++++++------------
 drivers/remoteproc/qcom_mdt_loader.h |   6 +-
 drivers/remoteproc/qcom_q6v5_pil.c   |   4 +-
 drivers/remoteproc/qcom_wcnss.c      |  29 +-------
 5 files changed, 100 insertions(+), 102 deletions(-)

<snip>

drivers/remoteproc/qcom_mdt_loader.c line 23

Quoted 10 lines
drivers/remoteproc/qcom_mdt_loader.c +1 -0
index 2393398f63ea..f239f6fddbb7 100644
@@ -19,6 +19,7 @@
 #include <linux/firmware.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/qcom_scm.h>
 #include <linux/remoteproc.h>

Please delete remoteproc.h it is not needed anymore.

Quoted 20 lines
drivers/remoteproc/qcom_mdt_loader.c +14 -0
 #include <linux/sizes.h>
 #include <linux/slab.h>
@@ -45,24 +46,33 @@ struct resource_table *qcom_mdt_find_rsc_table(struct rproc *rproc,
 }
 EXPORT_SYMBOL_GPL(qcom_mdt_find_rsc_table);
 
+static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
+{
+	if (phdr->p_type != PT_LOAD)
+		return false;
+
+	if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
+		return false;
+
+	if (!phdr->p_memsz)
+		return false;
+
+	return true;
+}
+

<snip>

Signature · 2 lines
regards,
Stan
#4 lore raw

Re: [PATCH 3/5] remoteproc: qcom: mdt_loader: Refactor MDT loader

In reply to Bjorn Andersson

Hi Bjorn,

Quoted 109 lines · Bjorn Andersson

Pushing the SCM calls into the MDT loader reduces duplication in the callers and allows for non-remoteproc clients to use the helper for parsing and loading MDT files.

---
 drivers/remoteproc/qcom_adsp_pil.c   |  29 +-------
 drivers/remoteproc/qcom_mdt_loader.c | 134 +++++++++++++++++++++++------------
 drivers/remoteproc/qcom_mdt_loader.h |   6 +-
 drivers/remoteproc/qcom_q6v5_pil.c   |   4 +-
 drivers/remoteproc/qcom_wcnss.c      |  29 +-------
 5 files changed, 100 insertions(+), 102 deletions(-)

/**
- * qcom_mdt_load() - load the firmware which header is defined in fw
- * @rproc: rproc handle
- * @fw: frimware object for the header
- * @firmware: filename of the firmware, for building .bXX names
+ * qcom_mdt_load() - load the firmware which header is loaded as fw
+ * @dev: device handle to associate resources with
+ * @fw: firmware object for the mdt file
+ * @firmware: name of the firmware, for construction of segment file names
+ * @pas_id: PAS identifier
+ * @mem_region: allocated memory region to load firmware into
+ * @mem_phys: physical address of allocated memory region
+ * @mem_size: size of the allocated memory region
*
* Returns 0 on success, negative errno otherwise.
*/

drivers/remoteproc/qcom_mdt_loader.c +47 -7
-int qcom_mdt_load(struct rproc *rproc,
-		  const struct firmware *fw,
-		  const char *firmware)
+int qcom_mdt_load(struct device *dev, const struct firmware *fw,
+		  const char *firmware, int pas_id, void *mem_region,
+		  phys_addr_t mem_phys, size_t mem_size)
 {
 	const struct elf32_phdr *phdrs;
 	const struct elf32_phdr *phdr;
 	const struct elf32_hdr *ehdr;
 	const struct firmware *seg_fw;
+	phys_addr_t mem_reloc;
+	phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX;
+	phys_addr_t max_addr = 0;
 	size_t fw_name_len;
+	size_t offset;
 	char *fw_name;
+	bool relocate = false;
 	void *ptr;
 	int ret;
 	int i;
 
+	if (!fw || !mem_region || !mem_phys || !mem_size)
+		return -EINVAL;
+
 	ehdr = (struct elf32_hdr *)fw->data;
 	phdrs = (struct elf32_phdr *)(ehdr + 1);
@@ -134,31 +140,68 @@ int qcom_mdt_load(struct rproc *rproc,
 	if (!fw_name)
 		return -ENOMEM;
 
+	ret = qcom_scm_pas_init_image(pas_id, fw->data, fw->size);
+	if (ret) {
+		dev_err(dev, "invalid firmware metadata\n");
+		goto out;
+	}
+
 	for (i = 0; i < ehdr->e_phnum; i++) {
 		phdr = &phdrs[i];
 
-		if (phdr->p_type != PT_LOAD)
+		if (!mdt_phdr_valid(phdr))
 			continue;
 
-		if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
-			continue;
+		if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
+			relocate = true;
+
+		if (phdr->p_paddr < min_addr)
+			min_addr = phdr->p_paddr;
+
+		if (phdr->p_paddr + phdr->p_memsz > max_addr)
+			max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
+	}
+
+	if (relocate) {
+		ret = qcom_scm_pas_mem_setup(pas_id, mem_phys, max_addr - min_addr);
+		if (ret) {
+			dev_err(dev, "unable to setup relocation\n");
+			goto out;
+		}
+
+		/*
+		 * The image is relocatable, so offset each segment based on
+		 * the lowest segment address.
+		 */
+		mem_reloc = min_addr;
+	} else {
+		/*
+		 * Image is not relocatable, so offset each segment based on
+		 * the allocated physical chunk of memory.
+		 */
+		mem_reloc = mem_phys;
+	}
 
-		if (!phdr->p_memsz)

drivers/remoteproc/qcom_mdt_loader.c int qcom_mdt_load(struct rproc *rproc, · line 191

Quoted 10 lines
drivers/remoteproc/qcom_mdt_loader.c +5 -3
+	for (i = 0; i < ehdr->e_phnum; i++) {
+		phdr = &phdrs[i];
+
+		if (!mdt_phdr_valid(phdr))
 			continue;
 
-		ptr = rproc_da_to_va(rproc, phdr->p_paddr, phdr->p_memsz);
-		if (!ptr) {
-			dev_err(&rproc->dev, "segment outside memory range\n");
+		offset = phdr->p_paddr - mem_reloc;

Shouldn't 'offset' variable be of signed type i.e. ssize_t? Also p_paddr is of type Elf32_Addr and mem_reloc is of type phys_addr_t which on 64bit systems is 64bit long, I think it should be better to make mem_reloc of the same type as p_paddr.

Quoted 19 lines
drivers/remoteproc/qcom_mdt_loader.c +6 -3
+		if (offset < 0 || offset + phdr->p_memsz > mem_size) {
+			dev_err(dev, "segment outside memory range\n");
 			ret = -EINVAL;
 			break;
 		}
 
+		ptr = mem_region + offset;
+
 		if (phdr->p_filesz) {
 			sprintf(fw_name + fw_name_len - 3, "b%02d", i);
-			ret = request_firmware(&seg_fw, fw_name, &rproc->dev);
+			ret = request_firmware(&seg_fw, fw_name, dev);
 			if (ret) {
-				dev_err(&rproc->dev, "failed to load %s\n",
-					fw_name);
+				dev_err(dev, "failed to load %s\n", fw_name);
 				break;
 			}
 
Signature · 2 lines
regards,
Stan
#5 lore raw

Re: [PATCH 3/5] remoteproc: qcom: mdt_loader: Refactor MDT loader

Quoted 3 lines · Stanimir Varbanov

Hi Bjorn,

On 01/30/2017 06:55 PM, Bjorn Andersson wrote:

[..]

Quoted 1 line
Quoted 1 line

+int qcom_mdt_load(struct device *dev, const struct firmware *fw,

[..]

Quoted 1 line
Quoted 1 line
drivers/remoteproc/qcom_mdt_loader.c +1 -0
+	size_t offset;

[..]

Quoted 6 lines
Quoted 6 lines
drivers/remoteproc/qcom_mdt_loader.c +4 -0
+	for (i = 0; i < ehdr->e_phnum; i++) {
+		phdr = &phdrs[i];
+
+		if (!mdt_phdr_valid(phdr))
 			continue;
Quoted 6 lines

drivers/remoteproc/qcom_mdt_loader.c int qcom_mdt_load(struct rproc *rproc, · line 191

Quoted 4 lines
drivers/remoteproc/qcom_mdt_loader.c +1 -3
-		ptr = rproc_da_to_va(rproc, phdr->p_paddr, phdr->p_memsz);
-		if (!ptr) {
-			dev_err(&rproc->dev, "segment outside memory range\n");
+		offset = phdr->p_paddr - mem_reloc;

Shouldn't 'offset' variable be of signed type i.e. ssize_t?

It should, as a small "negative" value will wrap back into the acceptable range of the second part of the check below. I got another report of this as well yesterday, so I will prepare a patch for this.

Quoted 4 lines

Also p_paddr is of type Elf32_Addr and mem_reloc is of type phys_addr_t which on 64bit systems is 64bit long, I think it should be better to make mem_reloc of the same type as p_paddr.

If I remember what the C standard says, mem_reloc would in the 64-bit case have higher "rank" and the type of p_paddr would therefor be converted to the type of mem_reloc (i.e. u64) and the result would be stored in a 64 bit type. But it's been a while and I can't find my C reference manual right now...

Casting mem_reloc to the type of p_paddr would cause invalid results in the event that the system is configured to actually load a relocatable blob into memory above 4GB.

Quoted 6 lines
Quoted 6 lines
drivers/remoteproc/qcom_mdt_loader.c +2 -0
+		if (offset < 0 || offset + phdr->p_memsz > mem_size) {
+			dev_err(dev, "segment outside memory range\n");
 			ret = -EINVAL;
 			break;
 		}
Quoted 14 lines

drivers/remoteproc/qcom_mdt_loader.c int qcom_mdt_load(struct rproc *rproc, · line 206

Quoted 13 lines
drivers/remoteproc/qcom_mdt_loader.c +4 -3
+		ptr = mem_region + offset;
+
 		if (phdr->p_filesz) {
 			sprintf(fw_name + fw_name_len - 3, "b%02d", i);
-			ret = request_firmware(&seg_fw, fw_name, &rproc->dev);
+			ret = request_firmware(&seg_fw, fw_name, dev);
 			if (ret) {
-				dev_err(&rproc->dev, "failed to load %s\n",
-					fw_name);
+				dev_err(dev, "failed to load %s\n", fw_name);
 				break;
 			}

Regards,
Bjorn