[PATCH v17 06/10] qcom-tgu: Add TGU decode support

From: Songwei Chai

Date: Fri Nov 13 2026 - 00:49:13 EST


Decoding is when all the potential pieces for creating a trigger
are brought together for a given step. Example - there may be a
counter keeping track of some occurrences and a priority-group that
is being used to detect a pattern on the sense inputs. These 2
inputs to condition_decode must be programmed, for a given step,
to establish the condition for the trigger, or movement to another
steps.

Reviewed-by: Jie Gan <jie.gan@xxxxxxxxxxxxxxxx>
Signed-off-by: Songwei Chai <songwei.chai@xxxxxxxxxxxxxxxx>
---
.../ABI/testing/sysfs-bus-amba-devices-tgu | 7 +
drivers/hwtracing/qcom/tgu.c | 187 ++++++++++++++++--
drivers/hwtracing/qcom/tgu.h | 35 +++-
3 files changed, 207 insertions(+), 22 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-bus-amba-devices-tgu b/Documentation/ABI/testing/sysfs-bus-amba-devices-tgu
index 09370481758c..b94da9497243 100644
--- a/Documentation/ABI/testing/sysfs-bus-amba-devices-tgu
+++ b/Documentation/ABI/testing/sysfs-bus-amba-devices-tgu
@@ -14,3 +14,10 @@ KernelVersion: 7.5
Contact: Jinlong Mao <jinlong.mao@xxxxxxxxxxxxxxxx>, Songwei Chai <songwei.chai@xxxxxxxxxxxxxxxx>
Description:
(RW) Set/Get the sensed signal with specific step and priority for TGU.
+
+What: /sys/bus/amba/devices/<tgu-name>/step[0:7]_condition_decode/reg[0:3]
+Date: November 2026
+KernelVersion: 7.5
+Contact: Jinlong Mao <jinlong.mao@xxxxxxxxxxxxxxxx>, Songwei Chai <songwei.chai@xxxxxxxxxxxxxxxx>
+Description:
+ (RW) Set/Get the decode mode with specific step for TGU.
diff --git a/drivers/hwtracing/qcom/tgu.c b/drivers/hwtracing/qcom/tgu.c
index c00e95e68214..72b98a96d7f2 100644
--- a/drivers/hwtracing/qcom/tgu.c
+++ b/drivers/hwtracing/qcom/tgu.c
@@ -18,8 +18,8 @@

struct tgu_attribute {
struct device_attribute attr;
+ enum operation_index operation_index;
u8 step_index;
- u8 priority;
u8 reg_num;
};

