Skip to contentSkip to conversation

remoteproc: qcom: pas: Misc fixes

PATCH Applied Mukesh Ojha 2 participants 12 messages Jul 24 – Aug 4

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

v5 cover remoteproc: qcom: pas: Misc fixes

This series is a collection of misc fixes for the Qualcomm PAS remoteproc driver and its supporting SCM/MDT loader infrastructure.

- Fix sparse __iomem warnings in qcom_adsp, qcom_pas and qcom_wcnss by annotating mem_region fields with __iomem and using __force at call sites.
- Guard the DTB metadata release in qcom_pas_load() with a dtb_pas_id check, consistent with all other release sites in the driver.
- Move PAS context allocation from probe into qcom_pas_alloc_memory_region() so the DTB context is only created for subsystems that actually use it.
- Map/unmap the subsystem memory region on demand around firmware load and coredump, avoiding potential XPU violations on EL2 platforms where the region may be handed off to the remote side.
- Drop the now-unused dtb_mem_region field from struct qcom_pas.
- Add a keep_mdt_buf flag to struct qcom_scm_pas_context to make metadata buffer retention explicit rather than implicit.

Depends on:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

Changes in v5:
link to v4(https://lore.kernel.org/lkml/[email protected]/)
- Rebased it latest changes on Generic PAS changes.
- Changed a bit related to map API so that it can be used by both
by mdt_loader and pas.( 4/6 )

Changes in v4:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

- Last series mistakenly divided two series cause laptop shutdown.
- Some minor refactor common code rest is same a rebased on latest kernel.
- Added new 6/6 to the series.

Mukesh Ojha (6):
remoteproc: qcom: annotate mem_region fields with __iomem
remoteproc: qcom: pas: Guard dtb metadata release with dtb_pas_id
check
remoteproc: qcom: pas: Fix the PAS context creation placement
remoteproc: qcom: pas: Map/unmap subsystem region before
auth_and_reset
remoteproc: qcom: pas: Drop unused dtb_mem_region field
firmware: qcom: scm: introduce keep_mdt_buf flag in PAS context

 drivers/firmware/qcom/qcom_scm.c       | 18 +++++-
 drivers/remoteproc/qcom_q6v5_adsp.c    |  6 +-
 drivers/remoteproc/qcom_q6v5_pas.c     | 87 ++++++++++++++------------
 drivers/remoteproc/qcom_wcnss.c        |  6 +-
 drivers/soc/qcom/mdt_loader.c          | 15 +++--
 include/linux/firmware/qcom/qcom_pas.h | 13 ++++
 include/linux/soc/qcom/mdt_loader.h    |  4 +-
 7 files changed, 94 insertions(+), 55 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v5 1/6 remoteproc: qcom: annotate mem_region fields with __iomem

The firmware memory regions in qcom_adsp, qcom_pas and qcom_wcnss are mapped using devm_ioremap_wc() / devm_ioremap_resource_wc(), which return void __iomem *. However, the mem_region (and dtb_mem_region) fields in the respective driver structs were declared as plain void *, causing sparse to flag address space mismatches:

qcom_q6v5_adsp.c:639:26: warning: incorrect type in assignment (different address spaces)
qcom_q6v5_adsp.c:639:26: expected void *mem_region
qcom_q6v5_adsp.c:639:26: got void [noderef] __iomem *
qcom_q6v5_pas.c:141:45: warning: incorrect type in argument 2 (different address spaces)
qcom_q6v5_pas.c:141:45: expected void const volatile [noderef] __iomem *src
qcom_q6v5_pas.c:141:45: got void *
qcom_q6v5_pas.c:637:25: warning: incorrect type in assignment (different address spaces)
qcom_q6v5_pas.c:637:25: expected void *mem_region
qcom_q6v5_pas.c:637:25: got void [noderef] __iomem *
qcom_q6v5_pas.c:654:29: warning: incorrect type in assignment (different address spaces)
qcom_q6v5_pas.c:654:29: expected void *dtb_mem_region
qcom_q6v5_pas.c:654:29: got void [noderef] __iomem *
qcom_wcnss.c:540:27: warning: incorrect type in assignment (different address spaces)
qcom_wcnss.c:540:27: expected void *mem_region
qcom_wcnss.c:540:27: got void [noderef] __iomem *

Fix this by annotating the struct fields with __iomem to correctly reflect the address space of the underlying mapping.

These regions are subsequently passed to qcom_mdt_load(), qcom_mdt_load_no_init() and qcom_mdt_pas_load(), all of which take void * and use plain memcpy()/memset() internally to write firmware segments into the region. This is intentional and safe: the mappings are write-combining (WC), which on arm64 permits bulk CPU stores without requiring the memcpy_toio()/memset_io() accessors. Changing the MDT loader API to accept void __iomem * would be a more invasive change and would affect callers.

---
 drivers/remoteproc/qcom_q6v5_adsp.c |  6 +++---
 drivers/remoteproc/qcom_q6v5_pas.c  | 10 +++++-----
 drivers/remoteproc/qcom_wcnss.c     |  6 +++---
 3 files changed, 11 insertions(+), 11 deletions(-)
drivers/remoteproc/qcom_q6v5_adsp.c +3 -3
index c81e6c33c747..39654206781d 100644
@@ -105,7 +105,7 @@ struct qcom_adsp {
 
 	phys_addr_t mem_phys;
 	phys_addr_t mem_reloc;
-	void *mem_region;
+	void __iomem *mem_region;
 	size_t mem_size;
 	bool has_iommu;
 
@@ -318,7 +318,7 @@ static int adsp_load(struct rproc *rproc, const struct firmware *fw)
 	int ret;
 
 	ret = qcom_mdt_load_no_init(adsp->dev, fw, rproc->firmware,
-				    adsp->mem_region, adsp->mem_phys,
+				    (__force void *)adsp->mem_region, adsp->mem_phys,
 				    adsp->mem_size, &adsp->mem_reloc);
 	if (ret)
 		return ret;
@@ -492,7 +492,7 @@ static void *adsp_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iom
 	if (offset < 0 || offset + len > adsp->mem_size)
 		return NULL;
 
-	return adsp->mem_region + offset;
+	return (__force void *)adsp->mem_region + offset;
 }
 
 static int adsp_parse_firmware(struct rproc *rproc, const struct firmware *fw)
drivers/remoteproc/qcom_q6v5_pas.c +5 -5
index 25599d728208..9076c82ac878 100644
@@ -101,8 +101,8 @@ struct qcom_pas {
 	phys_addr_t mem_reloc;
 	phys_addr_t dtb_mem_reloc;
 	phys_addr_t region_assign_phys[MAX_ASSIGN_COUNT];
-	void *mem_region;
-	void *dtb_mem_region;
+	void __iomem *mem_region;
+	void __iomem *dtb_mem_region;
 	size_t mem_size;
 	size_t dtb_mem_size;
 	size_t region_assign_size[MAX_ASSIGN_COUNT];
@@ -243,7 +243,7 @@ static int qcom_pas_load(struct rproc *rproc, const struct firmware *fw)
 		}
 
 		ret = qcom_mdt_pas_load(pas->dtb_pas_ctx, pas->dtb_firmware,
-					pas->dtb_firmware_name, pas->dtb_mem_region,
+					pas->dtb_firmware_name, (__force void *)pas->dtb_mem_region,
 					&pas->dtb_mem_reloc);
 		if (ret)
 			goto release_dtb_metadata;
@@ -321,7 +321,7 @@ static int qcom_pas_start(struct rproc *rproc)
 	}
 
 	ret = qcom_mdt_pas_load(pas->pas_ctx, pas->firmware, rproc->firmware,
-				pas->mem_region, &pas->mem_reloc);
+				(__force void *)pas->mem_region, &pas->mem_reloc);
 	if (ret)
 		goto release_pas_metadata;
 
