[PATCH v2 07/12] coresight-tmc: Support probe and initialization for CPU cluster TMCs

Yuanfang Zhang
2025-12-18T08:10:52Z

TMC instances associated with CPU clusters reside in the cluster's power
domain. Unlike system-level TMCs, their registers are only accessible
when the cluster is powered on. Standard runtime PM may not suffice to
wake up a cluster from low-power states during probe, making direct
register access unreliable.

Refactor the probe sequence to handle these per-cluster devices safely:

1. Identify per-cluster TMCs using the "qcom,cpu-bound-components"
property.
2. For such devices, use `smp_call_function_single()` to perform
hardware initialization (`tmc_init_hw_config`) on a CPU within the
cluster. This ensures the domain is powered during access.
3. Factor out the device registration logic into `tmc_add_coresight_dev()`.
This allows common registration code to be shared between the standard
probe path and the deferred probe path (used when the associated CPUs
are initially offline).

This change ensures reliable initialization for per-cluster TMCs while
maintaining backward compatibility for standard system-level TMCs.

Signed-off-by: Yuanfang Zhang <yuanfang.zhang@oss.qualcomm.com>
---
drivers/hwtracing/coresight/coresight-tmc-core.c | 195 +++++++++++++++--------
drivers/hwtracing/coresight/coresight-tmc.h | 6 +
2 files changed, 132 insertions(+), 69 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-tmc-core.c b/drivers/hwtracing/coresight/coresight-tmc-core.c
index 36599c431be6203e871fdcb8de569cc6701c52bb..0e1b5956398d3cefdd938a8a8404076eb4850b44 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-core.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-core.c
@@ -21,6 +21,7 @@
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include <linux/spinlock.h>
+#include <linux/pm_domain.h>
#include <linux/pm_runtime.h>
#include <linux/of.h>
#include <linux/of_address.h>
@@ -769,56 +770,14 @@ static void register_crash_dev_interface(struct tmc_drvdata *drvdata,
"Valid crash tracedata found\n");
}

-static int __tmc_probe(struct device *dev, struct resource *res)
+static int tmc_add_coresight_dev(struct device *dev)
{
- int ret = 0;
- u32 devid;
- void __iomem *base;
- struct coresight_platform_data *pdata = NULL;
- struct tmc_drvdata *drvdata;
+ struct tmc_drvdata *drvdata = dev_get_drvdata(dev);
struct coresight_desc desc = { 0 };
struct coresight_dev_list *dev_list = NULL;
+ int ret = 0;

- drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
- if (!drvdata)
- return -ENOMEM;
-
- dev_set_drvdata(dev, drvdata);
-
- ret = coresight_get_enable_clocks(dev, &drvdata->pclk, &drvdata->atclk);
- if (ret)
- return ret;
-
- ret = -ENOMEM;
-
- /* Validity for the resource is already checked by the AMBA core */
- base = devm_ioremap_resource(dev, res);
- if (IS_ERR(base)) {
- ret = PTR_ERR(base);
- goto out;
- }
-
- drvdata->base = base;
- desc.access = CSDEV_ACCESS_IOMEM(base);
-
- raw_spin_lock_init(&drvdata->spinlock);
-
- devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
- drvdata->config_type = BMVAL(devid, 6, 7);
- drvdata->memwidth = tmc_get_memwidth(devid);
- /* This device is not associated with a session */
- drvdata->pid = -1;
- drvdata->etr_mode = ETR_MODE_AUTO;
-
- if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
- drvdata->size = tmc_etr_get_default_buffer_size(dev);
- drvdata->max_burst_size = tmc_etr_get_max_burst_size(dev);
- } else {
- drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
- }
-
- tmc_get_reserved_region(dev);
-
+ desc.access = CSDEV_ACCESS_IOMEM(drvdata->base);
desc.dev = dev;

switch (drvdata->config_type) {
@@ -834,9 +793,9 @@ static int __tmc_probe(struct device *dev, struct resource *res)
desc.type = CORESIGHT_DEV_TYPE_SINK;
desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_SYSMEM;
desc.ops = &tmc_etr_cs_ops;
- ret = tmc_etr_setup_caps(dev, devid, &desc.access);
+ ret = tmc_etr_setup_caps(dev, drvdata->devid, &desc.access);
if (ret)
- goto out;
+ return ret;
idr_init(&drvdata->idr);
mutex_init(&drvdata->idr_mutex);
dev_list = &etr_devs;
@@ -851,44 +810,141 @@ static int __tmc_probe(struct device *dev, struct resource *res)
break;
default:
pr_err("%s: Unsupported TMC config\n", desc.name);
- ret = -EINVAL;
- goto out;
+ return -EINVAL;
}