@@ -27,11 +27,36 @@ struct tgu_attribute {
* drvdata->priority is a flat [priority][step][reg] array.
*/
static int calculate_array_location(struct tgu_drvdata *drvdata,
- int step_index, int priority,
+ int step_index, int operation_index,
int reg_index)
{
- return priority * (drvdata->num_step) * (drvdata->num_reg) +
- step_index * (drvdata->num_reg) + reg_index;
+ switch (operation_index) {
+ case TGU_PRIORITY0:
+ case TGU_PRIORITY1:
+ case TGU_PRIORITY2:
+ case TGU_PRIORITY3:
+ return operation_index * (drvdata->num_step) *
+ (drvdata->num_reg) +
+ step_index * (drvdata->num_reg) + reg_index;
+ case TGU_CONDITION_DECODE:
+ return step_index * (drvdata->num_condition_decode) +
+ reg_index;
+ default:
+ break;
+ }
+
+ return -EINVAL;
+}
+
+static int check_array_location(struct tgu_drvdata *drvdata, int step,
+ int ops, int reg)
+{
+ int result = calculate_array_location(drvdata, step, ops, reg);
+
+ if (result == -EINVAL)
+ dev_err(drvdata->dev, "check array location - Fail\n");
+
+ return result;
}

static ssize_t tgu_dataset_show(struct device *dev,
@@ -42,9 +67,11 @@ static ssize_t tgu_dataset_show(struct device *dev,
container_of(attr, struct tgu_attribute, attr);
int index;

- index = calculate_array_location(drvdata, tgu_attr->step_index,
- tgu_attr->priority,
- tgu_attr->reg_num);
+ index = check_array_location(drvdata, tgu_attr->step_index,
+ tgu_attr->operation_index, tgu_attr->reg_num);
+
+ if (index == -EINVAL)
+ return index;

/*
* Serialise against tgu_dataset_store() and tgu_enable(), which read
@@ -53,7 +80,20 @@ static ssize_t tgu_dataset_show(struct device *dev,
*/
guard(mutex)(&drvdata->lock);

- return sysfs_emit(buf, "0x%x\n", drvdata->priority[index]);
+ switch (tgu_attr->operation_index) {
+ case TGU_PRIORITY0:
+ case TGU_PRIORITY1:
+ case TGU_PRIORITY2:
+ case TGU_PRIORITY3:
+ return sysfs_emit(buf, "0x%x\n",
+ drvdata->priority[index]);
+ case TGU_CONDITION_DECODE:
+ return sysfs_emit(buf, "0x%x\n",
+ drvdata->condition_decode[index]);
+ default:
+ break;
+ }
+ return -EINVAL;
}

static ssize_t tgu_dataset_store(struct device *dev,
@@ -73,12 +113,30 @@ static ssize_t tgu_dataset_store(struct device *dev,

guard(mutex)(&tgu_drvdata->lock);
index = calculate_array_location(tgu_drvdata, tgu_attr->step_index,
- tgu_attr->priority,
+ tgu_attr->operation_index,
tgu_attr->reg_num);

- tgu_drvdata->priority[index] = val;
+ if (index == -EINVAL)
+ return index;
+
+ switch (tgu_attr->operation_index) {
+ case TGU_PRIORITY0:
+ case TGU_PRIORITY1:
+ case TGU_PRIORITY2:
+ case TGU_PRIORITY3:
+ tgu_drvdata->priority[index] = val;
+ ret = size;
+ break;
+ case TGU_CONDITION_DECODE:
+ tgu_drvdata->condition_decode[index] = val;
+ ret = size;
+ break;
+ default:
+ ret = -EINVAL;
+ break;
+ }

- return size;
+ return ret;
}

static umode_t tgu_node_visible(struct kobject *kobject,
@@ -95,22 +153,41 @@ static umode_t tgu_node_visible(struct kobject *kobject,
if (tgu_attr->step_index >= drvdata->num_step)
return SYSFS_GROUP_INVISIBLE;

- if (tgu_attr->reg_num >= drvdata->num_reg)
- return 0;
+ switch (tgu_attr->operation_index) {
+ case TGU_PRIORITY0:
+ case TGU_PRIORITY1:
+ case TGU_PRIORITY2:
+ case TGU_PRIORITY3:
+ if (!drvdata->num_reg)
+ return SYSFS_GROUP_INVISIBLE;
+ if (tgu_attr->reg_num < drvdata->num_reg)
+ return attr->mode;
+ break;
+ case TGU_CONDITION_DECODE:
+ if (!drvdata->num_condition_decode)
+ return SYSFS_GROUP_INVISIBLE;
+ if (tgu_attr->reg_num < drvdata->num_condition_decode)
+ return attr->mode;
+ break;
+ default:
+ break;
+ }

- return attr->mode;
+ return 0;
}

-static void tgu_write_all_hw_regs(struct tgu_drvdata *drvdata)
+static ssize_t tgu_write_all_hw_regs(struct tgu_drvdata *drvdata)
{
- int i, j, k, index;
+ int i, j, k, index = 0;

CS_UNLOCK(drvdata->base);
for (i = 0; i < drvdata->num_step; i++) {
for (j = 0; j < MAX_PRIORITY; j++) {
for (k = 0; k < drvdata->num_reg; k++) {
- index = calculate_array_location(
+ index = check_array_location(
drvdata, i, j, k);
+ if (index == -EINVAL)
+ goto exit;

writel(drvdata->priority[index],
drvdata->base +
@@ -118,14 +195,28 @@ static void tgu_write_all_hw_regs(struct tgu_drvdata *drvdata)
}
}
}
+
+ for (i = 0; i < drvdata->num_step; i++) {
+ for (j = 0; j < drvdata->num_condition_decode; j++) {
+ index = check_array_location(drvdata, i,
+ TGU_CONDITION_DECODE, j);
+ if (index == -EINVAL)
+ goto exit;
+
+ writel(drvdata->condition_decode[index],
+ drvdata->base + CONDITION_DECODE_STEP(i, j));
+ }
+ }
/* Enable TGU to program the triggers */
writel(1, drvdata->base + TGU_CONTROL);
+exit:
CS_LOCK(drvdata->base);
+ return index >= 0 ? 0 : -EINVAL;
}

static void tgu_set_dev_caps(struct tgu_drvdata *drvdata)
{
- int num_sense_input, num_reg, num_step;
+ int num_sense_input, num_reg, num_step, num_condition_decode;
u32 devid;

devid = readl(drvdata->base + CORESIGHT_DEVID);
@@ -153,12 +244,28 @@ static void tgu_set_dev_caps(struct tgu_drvdata *drvdata)
num_step = TGU_MAX_STEPS;
}
drvdata->num_step = num_step;
+
+ num_condition_decode = TGU_DEVID_CONDITIONS(devid);
+
+ if (num_condition_decode > TGU_MAX_CONDITION_DECODE) {
+ dev_warn(drvdata->dev,
+ "DEVID reports %d condition decode regs, clamping to %d\n",
+ num_condition_decode, TGU_MAX_CONDITION_DECODE);
+ num_condition_decode = TGU_MAX_CONDITION_DECODE;
+ }
+ drvdata->num_condition_decode = num_condition_decode;
}

-static void tgu_enable(struct tgu_drvdata *drvdata)
+static int tgu_enable(struct tgu_drvdata *drvdata)
{
- tgu_write_all_hw_regs(drvdata);
+ int ret;
+
+ ret = tgu_write_all_hw_regs(drvdata);
+ if (ret)
+ return ret;
+
drvdata->enabled = true;
+ return 0;
}

static void tgu_disable(struct tgu_drvdata *drvdata)
@@ -212,7 +319,11 @@ static ssize_t enable_tgu_store(struct device *dev,
if (ret)
return ret;

- tgu_enable(drvdata);
+ ret = tgu_enable(drvdata);
+ if (ret) {
+ pm_runtime_put(dev);
+ return ret;
+ }
} else {
if (!drvdata->enabled)
return -EINVAL;
@@ -280,6 +391,24 @@ static const struct attribute_group tgu_common_grp = {
.name = "step" #step "_priority" #priority \
})

+#define STEP_DECODE(step_index, reg_num) \
+ tgu_dataset_rw(reg##reg_num, step_index, TGU_CONDITION_DECODE, reg_num)
+
+#define STEP_DECODE_LIST(step_index) \
+ {STEP_DECODE(step_index, 0), \
+ STEP_DECODE(step_index, 1), \
+ STEP_DECODE(step_index, 2), \
+ STEP_DECODE(step_index, 3), \
+ NULL \
+ }
+
+#define CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(step)\
+ (&(const struct attribute_group){\
+ .attrs = (struct attribute*[])STEP_DECODE_LIST(step),\
+ .is_visible = tgu_node_visible,\
+ .name = "step" #step "_condition_decode" \
+ })
+
static const struct attribute_group *tgu_attr_groups[] = {
&tgu_common_grp,
PRIORITY_ATTRIBUTE_GROUP_INIT(0, 0),
@@ -314,6 +443,14 @@ static const struct attribute_group *tgu_attr_groups[] = {
PRIORITY_ATTRIBUTE_GROUP_INIT(7, 1),
PRIORITY_ATTRIBUTE_GROUP_INIT(7, 2),
PRIORITY_ATTRIBUTE_GROUP_INIT(7, 3),
+ CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(0),
+ CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(1),
+ CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(2),
+ CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(3),
+ CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(4),
+ CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(5),
+ CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(6),
+ CONDITION_DECODE_ATTRIBUTE_GROUP_INIT(7),
NULL,
};

@@ -321,6 +458,7 @@ static int tgu_probe(struct amba_device *adev, const struct amba_id *id)
{
struct device *dev = &adev->dev;
struct tgu_drvdata *drvdata;
+ int i, j;
int ret;

drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
@@ -340,6 +478,13 @@ static int tgu_probe(struct amba_device *adev, const struct amba_id *id)

tgu_set_dev_caps(drvdata);

+ /* Initialize all condition decode registers to NOT */
+ for (i = 0; i < drvdata->num_step; i++)
+ for (j = 0; j < drvdata->num_condition_decode; j++)
+ drvdata->condition_decode[calculate_array_location(
+ drvdata, i, TGU_CONDITION_DECODE, j)] =
+ TGU_CONDITION_DECODE_NOT;
+
drvdata->enabled = false;

ret = sysfs_create_groups(&dev->kobj, tgu_attr_groups);
diff --git a/drivers/hwtracing/qcom/tgu.h b/drivers/hwtracing/qcom/tgu.h
index d769379b8421..7d88ad8e730f 100644
--- a/drivers/hwtracing/qcom/tgu.h
+++ b/drivers/hwtracing/qcom/tgu.h
@@ -16,7 +16,8 @@
((int)FIELD_GET(GENMASK(17, 10), devid_val))
#define TGU_DEVID_STEPS(devid_val) \
((int)FIELD_GET(GENMASK(6, 3), devid_val))
-
+#define TGU_DEVID_CONDITIONS(devid_val) \
+ ((int)FIELD_GET(GENMASK(2, 0), devid_val))
#define TGU_BITS_PER_SIGNAL 4
#define LENGTH_REGISTER 32

@@ -44,6 +45,7 @@
*/
#define STEP_SIZE 0x1D8
#define PRIORITY0_OFFSET 0x0074
+#define CONDITION_DECODE_OFFSET 0x0050
#define PRIORITY_STRIDE 0x0060
#define REG_SIZE 0x4

@@ -54,6 +56,7 @@
*
* TGU_MAX_STEPS matches the static step0..step7 sysfs tables.
* TGU_MAX_PRIORITY_REGS matches the static reg0..reg23 sysfs tables.
+ * TGU_MAX_CONDITION_DECODE decode region 0x50..0x5F holds 4 u32 registers
*
* Each priority level's block spans a full PRIORITY_STRIDE (0x60 bytes),
* i.e. PRIORITY_STRIDE / REG_SIZE = 24 registers, all of them implemented.
@@ -61,15 +64,40 @@

#define TGU_MAX_STEPS 8
#define TGU_MAX_PRIORITY_REGS 24
+#define TGU_MAX_CONDITION_DECODE 4

/* Maximum priority that TGU supports */
#define MAX_PRIORITY 4

+/*
+ * Values for struct tgu_attribute.operation_index: TGU_PRIORITY0..
+ * TGU_PRIORITY3 select a priority level, TGU_CONDITION_DECODE selects the
+ * condition decode registers instead.
+ */
+enum operation_index {
+ TGU_PRIORITY0,
+ TGU_PRIORITY1,
+ TGU_PRIORITY2,
+ TGU_PRIORITY3,
+ TGU_CONDITION_DECODE,
+};
+
/* Calculate compare step addresses */
#define PRIORITY_REG_STEP(step, priority, reg)\
(PRIORITY0_OFFSET + PRIORITY_STRIDE * priority +\
REG_SIZE * reg + STEP_SIZE * step)

+#define CONDITION_DECODE_STEP(step, decode) \
+ (CONDITION_DECODE_OFFSET + REG_SIZE * decode + STEP_SIZE * step)
+
+/*
+ * Neutral ('NOT') value for the condition decode registers. This is the
+ * driver's default so that, before userspace programs a real decode, an
+ * enabled step does not spuriously match. Used both when seeding the shadow
+ * table at probe and when clearing it via reset_tgu.
+ */
+#define TGU_CONDITION_DECODE_NOT 0x1000000
+
/**
* struct tgu_drvdata - Data structure for a TGU (Trigger Generation Unit)
* @base: Memory-mapped base address of the TGU device
@@ -79,8 +107,11 @@
* @enabled: Flag indicating whether the TGU device is enabled
* @priority: Per-step, per-priority-level, per-register values staged by
* sysfs writes and flushed to hardware by tgu_enable()
+ * @condition_decode: Per-step, per-register decode values staged by sysfs
+ * writes and flushed to hardware by tgu_enable()
* @num_reg: Number of priority registers implemented per priority level
* @num_step: Number of steps implemented
+ * @num_condition_decode: Maximum number of condition_decode
*
* This structure defines the data associated with a TGU device,
* including its base address, device pointer, the lock serialising
@@ -92,8 +123,10 @@ struct tgu_drvdata {
struct mutex lock;
bool enabled;
unsigned int priority[TGU_MAX_STEPS * MAX_PRIORITY * TGU_MAX_PRIORITY_REGS];
+ unsigned int condition_decode[TGU_MAX_STEPS * TGU_MAX_CONDITION_DECODE];
int num_reg;
int num_step;
+ int num_condition_decode;
};

#endif
--
2.34.1