@@ -447,7 +447,7 @@ static void *qcom_pas_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is
 	if (is_iomem)
 		*is_iomem = true;
 
-	return pas->mem_region + offset;
+	return (__force void *)pas->mem_region + offset;
 }
 
 static int qcom_pas_parse_firmware(struct rproc *rproc, const struct firmware *fw)
drivers/remoteproc/qcom_wcnss.c +3 -3
index e9a00efe97f6..c856a92af43c 100644
@@ -94,7 +94,7 @@ struct qcom_wcnss {
 
 	phys_addr_t mem_phys;
 	phys_addr_t mem_reloc;
-	void *mem_region;
+	void __iomem *mem_region;
 	size_t mem_size;
 
 	struct qcom_rproc_subdev smd_subdev;
@@ -158,7 +158,7 @@ static int wcnss_load(struct rproc *rproc, const struct firmware *fw)
 	int ret;
 
 	ret = qcom_mdt_load(wcnss->dev, fw, rproc->firmware, WCNSS_PAS_ID,
-			    wcnss->mem_region, wcnss->mem_phys,
+			    (__force void *)wcnss->mem_region, wcnss->mem_phys,
 			    wcnss->mem_size, &wcnss->mem_reloc);
 	if (ret)
 		return ret;
@@ -327,7 +327,7 @@ static void *wcnss_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_io
 	if (offset < 0 || offset + len > wcnss->mem_size)
 		return NULL;
 
-	return wcnss->mem_region + offset;
+	return (__force void *)wcnss->mem_region + offset;
 }
 
 static const struct rproc_ops wcnss_ops = {
Signature · 1 line
2.53.0
#2 lore raw

PATCH v5 2/6 remoteproc: qcom: pas: Guard dtb metadata release with dtb_pas_id check

In reply to Mukesh Ojha

All other call sites of qcom_scm_pas_metadata_release() for the DTB context are guarded by a check on pas->dtb_pas_id, but the call inside qcom_pas_load() was not. Fix this by moving the call to the guarded block.

Fixes 29814986b82e ("remoteproc: qcom_q6v5_pas: add support for dtb co-firmware loading")
---
 drivers/remoteproc/qcom_q6v5_pas.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/remoteproc/qcom_q6v5_pas.c +5 -8
index 9076c82ac878..f70836cc382d 100644
@@ -245,17 +245,14 @@ static int qcom_pas_load(struct rproc *rproc, const struct firmware *fw)
 		ret = qcom_mdt_pas_load(pas->dtb_pas_ctx, pas->dtb_firmware,
 					pas->dtb_firmware_name, (__force void *)pas->dtb_mem_region,
 					&pas->dtb_mem_reloc);
-		if (ret)
-			goto release_dtb_metadata;
+		if (ret) {
+			qcom_pas_metadata_release(pas->dtb_pas_ctx);
+			release_firmware(pas->dtb_firmware);
+			return ret;
+		}
 	}
 
 	return 0;
-
-release_dtb_metadata:
-	qcom_pas_metadata_release(pas->dtb_pas_ctx);
-	release_firmware(pas->dtb_firmware);
-
-	return ret;
 }
 
 static void qcom_pas_unmap_carveout(struct rproc *rproc, phys_addr_t mem_phys, size_t size)
Signature · 1 line
2.53.0
#3 lore raw

PATCH v5 3/6 remoteproc: qcom: pas: Fix the PAS context creation placement

In reply to Mukesh Ojha

DTB PAS context creation should be done only for subsystems that support a DTB firmware binary; otherwise, memory is wasted. Move the context creation to the appropriate location.