desc.name = coresight_alloc_device_name(dev_list, dev);
- if (!desc.name) {
- ret = -ENOMEM;
+ if (!desc.name)
+ return -ENOMEM;
+
+ drvdata->desc_name = desc.name;
+
+ desc.pdata = dev->platform_data;
+
+ drvdata->csdev = coresight_register(&desc);
+ if (IS_ERR(drvdata->csdev))
+ return PTR_ERR(drvdata->csdev);
+
+ drvdata->miscdev.name = desc.name;
+ drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
+ drvdata->miscdev.fops = &tmc_fops;
+ ret = misc_register(&drvdata->miscdev);
+ if (ret)
+ coresight_unregister(drvdata->csdev);
+
+ return ret;
+}
+
+static void tmc_clear_self_claim_tag(struct tmc_drvdata *drvdata)
+{
+ struct csdev_access access = CSDEV_ACCESS_IOMEM(drvdata->base);
+
+ coresight_clear_self_claim_tag(&access);
+}
+
+static void tmc_init_hw_config(struct tmc_drvdata *drvdata)
+{
+ u32 devid;
+
+ devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
+ drvdata->config_type = BMVAL(devid, 6, 7);
+ drvdata->memwidth = tmc_get_memwidth(devid);
+ drvdata->devid = devid;
+ drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
+ tmc_clear_self_claim_tag(drvdata);
+}
+
+static void tmc_init_on_cpu(void *info)
+{
+ struct tmc_drvdata *drvdata = info;
+
+ tmc_init_hw_config(drvdata);
+}
+
+static struct cpumask *tmc_get_supported_cpus(struct device *dev)
+{
+ struct generic_pm_domain *pd;
+
+ pd = pd_to_genpd(dev->pm_domain);
+ if (pd)
+ return pd->cpus;
+
+ return NULL;
+}
+
+static int __tmc_probe(struct device *dev, struct resource *res)
+{
+ int cpu, ret = 0;
+ void __iomem *base;
+ struct coresight_platform_data *pdata = NULL;
+ struct tmc_drvdata *drvdata;
+
+ drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
+ if (!drvdata)
+ return -ENOMEM;
+
+ dev_set_drvdata(dev, drvdata);
+
+ ret = coresight_get_enable_clocks(dev, &drvdata->pclk, &drvdata->atclk);
+ if (ret)
+ return ret;
+
+ ret = -ENOMEM;
+
+ /* Validity for the resource is already checked by the AMBA core */
+ base = devm_ioremap_resource(dev, res);
+ if (IS_ERR(base)) {
+ ret = PTR_ERR(base);
goto out;
}

+ drvdata->base = base;
+
+ raw_spin_lock_init(&drvdata->spinlock);
+ /* This device is not associated with a session */
+ drvdata->pid = -1;
+ drvdata->etr_mode = ETR_MODE_AUTO;
+ tmc_get_reserved_region(dev);
+
pdata = coresight_get_platform_data(dev);
if (IS_ERR(pdata)) {
ret = PTR_ERR(pdata);
goto out;
}
dev->platform_data = pdata;
- desc.pdata = pdata;

- coresight_clear_self_claim_tag(&desc.access);
- drvdata->csdev = coresight_register(&desc);
- if (IS_ERR(drvdata->csdev)) {
- ret = PTR_ERR(drvdata->csdev);
- goto out;
+ if (fwnode_property_present(dev_fwnode(dev), "qcom,cpu-bound-components")) {
+ drvdata->supported_cpus = tmc_get_supported_cpus(dev);
+ if (!drvdata->supported_cpus)
+ return -EINVAL;
+
+ cpus_read_lock();
+ for_each_cpu(cpu, drvdata->supported_cpus) {
+ ret = smp_call_function_single(cpu,
+ tmc_init_on_cpu, drvdata, 1);
+ if (!ret)
+ break;
+ }
+ cpus_read_unlock();
+ if (ret) {
+ ret = 0;
+ goto out;
+ }
+ } else {
+ tmc_init_hw_config(drvdata);
}

- drvdata->miscdev.name = desc.name;
- drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
- drvdata->miscdev.fops = &tmc_fops;
- ret = misc_register(&drvdata->miscdev);
- if (ret) {
- coresight_unregister(drvdata->csdev);
- goto out;
+ if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
+ drvdata->size = tmc_etr_get_default_buffer_size(dev);
+ drvdata->max_burst_size = tmc_etr_get_max_burst_size(dev);
}

+ ret = tmc_add_coresight_dev(dev);
+
out:
if (is_tmc_crashdata_valid(drvdata) &&
!tmc_prepare_crashdata(drvdata))
- register_crash_dev_interface(drvdata, desc.name);
+ register_crash_dev_interface(drvdata, drvdata->desc_name);
return ret;
}

@@ -934,10 +990,12 @@ static void __tmc_remove(struct device *dev)
* etb fops in this case, device is there until last file
* handler to this device is closed.
*/
- misc_deregister(&drvdata->miscdev);
+ if (!drvdata->supported_cpus)
+ misc_deregister(&drvdata->miscdev);
if (drvdata->crashdev.fops)
misc_deregister(&drvdata->crashdev);
- coresight_unregister(drvdata->csdev);
+ if (drvdata->csdev)
+ coresight_unregister(drvdata->csdev);
}

static void tmc_remove(struct amba_device *adev)
@@ -992,7 +1050,6 @@ static void tmc_platform_remove(struct platform_device *pdev)

if (WARN_ON(!drvdata))
return;
-
__tmc_remove(&pdev->dev);
pm_runtime_disable(&pdev->dev);
}
diff --git a/drivers/hwtracing/coresight/coresight-tmc.h b/drivers/hwtracing/coresight/coresight-tmc.h
index 95473d1310323425b7d136cbd46f118faa7256be..b104b7bf82d2a7a99382636e41d3718cf258d820 100644
--- a/drivers/hwtracing/coresight/coresight-tmc.h
+++ b/drivers/hwtracing/coresight/coresight-tmc.h
@@ -243,6 +243,9 @@ struct tmc_resrv_buf {
* (after crash) by default.
* @crash_mdata: Reserved memory for storing tmc crash metadata.
* Used by ETR/ETF.
+ * @supported_cpus: Represent the CPUs related to this TMC.
+ * @devid: TMC variant ID inferred from the device configuration register.
+ * @desc_name: Name to be used while creating crash interface.
*/
struct tmc_drvdata {
struct clk *atclk;
@@ -273,6 +276,9 @@ struct tmc_drvdata {
struct etr_buf *perf_buf;
struct tmc_resrv_buf resrv_buf;
struct tmc_resrv_buf crash_mdata;
+ struct cpumask *supported_cpus;
+ u32 devid;
+ const char *desc_name;
};

struct etr_buf_operations {

--
2.34.1