---
 drivers/remoteproc/qcom_q6v5_pas.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/remoteproc/qcom_q6v5_pas.c +13 -16
index f70836cc382d..997b3a7ce672 100644
@@ -702,6 +702,11 @@ static int qcom_pas_alloc_memory_region(struct qcom_pas *pas)
 		return PTR_ERR(pas->mem_region);
 	}
 
+	pas->pas_ctx = devm_qcom_pas_context_alloc(pas->dev, pas->pas_id,
+						   pas->mem_phys, pas->mem_size);
+	if (IS_ERR(pas->pas_ctx))
+		return PTR_ERR(pas->pas_ctx);
+
 	if (!pas->dtb_pas_id)
 		return 0;
 
@@ -719,6 +724,12 @@ static int qcom_pas_alloc_memory_region(struct qcom_pas *pas)
 		return PTR_ERR(pas->dtb_mem_region);
 	}
 
+	pas->dtb_pas_ctx = devm_qcom_pas_context_alloc(pas->dev, pas->dtb_pas_id,
+						       pas->dtb_mem_phys,
+						       pas->dtb_mem_size);
+	if (IS_ERR(pas->dtb_pas_ctx))
+		return PTR_ERR(pas->dtb_pas_ctx);
+
 	return 0;
 }
 
@@ -901,23 +912,9 @@ static int qcom_pas_probe(struct platform_device *pdev)
 
 	qcom_add_ssr_subdev(rproc, &pas->ssr_subdev, desc->ssr_name);
 
-	pas->pas_ctx = devm_qcom_pas_context_alloc(pas->dev, pas->pas_id,
-						   pas->mem_phys, pas->mem_size);
-	if (IS_ERR(pas->pas_ctx)) {
-		ret = PTR_ERR(pas->pas_ctx);
-		goto remove_ssr_sysmon;
-	}
-
-	pas->dtb_pas_ctx = devm_qcom_pas_context_alloc(pas->dev, pas->dtb_pas_id,
-						       pas->dtb_mem_phys,
-						       pas->dtb_mem_size);
-	if (IS_ERR(pas->dtb_pas_ctx)) {
-		ret = PTR_ERR(pas->dtb_pas_ctx);
-		goto remove_ssr_sysmon;
-	}
-
 	pas->pas_ctx->use_tzmem = desc->needs_tzmem || rproc->has_iommu;
-	pas->dtb_pas_ctx->use_tzmem = desc->needs_tzmem || rproc->has_iommu;
+	if (pas->dtb_pas_id)
+		pas->dtb_pas_ctx->use_tzmem = desc->needs_tzmem || rproc->has_iommu;
 
 	if (desc->early_boot)
 		pas->rproc->state = RPROC_DETACHED;
Signature · 1 line
2.53.0
#4 lore raw

PATCH v5 4/6 remoteproc: qcom: pas: Map/unmap subsystem region before auth_and_reset

In reply to Mukesh Ojha

Qualcomm remoteproc drivers such as qcom_q6v5_mss, which do not use the Peripheral Authentication Service (PAS), always map the MBA region before use and unmap it once the usage is complete. This behavior was introduced to avoid issues seen in the past where speculative accesses from the application processor to the MBA region after it was assigned to the remote Q6 led to an XPU violation. The issue was mitigated by unmapping the region before handing control to the remote Q6.

Currently, most Qualcomm SoCs using the PAS driver run either with a standalone QHEE or the Gunyah hypervisor. In these environments, the hypervisor unmaps the Q6 memory from HLOS Stage-2 and remaps it into the Q6 Stage-2 page table. As a result, speculative accesses from HLOS cannot reach the region even if it remains mapped in HLOS Stage-1; therefore, XPU violations cannot occur.

However, when the same SoC runs Linux at EL2, Linux itself must perform the unmapping to avoid such issues. It is still correct to apply this mapping/ unmapping sequence even for SoCs that run under Gunyah, so this behavior should not be conditional.

Introduce qcom_pas_ctx_map() in qcom_pas.h to centralise the ioremap_wc pattern used by both qcom_q6v5_pas and qcom_mdt_pas_load, and use it in both places.

---
 drivers/remoteproc/qcom_q6v5_pas.c     | 35 ++++++++++++++++----------
 drivers/soc/qcom/mdt_loader.c          | 15 ++++++++---
 include/linux/firmware/qcom/qcom_pas.h | 12 +++++++++
 include/linux/soc/qcom/mdt_loader.h    |  4 +--
 4 files changed, 47 insertions(+), 19 deletions(-)
drivers/remoteproc/qcom_q6v5_pas.c +22 -13
index 997b3a7ce672..5cd34086ccac 100644
@@ -150,7 +150,13 @@ static void qcom_pas_minidump(struct rproc *rproc)
 	if (rproc->dump_conf == RPROC_COREDUMP_DISABLED)
 		return;
 
+	pas->mem_region = qcom_pas_ctx_map(pas->pas_ctx);
+	if (!pas->mem_region)
+		return;
+
 	qcom_minidump(rproc, pas->minidump_id, qcom_pas_segment_dump);
+	iounmap(pas->mem_region);
+	pas->mem_region = NULL;
 }
 
 static int qcom_pas_pds_enable(struct qcom_pas *pas, struct device **pds,
@@ -243,8 +249,7 @@ static int qcom_pas_load(struct rproc *rproc, const struct firmware *fw)
 		}
 
 		ret = qcom_mdt_pas_load(pas->dtb_pas_ctx, pas->dtb_firmware,
-					pas->dtb_firmware_name, (__force void *)pas->dtb_mem_region,
-					&pas->dtb_mem_reloc);
+					pas->dtb_firmware_name, &pas->dtb_mem_reloc);
 		if (ret) {
 			qcom_pas_metadata_release(pas->dtb_pas_ctx);
 			release_firmware(pas->dtb_firmware);
@@ -318,7 +323,7 @@ static int qcom_pas_start(struct rproc *rproc)
 	}
 
 	ret = qcom_mdt_pas_load(pas->pas_ctx, pas->firmware, rproc->firmware,
-				(__force void *)pas->mem_region, &pas->mem_reloc);
+				&pas->mem_reloc);
 	if (ret)
 		goto release_pas_metadata;
 
@@ -570,6 +575,19 @@ static int qcom_pas_attach(struct rproc *rproc)
 	return ret;
 }
 
+static void qcom_pas_coredump(struct rproc *rproc)
+{
+	struct qcom_pas *pas = rproc->priv;
+
+	pas->mem_region = qcom_pas_ctx_map(pas->pas_ctx);
+	if (!pas->mem_region)
+		return;
+
+	rproc_coredump(rproc);
+	iounmap(pas->mem_region);
+	pas->mem_region = NULL;
+}
+
 static const struct rproc_ops qcom_pas_ops = {
 	.unprepare = qcom_pas_unprepare,
 	.start = qcom_pas_start,
@@ -579,6 +597,7 @@ static const struct rproc_ops qcom_pas_ops = {
 	.load = qcom_pas_load,
 	.panic = qcom_pas_panic,
 	.attach = qcom_pas_attach,
+	.coredump = qcom_pas_coredump,
 };
 
 static const struct rproc_ops qcom_pas_minidump_ops = {
@@ -696,11 +715,6 @@ static int qcom_pas_alloc_memory_region(struct qcom_pas *pas)
 
 	pas->mem_phys = pas->mem_reloc = res.start;
 	pas->mem_size = resource_size(&res);
-	pas->mem_region = devm_ioremap_resource_wc(pas->dev, &res);
-	if (IS_ERR(pas->mem_region)) {
-		dev_err(pas->dev, "unable to map memory region: %pR\n", &res);
-		return PTR_ERR(pas->mem_region);
-	}
 
 	pas->pas_ctx = devm_qcom_pas_context_alloc(pas->dev, pas->pas_id,
 						   pas->mem_phys, pas->mem_size);
@@ -718,11 +732,6 @@ static int qcom_pas_alloc_memory_region(struct qcom_pas *pas)
 
 	pas->dtb_mem_phys = pas->dtb_mem_reloc = res.start;
 	pas->dtb_mem_size = resource_size(&res);
-	pas->dtb_mem_region = devm_ioremap_resource_wc(pas->dev, &res);
-	if (IS_ERR(pas->dtb_mem_region)) {
-		dev_err(pas->dev, "unable to map dtb memory region: %pR\n", &res);
-		return PTR_ERR(pas->dtb_mem_region);
-	}
 
 	pas->dtb_pas_ctx = devm_qcom_pas_context_alloc(pas->dev, pas->dtb_pas_id,
 						       pas->dtb_mem_phys,
drivers/soc/qcom/mdt_loader.c +11 -4
index 137992456b71..002100fe2d32 100644
@@ -11,6 +11,7 @@
 #include <linux/device.h>
 #include <linux/elf.h>
 #include <linux/firmware.h>
+#include <linux/io.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/firmware/qcom/qcom_pas.h>
@@ -478,22 +479,28 @@ EXPORT_SYMBOL_GPL(qcom_mdt_load);
  * @ctx:        Pointer to the PAS (Peripheral Authentication Service) context
  * @fw:         Firmware object representing the .mdt file
  * @firmware:   Name of the firmware used to construct segment file names
- * @mem_region: Memory region allocated for loading the firmware
  * @reloc_base: Physical address adjusted after relocation
  *
  * Return: 0 on success or a negative error code on failure.
  */
 int qcom_mdt_pas_load(struct qcom_pas_context *ctx, const struct firmware *fw,
-		      const char *firmware, void *mem_region, phys_addr_t *reloc_base)
+		      const char *firmware, phys_addr_t *reloc_base)
 {
+	void __iomem *mem_region;
 	int ret;
 
 	ret = __qcom_mdt_pas_init(ctx->dev, fw, firmware, ctx->pas_id, ctx->mem_phys, ctx);
 	if (ret)
 		return ret;
 
-	return qcom_mdt_load_no_init(ctx->dev, fw, firmware, mem_region, ctx->mem_phys,
-				     ctx->mem_size, reloc_base);
+	mem_region = qcom_pas_ctx_map(ctx);
+	if (!mem_region)
+		return -ENOMEM;
+
+	ret = qcom_mdt_load_no_init(ctx->dev, fw, firmware, (__force void *)mem_region,
+				    ctx->mem_phys, ctx->mem_size, reloc_base);
+	iounmap(mem_region);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(qcom_mdt_pas_load);
 
include/linux/firmware/qcom/qcom_pas.h +12 -0
index 65b1c9564458..fb2ec3be6a16 100644
@@ -8,7 +8,9 @@
 #ifndef __QCOM_PAS_H
 #define __QCOM_PAS_H
 
+#include <linux/device.h>
 #include <linux/err.h>
+#include <linux/io.h>
 #include <linux/types.h>
 
 struct qcom_pas_context {
@@ -22,6 +24,16 @@ struct qcom_pas_context {
 	bool use_tzmem;
 };
 
+static inline void __iomem *qcom_pas_ctx_map(struct qcom_pas_context *ctx)
+{
+	void __iomem *ptr = ioremap_wc(ctx->mem_phys, ctx->mem_size);
+
+	if (!ptr)
+		dev_err(ctx->dev, "unable to map memory region: %pa+%zx\n",
+			&ctx->mem_phys, ctx->mem_size);
+	return ptr;
+}
+
 bool qcom_pas_is_available(void);
 struct qcom_pas_context *devm_qcom_pas_context_alloc(struct device *dev,
 						     u32 pas_id,
include/linux/soc/qcom/mdt_loader.h +2 -2
index 142409555425..74886f772d46 100644
@@ -21,7 +21,7 @@ int qcom_mdt_load(struct device *dev, const struct firmware *fw,
 		  phys_addr_t *reloc_base);
 
 int qcom_mdt_pas_load(struct qcom_pas_context *ctx, const struct firmware *fw,
-		      const char *firmware, void *mem_region, phys_addr_t *reloc_base);
+		      const char *firmware, phys_addr_t *reloc_base);
 
 int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
 			  const char *fw_name, void *mem_region,
@@ -47,7 +47,7 @@ static inline int qcom_mdt_load(struct device *dev, const struct firmware *fw,
 
 static inline int qcom_mdt_pas_load(struct qcom_pas_context *ctx,
 				    const struct firmware *fw, const char *firmware,
-				    void *mem_region, phys_addr_t *reloc_base)
+				    phys_addr_t *reloc_base)
 {
 	return -ENODEV;
 }
Signature · 1 line
2.53.0
#5 lore raw

PATCH v5 5/6 remoteproc: qcom: pas: Drop unused dtb_mem_region field

In reply to Mukesh Ojha

dtb_mem_region is no longer referenced after the ioremap was moved to respective places where mapping is required. Remove it from struct qcom_pas.

---
 drivers/remoteproc/qcom_q6v5_pas.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/remoteproc/qcom_q6v5_pas.c +2 -1
index 5cd34086ccac..0391e30f42ba 100644
@@ -101,8 +101,9 @@ struct qcom_pas {
 	phys_addr_t mem_reloc;
 	phys_addr_t dtb_mem_reloc;
 	phys_addr_t region_assign_phys[MAX_ASSIGN_COUNT];
+
 	void __iomem *mem_region;
-	void __iomem *dtb_mem_region;
+
 	size_t mem_size;
 	size_t dtb_mem_size;
 	size_t region_assign_size[MAX_ASSIGN_COUNT];
Signature · 1 line
2.53.0
#6 lore raw

PATCH v5 6/6 firmware: qcom: scm: introduce keep_mdt_buf flag in PAS context

In reply to Mukesh Ojha

The PAS image initialization path always retains the metadata buffer when a valid qcom_scm_pas_context is provided, even if the caller does not require it. This implicit behavior leads to unclear buffer ownership and forces new users of qcom_mdt_pas_load() to manually release metadata, which is error‑ prone and incorrect.

Add a keep_mdt_buf flag to struct qcom_scm_pas_context to make metadata retention explicit. Metadata buffers are now freed by default and are only preserved when this flag is set. qcom_q6v5_pas enables this during probe for contexts that require retained metadata for subsequent PAS operations, while existing callers continue to work unchanged.

---
 drivers/firmware/qcom/qcom_scm.c       | 18 +++++++++++++++---
 drivers/remoteproc/qcom_q6v5_pas.c     |  3 +++
 include/linux/firmware/qcom/qcom_pas.h |  1 +
 3 files changed, 19 insertions(+), 3 deletions(-)
drivers/firmware/qcom/qcom_scm.c +15 -3
index 16ae42e6c434..54ffec97cc26 100644
@@ -625,7 +625,7 @@ static int qcom_scm_pas_prep_and_init_image(struct device *dev,
 	mdata_phys = qcom_tzmem_to_phys(mdata_buf);
 
 	ret = __qcom_scm_pas_init_image(dev, ctx->pas_id, mdata_phys, &res);
-	if (ret < 0)
+	if (ret < 0 || !ctx->keep_mdt_buf)
 		qcom_tzmem_free(mdata_buf);
 	else
 		ctx->ptr = mdata_buf;
@@ -664,9 +664,21 @@ static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id,
 	memcpy(mdata_buf, metadata, size);
 
 	ret = __qcom_scm_pas_init_image(dev, pas_id, mdata_phys, &res);
-	if (ret < 0 || !ctx) {
+
+	/*
+	 * Some clients still pass the PAS context as NULL. Until all clients
+	 * switch to qcom_mdt_pas_load() and provide a valid PAS context, check
+	 * for NULL before dereferencing it.
+	 *
+	 * When a valid context is provided, keep_mdt_buf controls whether the
+	 * metadata buffer is retained after PAS_INIT. PAS remoteproc subsystems
+	 * set this flag so metadata persists until auth_and_reset() completes,
+	 * as TrustZone keeps the buffers locked until then. Other callers leave
+	 * it unset and metadata is freed immediately after the PAS_INIT call.
+	 */
+	if (ret < 0 || !ctx || !ctx->keep_mdt_buf) {
 		dma_free_coherent(dev, size, mdata_buf, mdata_phys);
-	} else if (ctx) {
+	} else {
 		ctx->ptr = mdata_buf;
 		ctx->phys = mdata_phys;
 		ctx->size = size;
drivers/remoteproc/qcom_q6v5_pas.c +3 -0
index 0391e30f42ba..38e6f6019825 100644
@@ -722,6 +722,7 @@ static int qcom_pas_alloc_memory_region(struct qcom_pas *pas)
 	if (IS_ERR(pas->pas_ctx))
 		return PTR_ERR(pas->pas_ctx);
 
+	pas->pas_ctx->keep_mdt_buf = true;
 	if (!pas->dtb_pas_id)
 		return 0;
 
@@ -740,6 +741,8 @@ static int qcom_pas_alloc_memory_region(struct qcom_pas *pas)
 	if (IS_ERR(pas->dtb_pas_ctx))
 		return PTR_ERR(pas->dtb_pas_ctx);
 
+	pas->dtb_pas_ctx->keep_mdt_buf = true;
+
 	return 0;
 }
 
include/linux/firmware/qcom/qcom_pas.h +1 -0
index fb2ec3be6a16..1d132e89536e 100644
@@ -22,6 +22,7 @@ struct qcom_pas_context {
 	dma_addr_t phys;
 	ssize_t size;
 	bool use_tzmem;
+	bool keep_mdt_buf;
 };
 
 static inline void __iomem *qcom_pas_ctx_map(struct qcom_pas_context *ctx)
Signature · 1 line
2.53.0
maintainer
#7 lore raw

Re: [PATCH v5 6/6] firmware: qcom: scm: introduce keep_mdt_buf flag in PAS context

Quoted 6 lines · Mukesh Ojha

The PAS image initialization path always retains the metadata buffer when a valid qcom_scm_pas_context is provided, even if the caller does not require it. This implicit behavior leads to unclear buffer ownership and forces new users of qcom_mdt_pas_load() to manually release metadata, which is error‑ prone and incorrect.

Quoted 6 lines

Add a keep_mdt_buf flag to struct qcom_scm_pas_context to make metadata retention explicit. Metadata buffers are now freed by default and are only preserved when this flag is set. qcom_q6v5_pas enables this during probe for contexts that require retained metadata for subsequent PAS operations, while existing callers continue to work unchanged.

I presume given that this was sent together with the remoteproc patches that this can't be merged until those other changes has made it into the tree.

Please confirm if there is an actual dependency here.

Quoted 21 lines
---
 drivers/firmware/qcom/qcom_scm.c       | 18 +++++++++++++++---
 drivers/remoteproc/qcom_q6v5_pas.c     |  3 +++
 include/linux/firmware/qcom/qcom_pas.h |  1 +
 3 files changed, 19 insertions(+), 3 deletions(-)
drivers/firmware/qcom/qcom_scm.c +1 -1
index 16ae42e6c434..54ffec97cc26 100644
@@ -625,7 +625,7 @@ static int qcom_scm_pas_prep_and_init_image(struct device *dev,
 	mdata_phys = qcom_tzmem_to_phys(mdata_buf);
 
 	ret = __qcom_scm_pas_init_image(dev, ctx->pas_id, mdata_phys, &res);
-	if (ret < 0)
+	if (ret < 0 || !ctx->keep_mdt_buf)
 		qcom_tzmem_free(mdata_buf);
 	else
 		ctx->ptr = mdata_buf;

drivers/firmware/qcom/qcom_scm.c static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id, · line 671

Quoted 9 lines
drivers/firmware/qcom/qcom_scm.c +5 -1
@@ -664,9 +664,21 @@ static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id,
 	memcpy(mdata_buf, metadata, size);
 
 	ret = __qcom_scm_pas_init_image(dev, pas_id, mdata_phys, &res);
-	if (ret < 0 || !ctx) {
+
+	/*
+	 * Some clients still pass the PAS context as NULL. Until all clients
+	 * switch to qcom_mdt_pas_load() and provide a valid PAS context, check
+	 * for NULL before dereferencing it.

Why don't we fix the problematic clients? There's no point in working around such things.

drivers/firmware/qcom/qcom_scm.c static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id, · line 677

Quoted 6 lines
drivers/firmware/qcom/qcom_scm.c +6 -0
+	 *
+	 * When a valid context is provided, keep_mdt_buf controls whether the
+	 * metadata buffer is retained after PAS_INIT. PAS remoteproc subsystems
+	 * set this flag so metadata persists until auth_and_reset() completes,
+	 * as TrustZone keeps the buffers locked until then. Other callers leave
+	 * it unset and metadata is freed immediately after the PAS_INIT call.

Please rewrite this to make it more succinct. It seems you could say the same thing with just the words "free the metadata on error or if client didn't request us to keep it".

Regards,
Bjorn

Quoted 44 lines
drivers/firmware/qcom/qcom_scm.c +3 -1
+	 */
+	if (ret < 0 || !ctx || !ctx->keep_mdt_buf) {
 		dma_free_coherent(dev, size, mdata_buf, mdata_phys);
-	} else if (ctx) {
+	} else {
 		ctx->ptr = mdata_buf;
 		ctx->phys = mdata_phys;
 		ctx->size = size;
drivers/remoteproc/qcom_q6v5_pas.c +3 -0
index 0391e30f42ba..38e6f6019825 100644
@@ -722,6 +722,7 @@ static int qcom_pas_alloc_memory_region(struct qcom_pas *pas)
 	if (IS_ERR(pas->pas_ctx))
 		return PTR_ERR(pas->pas_ctx);
 
+	pas->pas_ctx->keep_mdt_buf = true;
 	if (!pas->dtb_pas_id)
 		return 0;
 
@@ -740,6 +741,8 @@ static int qcom_pas_alloc_memory_region(struct qcom_pas *pas)
 	if (IS_ERR(pas->dtb_pas_ctx))
 		return PTR_ERR(pas->dtb_pas_ctx);
 
+	pas->dtb_pas_ctx->keep_mdt_buf = true;
+
 	return 0;
 }
 
include/linux/firmware/qcom/qcom_pas.h +1 -0
index fb2ec3be6a16..1d132e89536e 100644
@@ -22,6 +22,7 @@ struct qcom_pas_context {
 	dma_addr_t phys;
 	ssize_t size;
 	bool use_tzmem;
+	bool keep_mdt_buf;
 };
 
 static inline void __iomem *qcom_pas_ctx_map(struct qcom_pas_context *ctx)
Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH v5 6/6] firmware: qcom: scm: introduce keep_mdt_buf flag in PAS context

Quoted 14 lines · Bjorn Andersson
Quoted 6 lines · Mukesh Ojha

The PAS image initialization path always retains the metadata buffer when a valid qcom_scm_pas_context is provided, even if the caller does not require it. This implicit behavior leads to unclear buffer ownership and forces new users of qcom_mdt_pas_load() to manually release metadata, which is error‑ prone and incorrect.

Quoted 6 lines

Add a keep_mdt_buf flag to struct qcom_scm_pas_context to make metadata retention explicit. Metadata buffers are now freed by default and are only preserved when this flag is set. qcom_q6v5_pas enables this during probe for contexts that require retained metadata for subsequent PAS operations, while existing callers continue to work unchanged.

Quoted 5 lines

I presume given that this was sent together with the remoteproc patches that this can't be merged until those other changes has made it into the tree.

Please confirm if there is an actual dependency here.

I had an expectation that Sumit's patches would get picked up completely somehow, which would avoid me adding the extra variable keep_mdt_buf to the temporary data structure qcom_scm_pas_context. That's the only dependency.

Quoted 24 lines
Quoted 24 lines
---
 drivers/firmware/qcom/qcom_scm.c       | 18 +++++++++++++++---
 drivers/remoteproc/qcom_q6v5_pas.c     |  3 +++
 include/linux/firmware/qcom/qcom_pas.h |  1 +
 3 files changed, 19 insertions(+), 3 deletions(-)
drivers/firmware/qcom/qcom_scm.c +1 -1
index 16ae42e6c434..54ffec97cc26 100644
@@ -625,7 +625,7 @@ static int qcom_scm_pas_prep_and_init_image(struct device *dev,
 	mdata_phys = qcom_tzmem_to_phys(mdata_buf);
 
 	ret = __qcom_scm_pas_init_image(dev, ctx->pas_id, mdata_phys, &res);
-	if (ret < 0)
+	if (ret < 0 || !ctx->keep_mdt_buf)
 		qcom_tzmem_free(mdata_buf);
 	else
 		ctx->ptr = mdata_buf;
@@ -664,9 +664,21 @@ static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id,
 	memcpy(mdata_buf, metadata, size);
 
Quoted 10 lines

drivers/firmware/qcom/qcom_scm.c static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id, · line 671

Quoted 7 lines
drivers/firmware/qcom/qcom_scm.c +5 -1
 	ret = __qcom_scm_pas_init_image(dev, pas_id, mdata_phys, &res);
-	if (ret < 0 || !ctx) {
+
+	/*
+	 * Some clients still pass the PAS context as NULL. Until all clients
+	 * switch to qcom_mdt_pas_load() and provide a valid PAS context, check
+	 * for NULL before dereferencing it.

Why don't we fix the problematic clients? There's no point in working around such things.

Was waiting for Sumit's patches to land so that I could convert every client to use PAS ctx.

Quoted 11 lines

drivers/firmware/qcom/qcom_scm.c static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id, · line 677

Quoted 7 lines
drivers/firmware/qcom/qcom_scm.c +6 -0
+	 *
+	 * When a valid context is provided, keep_mdt_buf controls whether the
+	 * metadata buffer is retained after PAS_INIT. PAS remoteproc subsystems
+	 * set this flag so metadata persists until auth_and_reset() completes,
+	 * as TrustZone keeps the buffers locked until then. Other callers leave
+	 * it unset and metadata is freed immediately after the PAS_INIT call.

Please rewrite this to make it more succinct. It seems you could say the same thing with just the words "free the metadata on error or if client didn't request us to keep it".

Sure.

Signature · 1 line
-Mukesh Ojha
maintainer
#9 lore raw

Re: [PATCH v5 6/6] firmware: qcom: scm: introduce keep_mdt_buf flag in PAS context

Quoted 19 lines · Mukesh Ojha
Quoted 18 lines · Bjorn Andersson
Quoted 6 lines · Mukesh Ojha

The PAS image initialization path always retains the metadata buffer when a valid qcom_scm_pas_context is provided, even if the caller does not require it. This implicit behavior leads to unclear buffer ownership and forces new users of qcom_mdt_pas_load() to manually release metadata, which is error‑ prone and incorrect.

Quoted 6 lines

Add a keep_mdt_buf flag to struct qcom_scm_pas_context to make metadata retention explicit. Metadata buffers are now freed by default and are only preserved when this flag is set. qcom_q6v5_pas enables this during probe for contexts that require retained metadata for subsequent PAS operations, while existing callers continue to work unchanged.

I presume given that this was sent together with the remoteproc patches that this can't be merged until those other changes has made it into the tree.

Quoted 6 lines
Quoted 1 line

Please confirm if there is an actual dependency here.

I had an expectation that Sumit's patches would get picked up completely somehow, which would avoid me adding the extra variable keep_mdt_buf to the temporary data structure qcom_scm_pas_context. That's the only dependency.

How on earth do you expect me to figure that out!?

Regards,
Bjorn

Quoted 54 lines
Quoted 24 lines
Quoted 24 lines
---
 drivers/firmware/qcom/qcom_scm.c       | 18 +++++++++++++++---
 drivers/remoteproc/qcom_q6v5_pas.c     |  3 +++
 include/linux/firmware/qcom/qcom_pas.h |  1 +
 3 files changed, 19 insertions(+), 3 deletions(-)
drivers/firmware/qcom/qcom_scm.c +1 -1
index 16ae42e6c434..54ffec97cc26 100644
@@ -625,7 +625,7 @@ static int qcom_scm_pas_prep_and_init_image(struct device *dev,
 	mdata_phys = qcom_tzmem_to_phys(mdata_buf);
 
 	ret = __qcom_scm_pas_init_image(dev, ctx->pas_id, mdata_phys, &res);
-	if (ret < 0)
+	if (ret < 0 || !ctx->keep_mdt_buf)
 		qcom_tzmem_free(mdata_buf);
 	else
 		ctx->ptr = mdata_buf;
@@ -664,9 +664,21 @@ static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id,
 	memcpy(mdata_buf, metadata, size);
 
Quoted 10 lines

drivers/firmware/qcom/qcom_scm.c static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id, · line 671

Quoted 7 lines
drivers/firmware/qcom/qcom_scm.c +5 -1
 	ret = __qcom_scm_pas_init_image(dev, pas_id, mdata_phys, &res);
-	if (ret < 0 || !ctx) {
+
+	/*
+	 * Some clients still pass the PAS context as NULL. Until all clients
+	 * switch to qcom_mdt_pas_load() and provide a valid PAS context, check
+	 * for NULL before dereferencing it.

Why don't we fix the problematic clients? There's no point in working around such things.

Was waiting for Sumit's patches to land so that I could convert every client to use PAS ctx.

Quoted 11 lines

drivers/firmware/qcom/qcom_scm.c static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id, · line 677

Quoted 7 lines
drivers/firmware/qcom/qcom_scm.c +6 -0
+	 *
+	 * When a valid context is provided, keep_mdt_buf controls whether the
+	 * metadata buffer is retained after PAS_INIT. PAS remoteproc subsystems
+	 * set this flag so metadata persists until auth_and_reset() completes,
+	 * as TrustZone keeps the buffers locked until then. Other callers leave
+	 * it unset and metadata is freed immediately after the PAS_INIT call.

Please rewrite this to make it more succinct. It seems you could say the same thing with just the words "free the metadata on error or if client didn't request us to keep it".

Sure.

Signature · 1 line
-Mukesh Ojha
#10 lore raw

Re: [PATCH v5 6/6] firmware: qcom: scm: introduce keep_mdt_buf flag in PAS context

Quoted 20 lines · Bjorn Andersson
Quoted 19 lines · Mukesh Ojha
Quoted 18 lines · Bjorn Andersson
Quoted 6 lines · Mukesh Ojha

The PAS image initialization path always retains the metadata buffer when a valid qcom_scm_pas_context is provided, even if the caller does not require it. This implicit behavior leads to unclear buffer ownership and forces new users of qcom_mdt_pas_load() to manually release metadata, which is error‑ prone and incorrect.

Quoted 6 lines

Add a keep_mdt_buf flag to struct qcom_scm_pas_context to make metadata retention explicit. Metadata buffers are now freed by default and are only preserved when this flag is set. qcom_q6v5_pas enables this during probe for contexts that require retained metadata for subsequent PAS operations, while existing callers continue to work unchanged.

I presume given that this was sent together with the remoteproc patches that this can't be merged until those other changes has made it into the tree.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Please confirm if there is an actual dependency here.

I had an expectation that Sumit's patches would get picked up completely somehow, which would avoid me adding the extra variable keep_mdt_buf to the temporary data structure qcom_scm_pas_context. That's the only dependency.

How on earth do you expect me to figure that out!?

Mistake on my part. Should have mentioned this all as part of the cover letter as the reason for the dependency.

Since, we are still uncertain about current merge of

https://lore.kernel.org/lkml/[email protected]/

Shall I send this with adding extra variable to qcom_scm_pas_context ?

-Mukesh

maintainer Applied
#11 lore raw

Re: (subset) [PATCH v5 0/6] remoteproc: qcom: pas: Misc fixes

In reply to Mukesh Ojha
Quoted 16 lines · Mukesh Ojha

This series is a collection of misc fixes for the Qualcomm PAS remoteproc driver and its supporting SCM/MDT loader infrastructure.

- Fix sparse __iomem warnings in qcom_adsp, qcom_pas and qcom_wcnss by annotating mem_region fields with __iomem and using __force at call sites.
- Guard the DTB metadata release in qcom_pas_load() with a dtb_pas_id check, consistent with all other release sites in the driver.
- Move PAS context allocation from probe into qcom_pas_alloc_memory_region() so the DTB context is only created for subsystems that actually use it.
- Map/unmap the subsystem memory region on demand around firmware load and coredump, avoiding potential XPU violations on EL2 platforms where the region may be handed off to the remote side.
- Drop the now-unused dtb_mem_region field from struct qcom_pas.
- Add a keep_mdt_buf flag to struct qcom_scm_pas_context to make metadata buffer retention explicit rather than implicit.

Quoted 1 line

[...]

Applied, thanks!

[1/6] remoteproc: qcom: annotate mem_region fields with __iomem

      commit: 142044ea7bbb83c180319368a8ba088452ceb7f5
[2/6] remoteproc: qcom: pas: Guard dtb metadata release with dtb_pas_id check
      commit: c06c5ab4945392d2c2aded6d832ab6b58cabe351
[3/6] remoteproc: qcom: pas: Fix the PAS context creation placement
      commit: a5464fa3173c83da28b46e3c12be6d4a27bf5728
[4/6] remoteproc: qcom: pas: Map/unmap subsystem region before auth_and_reset
      commit: 0b0379fcf9ce395bb51cadb6552ec3b1380436bb
[5/6] remoteproc: qcom: pas: Drop unused dtb_mem_region field
      commit: d402a23bf4337a5050306847ea19e1bdd8e05736

Best regards,

Signature · 1 line
Bjorn Andersson <[email protected]>