* [PATCH v17 00/34] Introduce SCMI Telemetry support
@ 2026-11-24 19:48 Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 01/34] firmware: arm_scmi: Harden iterators state parsing Cristian Marussi
` (34 more replies)
0 siblings, 35 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Hi all,
[TLDR Summary]
[V17 highlights]
- addressed more Sashiko reports
- added a few more SCMI Core fixes that ease per-protocol logic:
+ added more consistency in the SCMI Core Iterators machinery
+ added centralized check (SCMI Core) for truncated reply
+ Reworked some SCM Clock behaviour to allow for truncated checks
+ SCMI protocol core initialization refactor and fixes
[V18 TODO]
- address any remaining outstanding Sashiko/reviewers issues
- address pending human reviews feedback
The upcoming SCMI v4.0 specification [0] introduces a new SCMI protocol
dedicated to System Telemetry.
In a nutshell, the SCMI Telemetry protocol allows an agent to discover at
runtime the set of Telemetry Data Events (DEs) available on a specific
platform and provides the means to configure the set of DEs that a user is
interested into, while reading them back using the collection method that
is deeemed more suitable for the usecase at hand. (...amongst the various
possible collection methods allowed by SCMI specification)
Without delving into the gory details of the whole SCMI Telemetry protocol
let's just say that the SCMI platform/server firmware advertises a number
of Telemetry Data Events, each one identified by a 32bit unique ID, and an
SCMI agent/client, like Linux, can discover them and read back at will the
associated data value in a number of ways.
Data collection is mainly intended to happen on demand via shared memory
areas exposed by the platform firmware, discovered dynamically via SCMI
Telemetry and accessed by Linux on-demand, but some DE can also be reported
via SCMI Notifications asynchronous messages or via direct dedicated
FastChannels (another kind of SCMI memory based access): all of this
underlying mechanism is anyway hidden to the user since it is mediated by
the kernel driver which will return the proper data value when queried.
Anyway, the set of well-known architected DE IDs defined by the spec is
limited to a dozen IDs, which means that the vast majority of DE IDs are
customizable per-platform: as a consequence, though, the same ID, say
'0x1234', could represent completely different things on different systems.
Precise definitions and semantic of such custom Data Event IDs are out of
the scope of the SCMI Telemetry specification and of this implementation:
they are supposed to be provided using some kind of JSON-like description
file that will have to be consumed by a userspace tool which would be
finally in charge of making sense of the set of available DEs.
IOW, in turn, this means that even though the DEs enumerated via SCMI come
with some sort of topological and qualitative description provided by the
protocol (like unit of measurements, name, topology info etc), kernel-wise
we CANNOT be completely sure of "what is what" without being fed-back some
sort of information about the DEs by the afore mentioned userspace tool.
For these reasons, currently this series does NOT attempt to register any
of these DEs with any of the usual in-kernel subsystems (like HWMON, IIO,
PERF etc), simply because we cannot be sure which DE is suitable, or even
desirable, for a given subsystem. This also means there are NO in-kernel
users of these Telemetry data events as of now.
So, while we do not exclude, for the future, to feed/register some of the
discovered DEs to/with some of the above mentioned Kernel subsystems, as
of now we have ONLY modeled a custom userspace API to make SCMI Telemetry
available to userspace tools.
With V5 we adopted a pure chardev/IOCTL ABI approach, refining the IOCTL
based interface already present with the previous FS-based ABI.
INTERFACES
For each discovered SCMI Instance a character device named tlm_<N> is
created under /dev/scmi/ subtree.
The IOCTL interface described at 'include/uapi/linux/scmi.h' is made
available to enumerate and configure Telemetry resources: Telemetry data
can be collected using a few different IOCTls, depending on the required
granularity.
Alternatively it is possible to obtain a list of the file descriptors
referencing directly the underlying SCMI Telemetry SHMTI memory areas and
implement in user space an SCMI Telemetry parser accessing directly the
SHMTI, while staying in compliance with the SCMI TDCF format.
NOTE THAT from v3 onwards the firmware interface level NOW supports ONLY
the latest SCMI v4.0 specification [0].
Based on V7.4-rc4, tested on an emulated setup and AGI HW.
This series is available also at [1].
If you still reading...any feedback welcome :P
Thanks,
Cristian
---
v15 --> v17
- rebased on v7.4-rc4
- [SYS/TLM]
+ return OPNOTSUPP on un-implemented IOCTLs instead of -ENOTTY
+ use proper IS_GROUP macros to discard GROUP request across IOCTLs
- [TLM]
+ fix leak and NULL deref on exit path
+ fix shmti_oveflow WITH LEN
+ check liveness on de_snapshot
+ remove spurious des_sample and notif structure and logic
+ add a comment on bulk read reporting on failures
+ return ENODATA when TLM off instead of -EINVAL
+ rename update_interval_ms to ename update_interval
+ use NULL not zero for pointers
+ fix liveness blkts_snapshot
+ fix tde_state_set_resp handling of INVALISD SHMTI_ID
+ refactor state_set and reject group state change
+ invalidate TDE cache also on enable
+ allow SHMTI_INVALID as bts_offs when DE does NOT use BLK_TS
+ fix err init in blkts_snapshot
+ add comment on LINE_LENGTH_BYTES upper bound
+ remove spurious return
+ removed unused node_lst
+ check TDE TS type, update from last_rate
+ check if used_qwords > avail_qwords early in tdcf_line_parse
+ add __iomem eplg annotation
+ fix shmti_overflow and make it return the line length
+ remove WARN_ON(fc_support), better dev_
+ log error on malformed line data
+ use %pa for phys_addr
+ skip LIST_SHMTI with num_shmti = 0
+ fix %pa and spell USPECIFIED
+ avoid FIELD_PREP check on size for exp
+ remove unused de.ts_rate
+ enaming discovered to enumerated
+ rework list_add to fcs_des
+ use list_del_init for free_des/fcs_des list removals
+ avoid attaching a drevres tdes_cleanup to enumeration
+ consider expected_size when reset_rx_to_max
+ add an unrelated fix to make SCMI clock attributes deterministic
v15 --> v16
- rebased on v7.4-rc3
- [ALL] removed Telemetry unused features
+ Groups/Notifications/AsyncSingleSample/Reset/Events
- [STLM]
+ handling new ABI Feature bits
- [SYS/TLM]
+ remove unsupported features from ABI features
+ clear out->flags in uapi_info
+ cap ABI INFO usize to PAGE_SIZE
+ sanitize input update interval
+ add missing pad/reserved checks in input
- [UAPI]
+ added GROUPS/SINGLE_SAMPLE abi_features flags definitions
- [TLM]
+ initialize/destroy debugFS around SHMTIs init
+ cleanup protocol dbg root after devres group closure
+ discard DE/FC at enumeration time that were found in SHMTI at boot
+ align DE local/global TS state after enumeration
+ check liveness in de_snapshot
+ add shmti_overflow check on snap
+ check liveness on blkts_snapshot too
+ refatcor blkts_update on DE enable and update de.ts_rate even
when BLK_TS line is the same
+ on disable do NOT assume SHMTI_ID_INVALID means disable
+ fix remote_update_needed logic
+ invalidate cache on DE disable
+ invalidate base/offset on disable if NOT FC
+ use new shmti_overflow with eplg directly
+ WARN_ON(BLK_TS_INVALID) but keep active_bts valid
+ invalidate TDE cache on blkts_unlin
+ cleanup stale lines on shmti cleanup
+ add more iterators consistency checks
+ added more checks in the SCMI Core Iterators machinery
+ added centralized check for truncated replies
+ droppepd unused support for non devres protocol acquire/release
operations
v14 --> v15
- rebased on v7.4-rc2
- [STLM]
+ add lazy enumeration
+ add des/info cmdline options
+ add missing SIGTERM handling
- [SYS/TLM]
+ harden/simplify batch helpers
+ fix shmti mmap size check to include offset
+ simplify session_unsubscribe cleanup on exit/unbind
+ added missing kref.h include
+ expose TLM EVENTS capabilities ONLY IF CONFIG_EVENTFD
+ mmap: improved check to avoid WRITE/EXEC mapping
+ reject non zero vm_pgoff
+ drop usage of guard(srcu) to avoid races with put_device
- [TLM]
+ avoid genration counter reset on TLM RESET
+ add an overflow check on NOTIF payload lines parsing
+ convert to Linux errno before building notification report
+ use better startm/endm initial markers in tdcf_de_payld_get
+ trigger resource enumeration also on de_data_read
+ skip unitialized FCs on reads and return -EPROTO
+ check FC size on enumeration (big enough to hold TS ?)
+ introduce localized single DE snapshot on single DE data read
+ widen tde->mtx locking in msg_payld_process
+ refactor msg_payld_process
+ unlink BLK_TS on DE disable
+ update linked BLK_TS on enable
+ update TDE location ONLY after successfull linked lines processing
+ trigger resource enumeration on state_get
+ trigger resource enumeration on colletion configure only for groups
+ widen tde->mtx locking section on state_set
+ refactor UUID/BLK_TS lines processing in enable
+ introduce BLK_TS localized snapshot during enable
+ check line type at de_offs
+ sanitize requested mode on collection conf
+ use snapshots to collect UUIDs during full scans
+ stop tracking UUIDs lines singularly (useless overhead)
+ use UUIDs line hashing for lookup
+ use snapshots to track and collect BLK_TS during full scan bulk reads
+ account for tstamped TDE during discovery
+ correct SHMTI overflow off-by-one
+ rework TDE cache hit/miss logic
+ introduce snapshots on full scan reads
+ validate DE location also on SCAN_UPDATE
+ fix TDCF prologue/epilogue endianity struct definitions
+ rework SHMTI initialization and allocation
+ add snapshot suppport data structures
+ added alignment checks on SHMTII length size
+ fixed error string formatting
+ warn if less than expected SHMTIs are enumerated
+ improve memory area sanity checks
+ added missing initialization of intrvs->grp_id
+ bailout from group initialization when NO DEs were found
+ better handling of error path on groups init
+ drop usage of devres for FC iomapping
+ trigger full enumeration of DE lookups
+ new IS_TDE_TS_ macro helpers
+ add a dedicated scmi_telemetry_free_tdes_add helper
+ dropping needless __must_check
+ flag as fully enumerated only when ALL declared des were found
+ reworked error path in scmi_allocate_pending_event_handler
+ hardened core iterator parsing of multi-part reply
[0]: https://developer.arm.com/documentation/den0056/f/?lang=en
[1]: https://git.kernel.org/pub/scm/linux/kernel/git/cris/linux.git/log/?h=scmi_telemetry_ng_V17
Cristian Marussi (33):
firmware: arm_scmi: Harden iterators state parsing
[RFC] firmware: arm_scmi: Additional iterators consistency checks
firmware: arm_scmi: Make Clock attributes size deterministic
firmware: arm_scmi: Add centralized detection of truncated replies
firmware: arm_scmi: Introduce a dedicated SCMI Errors header
firmware: arm_scmi: Add new SCMIv4.0 error codes definitions
firmware: arm_scmi: Allow registration of unknown-size events/reports
firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper
dt-bindings: firmware: arm,scmi: Add support for telemetry protocol
include: trace: Add Telemetry trace events
firmware: arm_scmi: Add Telemetry basic attributes enumeration
firmware: arm_scmi: Add Telemetry resources enumeration
firmware: arm_scmi: Add Telemetry global update intervals enumeration
firmware: arm_scmi: Add Telemetry SHMTI areas enumeration
firmware: arm_scmi: Add Telemetry TDCF areas parsing support
firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines
firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines
firmware: arm_scmi: Add Telemetry DEs configuration support
firmware: arm_scmi: Add Telemetry global configuration support
firmware: arm_scmi: Add Telemetry Single/Bulk DEs reads
firmware: arm_scmi: Add Telemetry boot-on support
firmware: arm_scmi: Invoke proper protocol cleanup on failure
firmware: arm_scmi: Refactor protocol allocation
firmware: arm_scmi: Add common per-protocol debugfs support
firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support
firmware: arm_scmi: Add Telemetry debugfs ABI documentation
firmware: arm_scmi: Expose per-instance identifier
uapi: Add ARM SCMI Telemetry definitions
firmware: arm_scmi: Add System Telemetry driver
firmware: arm_scmi: Enable autoloading for System Telemetry driver
docs: ioctl-number: Add SCMI Ioctls
[RFC] Documentation: Add SCMI System Telemetry documentation
[RFC] tools/testing/scmi: Add SCMI Telemetry testing tool
Philip Radford (1):
kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases
Documentation/ABI/testing/debugfs-scmi | 22 +
.../bindings/firmware/arm,scmi.yaml | 8 +
Documentation/userspace-api/index.rst | 1 +
.../userspace-api/ioctl/ioctl-number.rst | 1 +
Documentation/userspace-api/stlm.rst | 590 +++
MAINTAINERS | 2 +
drivers/firmware/arm_scmi/Kconfig | 24 +
drivers/firmware/arm_scmi/Makefile | 3 +-
drivers/firmware/arm_scmi/bus.c | 1 +
drivers/firmware/arm_scmi/clock.c | 12 +-
drivers/firmware/arm_scmi/common.h | 41 +-
drivers/firmware/arm_scmi/driver.c | 158 +-
drivers/firmware/arm_scmi/errors.h | 56 +
drivers/firmware/arm_scmi/notify.c | 102 +-
drivers/firmware/arm_scmi/notify.h | 8 +-
drivers/firmware/arm_scmi/protocols.h | 22 +
drivers/firmware/arm_scmi/raw_mode.c | 1 +
.../firmware/arm_scmi/scmi_system_telemetry.c | 1376 ++++++
drivers/firmware/arm_scmi/telemetry.c | 4352 +++++++++++++++++
include/linux/scmi_protocol.h | 200 +-
include/trace/events/scmi.h | 45 +
include/uapi/linux/scmi.h | 533 ++
tools/testing/scmi/Makefile | 25 +
tools/testing/scmi/stlm.c | 1698 +++++++
tools/testing/scmi/stlm_decode.c | 252 +
tools/testing/scmi/stlm_decode.h | 7 +
tools/testing/selftests/arm64/Makefile | 2 +-
tools/testing/selftests/arm64/scmi/Makefile | 12 +
tools/testing/selftests/arm64/scmi/config | 2 +
.../arm64/scmi/test_scmi_telemetry_uapi.c | 2034 ++++++++
30 files changed, 11477 insertions(+), 113 deletions(-)
create mode 100644 Documentation/userspace-api/stlm.rst
create mode 100644 drivers/firmware/arm_scmi/errors.h
create mode 100644 drivers/firmware/arm_scmi/scmi_system_telemetry.c
create mode 100644 drivers/firmware/arm_scmi/telemetry.c
create mode 100644 include/uapi/linux/scmi.h
create mode 100644 tools/testing/scmi/Makefile
create mode 100644 tools/testing/scmi/stlm.c
create mode 100644 tools/testing/scmi/stlm_decode.c
create mode 100644 tools/testing/scmi/stlm_decode.h
create mode 100644 tools/testing/selftests/arm64/scmi/Makefile
create mode 100644 tools/testing/selftests/arm64/scmi/config
create mode 100644 tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c
--
2.54.0
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v17 01/34] firmware: arm_scmi: Harden iterators state parsing
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 22:54 ` Jonathan Cameron
2026-11-24 19:48 ` [PATCH v17 02/34] [RFC] firmware: arm_scmi: Additional iterators consistency checks Cristian Marussi
` (33 subsequent siblings)
34 siblings, 1 reply; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add checks in the core multi-part iterators support to detect generally
malformed and inconsistent multi-part replies.
Fixes: 36b6ea0fc6bc ("firmware: arm_scmi: Add iterators for multi-part commands")
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
drivers/firmware/arm_scmi/driver.c | 10 ++++++++++
drivers/firmware/arm_scmi/protocols.h | 7 +++++++
2 files changed, 17 insertions(+)
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 05092301db29..55d9cc487916 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -1873,11 +1873,21 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
if (ret)
return ret;
+ if (i->t->rx.len < SCMI_MULTIPART_REPLY_MIN_SZ) {
+ dev_err(ph->dev, "Invalid multi-part reply.\n");
+ return -EINVAL;
+ }
+
st->rx_len = i->t->rx.len;
ret = iops->update_state(st, i->resp, i->priv);
if (ret)
return ret;
+ if (!st->num_returned && st->num_remaining) {
+ dev_err(ph->dev, "Inconsistent multi-part reply.\n");
+ return -EINVAL;
+ }
+
if (st->num_returned > st->max_resources - st->desc_index) {
dev_err(ph->dev,
"No. of resources can't exceed %d\n",
diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_scmi/protocols.h
index 8583159059e6..c15ec188291a 100644
--- a/drivers/firmware/arm_scmi/protocols.h
+++ b/drivers/firmware/arm_scmi/protocols.h
@@ -33,6 +33,13 @@
#define MSG_SUPPORTS_FASTCHANNEL(x) ((x) & BIT(0))
+/*
+ * Common minimum size required by all the existing standard and vendor
+ * protocols to hold number of returned and remaining items in a multi-part
+ * reply.
+ */
+#define SCMI_MULTIPART_REPLY_MIN_SZ (sizeof(u32))
+
enum scmi_common_cmd {
PROTOCOL_VERSION = 0x0,
PROTOCOL_ATTRIBUTES = 0x1,
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 02/34] [RFC] firmware: arm_scmi: Additional iterators consistency checks
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 01/34] firmware: arm_scmi: Harden iterators state parsing Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 23:08 ` Jonathan Cameron
2026-11-24 19:48 ` [PATCH v17 03/34] firmware: arm_scmi: Make Clock attributes size deterministic Cristian Marussi
` (32 subsequent siblings)
34 siblings, 1 reply; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add a few more consistency checks around the number of returned and
remaining items during the life of an iteration: this is useful to spot
prematurely terminated iterations that would produce shorter than expected
results.
Handling these anomalies in the platform multi-part replies within the
core SCMI iterators machinery avoids the need to address such issues
multiple times at the protocol layer.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
RFC because this is another fix that is potentially disrupting for any
non compliant SCMI platform deployed in the wild.
---
drivers/firmware/arm_scmi/driver.c | 36 ++++++++++++++++++++++++++----
1 file changed, 32 insertions(+), 4 deletions(-)
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 55d9cc487916..6dd249c08f5f 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -1852,7 +1852,7 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
const struct scmi_protocol_handle *ph;
struct scmi_iterator_state *st;
struct scmi_iterator *i;
- unsigned int n;
+ unsigned int n, i_start, i_max, i_returned;
if (!iter)
return -EINVAL;
@@ -1862,10 +1862,15 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
ph = i->ph;
st = &i->state;
+ /* Initialize iteration counters used to check consistency */
+ i_max = 0;
+ i_returned = 0;
+ i_start = start ? *start : 0;
+
/* Reinitialize state for next run */
st->num_returned = 0;
st->num_remaining = 0;
- st->desc_index = start ? *start : 0;
+ st->desc_index = i_start;
do {
iops->prepare_message(i->msg, st->desc_index, i->priv);
@@ -1883,11 +1888,34 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
if (ret)
return ret;
- if (!st->num_returned && st->num_remaining) {
- dev_err(ph->dev, "Inconsistent multi-part reply.\n");
+ /*
+ * Initialize max items expectation for this run based on
+ * values returned on first message exchange: during the
+ * following iterations consistency will be validated against
+ * these parameters, so as to spot FW anomalies like sudden
+ * termination or unplausible conditions.
+ */
+ if (!i_max)
+ i_max = i_start + st->num_returned + st->num_remaining;
+
+ if ((!st->num_returned && st->num_remaining) ||
+ (i_max != i_start + i_returned + st->num_returned + st->num_remaining)) {
+ dev_err(ph->dev,
+ "Inconsistent multi-part reply. max:%u i_max:%u i_start:%u i_returned:%u returned:%u remaining:%u desc_index:%u\n",
+ st->max_resources, i_max, i_start, i_returned,
+ st->num_returned, st->num_remaining,
+ st->desc_index);
return -EINVAL;
}
+ dev_dbg(ph->dev,
+ "Multi-part - max:%u i_max:%u i_start:%u i_returned:%u returned:%u remaining:%u desc_index:%u\n",
+ st->max_resources, i_max, i_start, i_returned,
+ st->num_returned, st->num_remaining, st->desc_index);
+
+ /* Account for last batch received */
+ i_returned += st->num_returned;
+
if (st->num_returned > st->max_resources - st->desc_index) {
dev_err(ph->dev,
"No. of resources can't exceed %d\n",
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 03/34] firmware: arm_scmi: Make Clock attributes size deterministic
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 01/34] firmware: arm_scmi: Harden iterators state parsing Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 02/34] [RFC] firmware: arm_scmi: Additional iterators consistency checks Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 04/34] firmware: arm_scmi: Add centralized detection of truncated replies Cristian Marussi
` (31 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
SCMI Clock protocol CLOCK_ATTRIBUTES command varied in its reply payload
size together with the specification evolution.
Between Clock version 0x10000 and 0x20000 one trailing field was added.
The current implementation used the maximum possible payload size across
any protocol version as the rx_size paramenter passed to xfer_get_init:
such solution works fine but inhibits any possible centralized check on
the size of the received reply.
Size the requested reply strictly according to the expected protocol
version.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
drivers/firmware/arm_scmi/clock.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/drivers/firmware/arm_scmi/clock.c b/drivers/firmware/arm_scmi/clock.c
index 6b5d50b35a48..ed93970c3b87 100644
--- a/drivers/firmware/arm_scmi/clock.c
+++ b/drivers/firmware/arm_scmi/clock.c
@@ -362,10 +362,14 @@ static int scmi_clock_attributes_get(const struct scmi_protocol_handle *ph,
u32 attributes;
struct scmi_xfer *t;
struct scmi_msg_resp_clock_attributes *attr;
+ size_t rx_size = sizeof(*attr);
struct scmi_clock_info *clk = CLOCK_INFO(cinfo, clk_id);
+ if (PROTOCOL_REV_MAJOR(ph->version) < 0x2)
+ rx_size -= sizeof(attr->clock_enable_latency);
+
ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES,
- sizeof(clk_id), sizeof(*attr), &t);
+ sizeof(clk_id), rx_size, &t);
if (ret)
return ret;
@@ -1002,12 +1006,16 @@ scmi_clock_config_get(const struct scmi_protocol_handle *ph, u32 clk_id,
int ret;
struct scmi_xfer *t;
struct scmi_msg_resp_clock_attributes *resp;
+ size_t rx_size = sizeof(*resp);
if (!enabled)
return -EINVAL;
+ if (PROTOCOL_REV_MAJOR(ph->version) < 0x2)
+ rx_size -= sizeof(resp->clock_enable_latency);
+
ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES,
- sizeof(clk_id), sizeof(*resp), &t);
+ sizeof(clk_id), rx_size, &t);
if (ret)
return ret;
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 04/34] firmware: arm_scmi: Add centralized detection of truncated replies
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (2 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 03/34] firmware: arm_scmi: Make Clock attributes size deterministic Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 05/34] firmware: arm_scmi: Introduce a dedicated SCMI Errors header Cristian Marussi
` (30 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
When an xfer is allocated with a very well known fixed reply size by
providing a non-zero rx_size parameter to xfer_get_init(), it becomes
possible to verify if the effectively received reply was truncated.
Add the logic to detect and report truncated replies.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- use %zu
- consider expected_size when reset_rx_to_max
---
drivers/firmware/arm_scmi/driver.c | 14 +++++++++++++-
drivers/firmware/arm_scmi/protocols.h | 8 ++++++++
drivers/firmware/arm_scmi/raw_mode.c | 1 +
3 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 6dd249c08f5f..35adee6a7a15 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -1356,6 +1356,16 @@ static int scmi_wait_for_reply(struct device *dev, const struct scmi_desc *desc,
}
}
+ /* Check for truncated messages..it will be logged by the caller */
+ if (!ret && !xfer->hdr.status && xfer->expected_rx_len &&
+ xfer->expected_rx_len != xfer->rx.len) {
+ dev_dbg(dev,
+ "Truncated reply - HDR:%08X expected:%zu received:%zu\n",
+ pack_scmi_header(&xfer->hdr), xfer->expected_rx_len,
+ xfer->rx.len);
+ xfer->hdr.status = SCMI_ERR_COMMS;
+ }
+
return ret;
}
@@ -1508,7 +1518,7 @@ static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
const struct scmi_protocol_instance *pi = ph_to_pi(ph);
struct scmi_info *info = handle_to_scmi_info(pi->handle);
- xfer->rx.len = info->desc->max_msg_size;
+ xfer->rx.len = xfer->expected_rx_len ?: info->desc->max_msg_size;
}
/**
@@ -1616,6 +1626,8 @@ static int xfer_get_init(const struct scmi_protocol_handle *ph,
xfer->tx.len = tx_size;
xfer->rx.len = rx_size ? : info->desc->max_msg_size;
+ /* When a specific rx_size was specified, check for truncation */
+ xfer->expected_rx_len = rx_size ? : 0;
xfer->hdr.type = MSG_TYPE_COMMAND;
xfer->hdr.id = msg_id;
xfer->hdr.poll_completion = false;
diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_scmi/protocols.h
index c15ec188291a..50b89c56e309 100644
--- a/drivers/firmware/arm_scmi/protocols.h
+++ b/drivers/firmware/arm_scmi/protocols.h
@@ -106,6 +106,13 @@ struct scmi_msg_hdr {
* @rx: Receive message, the buffer should be pre-allocated to store
* message. If request-ACK protocol is used, we can reuse the same
* buffer for the rx path as we use for the tx path.
+ * @expected_rx_len: The expected length of the payload in the reply, excluding
+ * the status field. When set to a non-zero value this is used
+ * to evaluate if a reply had been possibly truncated due to a
+ * misconfigured transport: it is only set to some meaningful
+ * non-zero value for replies having a very well known fixed
+ * size, definitely not for variable-sized xfers with an RX
+ * len initialized to max_msg_size.
* @done: command message transmit completion event
* @async_done: pointer to delayed response message received event completion,
* or NULL when no delayed response is expected. Protected by
@@ -139,6 +146,7 @@ struct scmi_xfer {
struct scmi_msg_hdr hdr;
struct scmi_msg tx;
struct scmi_msg rx;
+ size_t expected_rx_len;
struct completion done;
struct completion *async_done;
bool pending;
diff --git a/drivers/firmware/arm_scmi/raw_mode.c b/drivers/firmware/arm_scmi/raw_mode.c
index 7e42dd6cc2af..45206edff446 100644
--- a/drivers/firmware/arm_scmi/raw_mode.c
+++ b/drivers/firmware/arm_scmi/raw_mode.c
@@ -574,6 +574,7 @@ static int scmi_xfer_raw_get_init(struct scmi_raw_mode_info *raw, void *buf,
xfer->hdr.status = SCMI_SUCCESS;
xfer->tx.len = tx_size;
xfer->rx.len = raw->desc->max_msg_size;
+ xfer->expected_rx_len = 0;
/* Clear the whole TX buffer */
memset(xfer->tx.buf, 0x00, raw->desc->max_msg_size);
if (xfer->tx.len)
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 05/34] firmware: arm_scmi: Introduce a dedicated SCMI Errors header
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (3 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 04/34] firmware: arm_scmi: Add centralized detection of truncated replies Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 06/34] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
` (29 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add a new dedicated header and move all of the errors definitions and
mapping helpers into it and out of common.h so that SCMI errors definitions
alone can be easily included in protocol files without all of the unrelated
additional stuff contained in common.h
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
drivers/firmware/arm_scmi/common.h | 39 +---------------------
drivers/firmware/arm_scmi/errors.h | 52 ++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 38 deletions(-)
create mode 100644 drivers/firmware/arm_scmi/errors.h
diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi/common.h
index af5b025ee072..cdd6f887fae7 100644
--- a/drivers/firmware/arm_scmi/common.h
+++ b/drivers/firmware/arm_scmi/common.h
@@ -26,6 +26,7 @@
#include <linux/unaligned.h>
+#include "errors.h"
#include "protocols.h"
#include "notify.h"
@@ -37,44 +38,6 @@
#define SCMI_TRANSPORT_DEVNAME_PREFIX "__scmi_transport_device"
-enum scmi_error_codes {
- SCMI_SUCCESS = 0, /* Success */
- SCMI_ERR_SUPPORT = -1, /* Not supported */
- SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
- SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
- SCMI_ERR_ENTRY = -4, /* Not found */
- SCMI_ERR_RANGE = -5, /* Value out of range */
- SCMI_ERR_BUSY = -6, /* Device busy */
- SCMI_ERR_COMMS = -7, /* Communication Error */
- SCMI_ERR_GENERIC = -8, /* Generic Error */
- SCMI_ERR_HARDWARE = -9, /* Hardware Error */
- SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
-};
-
-static const int scmi_linux_errmap[] = {
- /* better than switch case as long as return value is continuous */
- 0, /* SCMI_SUCCESS */
- -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
- -EINVAL, /* SCMI_ERR_PARAM */
- -EACCES, /* SCMI_ERR_ACCESS */
- -ENOENT, /* SCMI_ERR_ENTRY */
- -ERANGE, /* SCMI_ERR_RANGE */
- -EBUSY, /* SCMI_ERR_BUSY */
- -ECOMM, /* SCMI_ERR_COMMS */
- -EIO, /* SCMI_ERR_GENERIC */
- -EREMOTEIO, /* SCMI_ERR_HARDWARE */
- -EPROTO, /* SCMI_ERR_PROTOCOL */
-};
-
-static inline int scmi_to_linux_errno(int errno)
-{
- int err_idx = -errno;
-
- if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
- return scmi_linux_errmap[err_idx];
- return -EIO;
-}
-
#define MSG_ID_MASK GENMASK(7, 0)
#define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
#define MSG_TYPE_MASK GENMASK(9, 8)
diff --git a/drivers/firmware/arm_scmi/errors.h b/drivers/firmware/arm_scmi/errors.h
new file mode 100644
index 000000000000..e1b265d5c148
--- /dev/null
+++ b/drivers/firmware/arm_scmi/errors.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * System Control and Management Interface (SCMI) Message Protocol
+ * header containing SCMI errors definitions and Linux mapping.
+ *
+ * Copyright (C) 2018-2026 ARM Ltd.
+ */
+#ifndef _SCMI_ERRORS_H
+#define _SCMI_ERRORS_H
+
+#include <linux/errno.h>
+#include <linux/array_size.h>
+
+enum scmi_error_codes {
+ SCMI_SUCCESS = 0, /* Success */
+ SCMI_ERR_SUPPORT = -1, /* Not supported */
+ SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
+ SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
+ SCMI_ERR_ENTRY = -4, /* Not found */
+ SCMI_ERR_RANGE = -5, /* Value out of range */
+ SCMI_ERR_BUSY = -6, /* Device busy */
+ SCMI_ERR_COMMS = -7, /* Communication Error */
+ SCMI_ERR_GENERIC = -8, /* Generic Error */
+ SCMI_ERR_HARDWARE = -9, /* Hardware Error */
+ SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
+};
+
+static const int scmi_linux_errmap[] = {
+ /* better than switch case as long as return value is continuous */
+ 0, /* SCMI_SUCCESS */
+ -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
+ -EINVAL, /* SCMI_ERR_PARAM */
+ -EACCES, /* SCMI_ERR_ACCESS */
+ -ENOENT, /* SCMI_ERR_ENTRY */
+ -ERANGE, /* SCMI_ERR_RANGE */
+ -EBUSY, /* SCMI_ERR_BUSY */
+ -ECOMM, /* SCMI_ERR_COMMS */
+ -EIO, /* SCMI_ERR_GENERIC */
+ -EREMOTEIO, /* SCMI_ERR_HARDWARE */
+ -EPROTO, /* SCMI_ERR_PROTOCOL */
+};
+
+static inline int scmi_to_linux_errno(int errno)
+{
+ int err_idx = -errno;
+
+ if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
+ return scmi_linux_errmap[err_idx];
+ return -EIO;
+}
+
+#endif /* _SCMI_ERRORS_H */
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 06/34] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (4 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 05/34] firmware: arm_scmi: Introduce a dedicated SCMI Errors header Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 07/34] firmware: arm_scmi: Allow registration of unknown-size events/reports Cristian Marussi
` (28 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
SCMIv4.0 introduces a couple of new possible protocol error codes: add the
needed definitions and mappings to Linux error values.
Note that SCMI_PARTIAL_ERROR has to be handled specially and as such it
needs to be distinguishable but, as of now, all of the available sensible
error codes are already mapped to some other pre-existent SCMI error
condition, so we settled on -EBADMSG which is far from ideal, but at least
kind of captures the nature of the underlying issue.
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v12 --> v13
- use EBADMSG to map SCMI_PARTIAL_ERROR
v4 --> v5
- map SCMI_ERR_IN_USE to EBUSY as natural
---
drivers/firmware/arm_scmi/errors.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/firmware/arm_scmi/errors.h b/drivers/firmware/arm_scmi/errors.h
index e1b265d5c148..707761cd8d34 100644
--- a/drivers/firmware/arm_scmi/errors.h
+++ b/drivers/firmware/arm_scmi/errors.h
@@ -23,6 +23,8 @@ enum scmi_error_codes {
SCMI_ERR_GENERIC = -8, /* Generic Error */
SCMI_ERR_HARDWARE = -9, /* Hardware Error */
SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
+ SCMI_ERR_IN_USE = -11, /* In Use Error */
+ SCMI_ERR_PARTIAL = -12, /* Partial Error */
};
static const int scmi_linux_errmap[] = {
@@ -38,6 +40,8 @@ static const int scmi_linux_errmap[] = {
-EIO, /* SCMI_ERR_GENERIC */
-EREMOTEIO, /* SCMI_ERR_HARDWARE */
-EPROTO, /* SCMI_ERR_PROTOCOL */
+ -EBUSY, /* SCMI_ERR_IN_USE */
+ -EBADMSG, /* SCMI_ERR_PARTIAL */
};
static inline int scmi_to_linux_errno(int errno)
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 07/34] firmware: arm_scmi: Allow registration of unknown-size events/reports
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (5 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 06/34] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 08/34] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Cristian Marussi
` (27 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Allow protocols to register events with build-time unknown sizes: such
events can be declared zero-sized and let the core SCMI stack perform the
needed safe-net boundary checks based on the configured transport size.
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v14 --> v15
- better commenting
v12 --> v13
- improve variable naming
v10 --> v11
- account for ktime_t field when checking boundary
v2 --> v3
- split out of previous patch on protocol notifier
- use max() instead of max_t()
---
drivers/firmware/arm_scmi/notify.c | 28 +++++++++++++++++++++++-----
drivers/firmware/arm_scmi/notify.h | 8 ++++++--
2 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c
index bc046f2f83a6..a30589c9b377 100644
--- a/drivers/firmware/arm_scmi/notify.c
+++ b/drivers/firmware/arm_scmi/notify.c
@@ -595,7 +595,16 @@ int scmi_notify(const struct scmi_handle *handle, u8 proto_id, u8 evt_id,
if (!r_evt)
return -EINVAL;
- if (len > r_evt->evt->max_payld_sz) {
+ /*
+ * Events with a zero max_payld_sz are sized to be of the maximum
+ * size allowed by the transport: no need to be size-checked here
+ * since the transport layer would have already dropped such
+ * over-sized messages.
+ * Note that, though, while this protects from buffer overruns, it is
+ * no more possible at this layer of the stack to detect zero-sized
+ * events corrupted by truncation.
+ */
+ if (r_evt->evt->max_payld_sz && len > r_evt->evt->max_payld_sz) {
dev_err(handle->dev, "discard badly sized message\n");
return -EINVAL;
}
@@ -751,7 +760,7 @@ int scmi_register_protocol_events(const struct scmi_handle *handle, u8 proto_id,
const struct scmi_protocol_handle *ph,
const struct scmi_protocol_events *ee)
{
- int i;
+ int i, max_msg_sz, default_report_sz;
unsigned int num_sources;
size_t payld_sz = 0;
struct scmi_registered_events_desc *pd;
@@ -766,6 +775,9 @@ int scmi_register_protocol_events(const struct scmi_handle *handle, u8 proto_id,
if (!ni)
return -ENOMEM;
+ max_msg_sz = ph->hops->get_max_msg_size(ph);
+ default_report_sz = max_msg_sz + sizeof(ktime_t);
+
/* num_sources cannot be <= 0 */
if (ee->num_sources) {
num_sources = ee->num_sources;
@@ -778,8 +790,13 @@ int scmi_register_protocol_events(const struct scmi_handle *handle, u8 proto_id,
}
evt = ee->evts;
- for (i = 0; i < ee->num_events; i++)
- payld_sz = max_t(size_t, payld_sz, evt[i].max_payld_sz);
+ for (i = 0; i < ee->num_events; i++) {
+ if (evt[i].max_payld_sz == 0) {
+ payld_sz = max_msg_sz;
+ break;
+ }
+ payld_sz = max(payld_sz, evt[i].max_payld_sz);
+ }
payld_sz += sizeof(struct scmi_event_header);
pd = scmi_allocate_registered_events_desc(ni, proto_id, ee->queue_sz,
@@ -805,7 +822,8 @@ int scmi_register_protocol_events(const struct scmi_handle *handle, u8 proto_id,
mutex_init(&r_evt->sources_mtx);
r_evt->report = devm_kzalloc(ni->handle->dev,
- evt->max_report_sz, GFP_KERNEL);
+ evt->max_report_sz ?: default_report_sz,
+ GFP_KERNEL);
if (!r_evt->report)
return -ENOMEM;
diff --git a/drivers/firmware/arm_scmi/notify.h b/drivers/firmware/arm_scmi/notify.h
index f18f98c5ab3b..ff4044fd7448 100644
--- a/drivers/firmware/arm_scmi/notify.h
+++ b/drivers/firmware/arm_scmi/notify.h
@@ -18,8 +18,12 @@
/**
* struct scmi_event - Describes an event to be supported
* @id: Event ID
- * @max_payld_sz: Max possible size for the payload of a notification message
- * @max_report_sz: Max possible size for the report of a notification message
+ * @max_payld_sz: Max possible size for the payload of a notification message.
+ * Set to zero to use the maximum message size allowed by the
+ * transport.
+ * @max_report_sz: Max possible size for the report of a notification message.
+ * Set to zero to use the maximum message size allowed by the
+ * transport plus the additional ktime_t timestamp.
*
* Each SCMI protocol, during its initialization phase, can describe the events
* it wishes to support in a few struct scmi_event and pass them to the core
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 08/34] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (6 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 07/34] firmware: arm_scmi: Allow registration of unknown-size events/reports Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 09/34] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Cristian Marussi
` (26 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Refactor the __scmi_event_handler_get_ops notification helper to simplify
its logic and make use of guards for locking.
Event handler allocation is split out of registration in a dedicated helper
so as to allow for future reuse.
Mostly no functional change, in the sense that while the functionality
remained the same, a richer set of errors is returned where before was
always a -EINVAL.
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v14 --> v15
- reworked error path in scmi_allocate_pending_event_handler
- improve commit message
---
drivers/firmware/arm_scmi/notify.c | 74 +++++++++++++++++++-----------
1 file changed, 47 insertions(+), 27 deletions(-)
diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c
index a30589c9b377..fa2339209a69 100644
--- a/drivers/firmware/arm_scmi/notify.c
+++ b/drivers/firmware/arm_scmi/notify.c
@@ -891,7 +891,6 @@ void scmi_deregister_protocol_events(const struct scmi_handle *handle,
/**
* scmi_allocate_event_handler() - Allocate Event handler
- * @ni: A reference to the notification instance to use
* @evt_key: 32bit key uniquely bind to the event identified by the tuple
* (proto_id, evt_id, src_id)
*
@@ -901,21 +900,33 @@ void scmi_deregister_protocol_events(const struct scmi_handle *handle,
* associated to this handler descriptor (hndl->r_evt == NULL), so the handler
* is initialized as pending.
*
- * Context: Assumes to be called with @pending_mtx already acquired.
* Return: the freshly allocated structure on Success
*/
-static struct scmi_event_handler *
-scmi_allocate_event_handler(struct scmi_notify_instance *ni, u32 evt_key)
+static struct scmi_event_handler *scmi_allocate_event_handler(u32 evt_key)
{
struct scmi_event_handler *hndl;
hndl = kzalloc_obj(*hndl);
if (!hndl)
return NULL;
+
hndl->key = evt_key;
BLOCKING_INIT_NOTIFIER_HEAD(&hndl->chain);
refcount_set(&hndl->users, 1);
- /* New handlers are created pending */
+
+ return hndl;
+}
+
+static struct scmi_event_handler *
+scmi_allocate_pending_event_handler(struct scmi_notify_instance *ni, u32 evt_key)
+{
+ struct scmi_event_handler *hndl;
+
+ lockdep_assert_held(&ni->pending_mtx);
+ hndl = scmi_allocate_event_handler(evt_key);
+ if (!hndl)
+ return NULL;
+
hash_add(ni->pending_events_handlers, &hndl->hash, hndl->key);
return hndl;
@@ -1085,7 +1096,8 @@ __scmi_event_handler_get_ops(struct scmi_notify_instance *ni,
u32 evt_key, bool create)
{
struct scmi_registered_event *r_evt;
- struct scmi_event_handler *hndl = NULL;
+ struct scmi_event_handler *hndl;
+ int ret;
r_evt = SCMI_GET_REVT(ni, KEY_XTRACT_PROTO_ID(evt_key),
KEY_XTRACT_EVT_ID(evt_key));
@@ -1093,39 +1105,47 @@ __scmi_event_handler_get_ops(struct scmi_notify_instance *ni,
if (r_evt && r_evt->not_supported_by_platform)
return ERR_PTR(-EOPNOTSUPP);
- mutex_lock(&ni->pending_mtx);
+ guard(mutex)(&ni->pending_mtx);
/* Search registered events at first ... if possible at all */
if (r_evt) {
- mutex_lock(&r_evt->proto->registered_mtx);
+ guard(mutex)(&r_evt->proto->registered_mtx);
hndl = KEY_FIND(r_evt->proto->registered_events_handlers,
hndl, evt_key);
- if (hndl)
+ if (hndl) {
refcount_inc(&hndl->users);
- mutex_unlock(&r_evt->proto->registered_mtx);
+ return hndl;
+ }
}
/* ...then amongst pending. */
- if (!hndl) {
- hndl = KEY_FIND(ni->pending_events_handlers, hndl, evt_key);
- if (hndl)
- refcount_inc(&hndl->users);
+ hndl = KEY_FIND(ni->pending_events_handlers, hndl, evt_key);
+ if (hndl) {
+ refcount_inc(&hndl->users);
+ return hndl;
}
- /* Create if still not found and required */
- if (!hndl && create) {
- hndl = scmi_allocate_event_handler(ni, evt_key);
- if (hndl && scmi_register_event_handler(ni, hndl)) {
- dev_dbg(ni->handle->dev,
- "purging UNKNOWN handler - key:%X\n",
- hndl->key);
- /* this hndl can be only a pending one */
- scmi_put_handler_unlocked(ni, hndl);
- hndl = ERR_PTR(-EINVAL);
- }
+ if (!create)
+ return ERR_PTR(-ENODEV);
+
+ /*
+ * Create if still not found and required, new handlers are
+ * created pending and transitioned to registered when bound to
+ * an event.
+ */
+ hndl = scmi_allocate_pending_event_handler(ni, evt_key);
+ if (!hndl)
+ return ERR_PTR(-ENOMEM);
+
+ ret = scmi_register_event_handler(ni, hndl);
+ if (ret) {
+ dev_dbg(ni->handle->dev,
+ "purging UNKNOWN handler - key:%X\n", hndl->key);
+ /* this hndl can be only a pending one */
+ scmi_put_handler_unlocked(ni, hndl);
+ return ERR_PTR(ret);
}
- mutex_unlock(&ni->pending_mtx);
- return hndl ?: ERR_PTR(-ENODEV);
+ return hndl;
}
static struct scmi_event_handler *
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 09/34] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (7 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 08/34] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 10/34] include: trace: Add Telemetry trace events Cristian Marussi
` (25 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi, Rob Herring (Arm),
Krzysztof Kozlowski, Conor Dooley, devicetree
Add new SCMI v4.0 Telemetry protocol bindings definitions.
Acked-by: Rob Herring (Arm) <robh@kernel.org>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v3 --> v4
- changed protocol number to lowercase 1b
- fixed misplaced block for protocol 0x1b
Cc: Rob Herring <robh@kernel.org>
Cc: Krzysztof Kozlowski <krzk+dt@kernel.org>
Cc: Conor Dooley <conor+dt@kernel.org>
Cc: devicetree@vger.kernel.org
---
Documentation/devicetree/bindings/firmware/arm,scmi.yaml | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/Documentation/devicetree/bindings/firmware/arm,scmi.yaml b/Documentation/devicetree/bindings/firmware/arm,scmi.yaml
index 6fc44105f1c3..b63543390fd9 100644
--- a/Documentation/devicetree/bindings/firmware/arm,scmi.yaml
+++ b/Documentation/devicetree/bindings/firmware/arm,scmi.yaml
@@ -324,6 +324,14 @@ properties:
required:
- reg
+ protocol@1b:
+ $ref: '#/$defs/protocol-node'
+ unevaluatedProperties: false
+
+ properties:
+ reg:
+ const: 0x1b
+
unevaluatedProperties: false
$defs:
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 10/34] include: trace: Add Telemetry trace events
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (8 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 09/34] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 11/34] firmware: arm_scmi: Add Telemetry basic attributes enumeration Cristian Marussi
` (24 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add custom traces to report Telemetry accesses and collection events.
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v12 --> v13
- better commit message
- use const char where required
v7 --> v8
- use proper FTRACE helpers for variable strings
---
include/trace/events/scmi.h | 45 +++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/include/trace/events/scmi.h b/include/trace/events/scmi.h
index b421c253df00..ae63a13fd141 100644
--- a/include/trace/events/scmi.h
+++ b/include/trace/events/scmi.h
@@ -178,6 +178,51 @@ TRACE_EVENT(scmi_msg_dump,
__get_str(tag), __entry->msg_id, __entry->seq, __entry->status,
__print_hex_str(__get_dynamic_array(cmd), __entry->len))
);
+
+TRACE_EVENT(scmi_tlm_access,
+ TP_PROTO(u64 de_id, const char *tag, u64 startm, u64 endm),
+ TP_ARGS(de_id, tag, startm, endm),
+
+ TP_STRUCT__entry(
+ __field(u64, de_id)
+ __string(tag, tag)
+ __field(u64, startm)
+ __field(u64, endm)
+ ),
+
+ TP_fast_assign(
+ __entry->de_id = de_id;
+ __assign_str(tag);
+ __entry->startm = startm;
+ __entry->endm = endm;
+ ),
+
+ TP_printk("[%s] de_id=0x%llX - startm=%016llX endm=%016llX",
+ __get_str(tag), __entry->de_id, __entry->startm, __entry->endm)
+);
+
+TRACE_EVENT(scmi_tlm_collect,
+ TP_PROTO(u64 ts, u64 de_id, u64 value, const char *tag),
+ TP_ARGS(ts, de_id, value, tag),
+
+ TP_STRUCT__entry(
+ __field(u64, ts)
+ __field(u64, de_id)
+ __field(u64, value)
+ __string(tag, tag)
+ ),
+
+ TP_fast_assign(
+ __entry->ts = ts;
+ __entry->de_id = de_id;
+ __entry->value = value;
+ __assign_str(tag);
+ ),
+
+ TP_printk("[%s] ts=%llu de_id=0x%04llX value=%016llu",
+ __get_str(tag), __entry->ts, __entry->de_id, __entry->value)
+);
+
#endif /* _TRACE_SCMI_H */
/* This part must be outside protection */
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 11/34] firmware: arm_scmi: Add Telemetry basic attributes enumeration
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (9 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 10/34] include: trace: Add Telemetry trace events Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 12/34] firmware: arm_scmi: Add Telemetry resources enumeration Cristian Marussi
` (23 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add SCMIv4.0 Telemetry basic support for minimal initialization and
protocol attributes enumeration: expose telemetry instance descriptor
via a simple information retrieval protocol operation.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v15 --> v16
- ignore groups
v14 --> v15
- add include uuid.h into scmi_protocol.h
- dropped needless __must_check
v13 --> v14
- split original basic Telemetry patch, reducing this patch to the
basics of protocol attributes enumeration only
- fix group unroll loop on groups_init failure
- remove goto from de_desc_parse
- renamed some fields to more strictly adhere to spec nomenclature
v12 --> v13
- headers includes re-order
- rework cleanup path for instance allocations
- relocate tde->mtx guards higher up and add lockdep assertions
- add more _RAW macros
- rename DE_CONFIGURE defines
- use *alloc_objs
- remove TO_CPU64 and useless READ_ONCEs
v11 --> v12
- dropped counted-by in smhtis
- reworked barriers around res_get
- added smp_store_release on TDE registration
v10 -->v11
- relocated a few parsing macros from another patch
- cleanup exit path on resource enumeration
- remove 'naked' ti->rinfo accesses
- walk xa_des to free DEs on removal
v8 --> v9
- Fixes for some Sashiko issues on v8
- use counted_by_lle where appropriate
- consider header in boudanry check in DE_DESCRIPTION iterators
- bail out if SHMTI areas are NOT 32bit aligned
- warn if SHMTI areas are NOT 64bit aligned
- convert SCMI_TDE_VAR_SZ macro to an inline function
- make some enum definition explicit
v7 --> v8
- Fixes for some Sashiko issues on v7
- add proper msg boundary parsing checks on DE_DESCRIPTION multipart
- add proper cleanup/rollback on groups_init err_path
- update num_des_tstamp ONLY after a DE is successfully initialized
- proper cleanup tde during tde_put (preserving info/mutex bits)
- add membarriers in enumerate resources
- add a check and a warn on SHMTI phys_addr alignment
- reword a few dev_errs
- use tde_free_put on resoucers_free
v6 --> v7
- use uuid_t for proper Telemetry UUID DE_IMPL_VERSION endianity
v5 --> v6
- define uuid_line dwords as BE
- fixed Sashiko reviews where applicable:
https://sashiko.dev/#/message/20260703123601.381275-8-cristian.marussi%40arm.com
- fix missing le32 and bound check in variable payload parsing
- fix TDE duplicatin logic
- fix intervals cleanup - fixes also KMEMLEAK report
- implement proper freeinfg for group resources
- proper SHMTI iterations
- removed useless/stale static in scmi_telemetry_tde_get()
v4 --> v5
- fix de->grp_id initialization for DEs not belonging to a group
- removed UAPI structures dependency
- expose discovered SHMTIs areas in scmi_protocol.h
- fixes sparse warnings
v3 -->v4
- bail-out on FW_BUG errors
- count timestamp capable DE to track enables
v2 --> v3
- split from monolithic Telemetry patch
- fix checkpatch macros complaints
- fix ACCESS_PRIVATE usage
- add a few comments on allocation/enumeration lifetime
- use interval.num_intervals
- removed needless cleanup handler usage
- simply return from scmi_telemetry_de_lookup()
- fixed composing_des name length to 08X
---
drivers/firmware/arm_scmi/Makefile | 2 +-
drivers/firmware/arm_scmi/driver.c | 2 +
drivers/firmware/arm_scmi/protocols.h | 1 +
drivers/firmware/arm_scmi/telemetry.c | 149 ++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 33 +++++-
5 files changed, 185 insertions(+), 2 deletions(-)
create mode 100644 drivers/firmware/arm_scmi/telemetry.c
diff --git a/drivers/firmware/arm_scmi/Makefile b/drivers/firmware/arm_scmi/Makefile
index 780cd62b2f78..fe55b7aa0707 100644
--- a/drivers/firmware/arm_scmi/Makefile
+++ b/drivers/firmware/arm_scmi/Makefile
@@ -8,7 +8,7 @@ scmi-driver-$(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT) += raw_mode.o
scmi-transport-$(CONFIG_ARM_SCMI_HAVE_SHMEM) = shmem.o
scmi-transport-$(CONFIG_ARM_SCMI_HAVE_MSG) += msg.o
scmi-protocols-y := base.o clock.o perf.o power.o reset.o sensors.o system.o voltage.o powercap.o
-scmi-protocols-y += pinctrl.o
+scmi-protocols-y += pinctrl.o telemetry.o
scmi-module-objs := $(scmi-driver-y) $(scmi-protocols-y) $(scmi-transport-y)
obj-$(CONFIG_ARM_SCMI_PROTOCOL) += transports/
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 35adee6a7a15..00a3484006d9 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -3683,6 +3683,7 @@ static int __init scmi_driver_init(void)
scmi_system_register();
scmi_powercap_register();
scmi_pinctrl_register();
+ scmi_telemetry_register();
return platform_driver_register(&scmi_driver);
}
@@ -3701,6 +3702,7 @@ static void __exit scmi_driver_exit(void)
scmi_system_unregister();
scmi_powercap_unregister();
scmi_pinctrl_unregister();
+ scmi_telemetry_unregister();
platform_driver_unregister(&scmi_driver);
diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_scmi/protocols.h
index 50b89c56e309..1aa3ee1fb2b0 100644
--- a/drivers/firmware/arm_scmi/protocols.h
+++ b/drivers/firmware/arm_scmi/protocols.h
@@ -409,5 +409,6 @@ DECLARE_SCMI_REGISTER_UNREGISTER(sensors);
DECLARE_SCMI_REGISTER_UNREGISTER(voltage);
DECLARE_SCMI_REGISTER_UNREGISTER(system);
DECLARE_SCMI_REGISTER_UNREGISTER(powercap);
+DECLARE_SCMI_REGISTER_UNREGISTER(telemetry);
#endif /* _SCMI_PROTOCOLS_H */
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
new file mode 100644
index 000000000000..402d9587046c
--- /dev/null
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -0,0 +1,149 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * System Control and Management Interface (SCMI) Telemetry Protocol
+ *
+ * Copyright (C) 2026 ARM Ltd.
+ */
+
+#include <linux/atomic.h>
+#include <linux/bitfield.h>
+#include <linux/compiler_types.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/limits.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/sprintf.h>
+#include <linux/string.h>
+#include <linux/uuid.h>
+
+#include "protocols.h"
+#include "notify.h"
+
+/* Updated only after ALL the mandatory features for that version are merged */
+#define SCMI_PROTOCOL_SUPPORTED_VERSION 0x10000
+
+#define SCMI_TLM_DE_IMPL_NUM_DWORDS 4
+
+/* An enum listing the protocol specific commands definitions */
+enum scmi_telemetry_protocol_cmd {
+ TELEMETRY_LIST_SHMTI = 0x3,
+ TELEMETRY_DE_DESCRIPTION = 0x4,
+ TELEMETRY_LIST_UPDATE_INTERVALS = 0x5,
+ TELEMETRY_DE_CONFIGURE = 0x6,
+ TELEMETRY_DE_ENABLED_LIST = 0x7,
+ TELEMETRY_CONFIG_SET = 0x8,
+ TELEMETRY_READING_COMPLETE = TELEMETRY_CONFIG_SET,
+ TELEMETRY_CONFIG_GET = 0x9,
+ TELEMETRY_RESET = 0xA,
+};
+
+struct scmi_msg_resp_telemetry_protocol_attributes {
+ __le32 de_num;
+ __le32 groups_num;
+ __le32 de_implem_rev_dword[SCMI_TLM_DE_IMPL_NUM_DWORDS];
+ __le32 attributes_1;
+#define SUPPORTS_SINGLE_READ(x) (le32_get_bits((x), BIT(31)))
+#define SUPPORTS_CONTINUOS_UPDATE(x) (le32_get_bits((x), BIT(30)))
+#define SUPPORTS_PER_GROUP_CONFIG(x) (le32_get_bits((x), BIT(18)))
+#define SUPPORTS_RESET(x) (le32_get_bits((x), BIT(17)))
+#define SUPPORTS_FC(x) (le32_get_bits((x), BIT(16)))
+ __le32 default_blk_ts_rate;
+};
+
+struct telemetry_info {
+ bool streaming_mode;
+ unsigned int num_shmti;
+ unsigned int num_des_tstamp;
+ unsigned int default_blk_ts_rate;
+ const struct scmi_protocol_handle *ph;
+ struct scmi_telemetry_info info;
+};
+
+static int
+scmi_telemetry_protocol_attributes_get(struct telemetry_info *ti)
+{
+ struct scmi_msg_resp_telemetry_protocol_attributes *resp;
+ const struct scmi_protocol_handle *ph = ti->ph;
+ struct scmi_xfer *t;
+ int ret;
+
+ ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0,
+ sizeof(*resp), &t);
+ if (ret)
+ return ret;
+
+ resp = t->rx.buf;
+ ret = ph->xops->do_xfer(ph, t);
+ if (!ret) {
+ __le32 attr = resp->attributes_1;
+ __be32 de_impl_version[SCMI_TLM_DE_IMPL_NUM_DWORDS];
+
+ ti->info.base.num_des = le32_to_cpu(resp->de_num);
+ ti->info.base.num_groups = 0;
+ for (int i = 0; i < SCMI_TLM_DE_IMPL_NUM_DWORDS; i++)
+ de_impl_version[i] =
+ cpu_to_be32(le32_to_cpu(resp->de_implem_rev_dword[i]));
+ import_uuid(&ti->info.base.primary_revision, (const __u8 *)&de_impl_version);
+ ti->info.single_read_support = SUPPORTS_SINGLE_READ(attr);
+ ti->info.continuous_update_support = SUPPORTS_CONTINUOS_UPDATE(attr);
+ ti->info.per_group_config_support = SUPPORTS_PER_GROUP_CONFIG(attr);
+ ti->info.reset_support = SUPPORTS_RESET(attr);
+ ti->info.fc_support = SUPPORTS_FC(attr);
+ ti->num_shmti = le32_get_bits(attr, GENMASK(15, 0));
+ ti->default_blk_ts_rate = le32_to_cpu(resp->default_blk_ts_rate);
+ }
+
+ ph->xops->xfer_put(ph, t);
+
+ return ret;
+}
+
+static const struct scmi_telemetry_info *
+scmi_telemetry_info_get(const struct scmi_protocol_handle *ph)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+
+ return &ti->info;
+}
+
+static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
+ .info_get = scmi_telemetry_info_get,
+};
+
+static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
+{
+ struct device *dev = ph->dev;
+ struct telemetry_info *ti;
+ int ret;
+
+ dev_dbg(dev, "Telemetry Version %d.%d\n",
+ PROTOCOL_REV_MAJOR(ph->version), PROTOCOL_REV_MINOR(ph->version));
+
+ ti = devm_kzalloc(dev, sizeof(*ti), GFP_KERNEL);
+ if (!ti)
+ return -ENOMEM;
+
+ ti->ph = ph;
+
+ ret = scmi_telemetry_protocol_attributes_get(ti);
+ if (ret) {
+ dev_err(dev, FW_BUG "Cannot retrieve protocol attributes. Abort.\n");
+ return ret;
+ }
+
+ ti->info.base.version = ph->version;
+
+ return ph->set_priv(ph, ti);
+}
+
+static const struct scmi_protocol scmi_telemetry = {
+ .id = SCMI_PROTOCOL_TELEMETRY,
+ .owner = THIS_MODULE,
+ .instance_init = &scmi_telemetry_protocol_init,
+ .ops = &tlm_proto_ops,
+ .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
+};
+
+DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(telemetry, scmi_telemetry)
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index 3f7cc35eb861..f076ce5601cf 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -2,17 +2,19 @@
/*
* SCMI Message Protocol driver header
*
- * Copyright (C) 2018-2021 ARM Ltd.
+ * Copyright (C) 2018-2026 ARM Ltd.
*/
#ifndef _LINUX_SCMI_PROTOCOL_H
#define _LINUX_SCMI_PROTOCOL_H
#include <linux/bitfield.h>
+#include <linux/bitops.h>
#include <linux/device-id/scmi.h>
#include <linux/device.h>
#include <linux/notifier.h>
#include <linux/types.h>
+#include <linux/uuid.h>
#define SCMI_MAX_STR_SIZE 64
#define SCMI_SHORT_NAME_MAX_SIZE 16
@@ -828,6 +830,35 @@ struct scmi_pinctrl_proto_ops {
int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin);
};
+struct scmi_telemetry_base_info {
+ unsigned int version;
+ uuid_t primary_revision;
+ unsigned int num_des;
+ unsigned int num_groups;
+ unsigned int num_intervals;
+ unsigned int num_shmtis;
+};
+
+struct scmi_telemetry_info {
+ bool single_read_support;
+ bool continuous_update_support;
+ bool per_group_config_support;
+ bool reset_support;
+ bool fc_support;
+ struct scmi_telemetry_base_info base;
+};
+
+/**
+ * struct scmi_telemetry_proto_ops - represents the various operations provided
+ * by SCMI Telemetry Protocol
+ *
+ * @info_get: get the general Telemetry information.
+ */
+struct scmi_telemetry_proto_ops {
+ const struct scmi_telemetry_info *(*info_get)
+ (const struct scmi_protocol_handle *ph);
+};
+
/**
* struct scmi_notify_ops - represents notifications' operations provided by
* SCMI core
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 12/34] firmware: arm_scmi: Add Telemetry resources enumeration
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (10 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 11/34] firmware: arm_scmi: Add Telemetry basic attributes enumeration Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 13/34] firmware: arm_scmi: Add Telemetry global update intervals enumeration Cristian Marussi
` (22 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add basic Telemetry allocation and initialization routines, introducing and
wiring-up the logic needed to enumerate all the SCMI Telemetry resources
optionally using a lazy on-demand scheme: this approach factors in the
flexibility needed to minimize the amount of initialization work based on
the effective access pattern used by the upper layer ABI.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- remove unused de.ts_rate
- renaming discovered to enumerated
- list_add to fcs_des only after successfull TDE registration
- use list_del_init for free_des/fcs_des list removals
- call tdes_cleanup on free_resources
v15 --> v16
- add tdes_cleanup as devm_action after enumeration
- use phys_addr_t and proper format string
- drop TDE_FC_SIZE and introduce better checks
- unlink FC TDEs from fcs_des list on free
- bailout on unknown TDE types
- reject FCs smaller than 8 bytes
v14 --> v15
- drop usage of devres for FC iomapping
- trigger full enumeration of DE lookups
- new IS_TDE_TS_ macro helpers
- add a dedicated scmi_telemetry_free_tdes_add helper
- better commenting
- dropping needless __must_check
- flag as fully enumerated only when ALL declared des were found
- drop redundant iounmap
- check FC size on enumeration
v13 --> v14
- splitted out of the old Basic Telemetry patch, contains the basic
instance allocations and the DEs enumeration process
- renamed some fields to more strictly adhere to spec nomenclature
---
drivers/firmware/arm_scmi/telemetry.c | 753 ++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 37 ++
2 files changed, 790 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 402d9587046c..94311cdc9398 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -8,15 +8,19 @@
#include <linux/atomic.h>
#include <linux/bitfield.h>
#include <linux/compiler_types.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/limits.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/sprintf.h>
#include <linux/string.h>
#include <linux/uuid.h>
+#include <linux/xarray.h>
#include "protocols.h"
#include "notify.h"
@@ -52,13 +56,216 @@ struct scmi_msg_resp_telemetry_protocol_attributes {
__le32 default_blk_ts_rate;
};
+struct de_desc_fastchannel {
+ __le32 addr_low;
+ __le32 addr_high;
+ __le32 size;
+};
+
+struct scmi_de_desc {
+ __le32 de_id;
+ __le32 group_id;
+ __le32 de_data_sz;
+ __le32 de_attributes_1;
+#define IS_NAME_SUPPORTED(d) (le32_get_bits((d)->de_attributes_1, BIT(31)))
+#define IS_FC_SUPPORTED(d) (le32_get_bits((d)->de_attributes_1, BIT(30)))
+#define GET_DE_TYPE(d) (le32_get_bits((d)->de_attributes_1, GENMASK(29, 22)))
+#define IS_PERSISTENT(d) (le32_get_bits((d)->de_attributes_1, BIT(21)))
+#define GET_DE_UNIT_EXP(d) \
+ ({ \
+ __u32 __signed_exp = \
+ le32_get_bits((d)->de_attributes_1, GENMASK(20, 13)); \
+ \
+ sign_extend32(__signed_exp, 7); \
+ })
+#define GET_DE_UNIT(d) (le32_get_bits((d)->de_attributes_1, GENMASK(12, 5)))
+#define TSTAMP_SUPPORT(d) (le32_get_bits((d)->de_attributes_1, GENMASK(1, 0)))
+ __le32 de_attributes_2;
+#define GET_DE_INSTA_ID(d) (le32_get_bits((d)->de_attributes_2, GENMASK(31, 24)))
+#define GET_COMPO_INSTA_ID(d) (le32_get_bits((d)->de_attributes_2, GENMASK(23, 8)))
+#define GET_COMPO_TYPE(d) (le32_get_bits((d)->de_attributes_2, GENMASK(7, 0)))
+ __le32 de_attributes_3;
+};
+
+struct scmi_msg_resp_telemetry_de_description {
+ __le32 num_desc; /* This does NOT count desc[] by the spec */
+ struct scmi_de_desc desc[];
+};
+
+#define IS_TDE_TSTAMP_TYPE(__t, __ttype) ((__t)->ts_type == (__ttype))
+#define IS_TDE_TS_NONE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE))
+#define IS_TDE_TS_LINE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE))
+#define IS_TDE_TS_BLK(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_BLK))
+
+enum timestamps {
+ TSTAMP_NONE = 0,
+ TSTAMP_LINE = 1,
+ TSTAMP_BLK = 2,
+};
+
+#define SHMTI_ID_INVALID GENMASK(31, 0)
+
+struct telemetry_de {
+ enum timestamps ts_type;
+ bool enumerated;
+ void __iomem *base;
+ u32 sid;
+ u32 offset;
+ /* NOTE THAT DE data_sz is registered in scmi_telemetry_de */
+ u32 fc_size;
+ struct list_head item;
+ struct scmi_telemetry_de de;
+ /* Protect last_val/ts/magic accesses - MUST BE KEPT LAST */
+ struct mutex mtx;
+};
+
+#define to_tde(d) container_of(d, struct telemetry_de, de)
+
struct telemetry_info {
bool streaming_mode;
unsigned int num_shmti;
unsigned int num_des_tstamp;
unsigned int default_blk_ts_rate;
const struct scmi_protocol_handle *ph;
+ struct telemetry_de *tdes;
+ struct xarray xa_des;
+ /* Mutex to protect access to @free_des */
+ struct mutex free_mtx;
+ struct list_head free_des;
+ struct list_head fcs_des;
struct scmi_telemetry_info info;
+ atomic_t rinfo_initializing;
+ struct completion rinfo_initdone;
+ struct scmi_telemetry_res_info *rinfo;
+ struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti);
+};
+
+static struct scmi_telemetry_res_info *
+__scmi_telemetry_resources_get(struct telemetry_info *ti);
+
+static struct telemetry_de *
+scmi_telemetry_free_tde_get(struct telemetry_info *ti)
+{
+ struct telemetry_de *tde;
+
+ guard(mutex)(&ti->free_mtx);
+
+ tde = list_first_entry_or_null(&ti->free_des, struct telemetry_de, item);
+ if (!tde)
+ return tde;
+
+ list_del_init(&tde->item);
+
+ return tde;
+}
+
+static inline void scmi_telemetry_free_tdes_add(struct telemetry_info *ti,
+ struct telemetry_de *tde)
+{
+ guard(mutex)(&ti->free_mtx);
+ list_add_tail(&tde->item, &ti->free_des);
+}
+
+static void scmi_telemetry_free_tde_put(struct telemetry_info *ti,
+ struct telemetry_de *tde)
+{
+ struct scmi_telemetry_de_info *info;
+
+ lockdep_assert_held(&tde->mtx);
+
+ /* Unlink and unmap any valid DE/FC */
+ if (tde->de.fc_support) {
+ /*
+ * During normal system operation this can be triggered only
+ * once by the DE description iteraror while enumerating
+ * resources, so the fcs_des list is not manipulated elsewhere
+ * when we remove this TDE from that list.
+ */
+ list_del_init(&tde->item);
+ if (tde->base)
+ iounmap(tde->base);
+ }
+
+ /* Save clear and restore */
+ info = tde->de.info;
+ memset(info, 0, sizeof(*info));
+ memset(tde, 0, offsetof(struct telemetry_de, mtx));
+ tde->de.info = info;
+
+ scmi_telemetry_free_tdes_add(ti, tde);
+}
+
+static struct telemetry_de *scmi_telemetry_tde_lookup(struct telemetry_info *ti,
+ unsigned int de_id)
+{
+ struct scmi_telemetry_de *de;
+
+ de = xa_load(&ti->xa_des, de_id);
+ if (!de)
+ return NULL;
+
+ return to_tde(de);
+}
+
+static struct telemetry_de *scmi_telemetry_tde_get(struct telemetry_info *ti,
+ unsigned int de_id)
+{
+ struct telemetry_de *tde;
+
+ tde = scmi_telemetry_free_tde_get(ti);
+ if (!tde) {
+ dev_err(ti->ph->dev, "Cannot get TDE for ID:0x%08X\n", de_id);
+ return ERR_PTR(-ENOSPC);
+ }
+
+ tde->sid = SHMTI_ID_INVALID;
+
+ return tde;
+}
+
+static int scmi_telemetry_tde_register(struct telemetry_info *ti,
+ struct telemetry_de *tde)
+{
+ struct scmi_telemetry_res_info *rinfo;
+ int ret;
+
+ lockdep_assert_held(&tde->mtx);
+
+ /* Get rinfo without triggering a recursive enumeration */
+ rinfo = __scmi_telemetry_resources_get(ti);
+
+ if (rinfo->num_des >= ti->info.base.num_des) {
+ ret = -ENOSPC;
+ goto err;
+ }
+
+ /* Store DE pointer by de_id ... */
+ ret = xa_insert(&ti->xa_des, tde->de.info->id, &tde->de, GFP_KERNEL);
+ if (ret)
+ goto err;
+
+ /* Add to FastChannels list */
+ if (tde->de.fc_support)
+ list_add(&tde->item, &ti->fcs_des);
+
+ /* ... and in the general array */
+ rinfo->des[rinfo->num_des] = &tde->de;
+ /* Make sure the freshly registered DE is visible before the index update */
+ smp_store_release(&rinfo->num_des, rinfo->num_des + 1);
+
+ return 0;
+
+err:
+ dev_err(ti->ph->dev, "Cannot register TDE for ID:0x%08X - ret:%d\n",
+ tde->de.info->id, ret);
+
+ return ret;
+}
+
+struct scmi_tlm_de_priv {
+ struct telemetry_info *ti;
+ void *next;
+ size_t rx_len;
};
static int
@@ -100,6 +307,324 @@ scmi_telemetry_protocol_attributes_get(struct telemetry_info *ti)
return ret;
}
+static void iter_tlm_prepare_message(void *message,
+ unsigned int desc_index, const void *priv)
+{
+ put_unaligned_le32(desc_index, message);
+}
+
+static int iter_de_descr_update_state(struct scmi_iterator_state *st,
+ const void *response, void *priv)
+{
+ const struct scmi_msg_resp_telemetry_de_description *r = response;
+ struct scmi_tlm_de_priv *p = priv;
+
+ st->num_returned = le32_get_bits(r->num_desc, GENMASK(15, 0));
+ st->num_remaining = le32_get_bits(r->num_desc, GENMASK(31, 16));
+
+ if (st->rx_len < (sizeof(*r) + sizeof(r->desc[0]) * st->num_returned))
+ return -EINVAL;
+
+ /* Initialized to first descriptor and initial payload size */
+ p->next = (void *)r->desc;
+ p->rx_len = st->rx_len - sizeof(*r);
+
+ return 0;
+}
+
+static inline size_t scmi_telemetry_tde_var_size_calc(struct telemetry_de *tde)
+{
+ size_t sz = 0;
+
+ sz += IS_TDE_TS_LINE(tde) ? sizeof(__le32) : 0;
+ sz += tde->de.fc_support ? sizeof(struct de_desc_fastchannel) : 0;
+ sz += tde->de.name_support ? SCMI_SHORT_NAME_MAX_SIZE : 0;
+
+ return sz;
+}
+
+static inline int
+scmi_telemetry_area_check(struct device *dev, const char *tag,
+ u32 low, u32 high, u32 size,
+ phys_addr_t *phys_addr, size_t *len)
+{
+ u64 addr = ((u64)high << 32) | low;
+ phys_addr_t pa = (phys_addr_t)addr;
+ phys_addr_t end_phys;
+
+ /* Does the returned pointer fits architetural size ? */
+ if ((u64)pa != addr) {
+ dev_err(dev, FW_BUG "%s phys_addr (%pa) pointer too big.\n",
+ tag, &pa);
+ return -EOVERFLOW;
+ }
+
+ if (!IS_ALIGNED(pa, 4)) {
+ dev_err(dev, FW_BUG "%s phys_addr (%pa) misaligned.\n",
+ tag, &pa);
+ return -EINVAL;
+ }
+
+ if (!IS_ALIGNED(size, 4)) {
+ dev_err(dev, FW_BUG "%s area length (%u) misaligned\n",
+ tag, size);
+ return -EINVAL;
+ }
+
+ if (check_add_overflow(pa, size, &end_phys)) {
+ dev_err(dev, FW_BUG "%s area (%pa/%u) wrap-around.\n",
+ tag, &pa, size);
+ return -EOVERFLOW;
+ }
+
+ *phys_addr = pa;
+ *len = size;
+
+ return 0;
+}
+
+static inline int
+scmi_telemetry_fc_size_check(struct telemetry_de *tde, size_t size)
+{
+ size_t fc_size, fc_base_sz = tde->de.info->data_sz;
+
+ if (fc_base_sz < 8 || !size)
+ return -EINVAL;
+
+ if (check_add_overflow(fc_base_sz,
+ tde->de.tstamp_support ? sizeof(u32) * 2 : 0,
+ &fc_size))
+ return -EOVERFLOW;
+
+ if (size < fc_size)
+ return -ENOSPC;
+
+ return 0;
+}
+
+static int scmi_telemetry_tde_fastchannel_setup(struct device *dev,
+ struct telemetry_de *tde, void **next)
+{
+ struct de_desc_fastchannel *dfc;
+ phys_addr_t phys_addr;
+ void __iomem *addr;
+ size_t size;
+ int ret;
+
+ dfc = *next;
+ ret = scmi_telemetry_area_check(dev, "FC",
+ le32_to_cpu(dfc->addr_low),
+ le32_to_cpu(dfc->addr_high),
+ le32_to_cpu(dfc->size),
+ &phys_addr, &size);
+ if (ret)
+ return ret;
+
+ /* Does it fit ? */
+ if (scmi_telemetry_fc_size_check(tde, size)) {
+ dev_err(dev, FW_BUG "TDE FC size inconsistent (%zu)\n", size);
+ return -EPROTO;
+ }
+
+ addr = ioremap(phys_addr, size);
+ if (!addr)
+ return -EADDRNOTAVAIL;
+
+ tde->base = addr;
+ tde->offset = 0;
+ tde->fc_size = size;
+
+ /* Variably sized depending on FC support */
+ *next += sizeof(*dfc);
+
+ return 0;
+}
+
+static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti,
+ struct telemetry_de *tde,
+ void **next, size_t *rx_len)
+{
+ const struct scmi_de_desc *desc = *next;
+ struct device *dev = ti->ph->dev;
+ size_t payld_sz = sizeof(*desc);
+ int ret;
+
+ lockdep_assert_held(&tde->mtx);
+
+ if (*rx_len < payld_sz)
+ return -ENOSPC;
+
+ tde->de.info->id = le32_to_cpu(desc->de_id);
+ tde->de.info->grp_id = le32_to_cpu(desc->group_id);
+ tde->de.info->data_sz = le32_to_cpu(desc->de_data_sz);
+ tde->de.info->type = GET_DE_TYPE(desc);
+ tde->de.info->unit = GET_DE_UNIT(desc);
+ tde->de.info->unit_exp = GET_DE_UNIT_EXP(desc);
+ tde->de.info->instance_id = GET_DE_INSTA_ID(desc);
+ tde->de.info->compo_instance_id = GET_COMPO_INSTA_ID(desc);
+ tde->de.info->compo_type = GET_COMPO_TYPE(desc);
+ tde->de.info->persistent = IS_PERSISTENT(desc);
+ tde->ts_type = TSTAMP_SUPPORT(desc);
+ tde->de.tstamp_support = !!tde->ts_type;
+ tde->de.fc_support = IS_FC_SUPPORTED(desc);
+ tde->de.name_support = IS_NAME_SUPPORTED(desc);
+ /* Update DE_DESCRIPTOR size for the next iteration */
+ *next += sizeof(*desc);
+
+ payld_sz += scmi_telemetry_tde_var_size_calc(tde);
+ if (*rx_len < payld_sz)
+ return -ENOSPC;
+
+ if (IS_TDE_TS_LINE(tde)) {
+ __le32 *line_ts_rate = *next;
+
+ tde->de.info->ts_rate = le32_to_cpu((*line_ts_rate));
+
+ /* Variably sized depending on TS support */
+ *next += sizeof(*line_ts_rate);
+ } else if (IS_TDE_TS_BLK(tde)) {
+ /* Setup default BLK TS value at first */
+ tde->de.info->ts_rate = ti->default_blk_ts_rate;
+ } else if (!IS_TDE_TS_NONE(tde)) {
+ dev_warn(dev, "Unknown TDE timestamp type %u\n", tde->ts_type);
+ return -EPROTO;
+ }
+
+ if (tde->de.fc_support) {
+ ret = scmi_telemetry_tde_fastchannel_setup(dev, tde, next);
+ if (ret)
+ return ret;
+ }
+
+ if (tde->de.name_support) {
+ const char *de_name = *next;
+
+ strscpy(tde->de.info->name, de_name, SCMI_SHORT_NAME_MAX_SIZE);
+ /* Variably sized depending on name support */
+ *next += SCMI_SHORT_NAME_MAX_SIZE;
+ }
+
+ *rx_len -= payld_sz;
+
+ return 0;
+}
+
+static int iter_de_descr_process_response(const struct scmi_protocol_handle *ph,
+ const void *response,
+ struct scmi_iterator_state *st,
+ void *priv)
+{
+ struct scmi_tlm_de_priv *p = priv;
+ struct telemetry_info *ti = p->ti;
+ const struct scmi_de_desc *desc = p->next;
+ struct telemetry_de *tde;
+ bool enumerated = false;
+ unsigned int de_id;
+ int ret;
+
+ /* Is at least reading de_id safe */
+ if (p->rx_len < sizeof(desc->de_id))
+ return -ENOSPC;
+
+ de_id = le32_to_cpu(desc->de_id);
+ /*
+ * Check if this DE was already been discovered by other means,
+ * such as during boot if Telemetry was detected as already enabled
+ * and the list of enabled DEs was enumerated separately.
+ */
+ tde = scmi_telemetry_tde_lookup(ti, de_id);
+ if (!tde) {
+ /* Create a new one */
+ tde = scmi_telemetry_tde_get(ti, de_id);
+ if (IS_ERR(tde))
+ return PTR_ERR(tde);
+
+ enumerated = true;
+ }
+
+ guard(mutex)(&tde->mtx);
+ if (tde->enumerated) {
+ /* Cannot be a duplicate of a DE already created by enumeration */
+ dev_err(ph->dev,
+ "Discovered INVALID DE with DUPLICATED ID:0x%08X\n",
+ de_id);
+ return -EINVAL;
+ }
+
+ ret = scmi_telemetry_de_descriptor_parse(ti, tde, &p->next, &p->rx_len);
+ if (ret) {
+ dev_err(ph->dev, "Malformed DE Descriptor - ret:%d\n", ret);
+ /* DE not enumerated at this point were created in this call */
+ if (enumerated)
+ scmi_telemetry_free_tde_put(ti, tde);
+ return ret;
+ }
+
+ /* Now fully enumerated */
+ tde->enumerated = true;
+
+ /* Register if it was not already ... */
+ if (enumerated) {
+ ret = scmi_telemetry_tde_register(ti, tde);
+ if (ret) {
+ /* DE not enumerated at this point were created in this call */
+ scmi_telemetry_free_tde_put(ti, tde);
+ return ret;
+ }
+ }
+
+ /* Count timestamped DEs */
+ ti->num_des_tstamp += !!tde->de.tstamp_support;
+
+ return 0;
+}
+
+static void scmi_telemetry_tdes_cleanup(struct telemetry_info *ti)
+{
+ struct scmi_telemetry_de *de;
+ unsigned long idx;
+
+ xa_for_each(&ti->xa_des, idx, de) {
+ struct telemetry_de *tde = to_tde(de);
+
+ guard(mutex)(&tde->mtx);
+
+ scmi_telemetry_free_tde_put(ti, tde);
+ }
+}
+
+static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti)
+{
+ const struct scmi_protocol_handle *ph = ti->ph;
+
+ struct scmi_iterator_ops ops = {
+ .prepare_message = iter_tlm_prepare_message,
+ .update_state = iter_de_descr_update_state,
+ .process_response = iter_de_descr_process_response,
+ };
+ struct scmi_tlm_de_priv tpriv = {
+ .ti = ti,
+ .next = NULL,
+ };
+ void *iter;
+ int ret;
+
+ if (!ti->info.base.num_des)
+ return 0;
+
+ iter = ph->hops->iter_response_init(ph, &ops, ti->info.base.num_des,
+ TELEMETRY_DE_DESCRIPTION,
+ sizeof(u32), &tpriv);
+ if (IS_ERR(iter))
+ return PTR_ERR(iter);
+
+ ret = ph->hops->iter_response_run(iter);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
static const struct scmi_telemetry_info *
scmi_telemetry_info_get(const struct scmi_protocol_handle *ph)
{
@@ -108,10 +633,232 @@ scmi_telemetry_info_get(const struct scmi_protocol_handle *ph)
return &ti->info;
}
+static const struct scmi_telemetry_res_info *
+scmi_telemetry_resources_get(const struct scmi_protocol_handle *ph)
+{
+ struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti);
+ struct telemetry_info *ti = ph->get_priv(ph);
+
+ /* Ensure the latest installed res_get helper is visible */
+ res_get = smp_load_acquire(&ti->res_get);
+
+ return res_get(ti);
+}
+
+static const struct scmi_telemetry_de *
+scmi_telemetry_de_lookup(const struct scmi_protocol_handle *ph, u32 id)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+
+ /* Trigger a full proper enumeration */
+ scmi_telemetry_resources_get(ph);
+
+ return xa_load(&ti->xa_des, id);
+}
+
static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.info_get = scmi_telemetry_info_get,
+ .de_lookup = scmi_telemetry_de_lookup,
+ .res_get = scmi_telemetry_resources_get,
};
+/**
+ * scmi_telemetry_resources_alloc - Resources allocation
+ * @ti: A reference to the telemetry info descriptor for this instance
+ *
+ * This allocates and initializes dedicated resources for the maximum possible
+ * number of needed telemetry resources, based on information gathered from
+ * the initial enumeration: these allocations represent an upper bound on
+ * the number of discoverable telemetry resources and they will be later
+ * populated during late deferred further discovery phases.
+ *
+ * Note that some of the allocations down below are designed to keep some
+ * class of data packed together since it eases retrieval from the ABI layer.
+ *
+ * Return: 0 on Success, errno otherwise
+ */
+static int scmi_telemetry_resources_alloc(struct telemetry_info *ti)
+{
+ /* Array to hold pointers to discovered DEs */
+ struct scmi_telemetry_de **des __free(kfree) =
+ kzalloc_objs(*des, ti->info.base.num_des);
+ if (!des)
+ return -ENOMEM;
+
+ /* The allocated DE descriptors */
+ struct telemetry_de *tdes __free(kfree) =
+ kzalloc_objs(*tdes, ti->info.base.num_des);
+ if (!tdes)
+ return -ENOMEM;
+
+ /* Allocate a set of contiguous DE info descriptors. */
+ struct scmi_telemetry_de_info *dei_store __free(kfree) =
+ kzalloc_objs(*dei_store, ti->info.base.num_des);
+ if (!dei_store)
+ return -ENOMEM;
+
+ struct scmi_telemetry_res_info *rinfo __free(kfree) =
+ kzalloc_obj(*rinfo);
+ if (!rinfo)
+ return -ENOMEM;
+
+ mutex_init(&ti->free_mtx);
+ INIT_LIST_HEAD(&ti->free_des);
+ for (int i = 0; i < ti->info.base.num_des; i++) {
+ mutex_init(&tdes[i].mtx);
+ /* Bind contiguous DE info structures */
+ tdes[i].de.info = &dei_store[i];
+ scmi_telemetry_free_tdes_add(ti, &tdes[i]);
+ }
+
+ INIT_LIST_HEAD(&ti->fcs_des);
+
+ ti->tdes = no_free_ptr(tdes);
+ xa_init(&ti->xa_des);
+
+ rinfo->des = no_free_ptr(des);
+ rinfo->dei_store = no_free_ptr(dei_store);
+
+ /* Ensure all of the above assignments are visible */
+ smp_store_release(&ti->rinfo, no_free_ptr(rinfo));
+
+ return 0;
+}
+
+static struct scmi_telemetry_res_info *
+__scmi_telemetry_resources_get(struct telemetry_info *ti)
+{
+ /* Ensure rinfo descriptor is visible */
+ return smp_load_acquire(&ti->rinfo);
+}
+
+static void scmi_telemetry_resources_free(struct telemetry_info *ti)
+{
+ struct scmi_telemetry_res_info *rinfo;
+
+ /* Get rinfo without triggering a recursive enumeration, cannot be NULL */
+ rinfo = __scmi_telemetry_resources_get(ti);
+ /* Ensure rinfo is no more accessible upfront */
+ smp_store_release(&ti->rinfo, NULL);
+
+ scmi_telemetry_tdes_cleanup(ti);
+ xa_destroy(&ti->xa_des);
+
+ kfree(ti->tdes);
+
+ kfree(rinfo->des);
+ kfree(rinfo->dei_store);
+
+ kfree(rinfo);
+
+ dev_dbg(ti->ph->dev, "SCMI Telemetry resources freed for instance\n");
+}
+
+static void scmi_telemetry_instance_cleanup(void *arg)
+{
+ struct telemetry_info *ti = arg;
+
+ scmi_telemetry_resources_free(ti);
+}
+
+/**
+ * scmi_telemetry_resources_enumerate - Enumeration helper
+ * @ti: A reference to the telemetry info descriptor for this instance
+ *
+ * This helper is configured to be called once on the first enumeration
+ * attempt, when triggered by invoking ti->res_get() from somewhere else.
+ *
+ * Once run it substitues itself in ti->res_get() with the simple accessor
+ * __scmi_telemetry_resources_get, which returns a descriptor to the resources
+ * that were possibly discovered.
+ *
+ * Note that, while it attempts to fully enumerate Data Events, it does NOT
+ * fail when such enumerations fail, instead it simply gives up with the end
+ * result that only a partially populated, but consistent, resources descriptor
+ * will be returned; in such a case the incomplete descriptor will be marked as
+ * NOT fully_enumerated: this design enables the kernel to deal with badly
+ * implemented out-of-spec firmware support while keep on providing a minimal
+ * sane, albeit possibly incomplete, set of telemetry respources.
+ *
+ * Return: A reference to a fully or partially populated resources descriptor
+ */
+static struct scmi_telemetry_res_info *
+scmi_telemetry_resources_enumerate(struct telemetry_info *ti)
+{
+ struct scmi_telemetry_res_info *rinfo;
+ struct device *dev = ti->ph->dev;
+ int ret;
+
+ /*
+ * Ensure the following initialization can be called only once
+ * from one thread of execution.
+ */
+ if (atomic_cmpxchg(&ti->rinfo_initializing, 0, 1)) {
+ /*
+ * When initialization is already ongoing in another thread,
+ * just wait for its completion and return the fully or partially
+ * populated rinfo.
+ */
+ if (!completion_done(&ti->rinfo_initdone))
+ wait_for_completion(&ti->rinfo_initdone);
+
+ /* Ensure rinfo descriptor is visible */
+ return smp_load_acquire(&ti->rinfo);
+ }
+
+ /* Note that this code below can be run only once by one thread */
+ ret = scmi_telemetry_de_descriptors_get(ti);
+ if (ret) {
+ dev_err(dev, FW_BUG "Cannot fully enumerate DEs resources. Degraded system.\n");
+ goto done;
+ }
+
+ /* Get rinfo without triggering a recursive enumeration */
+ rinfo = __scmi_telemetry_resources_get(ti);
+ rinfo->fully_enumerated = rinfo->num_des == ti->info.base.num_des;
+done:
+ /* Disable initialization permanently */
+ smp_store_release(&ti->res_get, __scmi_telemetry_resources_get);
+
+ /* Unblock concurrent threads that have been stalled */
+ complete_all(&ti->rinfo_initdone);
+
+ /* Ensure local rinfo is visible before returning it */
+ smp_mb();
+ return ti->rinfo;
+}
+
+/**
+ * scmi_telemetry_instance_init - Instance initializer
+ * @ti: A reference to the telemetry info descriptor for this instance
+ *
+ * Note that this allocates and initialize all the resources possibly needed
+ * and then setups the @scmi_telemetry_resources_enumerate helper as the
+ * default method for the first call to ti->res_get(): this mechanism enables
+ * the possibility of optionally implementing deferred enumeration policies
+ * which optionally delay the discovery phase and related SCMI message exchanges
+ * to a later point in time.
+ *
+ * Return: 0 on Success, errno otherwise
+ */
+static int scmi_telemetry_instance_init(struct telemetry_info *ti)
+{
+ int ret;
+
+ ret = scmi_telemetry_resources_alloc(ti);
+ if (ret)
+ return ret;
+
+ /* Setup resources lazy initialization */
+ atomic_set(&ti->rinfo_initializing, 0);
+ init_completion(&ti->rinfo_initdone);
+ /* Ensure the new res_get() operation is visible after this point */
+ smp_store_mb(ti->res_get, scmi_telemetry_resources_enumerate);
+
+ return devm_add_action_or_reset(ti->ph->dev,
+ scmi_telemetry_instance_cleanup, ti);
+}
+
static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
{
struct device *dev = ph->dev;
@@ -133,6 +880,12 @@ static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
return ret;
}
+ ret = scmi_telemetry_instance_init(ti);
+ if (ret) {
+ dev_err(dev, "Cannot initialize instance. Abort.\n");
+ return ret;
+ }
+
ti->info.base.version = ph->version;
return ph->set_priv(ph, ti);
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index f076ce5601cf..cc5bd87705ed 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -830,6 +830,37 @@ struct scmi_pinctrl_proto_ops {
int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin);
};
+struct scmi_telemetry_de_info {
+ unsigned int id;
+ unsigned int grp_id;
+ unsigned int data_sz;
+ unsigned int type;
+ unsigned int unit;
+ int unit_exp;
+ unsigned int ts_rate;
+ unsigned int instance_id;
+ unsigned int compo_instance_id;
+ unsigned int compo_type;
+ bool persistent;
+ char name[16];
+};
+
+struct scmi_telemetry_de {
+ bool tstamp_support;
+ bool fc_support;
+ bool name_support;
+ bool enabled;
+ bool tstamp_enabled;
+ struct scmi_telemetry_de_info *info;
+};
+
+struct scmi_telemetry_res_info {
+ bool fully_enumerated;
+ unsigned int num_des;
+ struct scmi_telemetry_de **des;
+ struct scmi_telemetry_de_info *dei_store;
+};
+
struct scmi_telemetry_base_info {
unsigned int version;
uuid_t primary_revision;
@@ -853,10 +884,16 @@ struct scmi_telemetry_info {
* by SCMI Telemetry Protocol
*
* @info_get: get the general Telemetry information.
+ * @de_lookup: get a specific DE descriptor from the DE id.
+ * @res_get: get a reference to the Telemetry resources descriptor.
*/
struct scmi_telemetry_proto_ops {
const struct scmi_telemetry_info *(*info_get)
(const struct scmi_protocol_handle *ph);
+ const struct scmi_telemetry_de __must_check *(*de_lookup)
+ (const struct scmi_protocol_handle *ph, u32 id);
+ const struct scmi_telemetry_res_info *(*res_get)
+ (const struct scmi_protocol_handle *ph);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 13/34] firmware: arm_scmi: Add Telemetry global update intervals enumeration
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (11 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 12/34] firmware: arm_scmi: Add Telemetry resources enumeration Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 14/34] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration Cristian Marussi
` (21 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add support to enumerate global available Telemetry update intervals.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- avoid FIELD_PREP check on size for exp
v15 --> v16
- split out group update interval enumeration
v14 --> v15
- removed unused dev reference in interval parsing
- added missing initialization of intrvs->grp_id
v13 --> v14
- splitted out of the old Basic Telemetry patch, contains the
update_intervals enumeration routines
---
drivers/firmware/arm_scmi/telemetry.c | 164 ++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 21 ++++
2 files changed, 185 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 94311cdc9398..4ef4e44230d1 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -92,6 +92,21 @@ struct scmi_msg_resp_telemetry_de_description {
struct scmi_de_desc desc[];
};
+struct scmi_msg_telemetry_update_intervals {
+ __le32 index;
+ __le32 group_identifier;
+#define ALL_DES_NO_GROUP 0x0
+#define SPECIFIC_GROUP_DES 0x1
+#define ALL_DES_ANY_GROUP 0x2
+ __le32 flags;
+};
+
+struct scmi_msg_resp_telemetry_update_intervals {
+ __le32 flags;
+#define INTERVALS_DISCRETE(x) (le32_get_bits((x), BIT(12)) == 0)
+ __le32 intervals[];
+};
+
#define IS_TDE_TSTAMP_TYPE(__t, __ttype) ((__t)->ts_type == (__ttype))
#define IS_TDE_TS_NONE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE))
#define IS_TDE_TS_LINE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE))
@@ -625,6 +640,149 @@ static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti)
return 0;
}
+struct scmi_tlm_ivl_priv {
+ struct scmi_telemetry_intervals **intrvs;
+ unsigned int grp_id;
+ unsigned int flags;
+};
+
+static void iter_intervals_prepare_message(void *message,
+ unsigned int desc_index,
+ const void *priv)
+{
+ struct scmi_msg_telemetry_update_intervals *msg = message;
+ const struct scmi_tlm_ivl_priv *p = priv;
+
+ msg->index = cpu_to_le32(desc_index);
+ msg->group_identifier = cpu_to_le32(p->grp_id);
+ msg->flags = cpu_to_le32(FIELD_PREP(GENMASK(3, 0), p->flags));
+}
+
+static int iter_intervals_update_state(struct scmi_iterator_state *st,
+ const void *response, void *priv)
+{
+ const struct scmi_msg_resp_telemetry_update_intervals *r = response;
+
+ st->num_returned = le32_get_bits(r->flags, GENMASK(11, 0));
+ st->num_remaining = le32_get_bits(r->flags, GENMASK(31, 16));
+
+ if (st->rx_len < (sizeof(*r) + sizeof(r->intervals[0]) * st->num_returned))
+ return -EINVAL;
+
+ /*
+ * total intervals is not declared previously anywhere so we
+ * assume it's returned+remaining on first call.
+ */
+ if (!st->max_resources) {
+ struct scmi_tlm_ivl_priv *p = priv;
+ struct scmi_telemetry_intervals *intrvs;
+ bool discrete;
+ int inum;
+
+ discrete = INTERVALS_DISCRETE(r->flags);
+ /* Check consistency on first call */
+ if (!discrete && (st->num_returned != 3 || st->num_remaining != 0))
+ return -EINVAL;
+
+ inum = st->num_returned + st->num_remaining;
+ intrvs = kzalloc_flex(*intrvs, update_intervals, inum);
+ if (!intrvs)
+ return -ENOMEM;
+
+ intrvs->num_intervals = inum;
+ intrvs->discrete = discrete;
+ intrvs->grp_id = p->grp_id;
+ st->max_resources = intrvs->num_intervals;
+
+ *p->intrvs = intrvs;
+ }
+
+ return 0;
+}
+
+static int
+iter_intervals_process_response(const struct scmi_protocol_handle *ph,
+ const void *response,
+ struct scmi_iterator_state *st, void *priv)
+{
+ const struct scmi_msg_resp_telemetry_update_intervals *r = response;
+ struct scmi_tlm_ivl_priv *p = priv;
+ struct scmi_telemetry_intervals *intrvs = *p->intrvs;
+ unsigned int idx = st->loop_idx;
+
+ intrvs->update_intervals[st->desc_index + idx] =
+ le32_to_cpu(r->intervals[idx]);
+
+ return 0;
+}
+
+static int
+scmi_tlm_enumerate_update_intervals(struct telemetry_info *ti,
+ struct scmi_telemetry_intervals **intervals,
+ int grp_id, unsigned int flags)
+{
+ struct scmi_iterator_ops ops = {
+ .prepare_message = iter_intervals_prepare_message,
+ .update_state = iter_intervals_update_state,
+ .process_response = iter_intervals_process_response,
+ };
+ const struct scmi_protocol_handle *ph = ti->ph;
+ struct scmi_telemetry_intervals *intrvs = NULL;
+ struct scmi_tlm_ivl_priv ipriv = {
+ .grp_id = grp_id,
+ .intrvs = &intrvs,
+ .flags = flags,
+ };
+ void *iter;
+ int ret;
+
+ iter = ph->hops->iter_response_init(ph, &ops, 0,
+ TELEMETRY_LIST_UPDATE_INTERVALS,
+ sizeof(struct scmi_msg_telemetry_update_intervals),
+ &ipriv);
+ if (IS_ERR(iter))
+ return PTR_ERR(iter);
+
+ /* Cleanup iterator inner allocations on failure */
+ ret = ph->hops->iter_response_run(iter);
+ if (ret) {
+ kfree(intrvs);
+ return ret;
+ }
+
+ *intervals = intrvs;
+
+ return 0;
+}
+
+static void scmi_telemetry_intervals_free(void *interval)
+{
+ kfree(interval);
+}
+
+static int
+scmi_telemetry_enumerate_common_intervals(struct telemetry_info *ti)
+{
+ unsigned int flags;
+ int ret;
+
+ flags = !ti->info.per_group_config_support ?
+ ALL_DES_ANY_GROUP : ALL_DES_NO_GROUP;
+
+ ret = scmi_tlm_enumerate_update_intervals(ti, &ti->info.intervals,
+ SCMI_TLM_GRP_INVALID, flags);
+ if (ret)
+ return ret;
+
+ /* A copy for UAPI access... */
+ ti->info.base.num_intervals = ti->info.intervals->num_intervals;
+
+ /* Delegate freeing of allocated intervals to unbind time */
+ return devm_add_action_or_reset(ti->ph->dev,
+ scmi_telemetry_intervals_free,
+ ti->info.intervals);
+}
+
static const struct scmi_telemetry_info *
scmi_telemetry_info_get(const struct scmi_protocol_handle *ph)
{
@@ -886,6 +1044,12 @@ static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
return ret;
}
+ ret = scmi_telemetry_enumerate_common_intervals(ti);
+ if (ret) {
+ dev_err(dev, FW_BUG "Cannot enumerate update intervals. Abort.\n");
+ return ret;
+ }
+
ti->info.base.version = ph->version;
return ph->set_priv(ph, ti);
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index cc5bd87705ed..a373b782f05e 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -830,6 +830,25 @@ struct scmi_pinctrl_proto_ops {
int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin);
};
+#define SCMI_TLM_GET_UPDATE_INTERVAL_SECS(x) (FIELD_GET(GENMASK(20, 5), (x)))
+#define SCMI_TLM_GET_UPDATE_INTERVAL_EXP(x) (sign_extend32((x), 4))
+
+#define SCMI_TLM_GET_UPDATE_INTERVAL(x) (FIELD_GET(GENMASK(20, 0), (x)))
+#define SCMI_TLM_BUILD_UPDATE_INTERVAL(s, e) \
+ (FIELD_PREP(GENMASK(20, 5), (s)) | FIELD_PREP(GENMASK(4, 0), (e) & GENMASK(4, 0)))
+
+#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF
+
+struct scmi_telemetry_intervals {
+ unsigned int grp_id;
+ bool discrete;
+ unsigned int num_intervals;
+#define SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW 0
+#define SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH 1
+#define SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP 2
+ unsigned int update_intervals[] __counted_by(num_intervals);
+};
+
struct scmi_telemetry_de_info {
unsigned int id;
unsigned int grp_id;
@@ -877,6 +896,8 @@ struct scmi_telemetry_info {
bool reset_support;
bool fc_support;
struct scmi_telemetry_base_info base;
+ unsigned int active_update_interval;
+ struct scmi_telemetry_intervals *intervals;
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 14/34] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (12 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 13/34] firmware: arm_scmi: Add Telemetry global update intervals enumeration Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 15/34] firmware: arm_scmi: Add Telemetry TDCF areas parsing support Cristian Marussi
` (20 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add Telemetry support to enumerate SHMTI areas.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- use %pa for phys_addr
- skip LIST_SHMTI with num_shmti = 0
- fix %pa and spell USPECIFIED
v15 --> v16
- fixed missing \n in dev_err
v14 --> v15
- fix prologue/epilogue endianity fields
- rework SHMTI initialization and allocation
- add snapshot suppport data structures
- added alignment checks on SHMTII length size
- fixed error string formatting
- warn if less than expected SHMTIs are enumerated
- improve memory area sanity checks
v13 --> v14
- splitted out of the old Basic Telemetry patch, contains the
SHMTI areas enumeration routines
---
drivers/firmware/arm_scmi/telemetry.c | 312 ++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 59 +++++
2 files changed, 371 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 4ef4e44230d1..62e7970f64af 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -16,6 +16,7 @@
#include <linux/limits.h>
#include <linux/module.h>
#include <linux/mutex.h>
+#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/sprintf.h>
#include <linux/string.h>
@@ -56,6 +57,19 @@ struct scmi_msg_resp_telemetry_protocol_attributes {
__le32 default_blk_ts_rate;
};
+struct scmi_shmti_desc {
+ __le32 id;
+ __le32 addr_low;
+ __le32 addr_high;
+ __le32 length;
+ __le32 flags;
+};
+
+struct scmi_msg_resp_telemetry_shmti_list {
+ __le32 num_shmti;
+ struct scmi_shmti_desc desc[];
+};
+
struct de_desc_fastchannel {
__le32 addr_low;
__le32 addr_high;
@@ -107,6 +121,72 @@ struct scmi_msg_resp_telemetry_update_intervals {
__le32 intervals[];
};
+/* TDCF */
+
+#define _I(__a) (ioread32((void __iomem *)(__a)))
+
+struct prlg {
+ __le32 sign_start;
+#define SIGNATURE_START 0x5442474E /* TBGN */
+ __le32 match_start;
+ __le32 num_qwords;
+ __le32 hdr_meta_1;
+#define TDCF_REVISION_GET(x) (le32_get_bits((x)->hdr_meta_1, GENMASK(7, 0)))
+};
+
+struct eplg {
+ __le32 match_end;
+ __le32 sign_end;
+#define SIGNATURE_END 0x54454E44 /* TEND */
+};
+
+#define TDCF_EPLG_SZ (sizeof(struct eplg))
+
+struct tdcf {
+ struct prlg prlg;
+ unsigned char payld[];
+};
+
+#define SHMTI_MIN_SIZE (sizeof(struct tdcf) + TDCF_EPLG_SZ)
+/* An arbitrary upper bound to avoid crazy allocations driven by buggy FW */
+#define SHMTI_MAX_SIZE SZ_64K
+
+#define TDCF_START_SIGNATURE(x) (_I(&((x)->prlg.sign_start)))
+#define TDCF_END_SIGNATURE(e) (_I(&((e)->sign_end)))
+
+struct telemetry_shmti;
+
+struct shmti_snap {
+ struct telemetry_shmti *shmti;
+ u32 magic;
+ u8 base[] __aligned(8);
+};
+
+struct telemetry_shmti {
+ u32 flags;
+ void __iomem *base;
+ struct scmi_telemetry_shmti_info info;
+ /* Protect srcu operation */
+ struct mutex lock;
+ struct srcu_struct srcu;
+ struct shmti_snap *snaps[2];
+ struct shmti_snap __rcu *cur;
+ /* Mutex to protect @xa_lines */
+ struct mutex lines_mtx;
+ struct xarray xa_lines;
+ #define PAYLD_HASH_BITS 4
+ struct mutex payld_locks[1 << PAYLD_HASH_BITS];
+};
+
+#define SHMTI_EPLG(s) \
+ ({ \
+ struct telemetry_shmti *_s = (s); \
+ struct eplg __iomem *_eplg; \
+ \
+ _eplg = _s->base + _s->info.len - TDCF_EPLG_SZ; \
+ (_eplg); \
+ })
+
#define IS_TDE_TSTAMP_TYPE(__t, __ttype) ((__t)->ts_type == (__ttype))
#define IS_TDE_TS_NONE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE))
#define IS_TDE_TS_LINE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE))
@@ -124,6 +204,7 @@ struct telemetry_de {
enum timestamps ts_type;
bool enumerated;
void __iomem *base;
+ struct eplg __iomem *eplg;
u32 sid;
u32 offset;
/* NOTE THAT DE data_sz is registered in scmi_telemetry_de */
@@ -142,6 +223,7 @@ struct telemetry_info {
unsigned int num_des_tstamp;
unsigned int default_blk_ts_rate;
const struct scmi_protocol_handle *ph;
+ struct telemetry_shmti *shmti;
struct telemetry_de *tdes;
struct xarray xa_des;
/* Mutex to protect access to @free_des */
@@ -783,6 +865,211 @@ scmi_telemetry_enumerate_common_intervals(struct telemetry_info *ti)
ti->info.intervals);
}
+static int iter_shmti_update_state(struct scmi_iterator_state *st,
+ const void *response, void *priv)
+{
+ const struct scmi_msg_resp_telemetry_shmti_list *r = response;
+
+ st->num_returned = le32_get_bits(r->num_shmti, GENMASK(15, 0));
+ st->num_remaining = le32_get_bits(r->num_shmti, GENMASK(31, 16));
+
+ if (st->rx_len < (sizeof(*r) + sizeof(r->desc[0]) * st->num_returned))
+ return -EINVAL;
+
+ return 0;
+}
+
+static inline int
+scmi_telemetry_shmti_validate(struct device *dev, struct telemetry_shmti *shmti)
+{
+ struct tdcf __iomem *tdcf = shmti->base;
+ u32 sign_start, sign_end;
+
+ sign_start = TDCF_START_SIGNATURE(tdcf);
+ sign_end = TDCF_END_SIGNATURE(SHMTI_EPLG(shmti));
+
+ if (sign_start != SIGNATURE_START || sign_end != SIGNATURE_END) {
+ dev_err(dev,
+ "BAD signature for SHMTI ID:%u @phys:%pa - START:0x%08X END:0x%08X\n",
+ shmti->info.sid, &shmti->info.phys, sign_start, sign_end);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int iter_shmti_process_response(const struct scmi_protocol_handle *ph,
+ const void *response,
+ struct scmi_iterator_state *st,
+ void *priv)
+{
+ const struct scmi_msg_resp_telemetry_shmti_list *r = response;
+ struct telemetry_info *ti = priv;
+ struct telemetry_shmti *shmti;
+ const struct scmi_shmti_desc *desc;
+ void __iomem *addr;
+ phys_addr_t phys_addr;
+ size_t len;
+ int ret;
+
+ desc = &r->desc[st->loop_idx];
+ shmti = &ti->shmti[st->desc_index + st->loop_idx];
+
+ shmti->info.sid = le32_to_cpu(desc->id);
+ shmti->flags = le32_to_cpu(desc->flags);
+
+ ret = scmi_telemetry_area_check(ph->dev, "SHMTI",
+ le32_to_cpu(desc->addr_low),
+ le32_to_cpu(desc->addr_high),
+ le32_to_cpu(desc->length),
+ &phys_addr, &len);
+ if (ret) {
+ dev_err(ph->dev, "Malformed SHMTI ID:%u. Abort.\n",
+ shmti->info.sid);
+ return ret;
+ }
+
+ if (len < SHMTI_MIN_SIZE || len > SHMTI_MAX_SIZE) {
+ dev_err(ph->dev,
+ FW_BUG "Invalid length for SHMTI ID:%u len:%zu. Abort.\n",
+ shmti->info.sid, len);
+ return -EINVAL;
+ }
+
+ /* SHMTI base phys_addr SHOULD be 64bit aligned by the SCMI spec */
+ if (!IS_ALIGNED(phys_addr, 8))
+ dev_warn(ph->dev,
+ "SHMTI ID:%u is NOT 64bit aligned (paddr:%pa)\n",
+ shmti->info.sid, &phys_addr);
+
+ addr = devm_ioremap(ph->dev, phys_addr, len);
+ if (!addr)
+ return -EADDRNOTAVAIL;
+
+ shmti->base = addr;
+ shmti->info.len = len;
+ shmti->info.phys = phys_addr;
+ shmti->info.offset = shmti->info.phys & ~PAGE_MASK;
+
+ ret = scmi_telemetry_shmti_validate(ph->dev, shmti);
+ if (ret)
+ return ret;
+
+ ti->num_shmti++;
+
+ return 0;
+}
+
+static int scmi_telemetry_shmti_list(const struct scmi_protocol_handle *ph,
+ struct telemetry_info *ti)
+{
+ struct scmi_iterator_ops ops = {
+ .prepare_message = iter_tlm_prepare_message,
+ .update_state = iter_shmti_update_state,
+ .process_response = iter_shmti_process_response,
+ };
+ int num_shmti;
+ void *iter;
+ int ret;
+
+ if (!ti->num_shmti)
+ return 0;
+
+ /* Save and zero...calculate real number of returned */
+ num_shmti = ti->num_shmti;
+ ti->num_shmti = 0;
+ iter = ph->hops->iter_response_init(ph, &ops, num_shmti,
+ TELEMETRY_LIST_SHMTI,
+ sizeof(u32), ti);
+ if (IS_ERR(iter))
+ return PTR_ERR(iter);
+
+ ret = ph->hops->iter_response_run(iter);
+ if (ret)
+ return ret;
+
+ if (num_shmti != ti->num_shmti)
+ dev_warn(ph->dev,
+ FW_BUG "SHMTI enumeration returned %u items, expected %u\n",
+ ti->num_shmti, num_shmti);
+
+ return 0;
+}
+
+static void scmi_telemetry_shmti_cleanup(void *arg)
+{
+ struct telemetry_shmti *shmti = arg;
+
+ cleanup_srcu_struct(&shmti->srcu);
+
+ kvfree(shmti->snaps[0]);
+ kvfree(shmti->snaps[1]);
+
+ xa_destroy(&shmti->xa_lines);
+}
+
+static int scmi_telemetry_shmti_init(struct device *dev,
+ struct telemetry_shmti *shmti)
+{
+ int ret;
+
+ struct shmti_snap *s0 __free(kvfree) =
+ kvzalloc_flex(*s0, base, shmti->info.len);
+ if (!s0)
+ return -ENOMEM;
+
+ struct shmti_snap *s1 __free(kvfree) =
+ kvzalloc_flex(*s1, base, shmti->info.len);
+ if (!s1)
+ return -ENOMEM;
+
+ ret = init_srcu_struct(&shmti->srcu);
+ if (ret)
+ return ret;
+
+ for (int i = 0; i < 1 << PAYLD_HASH_BITS; i++)
+ mutex_init(&shmti->payld_locks[i]);
+
+ mutex_init(&shmti->lock);
+ RCU_INIT_POINTER(shmti->cur, NULL);
+
+ mutex_init(&shmti->lines_mtx);
+ xa_init(&shmti->xa_lines);
+
+ s0->shmti = shmti;
+ shmti->snaps[0] = no_free_ptr(s0);
+
+ s1->shmti = shmti;
+ shmti->snaps[1] = no_free_ptr(s1);
+
+ return devm_add_action_or_reset(dev, scmi_telemetry_shmti_cleanup, shmti);
+}
+
+static int scmi_telemetry_enumerate_shmti(struct telemetry_info *ti)
+{
+ const struct scmi_protocol_handle *ph = ti->ph;
+ int ret;
+
+ ret = scmi_telemetry_shmti_list(ph, ti);
+ if (ret) {
+ dev_err(ph->dev, "Cannot get SHMTI list descriptors\n");
+ return ret;
+ }
+
+ for (int i = 0; i < ti->num_shmti; i++) {
+ ret = scmi_telemetry_shmti_init(ph->dev, &ti->shmti[i]);
+ if (ret)
+ return ret;
+
+ ti->info.shmtis[i] = &ti->shmti[i].info;
+ }
+
+ /* Expose the fully initialized SHMTI infos */
+ smp_store_release(&ti->info.base.num_shmtis, ti->num_shmti);
+
+ return 0;
+}
+
static const struct scmi_telemetry_info *
scmi_telemetry_info_get(const struct scmi_protocol_handle *ph)
{
@@ -836,7 +1123,20 @@ static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
* Return: 0 on Success, errno otherwise
*/
static int scmi_telemetry_resources_alloc(struct telemetry_info *ti)
+
{
+ /* Array to hold SHMTIs descriptors */
+ struct telemetry_shmti *shmtis __free(kfree) =
+ kzalloc_objs(*shmtis, ti->num_shmti);
+ if (!shmtis)
+ return -ENOMEM;
+
+ /* Array of initialized SHMTIs references */
+ struct scmi_telemetry_shmti_info **shmtis_info __free(kfree) =
+ kzalloc_objs(*shmtis_info, ti->num_shmti);
+ if (!shmtis_info)
+ return -ENOMEM;
+
/* Array to hold pointers to discovered DEs */
struct scmi_telemetry_de **des __free(kfree) =
kzalloc_objs(*des, ti->info.base.num_des);
@@ -871,6 +1171,9 @@ static int scmi_telemetry_resources_alloc(struct telemetry_info *ti)
INIT_LIST_HEAD(&ti->fcs_des);
+ ti->shmti = no_free_ptr(shmtis);
+ ti->info.shmtis = no_free_ptr(shmtis_info);
+
ti->tdes = no_free_ptr(tdes);
xa_init(&ti->xa_des);
@@ -902,6 +1205,9 @@ static void scmi_telemetry_resources_free(struct telemetry_info *ti)
scmi_telemetry_tdes_cleanup(ti);
xa_destroy(&ti->xa_des);
+ kfree(ti->info.shmtis);
+ kfree(ti->shmti);
+
kfree(ti->tdes);
kfree(rinfo->des);
@@ -1050,6 +1356,12 @@ static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
return ret;
}
+ ret = scmi_telemetry_enumerate_shmti(ti);
+ if (ret) {
+ dev_err(dev, FW_BUG "Cannot enumerate SHMTIs. Abort.\n");
+ return ret;
+ }
+
ti->info.base.version = ph->version;
return ph->set_priv(ph, ti);
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index a373b782f05e..4a765a529ebf 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -830,6 +830,57 @@ struct scmi_pinctrl_proto_ops {
int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin);
};
+enum scmi_telemetry_de_type {
+ SCMI_TLM_DE_TYPE_UNSPECIFIED,
+ SCMI_TLM_DE_TYPE_ACCUMUL_IDLE_RESIDENCY,
+ SCMI_TLM_DE_TYPE_ACCUMUL_IDLE_COUNTS,
+ SCMI_TLM_DE_TYPE_ACCUMUL_OTHERS,
+ SCMI_TLM_DE_TYPE_INSTA_IDLE_STATE,
+ SCMI_TLM_DE_TYPE_INSTA_OTHERS,
+ SCMI_TLM_DE_TYPE_AVERAGE,
+ SCMI_TLM_DE_TYPE_STATUS,
+ SCMI_TLM_DE_TYPE_RESERVED_START,
+ SCMI_TLM_DE_TYPE_RESERVED_END = 0xef,
+ SCMI_TLM_DE_TYPE_OEM_START = 0xf0,
+ SCMI_TLM_DE_TYPE_OEM_END = 0xff,
+};
+
+enum scmi_telemetry_compo_type {
+ SCMI_TLM_COMPO_TYPE_UNSPECIFIED,
+ SCMI_TLM_COMPO_TYPE_CPU,
+ SCMI_TLM_COMPO_TYPE_CLUSTER,
+ SCMI_TLM_COMPO_TYPE_GPU,
+ SCMI_TLM_COMPO_TYPE_NPU,
+ SCMI_TLM_COMPO_TYPE_INTERCONNECT,
+ SCMI_TLM_COMPO_TYPE_MEM_CNTRL,
+ SCMI_TLM_COMPO_TYPE_L1_CACHE,
+ SCMI_TLM_COMPO_TYPE_L2_CACHE,
+ SCMI_TLM_COMPO_TYPE_L3_CACHE,
+ SCMI_TLM_COMPO_TYPE_LL_CACHE,
+ SCMI_TLM_COMPO_TYPE_SYS_CACHE,
+ SCMI_TLM_COMPO_TYPE_DISP_CNTRL,
+ SCMI_TLM_COMPO_TYPE_IPU,
+ SCMI_TLM_COMPO_TYPE_CHIPLET,
+ SCMI_TLM_COMPO_TYPE_PACKAGE,
+ SCMI_TLM_COMPO_TYPE_SOC,
+ SCMI_TLM_COMPO_TYPE_SYSTEM,
+ SCMI_TLM_COMPO_TYPE_SMCU,
+ SCMI_TLM_COMPO_TYPE_ACCEL,
+ SCMI_TLM_COMPO_TYPE_BATTERY,
+ SCMI_TLM_COMPO_TYPE_CHARGER,
+ SCMI_TLM_COMPO_TYPE_PMIC,
+ SCMI_TLM_COMPO_TYPE_BOARD,
+ SCMI_TLM_COMPO_TYPE_MEMORY,
+ SCMI_TLM_COMPO_TYPE_PERIPH,
+ SCMI_TLM_COMPO_TYPE_PERIPH_SUBC,
+ SCMI_TLM_COMPO_TYPE_LID,
+ SCMI_TLM_COMPO_TYPE_DISPLAY,
+ SCMI_TLM_COMPO_TYPE_RESERVED_START = 0x1d,
+ SCMI_TLM_COMPO_TYPE_RESERVED_END = 0xdf,
+ SCMI_TLM_COMPO_TYPE_OEM_START = 0xe0,
+ SCMI_TLM_COMPO_TYPE_OEM_END = 0xff,
+};
+
#define SCMI_TLM_GET_UPDATE_INTERVAL_SECS(x) (FIELD_GET(GENMASK(20, 5), (x)))
#define SCMI_TLM_GET_UPDATE_INTERVAL_EXP(x) (sign_extend32((x), 4))
@@ -889,6 +940,13 @@ struct scmi_telemetry_base_info {
unsigned int num_shmtis;
};
+struct scmi_telemetry_shmti_info {
+ unsigned int sid;
+ unsigned int len;
+ unsigned long offset;
+ phys_addr_t phys;
+};
+
struct scmi_telemetry_info {
bool single_read_support;
bool continuous_update_support;
@@ -898,6 +956,7 @@ struct scmi_telemetry_info {
struct scmi_telemetry_base_info base;
unsigned int active_update_interval;
struct scmi_telemetry_intervals *intervals;
+ struct scmi_telemetry_shmti_info **shmtis;
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 15/34] firmware: arm_scmi: Add Telemetry TDCF areas parsing support
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (13 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 14/34] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 16/34] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines Cristian Marussi
` (19 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add logic to scan the SHMTI areas, parsing the TDCF descriptors and collect
DataEvent contained in TDCF DATA lines. Ignore TDCF BLK_TS and UUID lines.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- check if used_qwords > avail_qwords early in tdcf_line_parse
- add __iomem eplg annotation
- fix shmti_overflow and make it return the line length
- remove WARN_ON(fc_support), better dev_
- log error on malformed line data
v15 --> v16
- fix des_enabled format string
- make magic const in caching helpers
- drop useless __iomem useless cast in de_offs
- improve qwords overflow check on qwords in data_line_parse
- consider cache_stale BAD_START_SEQ(last_magic)
- fix offset_safe_calc helper
- discard and WARN when DE/FC are found on SHMTI
- remove unused SCAN_DISCOVERY
- shmti_oveflow use eplg directly
v14 --> v15
- account for tstamped TDE during discovery
- correct SHMTI overflow off-by-one
- rework TDE cache hit/miss logic
- introduce snapshots on full scan reads
- validate DE location on SCAN_UPDATE
- add custom SNAP eplg and overflow helpers
- moved here definition of scmi_telemetry_payld_offset_safe_calc
v13 --> v14
- fixed AB-BA dealock on blkts_link
- relocate xa_destroy for xa_lines after uuids cleanup
- report errors on scmi_telemetry_line_data_parse()
- moved some common protocol definitions here from previous
patches
- split out of this patch all of the BLK_TS and UUID lines
management, keeping only DATA line processing and delegating
the other line types to following smaller patches
- using a less regular time interval for TDCF scan retries
v12 --> v13
- refactored cleanup resources
- relocate tde->mtx guards higher up and add lockdep assertions
- drop refcount on previous active lines when scanning
- harden freeing path of UUIDs RCU items
- better DATA LINE size-validation while parsing
- reset active lines trackers on scan retries
- validate used_qwords
- redefine startm/endm bad sentinels and error path
- add more BLKTS_INVALID checks on parsing
- unlink stale BTS line when finding a new actibe for the TDE
- retry scans on TDCF sanity failures..could be a torn read
- use *alloc_objs
- remove TO_CPU64
v11 --> v12
- reject zero as MATCHSEQ
- reworked locking around line_get/put
- reworked locking around blkts_read
- a few comments on locking acquisition order
- cleanup active line refs even when bailing out of a scan in error
- added hashed locks for fine grain locking around blkts/uuid line creation
- added RCU protection on UUIDs access
v10 --> v11
- relocated some line handlers from another patch
- reworked TDE and lines locking logic
- fix UUID/BLKTS line refcounting to properly drop unused lines
- add a mutex to protect des cache accesses
v7 --> v9
- avoid referencing a NULL in trace_scmi_tlm_access O_o
- refactor scmi_telemetry_line_put to free its container on release
- convert SHMTI_OVERFLOW macro to an inline function
v7 --> v8
- Fixes for some Sashiko issues on v7
- reword a few dev_errs
- removed a few unused function (moved to next patch)
- add more safeguards and checks to SHMTI_OVERFLOWS
- fix UUIDs copy and allocation
- fix UUID reference store and cleanup
- cleanup all UNBOUND lines and their containers in resources_free
- remove mutex on primary init
v6 --> v7
- add _RAW helpers to manage non MMIO accesses
- sparse fixes
- use uuid_t for UUID lines
- fix proper UUID line parsing and binding
- adding a few traces for TDCF line handling
- add new dynamically sized UUID DB to ease discovered UUIDs lookup
- fix endianity handling in UUID TDCF lines
- handle Primary UUID lines with regular uuid_line
v5 --> v6
- consider UUID line in TDCF as BE when scanning
- use const booleans in scmi_telemetry_de_state_update()
- fix Sashiko review https://sashiko.dev/#/patchset/20260703123601.381275-1-cristian.marussi%40arm.com?part=8
- check SHMTI overflow while parsing
- use proper bytes increment while parsing line_by_type
- use mutex guard when link BLK_TS lines
v4 --> v5
- converted TDE_HAS_TSTAMP to inline scmi_tde_has_tstamp
- fixes sparse warnings
v3 --> v4
- use kzalloc_obj
- track SHMTI-discovered Telemetry DE states
- refactor TDE cache with dedicated helpers
- force tstamp to zero when timestamp is NOT supported or disabled
v2 --> v3
- split from monolithic Telemetry patch
- avoid devres allocation for resources that are added to the xa_lines XArray
- simplify prototype of line parsing helpers to drop unneeded dev
- flip tstmap logic in scmi_telemetry_line_data_parse() to properly emit
a TLM ftrace event
- use ternary ops to simplify quite a few expressions
---
drivers/firmware/arm_scmi/telemetry.c | 756 +++++++++++++++++++++++++-
1 file changed, 755 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 62e7970f64af..ba4ba1d2c00c 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -14,21 +14,29 @@
#include <linux/err.h>
#include <linux/io.h>
#include <linux/limits.h>
+#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/mutex.h>
+#include <linux/refcount.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/sprintf.h>
#include <linux/string.h>
+#include <linux/time.h>
#include <linux/uuid.h>
#include <linux/xarray.h>
#include "protocols.h"
#include "notify.h"
+#include <trace/events/scmi.h>
+
/* Updated only after ALL the mandatory features for that version are merged */
#define SCMI_PROTOCOL_SUPPORTED_VERSION 0x10000
+#define SCMI_TLM_TDCF_MAX_RETRIES 15
+#define SCMI_TLM_TDCF_RETRY_GAP_US 1234
+
#define SCMI_TLM_DE_IMPL_NUM_DWORDS 4
/* An enum listing the protocol specific commands definitions */
@@ -125,6 +133,157 @@ struct scmi_msg_resp_telemetry_update_intervals {
#define _I(__a) (ioread32((void __iomem *)(__a)))
+/*
+ * Define the behaviour of a SHMTI scan defining what information will
+ * be gathered and which Telemetry items can be updated.
+ */
+enum scan_mode {
+ SCAN_LOOKUP, /* Update only value/tstamp */
+ SCAN_UPDATE, /* Update also location offset */
+};
+
+struct fc_line {
+ __le32 data_low;
+ __le32 data_high;
+};
+
+struct fc_tsline {
+ __le32 data_low;
+ __le32 data_high;
+ __le32 ts_low;
+ __le32 ts_high;
+};
+
+struct line {
+ __le32 data_low;
+ __le32 data_high;
+};
+
+struct tsline {
+ __le32 data_low;
+ __le32 data_high;
+ __le32 ts_low;
+ __le32 ts_high;
+};
+
+#define LINE_DATA_GET_RAW(f) \
+({ \
+ typeof(f) _f = (f); \
+ u32 _high = le32_to_cpu(_f->data_high); \
+ u32 _low = le32_to_cpu(_f->data_low); \
+ u64 _res; \
+ \
+ _res = (((u64)(_high)) << 32) | (_low); \
+ _res; \
+})
+
+#define LINE_DATA_GET(f) \
+({ \
+ typeof(f) _f = (f); \
+ u32 _high = _I(&_f->data_high); \
+ u32 _low = _I(&_f->data_low); \
+ u64 _res; \
+ \
+ _res = (((u64)(_high)) << 32) | (_low); \
+ _res; \
+})
+
+#define LINE_TSTAMP_GET_RAW(f) \
+({ \
+ typeof(f) _f = (f); \
+ u32 _high = le32_to_cpu(_f->ts_high); \
+ u32 _low = le32_to_cpu(_f->ts_low); \
+ u64 _res; \
+ \
+ _res = (((u64)(_high)) << 32) | (_low); \
+ _res; \
+})
+
+#define LINE_TSTAMP_GET(f) \
+({ \
+ typeof(f) _f = (f); \
+ u32 _high = _I(&_f->ts_high); \
+ u32 _low = _I(&_f->ts_low); \
+ u64 _res; \
+ \
+ _res = (((u64)(_high)) << 32) | (_low); \
+ _res; \
+})
+
+#define BLK_TS_STAMP(f) LINE_TSTAMP_GET(f)
+#define BLK_TS_STAMP_RAW(f) LINE_TSTAMP_GET_RAW(f)
+#define BLK_TS_RATE(p) PAYLD_ID(p)
+#define BLK_TS_RATE_RAW(p) PAYLD_ID_RAW(p)
+
+enum tdcf_line_types {
+ TDCF_DATA_LINE = 0,
+ TDCF_BLK_TS_LINE = 1,
+ TDCF_UUID_LINE = 2,
+};
+
+struct payload {
+ __le32 meta;
+#define PAYLD_META(x) (_I(&((x)->meta)))
+#define PAYLD_META_RAW(x) (le32_to_cpu((x)->meta))
+#define LINE_TYPE_RAW(m) (FIELD_GET(GENMASK(7, 4), (m)))
+#define LINE_TYPE(x) (LINE_TYPE_RAW(PAYLD_META(x)))
+#define IS_DATA_LINE(x) (LINE_TYPE(x) == TDCF_DATA_LINE)
+#define IS_DATA_LINE_RAW(x) (LINE_TYPE_RAW(x) == TDCF_DATA_LINE)
+#define IS_BLK_TS_LINE(x) (LINE_TYPE(x) == TDCF_BLK_TS_LINE)
+#define IS_BLK_TS_LINE_RAW(x) (LINE_TYPE_RAW(x) == TDCF_BLK_TS_LINE)
+#define IS_UUID_LINE(x) (LINE_TYPE(x) == TDCF_UUID_LINE)
+#define USE_BLK_TS_RAW(m) ((m) & BIT(3))
+#define USE_BLK_TS(x) (USE_BLK_TS_RAW(PAYLD_META(x)))
+#define HAS_LINE_EXT_RAW(m) ((m) & BIT(2))
+#define HAS_LINE_EXT(x) (HAS_LINE_EXT_RAW(PAYLD_META(x)))
+#define LINE_TS_VALID_RAW(m) ((m) & BIT(1))
+#define LINE_TS_VALID(x) (LINE_TS_VALID_RAW(PAYLD_META(x)))
+#define DATA_INVALID_RAW(m) ((m) & BIT(0))
+#define DATA_INVALID(x) (DATA_INVALID_RAW(PAYLD_META(x)))
+#define BLK_TS_INVALID_RAW(m) \
+({ \
+ bool invalid; \
+ \
+ invalid = LINE_TS_VALID_RAW(m) || HAS_LINE_EXT_RAW(m) || \
+ USE_BLK_TS_RAW(m) || DATA_INVALID_RAW(m); \
+ invalid; \
+})
+#define BLK_TS_INVALID(p) BLK_TS_INVALID_RAW(PAYLD_META(p))
+
+#define UUID_INVALID_RAW(m) \
+({ \
+ bool invalid; \
+ \
+ invalid = LINE_TS_VALID_RAW(m) || USE_BLK_TS_RAW(m) || \
+ DATA_INVALID_RAW(m) || !HAS_LINE_EXT_RAW(m); \
+ invalid; \
+})
+#define UUID_INVALID(p) UUID_INVALID_RAW(PAYLD_META(p))
+ __le32 id;
+ union {
+ struct line l;
+ struct tsline tsl;
+ };
+};
+
+#define PAYLD_ID(x) (_I(&(((struct payload __iomem *)(x))->id)))
+#define PAYLD_ID_RAW(x) (le32_to_cpu((((struct payload *)(x))->id)))
+
+#define LINE_DATA_PAYLD_WORDS \
+ ((sizeof(u32) + sizeof(u32) + sizeof(struct line)) / sizeof(u32))
+#define EXT_LINE_DATA_PAYLD_WORDS \
+ ((sizeof(u32) + sizeof(u32) + sizeof(struct tsline)) / sizeof(u32))
+
+#define LINE_LENGTH_WORDS_RAW(m) \
+ (HAS_LINE_EXT_RAW((m)) ? EXT_LINE_DATA_PAYLD_WORDS : LINE_DATA_PAYLD_WORDS)
+
+#define LINE_LENGTH_WORDS(x) LINE_LENGTH_WORDS_RAW(PAYLD_META(x))
+#define LINE_LENGTH_QWORDS(x) ((LINE_LENGTH_WORDS(x)) / 2)
+#define LINE_LENGTH_QWORDS_RAW(x) ((LINE_LENGTH_WORDS_RAW(x)) / 2)
+
+#define LINE_LENGTH_BYTES_RAW(m) ((LINE_LENGTH_WORDS_RAW(m)) * sizeof(u32))
+#define LINE_LENGTH_BYTES(x) (LINE_LENGTH_BYTES_RAW(PAYLD_META(x)))
+
struct prlg {
__le32 sign_start;
#define SIGNATURE_START 0x5442474E /* TBGN */
@@ -147,12 +306,24 @@ struct tdcf {
unsigned char payld[];
};
+#define QWORDS(_t) (_I(&(_t)->prlg.num_qwords))
+#define QWORDS_RAW(_t) (le32_to_cpu((_t)->prlg.num_qwords))
+
#define SHMTI_MIN_SIZE (sizeof(struct tdcf) + TDCF_EPLG_SZ)
/* An arbitrary upper bound to avoid crazy allocations driven by buggy FW */
#define SHMTI_MAX_SIZE SZ_64K
-#define TDCF_START_SIGNATURE(x) (_I(&((x)->prlg.sign_start)))
+#define TDCF_START_SIGNATURE(x) (_I(&((x)->prlg.sign_start)))
+#define TDCF_START_SEQ_GET(x) (_I(&((x)->prlg.match_start)))
+#define TDCF_START_SEQ_GET_RAW(x) (le32_to_cpu((x)->prlg.match_start))
+/* TDCF MatchSequence cannot be zero by the SCMI v4.0 Specification */
+#define TDCF_INVALID_MATCH_SEQ 0
+#define IS_BAD_START_SEQ(s) ((s) & 0x1 || (s) == TDCF_INVALID_MATCH_SEQ)
+
+#define TDCF_END_SEQ_GET(e) (_I(&((e)->match_end)))
+#define TDCF_END_SEQ_GET_RAW(e) (le32_to_cpu((e)->match_end))
#define TDCF_END_SIGNATURE(e) (_I(&((e)->sign_end)))
+#define TDCF_BAD_END_SEQ GENMASK(31, 0)
struct telemetry_shmti;
@@ -178,6 +349,9 @@ struct telemetry_shmti {
struct mutex payld_locks[1 << PAYLD_HASH_BITS];
};
+#define TDCF_OFFS(_b, _p) \
+ ((unsigned long)((__force void *)(_p) - (__force void *)(_b)))
+
#define SHMTI_EPLG(s) \
({ \
struct telemetry_shmti *_s = (s); \
@@ -187,6 +361,44 @@ struct telemetry_shmti {
(_eplg); \
})
+#define SNAP_EPLG(s) \
+ ({ \
+ struct shmti_snap *_s = (s); \
+ struct eplg *_eplg; \
+ \
+ _eplg = (void *)_s->base + _s->shmti->info.len - TDCF_EPLG_SZ; \
+ (_eplg); \
+ })
+
+struct telemetry_info;
+
+struct telemetry_line {
+ enum tdcf_line_types type;
+ u32 last_magic;
+ unsigned long offset;
+ refcount_t users;
+ struct telemetry_shmti *shmti;
+ /* Protect line accesses */
+ struct mutex mtx;
+};
+
+struct telemetry_block_ts {
+ u64 last_ts;
+ u32 last_rate;
+ struct telemetry_line line;
+};
+
+#define to_blkts(l) container_of(l, struct telemetry_block_ts, line)
+
+struct telemetry_uuid {
+ u32 key;
+ struct hlist_node node_ht;
+ struct hlist_node node_lst;
+ uuid_t uuid;
+};
+
+#define to_uuid_from_uuid_t(u) container_of(u, struct telemetry_uuid, uuid)
+
#define IS_TDE_TSTAMP_TYPE(__t, __ttype) ((__t)->ts_type == (__ttype))
#define IS_TDE_TS_NONE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE))
#define IS_TDE_TS_LINE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE))
@@ -209,7 +421,14 @@ struct telemetry_de {
u32 offset;
/* NOTE THAT DE data_sz is registered in scmi_telemetry_de */
u32 fc_size;
+ u64 last_val;
+ u64 last_ts;
+ u32 last_magic;
struct list_head item;
+ struct telemetry_block_ts *bts;
+#define BLK_TS_CHANGED(t, s, o) \
+ (!((t)->bts) || (t)->bts->line.shmti != (s) || (t)->bts->line.offset != (o))
+ struct telemetry_uuid *uuid;
struct scmi_telemetry_de de;
/* Protect last_val/ts/magic accesses - MUST BE KEPT LAST */
struct mutex mtx;
@@ -217,10 +436,24 @@ struct telemetry_de {
#define to_tde(d) container_of(d, struct telemetry_de, de)
+static inline bool scmi_tde_has_tstamp(struct telemetry_de *t)
+{
+ return t->de.tstamp_support && t->de.tstamp_enabled;
+}
+
+enum de_state {
+ ENA_STATE,
+ ENA_TSTAMP,
+ ENA_MAX
+};
+
struct telemetry_info {
bool streaming_mode;
unsigned int num_shmti;
unsigned int num_des_tstamp;
+ /* Mutex to protect des_enabled_mtx */
+ struct mutex des_enabled_mtx;
+ atomic_t des_enabled[ENA_MAX];
unsigned int default_blk_ts_rate;
const struct scmi_protocol_handle *ph;
struct telemetry_shmti *shmti;
@@ -240,6 +473,27 @@ struct telemetry_info {
static struct scmi_telemetry_res_info *
__scmi_telemetry_resources_get(struct telemetry_info *ti);
+static inline struct tdcf *snap_tdcf(struct shmti_snap *s);
+
+static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
+ unsigned int shmti_id, enum scan_mode mode);
+
+static inline void
+scmi_telemetry_de_state_update(struct telemetry_info *ti, enum de_state state,
+ bool *current_state, const bool next_state)
+{
+ guard(mutex)(&ti->des_enabled_mtx);
+ if (!current_state || *current_state != next_state)
+ atomic_add(next_state ? 1 : -1, &ti->des_enabled[state]);
+
+ if (current_state)
+ *current_state = next_state;
+
+ dev_dbg(ti->ph->dev, "Telemetry des_enabled[%s]:%d\n",
+ state == ENA_STATE ? "STATE" : "TSTAMP",
+ atomic_read(&ti->des_enabled[state]));
+}
+
static struct telemetry_de *
scmi_telemetry_free_tde_get(struct telemetry_info *ti)
{
@@ -359,6 +613,36 @@ static int scmi_telemetry_tde_register(struct telemetry_info *ti,
return ret;
}
+static inline bool
+scmi_telemetry_tde_cache_stale(struct telemetry_de *tde, const u32 magic)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ /* last_magic < magic */
+ return IS_BAD_START_SEQ(tde->last_magic) ||
+ time_before32(tde->last_magic, magic);
+}
+
+static void
+scmi_telemetry_tde_cache_update(struct telemetry_de *tde, u64 val,
+ u64 *tstamp, const u32 *magic)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ /*
+ * Update the cache only if stale compared to the current
+ * sample, so bail-out when last_magic >= *magic
+ */
+ if (magic && !scmi_telemetry_tde_cache_stale(tde, *magic))
+ return;
+
+ tde->last_magic = magic ? *magic : TDCF_BAD_END_SEQ;
+ tde->last_val = val;
+ tde->last_ts = tstamp && scmi_tde_has_tstamp(tde) ? *tstamp : 0;
+ if (tstamp)
+ *tstamp = tde->last_ts;
+}
+
struct scmi_tlm_de_priv {
struct telemetry_info *ti;
void *next;
@@ -1101,6 +1385,473 @@ scmi_telemetry_de_lookup(const struct scmi_protocol_handle *ph, u32 id)
return xa_load(&ti->xa_des, id);
}
+static inline struct payload __iomem *
+scmi_telemetry_payld_offset_safe_calc(struct telemetry_shmti *shmti, u32 offs)
+{
+ struct payload __iomem *payld;
+ unsigned long p;
+ u32 end;
+
+ if (offs < sizeof(struct tdcf))
+ return IOMEM_ERR_PTR(-EPROTO);
+
+ /* Check if accessing payld->meta is safe. */
+ if (check_add_overflow(offs, sizeof(u32), &end) ||
+ end > shmti->info.len)
+ return IOMEM_ERR_PTR(-EPROTO);
+
+ if (check_add_overflow((__force unsigned long)shmti->base, offs, &p))
+ return IOMEM_ERR_PTR(-EPROTO);
+
+ payld = shmti->base + offs;
+ /* Check line boundary */
+ if (check_add_overflow(offs, LINE_LENGTH_BYTES(payld), &end) ||
+ end > shmti->info.len - TDCF_EPLG_SZ)
+ return IOMEM_ERR_PTR(-EPROTO);
+
+ return payld;
+}
+
+static u64
+scmi_telemetry_blkts_read(u32 magic, struct telemetry_de *tde)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ return 0;
+}
+
+static inline int
+scmi_telemetry_line_data_parse(struct telemetry_de *tde, struct shmti_snap *snap,
+ u64 *val, u64 *tstamp, struct payload *payld,
+ struct payload *bts_payld, u32 magic)
+{
+ u32 meta = PAYLD_META_RAW(payld);
+
+ lockdep_assert_held(&tde->mtx);
+
+ /* Clear returned values in case of errors */
+ *val = 0;
+
+ if (tstamp) {
+ *tstamp = 0;
+ if (USE_BLK_TS_RAW(meta)) {
+ /* Read out the actual BLK_TS */
+ *tstamp = scmi_telemetry_blkts_read(magic, tde);
+ } else if (LINE_TS_VALID_RAW(meta)) {
+ /*
+ * Note that LINE_TS_VALID implies HAS_LINE_EXT and that
+ * the per DE line_ts_rate is advertised in the DE
+ * descriptor, BUT we check anyway against buggy FW.
+ */
+ if (!HAS_LINE_EXT_RAW(meta)) {
+ trace_scmi_tlm_access(tde->de.info->id,
+ "SHMTI_BAD_TS_LINE", 0, 0);
+ return -EPROTO;
+ }
+
+ *tstamp = LINE_TSTAMP_GET_RAW(&payld->tsl);
+ }
+ }
+
+ /* Data is always valid since we are NOT handling BLK TS lines here */
+ *val = LINE_DATA_GET_RAW(&payld->l);
+
+ trace_scmi_tlm_collect(tstamp ? *tstamp : 0, tde->de.info->id,
+ *val, "SHMTI_DE_READ");
+
+ scmi_telemetry_tde_cache_update(tde, *val, tstamp, &magic);
+
+ return 0;
+}
+
+/**
+ * scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing
+ * @ti: A reference to the telemetry info descriptor
+ * @payld: Line payload to parse
+ * @snap: A reference to the working SHMTI snapshot that will be processed
+ * @mode: A flag to determine the behaviour of the scan
+ * @active_bts: A pointer to keep track and report any found BLK timestamp line
+ * @active_uuid: A pointer to keep track and report any found UUID line
+ *
+ * This routine takes care to:
+ * - verify line consistency in relation to the used flags and the current
+ * context: e.g. is there an active preceding BLK_TS line if the DataLine
+ * sports a USE_BLKTS flag ?
+ * - verify the related Data Event ID exists OR create a brand new DE
+ * (depending on the @mode of operation)
+ * - links any active BLK_TS or UUID line to the current DE
+ * - read and save value/tstamp for the DE ONLY if anything has changed (by
+ * tracking the last TDCF magic) and update related magic: this allows to
+ * minimize future needs of single-DE reads
+ *
+ * Modes of operation.
+ *
+ * The scan behaviour depends on the chosen @mode:
+ * - SCAN_LOOKUP: the basic scan which aims to update value associated to
+ * existing DEs. Any discovered DataLine that could NOT be
+ * matched to an existing, previously discovered, DE is
+ * discarded. This is the normal scan behaviour.
+ * - SCAN_UPDATE: a more advanced scan which provides all the SCAN_LOOKUP
+ * features plus takes care to update the DEs location
+ * coordinates inside the SHMTI: note that the related DEs are
+ * still supposed to have been previously discovered when
+ * this scan runs. This is used to update location
+ * coordinates for DEs contained in a Group when such group
+ * is enabled.
+ */
+static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
+ struct payload *payld,
+ struct shmti_snap *snap,
+ enum scan_mode mode,
+ struct telemetry_block_ts *active_bts,
+ struct telemetry_uuid *active_uuid)
+{
+ struct telemetry_shmti *shmti = snap->shmti;
+ struct device *dev = ti->ph->dev;
+ struct telemetry_de *tde;
+ u64 val, tstamp = 0;
+ u32 de_id;
+
+ de_id = PAYLD_ID_RAW(payld);
+ /* Is this DE ID known ? */
+ tde = scmi_telemetry_tde_lookup(ti, de_id);
+ if (!tde) {
+ trace_scmi_tlm_access(de_id, "DE_INVALID", 0, 0);
+ return;
+ }
+
+ guard(mutex)(&tde->mtx);
+
+ if (tde->de.fc_support) {
+ dev_err(dev, "Found FastChannel DE while parsing a SHMTI.\n");
+ trace_scmi_tlm_access(de_id, "DE_INVALID_FC_ON_SHMTI", 0, 0);
+ return;
+ }
+
+ /*
+ * Update DE location refs if requested: normally done only on group
+ * enable.
+ * Note that all the DE location information is relative to the
+ * original SHMTI device-memory area, NOT to the snapshot we are parsing.
+ */
+ if (mode >= SCAN_UPDATE) {
+ u32 de_offs;
+ void __iomem *p;
+
+ /* Offest is the same in the SHMTI and in the snapshot indeed */
+ de_offs = (void *)payld - (void *)snap->base;
+ /* Validate location before update */
+ p = scmi_telemetry_payld_offset_safe_calc(shmti, de_offs);
+ if (IS_ERR(p)) {
+ dev_err(dev,
+ "TDE 0x%08X updated location invalid - shmti:%u offs:%u\n",
+ de_id, shmti->info.sid, de_offs);
+ trace_scmi_tlm_access(de_id, "DE_LOCATION_INVALID", 0, 0);
+ return;
+ }
+
+ tde->sid = shmti->info.sid;
+ tde->base = shmti->base;
+ tde->eplg = SHMTI_EPLG(shmti);
+ tde->offset = de_offs;
+
+ trace_scmi_tlm_access(de_id, "DE_LOCATION_UPDATED", 0, 0);
+
+ dev_dbg(dev, "TDCF-updated DE_ID:0x%08X - shmti:%pK offset:%u\n",
+ tde->de.info->id, tde->base, tde->offset);
+ }
+
+ /*
+ * Has any value/tstamp really changed ?
+ * Note that, if the magic currently associated with this TDE has
+ * NOT changed compared to the current SHMTI magic, nothing could have
+ * been changed across the whole TDCF, including any relation to
+ * BLK_TS or UUID lines.
+ */
+ if (!scmi_telemetry_tde_cache_stale(tde, snap->magic))
+ return;
+
+ /*
+ * Parse data words ... only for the sake of updating TDE cache,
+ * any error is logged but ignored.
+ */
+ if (scmi_telemetry_line_data_parse(tde, snap, &val, &tstamp, payld,
+ NULL, snap->magic))
+ dev_err(dev, "Malformed DATA line.\n");
+}
+
+/**
+ * scmi_telemetry_tdcf_line_parse - Line parser
+ * @ti: A reference to the Telemetry instance
+ * @avail_qwords: Number of remaining qwords
+ * @payld: A reference to the payload representing the line to parse
+ * @snap: A reference to the working SHMTI snapshot that will be processed
+ * @mode: Specification of the scanning mode
+ * @active_bts: A reference used to track the last BLK_TS line found
+ * @active_uuid: A reference used to track the last UUID line found
+ *
+ * Note that @active_bts and @active_uuid references are propagated back and
+ * forth from the line parsers to keep track of the nearest active BLK_TS or
+ * UUID line found in the SHMTI areas and, while doing that, such descriptors
+ * are refcounted and tracked in the xa_lines XArray indexed by @payld.
+ * BLK_TS and UUID descriptors are dropped when no more linked to any DATA line,
+ * during the normal system operation OR later on when the protocol is
+ * de-initialized following a driver removal.
+ *
+ * Return: Number of QWORDS consumed from @payld by this line parsing; note
+ * that buggy firmware could return bad, out of range values, so this
+ * value MUST be carefully checked for SHMTI boundary overflows by the
+ * caller.
+ */
+static int scmi_telemetry_tdcf_line_parse(struct telemetry_info *ti,
+ unsigned int avail_qwords,
+ struct payload *payld,
+ struct shmti_snap *snap,
+ enum scan_mode mode,
+ struct telemetry_block_ts **active_bts,
+ struct telemetry_uuid **active_uuid)
+{
+ u32 meta = le32_to_cpu(payld->meta);
+ int used_qwords;
+
+ used_qwords = LINE_LENGTH_QWORDS_RAW(meta);
+ /* Does they fit in the snapshot (sane) available qwords ? */
+ if (used_qwords > avail_qwords)
+ return -EINVAL;
+
+ /* Invalid lines are not an error, could simply be disabled DEs */
+ if (DATA_INVALID_RAW(meta)) {
+ trace_scmi_tlm_access(le32_to_cpu(payld->id), "TDCF_INVALID", 0, 0);
+ return used_qwords;
+ }
+
+ /*
+ * Problematic lines are ignored, their information discarded, and such
+ * issues are logged, but the scan process continues.
+ */
+ switch (LINE_TYPE_RAW(meta)) {
+ case TDCF_DATA_LINE:
+ scmi_telemetry_tdcf_data_parse(ti, payld, snap, mode,
+ *active_bts, *active_uuid);
+ break;
+ default:
+ trace_scmi_tlm_access(PAYLD_ID_RAW(payld), "TDCF_LINE_UNKNOWN", 0, 0);
+ break;
+ }
+
+ return used_qwords;
+}
+
+static inline ssize_t
+scmi_telemetry_shmti_overflow(void __iomem *base, void __iomem *eplg,
+ void __iomem *next)
+{
+ ssize_t len;
+
+ /* Is payld->meta accessible and within usable TDCF space ? */
+ if (next < base + sizeof(struct tdcf) || next + sizeof(u32) > eplg)
+ return -EOVERFLOW;
+
+ len = LINE_LENGTH_BYTES((struct payload __iomem *)next);
+ if (next + len > eplg)
+ return -EOVERFLOW;
+
+ return len;
+}
+
+static inline struct tdcf *snap_tdcf(struct shmti_snap *s)
+{
+ return (struct tdcf *)s->base;
+}
+
+static inline bool
+scmi_telemetry_snap_overflow(struct shmti_snap *snap, const void *next)
+{
+ void *limit = SNAP_EPLG(snap);
+
+ /* Is payld->meta accessible ? */
+ if (next >= limit - sizeof(u32))
+ return true;
+
+ next += LINE_LENGTH_BYTES_RAW(PAYLD_META_RAW((struct payload *)next));
+
+ return next > limit;
+}
+
+static int scmi_telemetry_shmti_snapshot(struct telemetry_shmti *shmti,
+ struct shmti_snap *work)
+{
+ struct tdcf __iomem *tdcf = shmti->base;
+ int retries = SCMI_TLM_TDCF_MAX_RETRIES;
+ struct tdcf *snap = snap_tdcf(work);
+ u32 startm = 0, endm = 0, live;
+
+ lockdep_assert_held(&shmti->lock);
+
+ do {
+ /* A bit of exponential backoff between retries */
+ fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP_US);
+
+ memcpy_fromio(snap, tdcf, shmti->info.len);
+ /* Assure the TDCF snapshot has been fully taken before checking seqnum */
+ rmb();
+ /* Used to detect changes while copying */
+ live = TDCF_START_SEQ_GET(tdcf);
+
+ startm = TDCF_START_SEQ_GET_RAW(snap);
+ if (IS_BAD_START_SEQ(startm)) {
+ trace_scmi_tlm_access(0, "MSEQ_BADSTART", startm, 0);
+ continue;
+ }
+
+ endm = TDCF_END_SEQ_GET_RAW(SNAP_EPLG(work));
+ if (startm != endm || startm != live) {
+ trace_scmi_tlm_access(0, "MSEQ_MISMATCH", startm,
+ startm != endm ? endm : live);
+ continue;
+ }
+
+ trace_scmi_tlm_access(0, "TDCF_SNAPSHOT_OK", startm, endm);
+
+ /* Ensure any snapshot is visible before updating magic */
+ smp_store_release(&work->magic, startm);
+
+ return 0;
+ } while (--retries);
+
+ trace_scmi_tlm_access(0, "TDCF_SNAPSHOT_FAIL", startm, endm);
+
+ return -EPROTO;
+}
+
+/*
+ * True if neither TDCF marker has moved since the cached snapshot was taken.
+ * Any platform update must write one marker before touching data (start first
+ * or end first, depending on the spec's order), so checking both is
+ * independent of that order.
+ */
+static bool scmi_telemetry_shmti_unchanged(struct telemetry_shmti *shmti, u32 magic)
+{
+ struct tdcf __iomem *tdcf = shmti->base;
+
+ return TDCF_START_SEQ_GET(tdcf) == magic &&
+ TDCF_END_SEQ_GET(SHMTI_EPLG(shmti)) == magic;
+}
+
+/* Returns with the SRCU read lock held (index in *idx) on a snapshot that matched at check time */
+static struct shmti_snap *
+scmi_telemetry_shmti_refresh(struct telemetry_shmti *shmti, int *idx)
+{
+ struct shmti_snap *cur, *work;
+ int ret;
+
+ guard(mutex)(&shmti->lock);
+
+ cur = rcu_dereference_protected(shmti->cur, lockdep_is_held(&shmti->lock));
+ /* Someone else may have refreshed while we waited for the mutex */
+ if (cur && scmi_telemetry_shmti_unchanged(shmti, cur->magic)) {
+ /* Safe: recycling cur requires shmti->lock, which we hold */
+ *idx = srcu_read_lock(&shmti->srcu);
+ return cur;
+ }
+
+ /* Pick the spare buffer */
+ work = (cur == shmti->snaps[0]) ? shmti->snaps[1] : shmti->snaps[0];
+ /*
+ * Wait for readers that may still be parsing 'work' from its time
+ * as current
+ */
+ synchronize_srcu(&shmti->srcu);
+
+ ret = scmi_telemetry_shmti_snapshot(shmti, work);
+ if (ret)
+ return ERR_PTR(ret);
+
+ /*
+ * Take our read lock BEFORE publishing so 'work' cannot be
+ * recycled under us
+ */
+ *idx = srcu_read_lock(&shmti->srcu);
+ rcu_assign_pointer(shmti->cur, work);
+
+ return work;
+}
+
+/**
+ * scmi_telemetry_shmti_scan - Full SHMTI scan
+ * @ti: A reference to the telemetry info descriptor
+ * @shmti_id: ID of the SHMTI area that has to be scanned
+ * @mode: A flag to determine the behaviour of the scan
+ *
+ * Return: 0 on Success
+ */
+static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
+ unsigned int shmti_id, enum scan_mode mode)
+{
+ struct telemetry_shmti *shmti = &ti->shmti[shmti_id];
+ struct telemetry_block_ts *active_bts = NULL;
+ struct telemetry_uuid *active_uuid = NULL;
+ unsigned int qwords;
+ struct shmti_snap *snap;
+ struct tdcf *tdcf;
+ int idx, ret = 0;
+ void *next;
+
+ if (!shmti->base)
+ return -ENODEV;
+
+ idx = srcu_read_lock(&shmti->srcu);
+ snap = srcu_dereference(shmti->cur, &shmti->srcu);
+ if (!snap || !scmi_telemetry_shmti_unchanged(shmti, snap->magic)) {
+ /* MUST drop the read lock: refresh calls synchronize_srcu() */
+ srcu_read_unlock(&shmti->srcu, idx);
+ snap = scmi_telemetry_shmti_refresh(shmti, &idx);
+ if (IS_ERR(snap))
+ return PTR_ERR(snap);
+ }
+
+ tdcf = snap_tdcf(snap);
+ next = tdcf->payld;
+ qwords = QWORDS_RAW(tdcf);
+ while (qwords) {
+ int used_qwords;
+
+ /*
+ * Bailout on any error since we are processing a supposedly
+ * stable snapshot at this point, so any further error is on
+ * firmware-side and NOT recoverable: this would mean having to
+ * drop the remaining part of the scan results after having
+ * cached the initial good snapshot: log this down below.
+ */
+ if (scmi_telemetry_snap_overflow(snap, next)) {
+ trace_scmi_tlm_access(0, "SHMTI_OVERFLOW", snap->magic, 0);
+ ret = -EPROTO;
+ break;
+ }
+
+ used_qwords = scmi_telemetry_tdcf_line_parse(ti, qwords,
+ next, snap, mode,
+ &active_bts, &active_uuid);
+ if (used_qwords <= 0) {
+ /* Stop scanning when qwords are inconsistent. */
+ trace_scmi_tlm_access(PAYLD_ID_RAW(next), "BAD_QWORDS",
+ snap->magic, 0);
+ ret = -EPROTO;
+ break;
+ }
+
+ next += used_qwords * 8;
+ qwords -= used_qwords;
+ }
+
+ srcu_read_unlock(&shmti->srcu, idx);
+
+ if (ret)
+ dev_err(ti->ph->dev, FW_BUG "Inconsistent TDCF. Scan aborted.\n");
+
+ return ret;
+}
+
static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.info_get = scmi_telemetry_info_get,
.de_lookup = scmi_telemetry_de_lookup,
@@ -1313,6 +2064,9 @@ static int scmi_telemetry_instance_init(struct telemetry_info *ti)
if (ret)
return ret;
+ atomic_set(&ti->des_enabled[ENA_STATE], 0);
+ atomic_set(&ti->des_enabled[ENA_TSTAMP], 0);
+ mutex_init(&ti->des_enabled_mtx);
/* Setup resources lazy initialization */
atomic_set(&ti->rinfo_initializing, 0);
init_completion(&ti->rinfo_initdone);
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 16/34] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (14 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 15/34] firmware: arm_scmi: Add Telemetry TDCF areas parsing support Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 17/34] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines Cristian Marussi
` (18 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add support to parse, track and link TDCF BLK_TS lines to the related
discovered DataEvents.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- check TDE TS type, update from last_rate
v15 --> v16
- WARN_ON(BLK_TS_INVALID) but keep active_bts valid
- invalidate TDE cache on blkts_unlin
- cleanup stale lines on shmti cleanup
v14 --> v15
- use proper cast on tlm_collect
- use snapshots to track and collect BLK_TS during full scan bulk reads
- fix too early invocation xa_destroy(xa_lines)
v13 --> v14
- split out of previous single patch implementing parse SHMTI routines
---
drivers/firmware/arm_scmi/telemetry.c | 352 +++++++++++++++++++++++++-
1 file changed, 344 insertions(+), 8 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index ba4ba1d2c00c..414765f46c77 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -12,6 +12,7 @@
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
+#include <linux/hash.h>
#include <linux/io.h>
#include <linux/limits.h>
#include <linux/minmax.h>
@@ -159,6 +160,11 @@ struct line {
__le32 data_high;
};
+struct blk_tsline {
+ __le32 ts_low;
+ __le32 ts_high;
+};
+
struct tsline {
__le32 data_low;
__le32 data_high;
@@ -263,6 +269,7 @@ struct payload {
union {
struct line l;
struct tsline tsl;
+ struct blk_tsline blk_tsl;
};
};
@@ -470,6 +477,10 @@ struct telemetry_info {
struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti);
};
+static void __scmi_telemetry_line_put(struct telemetry_line *line);
+
+static void scmi_telemetry_de_unlink(struct scmi_telemetry_de *de);
+
static struct scmi_telemetry_res_info *
__scmi_telemetry_resources_get(struct telemetry_info *ti);
@@ -478,6 +489,12 @@ static inline struct tdcf *snap_tdcf(struct shmti_snap *s);
static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
unsigned int shmti_id, enum scan_mode mode);
+static inline
+struct mutex *lock_for_payld(struct mutex *locks, unsigned long offset)
+{
+ return &locks[hash_long(offset, PAYLD_HASH_BITS)];
+}
+
static inline void
scmi_telemetry_de_state_update(struct telemetry_info *ti, enum de_state state,
bool *current_state, const bool next_state)
@@ -613,6 +630,14 @@ static int scmi_telemetry_tde_register(struct telemetry_info *ti,
return ret;
}
+static inline void scmi_telemetry_tde_cache_invalidate(struct telemetry_de *tde)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ /* Invalidate cache */
+ tde->last_magic = TDCF_BAD_END_SEQ;
+}
+
static inline bool
scmi_telemetry_tde_cache_stale(struct telemetry_de *tde, const u32 magic)
{
@@ -960,16 +985,24 @@ static int iter_de_descr_process_response(const struct scmi_protocol_handle *ph,
return 0;
}
-static void scmi_telemetry_tdes_cleanup(struct telemetry_info *ti)
+static void scmi_telemetry_tdes_cleanup(void *arg)
{
+ struct telemetry_info *ti = arg;
struct scmi_telemetry_de *de;
unsigned long idx;
+ /*
+ * Unlinking all the BLK_TS/UUID lines related to a DE triggers also
+ * the deallocation of such lines when the embedded refcount hits zero.
+ * Note that the UUID lines are also referenced and refcounted by the
+ * global UUID RCU array, so they won't be deallocated here.
+ */
xa_for_each(&ti->xa_des, idx, de) {
struct telemetry_de *tde = to_tde(de);
guard(mutex)(&tde->mtx);
+ scmi_telemetry_de_unlink(&tde->de);
scmi_telemetry_free_tde_put(ti, tde);
}
}
@@ -1280,16 +1313,29 @@ static int scmi_telemetry_shmti_list(const struct scmi_protocol_handle *ph,
return 0;
}
+static void scmi_telemetry_shmti_lines_cleanup(struct telemetry_shmti *shmti)
+{
+ struct telemetry_line *line;
+ unsigned long idx;
+
+ guard(mutex)(&shmti->lines_mtx);
+ /* Drop all remaining currently unbound lines and their containers */
+ xa_for_each(&shmti->xa_lines, idx, line)
+ __scmi_telemetry_line_put(line);
+
+ xa_destroy(&shmti->xa_lines);
+}
+
static void scmi_telemetry_shmti_cleanup(void *arg)
{
struct telemetry_shmti *shmti = arg;
+ scmi_telemetry_shmti_lines_cleanup(shmti);
+
cleanup_srcu_struct(&shmti->srcu);
kvfree(shmti->snaps[0]);
kvfree(shmti->snaps[1]);
-
- xa_destroy(&shmti->xa_lines);
}
static int scmi_telemetry_shmti_init(struct device *dev,
@@ -1351,7 +1397,7 @@ static int scmi_telemetry_enumerate_shmti(struct telemetry_info *ti)
/* Expose the fully initialized SHMTI infos */
smp_store_release(&ti->info.base.num_shmtis, ti->num_shmti);
- return 0;
+ return devm_add_action_or_reset(ph->dev, scmi_telemetry_tdes_cleanup, ti);
}
static const struct scmi_telemetry_info *
@@ -1413,11 +1459,242 @@ scmi_telemetry_payld_offset_safe_calc(struct telemetry_shmti *shmti, u32 offs)
}
static u64
-scmi_telemetry_blkts_read(u32 magic, struct telemetry_de *tde)
+scmi_telemetry_blkts_read(u32 magic, struct telemetry_de *tde,
+ struct payload *bts_payld, struct shmti_snap *snap)
{
+ struct telemetry_block_ts *bts;
+
+ /* Acquiring order is: tde -> line */
lockdep_assert_held(&tde->mtx);
+ if (WARN_ON(!tde->bts || (!bts_payld && !snap)))
+ return 0;
- return 0;
+ bts = tde->bts;
+ guard(mutex)(&bts->line.mtx);
+
+ if (bts->line.last_magic == magic)
+ return bts->last_ts;
+
+ /* Unless specified otherwise, use the snapshot BLK_TS paylod */
+ if (!bts_payld) {
+ void *base = snap_tdcf(snap);
+
+ bts_payld = base + bts->line.offset;
+ }
+
+ /* Note that the bts->last_rate can change ONLY on creation */
+ bts->last_ts = BLK_TS_STAMP_RAW(&bts_payld->blk_tsl);
+ bts->line.last_magic = magic;
+
+ return bts->last_ts;
+}
+
+static void
+scmi_telemetry_blkts_update_snap(struct telemetry_info *ti,
+ struct telemetry_block_ts *bts,
+ struct payload *bts_payld, u32 magic)
+{
+ guard(mutex)(&bts->line.mtx);
+ if (bts->line.last_magic != magic) {
+ bts->last_ts = BLK_TS_STAMP_RAW(&bts_payld->blk_tsl);
+ bts->last_rate = BLK_TS_RATE_RAW(bts_payld);
+ /* BLK_TS clock rate value can change ONLY here on creation */
+ if (!bts->last_rate)
+ bts->last_rate = ti->default_blk_ts_rate;
+ bts->line.last_magic = magic;
+
+ trace_scmi_tlm_access(0, "BLK_TS_UPDATE", magic, 0);
+ }
+}
+
+static inline void scmi_telemetry_line_free(struct telemetry_line *line)
+{
+ void *blob;
+
+ switch (line->type) {
+ case TDCF_BLK_TS_LINE:
+ blob = to_blkts(line);
+ kfree(blob);
+ break;
+ default:
+ WARN_ON(1);
+ break;
+ }
+}
+
+static void __scmi_telemetry_line_put(struct telemetry_line *line)
+{
+ lockdep_assert_held(&line->shmti->lines_mtx);
+
+ mutex_lock(&line->mtx);
+ if (refcount_dec_and_test(&line->users)) {
+ xa_erase(&line->shmti->xa_lines, line->offset);
+
+ /* Unlock and free the containing structure: blkts or uuid */
+ mutex_unlock(&line->mtx);
+
+ trace_scmi_tlm_access(0, "DROP_LINE", line->type, line->last_magic);
+ scmi_telemetry_line_free(line);
+
+ return;
+ }
+ mutex_unlock(&line->mtx);
+}
+
+static void scmi_telemetry_line_put(struct telemetry_line *line)
+{
+ /* Acquiring coarse grain first ... */
+ guard(mutex)(&line->shmti->lines_mtx);
+ __scmi_telemetry_line_put(line);
+}
+
+static void scmi_telemetry_blkts_unlink(struct telemetry_de *tde)
+{
+ /* Acquiring order is: tde -> line */
+ lockdep_assert_held(&tde->mtx);
+ if (!tde->bts)
+ return;
+
+ scmi_telemetry_line_put(&tde->bts->line);
+
+ scmi_telemetry_tde_cache_invalidate(tde);
+ tde->bts = NULL;
+
+ trace_scmi_tlm_access(tde->de.info->id, "BLKTS_UNLINK", 0, 0);
+}
+
+static void scmi_telemetry_de_unlink(struct scmi_telemetry_de *de)
+{
+ struct telemetry_de *tde = to_tde(de);
+
+ lockdep_assert_held(&tde->mtx);
+
+ /* Unlink all related lines triggering their deallocation */
+ scmi_telemetry_blkts_unlink(tde);
+}
+
+static struct telemetry_line *
+scmi_telemetry_line_get(struct telemetry_shmti *shmti, unsigned long offset)
+{
+ struct telemetry_line *line;
+
+ /* Acquiring coarse grain first ... */
+ guard(mutex)(&shmti->lines_mtx);
+ line = xa_load(&shmti->xa_lines, offset);
+ if (!line)
+ return NULL;
+
+ guard(mutex)(&line->mtx);
+ refcount_inc(&line->users);
+
+ return line;
+}
+
+static int
+scmi_telemetry_line_init(struct telemetry_shmti *shmti, struct telemetry_line *line,
+ unsigned long offset, enum tdcf_line_types type)
+{
+ line->type = type;
+ refcount_set(&line->users, 1);
+ line->offset = offset;
+ line->shmti = shmti;
+ mutex_init(&line->mtx);
+
+ guard(mutex)(&shmti->lines_mtx);
+ return xa_insert(&shmti->xa_lines, offset, line, GFP_KERNEL);
+}
+
+static struct telemetry_block_ts *
+scmi_telemetry_blkts_create(struct telemetry_shmti *shmti, unsigned long offset)
+{
+ struct telemetry_block_ts *bts;
+ int ret;
+
+ bts = kzalloc_obj(*bts);
+ if (!bts)
+ return NULL;
+
+ ret = scmi_telemetry_line_init(shmti, &bts->line, offset, TDCF_BLK_TS_LINE);
+ if (ret) {
+ kfree(bts);
+ return NULL;
+ }
+
+ trace_scmi_tlm_collect(0, shmti->info.sid, offset, "SHMTI_NEW_BLKTS");
+
+ return bts;
+}
+
+static struct telemetry_block_ts *
+scmi_telemetry_blkts_get_or_create(struct telemetry_shmti *shmti,
+ unsigned long offset)
+{
+ struct telemetry_line *line;
+
+ /* Avoid races on line creation for this payld */
+ guard(mutex)(lock_for_payld(shmti->payld_locks, offset));
+
+ line = scmi_telemetry_line_get(shmti, offset);
+ if (line)
+ return to_blkts(line);
+
+ return scmi_telemetry_blkts_create(shmti, offset);
+}
+
+/**
+ * scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser
+ *
+ * @ti: A reference to the telemetry_info descriptor
+ * @bts_payld: TDCF payld line to process
+ * @snap: A reference to the working SHMTI snapshot that will be processed
+ * @active_bts: Input/output reference to keep track of the last blk_ts found
+ *
+ * Process a valid TDCF BLK_TS line and, after having looked up or created a
+ * blk_ts descriptor, update the related data and return it as the currently
+ * active blk_ts, given that it is effectively the last found during this
+ * scan.
+ *
+ * Note that the current BLK_TS line could be malformed due to garbage in
+ * the TDCF: in such a case we warn/log and bailout BUT we don't throw away
+ * the currently active_bts (if any) since the current malformed line could
+ * be bogus and never intended to be a BLK_TS line.
+ */
+static void scmi_telemetry_tdcf_blkts_parse(struct telemetry_info *ti,
+ struct payload *bts_payld,
+ struct shmti_snap *snap,
+ struct telemetry_block_ts **active_bts)
+{
+ struct telemetry_block_ts *bts, *previous = *active_bts;
+ struct telemetry_shmti *shmti = snap->shmti;
+ u32 meta = PAYLD_META_RAW(bts_payld);
+ unsigned long offset;
+
+ /* Check for spec compliance. */
+ if (WARN_ON(BLK_TS_INVALID_RAW(meta))) {
+ trace_scmi_tlm_access(0, "BLK_TS_INVALID", 0, meta);
+ return;
+ }
+
+ offset = TDCF_OFFS(snap->base, bts_payld);
+ /* A BLK_TS descriptor MUST be returned: it is found or it is created */
+ bts = scmi_telemetry_blkts_get_or_create(shmti, offset);
+ /*
+ * On error processing the new BLK_TS bailout taking care to clear
+ * the previous last active BLK_TS to avoid mistakenly binding to the
+ * next data lines.
+ */
+ if (WARN_ON(!bts))
+ goto out;
+
+ /* Update the descriptor with the lastest TS */
+ scmi_telemetry_blkts_update_snap(ti, bts, bts_payld, snap->magic);
+
+out:
+ /* Drop one refcount on the previous active since we found a new one */
+ if (previous)
+ scmi_telemetry_line_put(&previous->line);
+
+ *active_bts = bts;
}
static inline int
@@ -1436,7 +1713,8 @@ scmi_telemetry_line_data_parse(struct telemetry_de *tde, struct shmti_snap *snap
*tstamp = 0;
if (USE_BLK_TS_RAW(meta)) {
/* Read out the actual BLK_TS */
- *tstamp = scmi_telemetry_blkts_read(magic, tde);
+ *tstamp = scmi_telemetry_blkts_read(magic, tde,
+ bts_payld, snap);
} else if (LINE_TS_VALID_RAW(meta)) {
/*
* Note that LINE_TS_VALID implies HAS_LINE_EXT and that
@@ -1464,6 +1742,26 @@ scmi_telemetry_line_data_parse(struct telemetry_de *tde, struct shmti_snap *snap
return 0;
}
+static inline void scmi_telemetry_blkts_link(struct telemetry_de *tde,
+ struct telemetry_block_ts *bts)
+{
+ /* Acquiring order is: tde -> line */
+ lockdep_assert_held(&tde->mtx);
+
+ /* Unlink old stale BTS if existent */
+ scmi_telemetry_blkts_unlink(tde);
+
+ guard(mutex)(&bts->line.mtx);
+ refcount_inc(&bts->line.users);
+
+ /* Update TS clock rate if provided by the BLK_TS */
+ if (bts->last_rate)
+ tde->de.info->ts_rate = bts->last_rate;
+
+ tde->bts = bts;
+ trace_scmi_tlm_access(tde->de.info->id, "BLKTS_LINK", 0, 0);
+}
+
/**
* scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing
* @ti: A reference to the telemetry info descriptor
@@ -1508,11 +1806,19 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
{
struct telemetry_shmti *shmti = snap->shmti;
struct device *dev = ti->ph->dev;
+ u32 meta = PAYLD_META_RAW(payld);
+ bool use_blk_ts = USE_BLK_TS_RAW(meta);
struct telemetry_de *tde;
u64 val, tstamp = 0;
u32 de_id;
de_id = PAYLD_ID_RAW(payld);
+ /* Discard malformed lines...a preceding BLK_TS must exist */
+ if (use_blk_ts && !active_bts) {
+ trace_scmi_tlm_access(de_id, "BAD_USE_BLK_TS", 0, 0);
+ return;
+ }
+
/* Is this DE ID known ? */
tde = scmi_telemetry_tde_lookup(ti, de_id);
if (!tde) {
@@ -1522,6 +1828,11 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
guard(mutex)(&tde->mtx);
+ if (use_blk_ts && !IS_TDE_TS_BLK(tde)) {
+ trace_scmi_tlm_access(de_id, "BAD_BLK_TS_TYPE", 0, 0);
+ return;
+ }
+
if (tde->de.fc_support) {
dev_err(dev, "Found FastChannel DE while parsing a SHMTI.\n");
trace_scmi_tlm_access(de_id, "DE_INVALID_FC_ON_SHMTI", 0, 0);
@@ -1571,6 +1882,19 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
if (!scmi_telemetry_tde_cache_stale(tde, snap->magic))
return;
+ /*
+ * Link the related BTS when needed, it's unlinked on disable.
+ * Note that each DEs timestamp types is statically defined and
+ * enumerated once for all at init: a Data line cannot suddenly
+ * 'migrate' to a different timestamp type.
+ */
+ if (use_blk_ts) {
+ if (BLK_TS_CHANGED(tde, shmti, active_bts->line.offset))
+ scmi_telemetry_blkts_link(tde, active_bts);
+ else if (tde->bts->last_rate != tde->de.info->ts_rate)
+ tde->de.info->ts_rate = tde->bts->last_rate;
+ }
+
/*
* Parse data words ... only for the sake of updating TDE cache,
* any error is logged but ignored.
@@ -1634,6 +1958,9 @@ static int scmi_telemetry_tdcf_line_parse(struct telemetry_info *ti,
scmi_telemetry_tdcf_data_parse(ti, payld, snap, mode,
*active_bts, *active_uuid);
break;
+ case TDCF_BLK_TS_LINE:
+ scmi_telemetry_tdcf_blkts_parse(ti, payld, snap, active_bts);
+ break;
default:
trace_scmi_tlm_access(PAYLD_ID_RAW(payld), "TDCF_LINE_UNKNOWN", 0, 0);
break;
@@ -1777,6 +2104,15 @@ scmi_telemetry_shmti_refresh(struct telemetry_shmti *shmti, int *idx)
return work;
}
+static inline void
+scmi_telemetry_reset_active_lines(struct telemetry_block_ts **active_bts)
+{
+ if (*active_bts) {
+ scmi_telemetry_line_put(&(*active_bts)->line);
+ *active_bts = NULL;
+ }
+}
+
/**
* scmi_telemetry_shmti_scan - Full SHMTI scan
* @ti: A reference to the telemetry info descriptor
@@ -1844,6 +2180,7 @@ static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
qwords -= used_qwords;
}
+ scmi_telemetry_reset_active_lines(&active_bts);
srcu_read_unlock(&shmti->srcu, idx);
if (ret)
@@ -1953,7 +2290,6 @@ static void scmi_telemetry_resources_free(struct telemetry_info *ti)
/* Ensure rinfo is no more accessible upfront */
smp_store_release(&ti->rinfo, NULL);
- scmi_telemetry_tdes_cleanup(ti);
xa_destroy(&ti->xa_des);
kfree(ti->info.shmtis);
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 17/34] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (15 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 16/34] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 18/34] firmware: arm_scmi: Add Telemetry DEs configuration support Cristian Marussi
` (17 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add support to parse, track and link TDCF UUID lines to the related
discovered DataEvents.
Maintain also a central list of discovered UUID lines.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- removed unused node+_lst
v15 --> v16
- WARN_ON invalid UUID line
- uuid array drop counted_by
v14 --> v15
- use proper casting in tlm_collect
- use snapshots to collect UUIDs during full scans
- stop tracking UUIDs lines singularly..it's useless
- use UUIDs line hashing for lookup
- clarified UUID/DE binding are permanent
- fixed WARN_ON invocation on missing active_uuid
v13 --> v14
- split out of previous single patch implementing parse SHMTI routines
---
drivers/firmware/arm_scmi/telemetry.c | 281 +++++++++++++++++++++++++-
include/linux/scmi_protocol.h | 2 +
2 files changed, 280 insertions(+), 3 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 414765f46c77..fe63e7f5d145 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -13,11 +13,14 @@
#include <linux/device.h>
#include <linux/err.h>
#include <linux/hash.h>
+#include <linux/hashtable.h>
+#include <linux/jhash.h>
#include <linux/io.h>
#include <linux/limits.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/mutex.h>
+#include <linux/rcupdate.h>
#include <linux/refcount.h>
#include <linux/sizes.h>
#include <linux/slab.h>
@@ -172,6 +175,10 @@ struct tsline {
__le32 ts_high;
};
+struct uuid_line {
+ __be32 dwords[SCMI_TLM_DE_IMPL_NUM_DWORDS];
+};
+
#define LINE_DATA_GET_RAW(f) \
({ \
typeof(f) _f = (f); \
@@ -270,6 +277,7 @@ struct payload {
struct line l;
struct tsline tsl;
struct blk_tsline blk_tsl;
+ struct uuid_line uuid_l;
};
};
@@ -398,9 +406,7 @@ struct telemetry_block_ts {
#define to_blkts(l) container_of(l, struct telemetry_block_ts, line)
struct telemetry_uuid {
- u32 key;
struct hlist_node node_ht;
- struct hlist_node node_lst;
uuid_t uuid;
};
@@ -458,6 +464,12 @@ struct telemetry_info {
bool streaming_mode;
unsigned int num_shmti;
unsigned int num_des_tstamp;
+#define SCMI_UUID_DB_THRESH 3
+ /* Protect uuids_len */
+ struct mutex uuids_mtx;
+ unsigned int uuids_len;
+#define SCMI_UUID_HASH_SZ 5
+ DECLARE_HASHTABLE(uuids_ht, SCMI_UUID_HASH_SZ);
/* Mutex to protect des_enabled_mtx */
struct mutex des_enabled_mtx;
atomic_t des_enabled[ENA_MAX];
@@ -466,6 +478,7 @@ struct telemetry_info {
struct telemetry_shmti *shmti;
struct telemetry_de *tdes;
struct xarray xa_des;
+ struct telemetry_uuid *primary_uuid;
/* Mutex to protect access to @free_des */
struct mutex free_mtx;
struct list_head free_des;
@@ -489,6 +502,9 @@ static inline struct tdcf *snap_tdcf(struct shmti_snap *s);
static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
unsigned int shmti_id, enum scan_mode mode);
+static inline void scmi_telemetry_uuid_link(struct telemetry_de *tde,
+ struct telemetry_uuid *uuid);
+
static inline
struct mutex *lock_for_payld(struct mutex *locks, unsigned long offset)
{
@@ -613,8 +629,10 @@ static int scmi_telemetry_tde_register(struct telemetry_info *ti,
goto err;
/* Add to FastChannels list */
- if (tde->de.fc_support)
+ if (tde->de.fc_support) {
+ scmi_telemetry_uuid_link(tde, ti->primary_uuid);
list_add(&tde->item, &ti->fcs_des);
+ }
/* ... and in the general array */
rinfo->des[rinfo->num_des] = &tde->de;
@@ -1563,6 +1581,17 @@ static void scmi_telemetry_blkts_unlink(struct telemetry_de *tde)
trace_scmi_tlm_access(tde->de.info->id, "BLKTS_UNLINK", 0, 0);
}
+static void scmi_telemetry_uuid_unlink(struct telemetry_de *tde)
+{
+ /* Acquiring order is: tde -> line */
+ lockdep_assert_held(&tde->mtx);
+ if (!tde->uuid)
+ return;
+
+ tde->uuid = NULL;
+ trace_scmi_tlm_access(tde->de.info->id, "UUID_UNLINK", 0, 0);
+}
+
static void scmi_telemetry_de_unlink(struct scmi_telemetry_de *de)
{
struct telemetry_de *tde = to_tde(de);
@@ -1571,6 +1600,7 @@ static void scmi_telemetry_de_unlink(struct scmi_telemetry_de *de)
/* Unlink all related lines triggering their deallocation */
scmi_telemetry_blkts_unlink(tde);
+ scmi_telemetry_uuid_unlink(tde);
}
static struct telemetry_line *
@@ -1641,6 +1671,154 @@ scmi_telemetry_blkts_get_or_create(struct telemetry_shmti *shmti,
return scmi_telemetry_blkts_create(shmti, offset);
}
+static int scmi_telemetry_uuids_update(struct telemetry_info *ti,
+ struct telemetry_uuid *uuid)
+{
+ uuid_t **cur_uuids;
+
+ /* Resize array if needed ... */
+ if (ti->info.num_uuids + SCMI_UUID_DB_THRESH >= ti->uuids_len) {
+ uuid_t **new_uuids, **old_uuids;
+ unsigned int new_len = ti->uuids_len * 2;
+
+ new_uuids = kzalloc_objs(*new_uuids, new_len);
+ if (!new_uuids)
+ return -ENOMEM;
+
+ old_uuids = rcu_dereference_protected(ti->info.uuids,
+ lockdep_is_held(&ti->uuids_mtx));
+ /* Copy/move old allocated UUIDs */
+ for (int i = 0; i < ti->info.num_uuids; i++)
+ new_uuids[i] = old_uuids[i];
+
+ /* Expose new UUID pointer */
+ rcu_assign_pointer(ti->info.uuids, new_uuids);
+ ti->uuids_len = new_len;
+
+ kfree_rcu_mightsleep(old_uuids);
+ }
+
+ /* Store new UUID reference into the globally accessible UUIDS */
+ cur_uuids = rcu_dereference_protected(ti->info.uuids,
+ lockdep_is_held(&ti->uuids_mtx));
+ cur_uuids[ti->info.num_uuids] = &uuid->uuid;
+ rcu_assign_pointer(ti->info.uuids, cur_uuids);
+ /* Update UUIDs count */
+ smp_store_release(&ti->info.num_uuids, ti->info.num_uuids + 1);
+
+ return 0;
+}
+
+static int scmi_telemetry_uuid_register(struct telemetry_info *ti, u32 key,
+ struct telemetry_uuid *uuid)
+{
+ int ret;
+
+ lockdep_assert_held(&ti->uuids_mtx);
+
+ ret = scmi_telemetry_uuids_update(ti, uuid);
+ if (ret)
+ return ret;
+
+ /* Register by key for easy lookup */
+ hash_add(ti->uuids_ht, &uuid->node_ht, key);
+
+ trace_scmi_tlm_collect(0, key, 0, "SHMTI_NEW_UUID");
+
+ return 0;
+}
+
+static struct telemetry_uuid *
+scmi_telemetry_uuid_create(struct telemetry_info *ti, __be32 *dwords)
+{
+ u32 key;
+ int ret;
+
+ lockdep_assert_held(&ti->uuids_mtx);
+
+ struct telemetry_uuid *uuid __free(kfree) = kzalloc_obj(*uuid);
+ if (!uuid)
+ return NULL;
+
+ /*
+ * Fetch BigEndian in-memory UUID fields as per SCMIv4.0
+ * specification 3.12.2.2
+ */
+ import_uuid(&uuid->uuid, (__force const __u8 *)dwords);
+ key = jhash2((__force const u32 *)dwords, SCMI_TLM_DE_IMPL_NUM_DWORDS, 1);
+ ret = scmi_telemetry_uuid_register(ti, key, uuid);
+ if (ret)
+ return NULL;
+
+ return no_free_ptr(uuid);
+}
+
+static struct telemetry_uuid *
+scmi_telemetry_uuid_lookup(struct telemetry_info *ti, __be32 *dwords)
+{
+ struct telemetry_uuid *uuid;
+ u32 key;
+
+ lockdep_assert_held(&ti->uuids_mtx);
+
+ key = jhash2((__force const u32 *)dwords, SCMI_TLM_DE_IMPL_NUM_DWORDS, 1);
+ hash_for_each_possible(ti->uuids_ht, uuid, node_ht, key)
+ if (!memcmp(&uuid->uuid, dwords, sizeof(uuid->uuid)))
+ return uuid;
+
+ return NULL;
+}
+
+static struct telemetry_uuid *
+scmi_telemetry_uuid_get_or_create(struct telemetry_info *ti, __be32 *dwords)
+{
+ struct telemetry_uuid *uuid;
+
+ guard(mutex)(&ti->uuids_mtx);
+
+ uuid = scmi_telemetry_uuid_lookup(ti, dwords);
+ if (uuid)
+ return uuid;
+
+ return scmi_telemetry_uuid_create(ti, dwords);
+}
+
+/**
+ * scmi_telemetry_tdcf_uuid_parse - A UUID line parser
+ *
+ * @ti: A reference to the telemetry_info descriptor
+ * @uuid_payld: UUID payld line to process
+ * @snap: A reference to the working SHMTI snapshot that will be processed
+ * @active_uuid: Input/output reference to keep track of the last uuid found
+ *
+ * Note that by the SCMI v4.0 Telemetry specification the associations between
+ * DEs and UUIDs, both primary and secondary, are permanent across the same
+ * session (boot): once discovered they cannot change.
+ *
+ * Moreover, since the current UUID line could be malformed due to garbage in
+ * the TDCF, in such a case we warn/log and bailout BUT we don't throw away
+ * the currently active_uuid (if any) since the current malformed line could
+ * be bogus and never intended to be a UUID line.
+ */
+static void scmi_telemetry_tdcf_uuid_parse(struct telemetry_info *ti,
+ struct payload *uuid_payld,
+ struct shmti_snap *snap,
+ struct telemetry_uuid **active_uuid)
+{
+ __be32 *dwords = uuid_payld->uuid_l.dwords;
+ u32 meta = PAYLD_META_RAW(uuid_payld);
+
+ if (WARN_ON(UUID_INVALID_RAW(meta))) {
+ trace_scmi_tlm_access(0, "UUID_INVALID",
+ snap->magic, snap->magic);
+ return;
+ }
+
+ *active_uuid = scmi_telemetry_uuid_get_or_create(ti, dwords);
+
+ WARN_ON(!*active_uuid);
+}
+
/**
* scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser
*
@@ -1762,6 +1940,18 @@ static inline void scmi_telemetry_blkts_link(struct telemetry_de *tde,
trace_scmi_tlm_access(tde->de.info->id, "BLKTS_LINK", 0, 0);
}
+static inline void scmi_telemetry_uuid_link(struct telemetry_de *tde,
+ struct telemetry_uuid *uuid)
+{
+ lockdep_assert_held(&tde->mtx);
+ if (tde->uuid)
+ return;
+
+ /* Update UUID association */
+ tde->uuid = uuid;
+ trace_scmi_tlm_access(tde->de.info->id, "UUID_LINK", 0, 0);
+}
+
/**
* scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing
* @ti: A reference to the telemetry info descriptor
@@ -1895,6 +2085,9 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
tde->de.info->ts_rate = tde->bts->last_rate;
}
+ /* Link the active UUID or the primary, it's unlinked on disable */
+ scmi_telemetry_uuid_link(tde, active_uuid ?: ti->primary_uuid);
+
/*
* Parse data words ... only for the sake of updating TDE cache,
* any error is logged but ignored.
@@ -1961,6 +2154,9 @@ static int scmi_telemetry_tdcf_line_parse(struct telemetry_info *ti,
case TDCF_BLK_TS_LINE:
scmi_telemetry_tdcf_blkts_parse(ti, payld, snap, active_bts);
break;
+ case TDCF_UUID_LINE:
+ scmi_telemetry_tdcf_uuid_parse(ti, payld, snap, active_uuid);
+ break;
default:
trace_scmi_tlm_access(PAYLD_ID_RAW(payld), "TDCF_LINE_UNKNOWN", 0, 0);
break;
@@ -2258,6 +2454,7 @@ static int scmi_telemetry_resources_alloc(struct telemetry_info *ti)
}
INIT_LIST_HEAD(&ti->fcs_des);
+ hash_init(ti->uuids_ht);
ti->shmti = no_free_ptr(shmtis);
ti->info.shmtis = no_free_ptr(shmtis_info);
@@ -2305,10 +2502,42 @@ static void scmi_telemetry_resources_free(struct telemetry_info *ti)
dev_dbg(ti->ph->dev, "SCMI Telemetry resources freed for instance\n");
}
+static void scmi_telemetry_uuids_cleanup(struct telemetry_info *ti)
+{
+ uuid_t **cur_uuids;
+
+ /* Drop reference to UUID lines kept in the growable array */
+ scoped_guard(mutex, &ti->uuids_mtx) {
+ int num_uuids;
+
+ /* Ensure looking up an updated counter */
+ num_uuids = smp_load_acquire(&ti->info.num_uuids);
+ /* Ensure zeroing is visible */
+ smp_store_release(&ti->info.num_uuids, 0);
+ /* Inhibit further accesses to cur_uuids */
+ cur_uuids = rcu_replace_pointer(ti->info.uuids, NULL,
+ lockdep_is_held(&ti->uuids_mtx));
+ synchronize_rcu();
+
+ for (int i = 0; i < num_uuids; i++) {
+ struct telemetry_uuid *uuid;
+
+ uuid = to_uuid_from_uuid_t(cur_uuids[i]);
+ hash_del(&uuid->node_ht);
+ kfree(uuid);
+
+ trace_scmi_tlm_access(0, "DROP_LINE", TDCF_UUID_LINE, 0);
+ }
+ }
+
+ kfree_rcu_mightsleep(cur_uuids);
+}
+
static void scmi_telemetry_instance_cleanup(void *arg)
{
struct telemetry_info *ti = arg;
+ scmi_telemetry_uuids_cleanup(ti);
scmi_telemetry_resources_free(ti);
}
@@ -2379,6 +2608,23 @@ scmi_telemetry_resources_enumerate(struct telemetry_info *ti)
return ti->rinfo;
}
+static int scmi_telemetry_primary_uuid_init(struct telemetry_info *ti)
+{
+ __be32 *dwords = (__be32 *)&ti->info.base.primary_revision.b;
+ struct telemetry_uuid *uuid;
+
+ uuid = scmi_telemetry_uuid_get_or_create(ti, dwords);
+ if (!uuid) {
+ dev_err(ti->ph->dev, "Failed to register Primary UUID\n");
+ return -ENOMEM;
+ }
+
+ /* Ensure Primary UUID association is visible */
+ smp_store_release(&ti->primary_uuid, uuid);
+
+ return 0;
+}
+
/**
* scmi_telemetry_instance_init - Instance initializer
* @ti: A reference to the telemetry info descriptor for this instance
@@ -2394,12 +2640,41 @@ scmi_telemetry_resources_enumerate(struct telemetry_info *ti)
*/
static int scmi_telemetry_instance_init(struct telemetry_info *ti)
{
+ unsigned int uuids_len;
+ uuid_t **cur_uuids;
int ret;
ret = scmi_telemetry_resources_alloc(ti);
if (ret)
return ret;
+ /*
+ * Always allocate at least one slot for the primary and anyway at
+ * least enough to avoid immediate resizing, assuring uuids_len
+ * always greater or equal to one.
+ */
+ uuids_len = max(ti->num_shmti * 2, SCMI_UUID_DB_THRESH + 1);
+ cur_uuids = kzalloc_objs(*cur_uuids, uuids_len);
+ if (!cur_uuids) {
+ scmi_telemetry_resources_free(ti);
+ return -ENOMEM;
+ }
+
+ mutex_init(&ti->uuids_mtx);
+ /* Expose new UUID pointer */
+ scoped_guard(mutex, &ti->uuids_mtx) {
+ rcu_assign_pointer(ti->info.uuids, cur_uuids);
+ ti->uuids_len = uuids_len;
+ }
+
+ /* Initialize the first UUIDs in the global list with the primary */
+ ret = scmi_telemetry_primary_uuid_init(ti);
+ if (ret) {
+ scmi_telemetry_uuids_cleanup(ti);
+ scmi_telemetry_resources_free(ti);
+ return ret;
+ }
+
atomic_set(&ti->des_enabled[ENA_STATE], 0);
atomic_set(&ti->des_enabled[ENA_TSTAMP], 0);
mutex_init(&ti->des_enabled_mtx);
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index 4a765a529ebf..b828cbc1eb21 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -957,6 +957,8 @@ struct scmi_telemetry_info {
unsigned int active_update_interval;
struct scmi_telemetry_intervals *intervals;
struct scmi_telemetry_shmti_info **shmtis;
+ unsigned int num_uuids;
+ uuid_t * __rcu *uuids;
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 18/34] firmware: arm_scmi: Add Telemetry DEs configuration support
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (16 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 17/34] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 19/34] firmware: arm_scmi: Add Telemetry global " Cristian Marussi
` (16 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add support for basic Telemetry DEs configuration operations to selectively
enable or disable DataEvents monitoring.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- use NULL not zero for pointers
- fix shmti_oveflow WITH LEN
- fix liveness blkts_snapshot
- fix tde_state_set_resp handling of INVALISD SHMTI_ID
- refactor state_set and reject group state change
- invalidate TDE cache also on enable
- allow SHMTI_INVALID as bts_offs when DE does NOT use BLK_TS
- fix err init in blkts_snapshot
- add comment on LINE_LENGTH_BYTES upper bound
- remove spurious return
v15 --> v16
- split out group config support
- split out global collection configure
- check liveness on blkts_snapshot too
- refatcor blkts_update on DE enable and update de.ts_rate even when
BLK_TS line is the same
- on disable do NOT assume SHMTI_ID_INVALID means disable
- fix remote_update_needed logic
- invalidate cache on DE disable
- invalidate base/offset on disable if NOT FC
- use new shmti_overflow with weplg directly
v14 --> v15
- unlink BLK_TS on DE disable
- update linked BLK_TS on enable
- update TDE location ONLY after successfull linked lines processing
- skip tstamp state update when tstamp NOT supported
- fix TDE location returned value on error path
- trigger resource enumeration on state_get
- trigger resource enumeration on colletion configure only for groups
- use rinfo->num_groups to check groups sanity
- fix per-group collection_configure calls
- fix streaming_mode variable handling
- widen tde->mtx locking section on state_set
- refactor UUID/BLK_TS lines processing in enable
- introduce BLK_TS localized snapshot during enable
- check line type at de_offs
- sanitize requested mode on collection conf
- missing err init
- moved away definition of scmi_telemetry_payload_offset_safe_calc
v13 --> v14
- move BLK_ST_CHANGED check when bts_payld is really known
- check lso LINE_TYPE when processing BLK_TS on enable
- grab individual tde->mtx while updating group state
- clear returned sid/offset/uid on error path
- do NOT mutate global config when configuring groups
- call state_update on do_xfer success even if response process fails
- moved some definitions from earlier split patch
- renamed some fields to more strictly adhere to spec nomenclature
v12 --> v13
- refactored cleanup path
- generalize payload_offset_safe_calc
- harden nearest_line_type scanner
- full refactoring of SET/GET helpers: keep DE and group in
distinct code path
- relocate tde->mtx guards higher up and add lockdep assertions
- cleanup xa_lines under mutex_lock on cleanup
- unlink stale BTS line when finding a new actibe for the TDE
- use *alloc_objs
v11 --> v12
- added a few locking order acquisition comments
- added more checks on wraparounds and alignment
- added proper locking around TDE access on state_get
- added a config_mtx to serialize configuration accesses between collection
configure and state_set
- refactored state_cache_update
- added locking around sid/offset/uuid updates on enable
v10 --> v11
- refactor reply processing code on DE enable/disable
- use the new des_ennabled mutex to protect des cache updates
- clear sid on group disable for each composing DEs
- add dev_warn_once on ZERO blk_ts offset
v8 --> v9
- use new scmi_telemetry_shmti_overflow() inline
- review DE offsets boundary and wraparound checks (Sashiko v8
- update per-DE ts_rate for DE using BLK_TS too
v7 --> v8
- Fixes for some Sashiko issues on v7
- re-add functions unused in previous patch
- add proper barriers to TDE/UUID binding process
- update tde->sid on SCAN_UPDATE too
- check wrap-arounds on shmti_de_offset manipulation
- fix GROUP enable pollutes status update (was polluting global state)
- remove .all_disable telemetry_operation since unused
- add WARN on if a valid SHMTI ID is reported on enabling a FastChannel/DE
v6 --> v7
- expose per-DE tracked sid/offset/UUIDs
- trigger a full SHMTI scan when a SHMTI-based is enabled and it still has NO
associated UUID
v5 --> v6
- constify a few booleans used as inpurt in telemetry_operations
- fixes (some) Sashiko reviews
https://sashiko.dev/#/patchset/20260703123601.381275-1-cristian.marussi%40arm.com?part=9
- underflow on de_offs
- SHMTI overflow checks
- bail out on failed group scan
- unlink comprising DEs on group disable
- add boudnary check on message response parsing
v4 --> v5
- use renamed grp_id instead of old id in group descriptors
- remove useless grp_ignore param from collection_configure
- fixes sparse warnings
v3 --> v4
- track configured Telemetry DE states
- add aggregate Telemetry state query
v2 --> v3
- split from monolithic Telemetry patch
- simplify clenaup with scmi_telemetry_de_unlink
---
drivers/firmware/arm_scmi/telemetry.c | 709 +++++++++++++++++++++++++-
include/linux/scmi_protocol.h | 11 +
2 files changed, 718 insertions(+), 2 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index fe63e7f5d145..140ff1072b99 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -133,6 +133,49 @@ struct scmi_msg_resp_telemetry_update_intervals {
__le32 intervals[];
};
+struct scmi_msg_telemetry_de_configure {
+ __le32 identifier;
+ __le32 flags;
+#define CFG_ENABLE_NO_TSTAMP BIT(0)
+#define CFG_ENABLE_WTH_TSTAMP BIT(1)
+#define CFG_DISABLE_ALL BIT(2)
+#define CFG_GROUP_SELECTOR BIT(3)
+#define CFG_EVENT_DE 0
+#define CFG_EVENT_GROUP 1
+#define CFG_DISABLE_ONE 0x0
+};
+
+struct scmi_msg_resp_telemetry_de_configure {
+ __le32 shmti_id;
+#define SHMTI_ID_INVALID GENMASK(31, 0)
+#define IS_SHMTI_ID_VALID(x) ((x) != SHMTI_ID_INVALID)
+ __le32 shmti_de_offset;
+ __le32 blk_ts_offset;
+};
+
+struct scmi_msg_telemetry_config_set {
+ __le32 group_identifier;
+ __le32 control;
+#define TELEMETRY_ENABLE (BIT(0))
+#define TELEMETRY_MODE GENMASK(4, 1)
+#define TELEMETRY_MODE_SET(x) (FIELD_PREP(TELEMETRY_MODE, (x)))
+#define TLM_ONDEMAND (0)
+#define TLM_NOTIFS (1)
+#define TLM_SINGLE (2)
+#define TELEMETRY_MODE_ONDEMAND TELEMETRY_MODE_SET(TLM_ONDEMAND)
+#define TELEMETRY_MODE_NOTIFS TELEMETRY_MODE_SET(TLM_NOTIFS)
+#define TELEMETRY_MODE_SINGLE TELEMETRY_MODE_SET(TLM_SINGLE)
+
+#define TLM_ORPHANS (0)
+#define TLM_GROUP (1)
+#define TLM_ALL (2)
+#define TELEMETRY_SET_SELECTOR(x) (FIELD_PREP(GENMASK(8, 5), (x)))
+#define TELEMETRY_SET_SELECTOR_ORPHANS TELEMETRY_SET_SELECTOR(TLM_ORPHANS)
+#define TELEMETRY_SET_SELECTOR_GROUP TELEMETRY_SET_SELECTOR(TLM_GROUP)
+#define TELEMETRY_SET_SELECTOR_ALL TELEMETRY_SET_SELECTOR(TLM_ALL)
+ __le32 sampling_rate;
+};
+
/* TDCF */
#define _I(__a) (ioread32((void __iomem *)(__a)))
@@ -423,8 +466,6 @@ enum timestamps {
TSTAMP_BLK = 2,
};
-#define SHMTI_ID_INVALID GENMASK(31, 0)
-
struct telemetry_de {
enum timestamps ts_type;
bool enumerated;
@@ -483,6 +524,8 @@ struct telemetry_info {
struct mutex free_mtx;
struct list_head free_des;
struct list_head fcs_des;
+ /* Protect global and per-group configuration data */
+ struct mutex config_mtx;
struct scmi_telemetry_info info;
atomic_t rinfo_initializing;
struct completion rinfo_initdone;
@@ -1819,6 +1862,70 @@ static void scmi_telemetry_tdcf_uuid_parse(struct telemetry_info *ti,
WARN_ON(!*active_uuid);
}
+static struct payload __iomem *
+scmi_telemetry_nearest_line_by_type(struct telemetry_shmti *shmti,
+ void __iomem *last, enum tdcf_line_types ltype)
+{
+ struct tdcf __iomem *tdcf = shmti->base;
+ void __iomem *next, *found = NULL;
+
+ /* Scan from start of the first TDCF payload up to last payld */
+ next = tdcf->payld;
+ while (next < last) {
+ struct payload __iomem *payld;
+ enum tdcf_line_types cur_type;
+ u32 meta, offs;
+
+ if (check_sub_overflow((__force unsigned long)next,
+ (__force unsigned long)tdcf, &offs)) {
+ trace_scmi_tlm_access(ltype, "LINE_OFFSET_OUT_OF_RANGE", 0, 0);
+ return NULL;
+ }
+
+ payld = scmi_telemetry_payld_offset_safe_calc(shmti, offs);
+ if (IS_ERR(payld)) {
+ trace_scmi_tlm_access(ltype, "LINE_PAYLD_MALFORMED", 0, 0);
+ return NULL;
+ }
+
+ meta = PAYLD_META(payld);
+ cur_type = LINE_TYPE_RAW(meta);
+
+ /*
+ * Bail-out on malformed lines, since the whole TDCF scan
+ * could be affected.
+ */
+ if ((cur_type == TDCF_BLK_TS_LINE && BLK_TS_INVALID_RAW(meta)) ||
+ (cur_type == TDCF_UUID_LINE && UUID_INVALID_RAW(meta))) {
+ trace_scmi_tlm_access(ltype, "LINE_TYPE_MALFORMED", 0, 0);
+ return NULL;
+ }
+
+ if (cur_type == ltype)
+ found = next;
+
+ next += LINE_LENGTH_BYTES_RAW(meta);
+ }
+
+ return found;
+}
+
+static struct telemetry_block_ts *
+scmi_telemetry_blkts_bind(struct telemetry_info *ti,
+ struct telemetry_shmti *shmti,
+ struct payload *bts_payld,
+ unsigned long offset, u32 magic)
+{
+ struct telemetry_block_ts *bts;
+
+ bts = scmi_telemetry_blkts_get_or_create(shmti, offset);
+ /* Update the descriptor with the lastest TS */
+ if (bts)
+ scmi_telemetry_blkts_update_snap(ti, bts, bts_payld, magic);
+
+ return bts;
+}
+
/**
* scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser
*
@@ -1952,6 +2059,32 @@ static inline void scmi_telemetry_uuid_link(struct telemetry_de *tde,
trace_scmi_tlm_access(tde->de.info->id, "UUID_LINK", 0, 0);
}
+static struct telemetry_uuid *
+scmi_telemetry_uuid_bind(struct telemetry_info *ti,
+ struct telemetry_shmti *shmti,
+ struct payload __iomem *payld)
+{
+ __be32 dwords[SCMI_TLM_DE_IMPL_NUM_DWORDS];
+ struct payload __iomem *uuid_payld;
+ struct uuid_line __iomem *uuid_l;
+
+ uuid_payld = scmi_telemetry_nearest_line_by_type(shmti, payld,
+ TDCF_UUID_LINE);
+ if (!uuid_payld)
+ return NULL;
+
+ /*
+ * Use proper mem accessors BUT no swapping, maintain UUID
+ * in memory BE layout.
+ */
+ uuid_l = &uuid_payld->uuid_l;
+ for (int i = 0; i < SCMI_TLM_DE_IMPL_NUM_DWORDS; i++)
+ dwords[i] = (__force __be32)__raw_readl(&uuid_l->dwords[i]);
+
+ /* Here the nearest UUID line just found has been already validated */
+ return scmi_telemetry_uuid_get_or_create(ti, dwords);
+}
+
/**
* scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing
* @ti: A reference to the telemetry info descriptor
@@ -2385,10 +2518,581 @@ static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
return ret;
}
+static struct payload __iomem *
+scmi_telemetry_blkts_payld_locate(struct device *dev,
+ struct telemetry_shmti *shmti, u32 *bts_offs,
+ struct payload __iomem *payld, u32 de_id)
+{
+ struct payload __iomem *bts_payld = NULL;
+
+ if (!bts_offs)
+ return IOMEM_ERR_PTR(-EINVAL);
+
+ if (*bts_offs &&
+ check_add_overflow((__force unsigned long)shmti->base, *bts_offs,
+ (__force unsigned long *)&bts_payld))
+ return IOMEM_ERR_PTR(-EPROTO);
+
+ /* Check SHMTI overflow on returned offset */
+ if (bts_payld) {
+ ssize_t ret = scmi_telemetry_shmti_overflow(shmti->base,
+ SHMTI_EPLG(shmti),
+ bts_payld);
+ if (ret < 0) {
+ trace_scmi_tlm_access(de_id, "SHMTI_OVERFLOW", 0, 0);
+ return IOMEM_ERR_PTR(ret);
+ }
+ } else {
+ dev_warn_once(dev, "Received BLK_TS with zero offset.\n");
+ trace_scmi_tlm_access(de_id, "BLKTS_WITH_ZERO_OFFSET", 0, 0);
+
+ /*
+ * Find the BLK_TS immediately preceding this DE payld by
+ * scanning.
+ */
+ bts_payld = scmi_telemetry_nearest_line_by_type(shmti, payld,
+ TDCF_BLK_TS_LINE);
+ if (WARN_ON(!bts_payld))
+ return IOMEM_ERR_PTR(-EPROTO);
+
+ /* Calculate offset of found blkts */
+ *bts_offs = (void __iomem *)bts_payld - shmti->base;
+ }
+
+ return bts_payld;
+}
+
+static int
+scmi_telemetry_tdcf_bts_payld_check(struct payload *bts_snap, u32 de_id)
+{
+ u32 meta = PAYLD_META_RAW(bts_snap);
+
+ if (!IS_BLK_TS_LINE_RAW(meta)) {
+ trace_scmi_tlm_access(de_id, "BAD_BLK_TS_LINE_OFFSET", 0, meta);
+ return -EPROTO;
+ }
+
+ /* Check for spec compliance */
+ if (BLK_TS_INVALID_RAW(meta)) {
+ trace_scmi_tlm_access(de_id, "BLK_TS_INVALID", 0, meta);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int scmi_telemetry_blkts_snapshot(struct telemetry_shmti *shmti,
+ struct payload __iomem *bts_payld,
+ struct payload *bts_snap, u32 *magic,
+ u32 de_id)
+{
+ int retries = SCMI_TLM_TDCF_MAX_RETRIES;
+ struct tdcf __iomem *tdcf = shmti->base;
+ u32 startm, endm, live = 0;
+ int err;
+
+ do {
+ /* A bit of exponential backoff between retries */
+ fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP_US);
+
+ err = 0;
+ startm = TDCF_START_SEQ_GET(tdcf);
+ /* Build a default endm certainly different from startm */
+ endm = startm + 1;
+ if (IS_BAD_START_SEQ(startm)) {
+ trace_scmi_tlm_access(de_id, "MSEQ_BADSTART_BLKTS",
+ startm, 0);
+ continue;
+ }
+
+ /*
+ * NOTE that while LINE_LENGTH_BYTES depends on firmware
+ * provided values, the upper bound does NOT depend on
+ * firmware
+ */
+ memcpy_fromio(bts_snap, bts_payld, LINE_LENGTH_BYTES(bts_payld));
+ /*
+ * In this context any anomaly detected on the payload could
+ * due to a torn read, and it is worth retrying a snapshot.
+ */
+ err = scmi_telemetry_tdcf_bts_payld_check(bts_snap, de_id);
+ if (err)
+ continue;
+
+ /* Assure the snapshot has been fully taken before checking seqnum */
+ rmb();
+
+ /* Used to detect changes while copying */
+ live = TDCF_START_SEQ_GET(tdcf);
+ endm = TDCF_END_SEQ_GET(SHMTI_EPLG(shmti));
+ if (startm != endm || startm != live)
+ trace_scmi_tlm_access(de_id, "MSEQ_MISMATCH_BLKTS",
+ startm, startm != endm ? endm : live);
+ } while ((startm != endm || startm != live || err) && --retries);
+
+ if (startm != endm || startm != live || err) {
+ trace_scmi_tlm_access(de_id, "TDCF_SNAPSHOT_BLKTS_FAIL",
+ startm, startm != endm ? endm : live);
+ return err ?: -EPROTO;
+ }
+
+ /* Ensure any snapshot is visible before updating magic */
+ smp_store_release(magic, startm);
+
+ trace_scmi_tlm_access(de_id, "TDCF_SNAPSHOT_BLKTS_OK", startm, endm);
+
+ return 0;
+}
+
+static struct telemetry_block_ts *
+scmi_telemetry_blkts_update(struct telemetry_info *ti,
+ struct telemetry_shmti *shmti,
+ struct payload __iomem *bts_payld,
+ u32 bts_offs, u32 de_id)
+{
+ struct telemetry_block_ts *bts;
+ struct payload bts_snap = {};
+ u32 magic = 0;
+ int ret;
+
+ ret = scmi_telemetry_blkts_snapshot(shmti, bts_payld, &bts_snap,
+ &magic, de_id);
+ if (ret)
+ return ERR_PTR(ret);
+
+ /* Look for a viable BLK_TS line */
+ bts = scmi_telemetry_blkts_bind(ti, shmti, &bts_snap, bts_offs, magic);
+ if (WARN_ON(!bts)) {
+ trace_scmi_tlm_access(de_id, "INVALID_USE_BLK_TS", 0, 0);
+ return ERR_PTR(-EPROTO);
+ }
+
+ return bts;
+}
+
+static int
+scmi_telemetry_tde_linked_lines_process(struct telemetry_info *ti,
+ struct telemetry_de *tde,
+ struct telemetry_shmti *shmti,
+ struct payload __iomem *payld,
+ u32 bts_offs)
+{
+ struct payload __iomem *bts_payld;
+
+ lockdep_assert_held(&ti->config_mtx);
+ lockdep_assert_held(&tde->mtx);
+
+ /*
+ * On enable, if a UUID was NOT already assigned, scan looking for
+ * possible Secondary UUID to bind: if NO secondary could be found
+ * embedded in the preceding SHMTI lines, install the primary UUID
+ * for this DE.
+ * NOTE that UUID lines content is immutable once created and no
+ * concurrent configuration request is possible at this point since
+ * config_mtx is held, so while the platform can still concurrently
+ * update all the existing SHMTI lines variable values, existing lines
+ * CANNOT be shuffled around: as a consequence a full snapshot is NOT
+ * needed as a pre-requisite to perform a scan when looking for
+ * UUID lines.
+ */
+ if (!tde->uuid) {
+ struct telemetry_uuid *uuid;
+
+ uuid = scmi_telemetry_uuid_bind(ti, shmti, payld);
+ if (uuid)
+ tde->uuid = uuid;
+ else
+ scmi_telemetry_uuid_link(tde, ti->primary_uuid);
+ }
+
+ /* Process related BLK_TS lines if any */
+ if (!USE_BLK_TS(payld)) {
+ /* Unlink old stale BTS if existent */
+ if (IS_TDE_TS_BLK(tde) && tde->bts)
+ scmi_telemetry_blkts_unlink(tde);
+ return 0;
+ }
+
+ if (!IS_TDE_TS_BLK(tde) || bts_offs == SHMTI_ID_INVALID) {
+ trace_scmi_tlm_access(tde->de.info->id, "BAD_DE_TSTAMP_TYPE", 0, 0);
+ return -EPROTO;
+ }
+
+ bts_payld = scmi_telemetry_blkts_payld_locate(ti->ph->dev, shmti,
+ &bts_offs, payld,
+ tde->de.info->id);
+ if (IS_ERR(bts_payld))
+ return PTR_ERR(bts_payld);
+
+ /* Has associated BLK_TS line changed at all ? */
+ if (BLK_TS_CHANGED(tde, shmti, bts_offs)) {
+ struct telemetry_block_ts *bts;
+
+ bts = scmi_telemetry_blkts_update(ti, shmti, bts_payld,
+ bts_offs, tde->de.info->id);
+ if (IS_ERR(bts))
+ return PTR_ERR(bts);
+
+ /* Unlink old stale BTS if existent */
+ scmi_telemetry_blkts_unlink(tde);
+
+ /* Store freshly discovered sane BLK_TS */
+ tde->bts = bts;
+ }
+
+ /* Update per-DE BLK_TS rate if any */
+ if (tde->bts->last_rate)
+ tde->de.info->ts_rate = tde->bts->last_rate;
+
+ return 0;
+}
+
+static int
+scmi_telemetry_state_set_tde_resp(struct telemetry_info *ti,
+ struct scmi_telemetry_de *de,
+ u32 sid, u32 de_offs, u32 bts_offs)
+{
+ struct telemetry_de *tde = to_tde(de);
+ struct telemetry_shmti *shmti;
+ struct payload __iomem *payld;
+ int ret;
+
+ lockdep_assert_held(&tde->mtx);
+
+ /* A FastChannel DE MUST return an INVALID SHMTI ID on enable */
+ if (WARN_ON(de->fc_support)) {
+ trace_scmi_tlm_access(de->info->id, "INVALID_DE_FC_ON_SHMTI", 0, 0);
+ return -EPROTO;
+ }
+
+ /* Update SHMTI/offset while skipping non-SHMTI based DES */
+ shmti = &ti->shmti[sid];
+ /* Check sanity of received location... */
+ payld = scmi_telemetry_payld_offset_safe_calc(shmti, de_offs);
+ if (IS_ERR(payld))
+ return PTR_ERR(payld);
+
+ /*
+ * Note that here we are processing a configuration request on a DE
+ * while holding @config_mtx: in this scenario platform cannot relocate
+ * or mutate this line freely, so we can directly access the TDCF and
+ * lookup the line sitting at @de_offs to check its line type without
+ * having to worry about consistency or taking snapshots.
+ */
+ if (!IS_DATA_LINE(payld))
+ return -EPROTO;
+
+ ret = scmi_telemetry_tde_linked_lines_process(ti, tde, shmti, payld,
+ bts_offs);
+ if (ret)
+ return ret;
+
+ /* ...then assign once validated */
+ tde->sid = sid;
+ tde->base = shmti->base;
+ tde->offset = de_offs;
+ /* A handy reference to the Epilogue updated */
+ tde->eplg = SHMTI_EPLG(shmti);
+
+ /* A relocation due to a TS toggle needs cache invalidation */
+ scmi_telemetry_tde_cache_invalidate(tde);
+
+ return 0;
+}
+
+static int
+scmi_telemetry_tde_state_set_resp_process(struct telemetry_info *ti,
+ struct scmi_telemetry_de *de,
+ struct scmi_msg_resp_telemetry_de_configure *r)
+{
+ u32 sid = le32_to_cpu(r->shmti_id);
+
+ /*
+ * 3.12.4.8 TELEMETRY_DE_CONFIGURE - SHMTI ID
+ * This field is optional and is set to 0xFFFFFFFF if any of the
+ * following are true:
+ * - no DE or event group is being enabled.
+ * - No SHMTI is supported.
+ * - SHMTI is not used for this DE or event group. This could
+ * be the case for example, if this DE is associated with a
+ * FastChannel.
+ * - the platform does not support returning this information.
+ */
+ if (de->enabled) {
+ u32 de_offs, bts_offs;
+
+ /* Platform does NOT support returning this information or a
+ * NON SHMTI based DE was enabled: in any case nothing to do.
+ */
+ if (!IS_SHMTI_ID_VALID(sid))
+ return 0;
+
+ if (sid >= ti->num_shmti)
+ return -EPROTO;
+
+ de_offs = le32_to_cpu(r->shmti_de_offset);
+ bts_offs = le32_to_cpu(r->blk_ts_offset);
+ if (!IS_ALIGNED(de_offs, 4) ||
+ (bts_offs != SHMTI_ID_INVALID && !IS_ALIGNED(bts_offs, 4))) {
+ dev_err(ti->ph->dev,
+ "Returned DE/BTS offsets are NOT 32-bit aligned - de_offs:%X bts_offs:%X\n",
+ de_offs, bts_offs);
+ return -EINVAL;
+ }
+
+ return scmi_telemetry_state_set_tde_resp(ti, de, sid,
+ de_offs, bts_offs);
+ } else {
+ /*
+ * Unlink the related BLK_TS lines on disable and update
+ * sid/offset, unless unsupported by platform or the
+ * current DE flavour (FCs/Notifs)
+ * UUID is never unlinked once discovered.
+ */
+ struct telemetry_de *tde = to_tde(de);
+
+ WARN_ON(IS_SHMTI_ID_VALID(sid));
+
+ lockdep_assert_held(&tde->mtx);
+
+ tde->sid = SHMTI_ID_INVALID;
+ if (!tde->de.fc_support) {
+ tde->base = NULL;
+ tde->offset = 0;
+ tde->eplg = NULL;
+
+ scmi_telemetry_blkts_unlink(tde);
+ }
+
+ /* Consider that a TDE could be re-located on next re-enable */
+ scmi_telemetry_tde_cache_invalidate(tde);
+ }
+
+ return 0;
+}
+
+static void
+scmi_telemetry_state_cache_update(struct telemetry_info *ti,
+ const bool *enable, bool *enabled_state,
+ const bool *tstamp, bool *tstamp_enabled_state,
+ struct scmi_telemetry_de *de)
+{
+ if (!de)
+ return;
+
+ if (enable)
+ scmi_telemetry_de_state_update(ti, ENA_STATE,
+ enabled_state, *enable);
+
+ if (tstamp && de->tstamp_support)
+ scmi_telemetry_de_state_update(ti, ENA_TSTAMP,
+ tstamp_enabled_state, *tstamp);
+}
+
+static inline bool
+scmi_telemetry_remote_state_update_needed(struct telemetry_info *ti,
+ const bool *enable, bool *enabled_state,
+ const bool *tstamp,
+ bool *tstamp_enabled_state,
+ bool is_group, void *obj)
+{
+ /* Is anything to do at all on this object ? */
+ if ((!enable || *enable == *enabled_state) &&
+ (!tstamp || *tstamp == *tstamp_enabled_state))
+ return false;
+
+ /*
+ * When the object is currently disabled AND no enable state change was
+ * requested, BUT timestamp is being changed, update only local state...
+ * no need to send a message now, it will be sent later on enable.
+ */
+ if ((!enable || !*enable) && !*enabled_state) {
+ scmi_telemetry_state_cache_update(ti, enable,
+ enabled_state, tstamp,
+ tstamp_enabled_state, obj);
+
+ return false;
+ }
+
+ return true;
+}
+
+static inline void
+scmi_telemetry_state_message_prepare(struct scmi_msg_telemetry_de_configure *msg,
+ unsigned int id, bool is_group,
+ const bool *enable, bool *enabled_state,
+ const bool *tstamp, bool *tstamp_enabled_state)
+{
+ msg->identifier = cpu_to_le32(id);
+ /* Default to disable mode */
+ msg->flags = CFG_DISABLE_ONE;
+ msg->flags |= cpu_to_le32(FIELD_PREP(GENMASK(3, 3),
+ !is_group ? CFG_EVENT_DE : CFG_EVENT_GROUP));
+
+ if ((!enable && *enabled_state) || (enable && *enable)) {
+ /* Already enabled but tstamp_enabled state changed */
+ if (tstamp) {
+ /* Here, tstamp cannot be NULL too */
+ msg->flags |= cpu_to_le32(*tstamp ?
+ CFG_ENABLE_WTH_TSTAMP : CFG_ENABLE_NO_TSTAMP);
+ } else {
+ msg->flags |= cpu_to_le32(*tstamp_enabled_state ?
+ CFG_ENABLE_WTH_TSTAMP : CFG_ENABLE_NO_TSTAMP);
+ }
+ }
+}
+
+static inline void
+scmi_telemetry_tde_location_update(struct telemetry_de *tde,
+ u32 *sid, u32 *offset, uuid_t *uuid)
+{
+ if (tde)
+ lockdep_assert_held(&tde->mtx);
+
+ if (sid)
+ *sid = tde ? tde->sid : SHMTI_ID_INVALID;
+ if (offset)
+ *offset = tde ? tde->offset : 0;
+ if (uuid) {
+ if (tde && tde->uuid)
+ uuid_copy(uuid, &tde->uuid->uuid);
+ else
+ memset(uuid, 0x0, sizeof(*uuid));
+ }
+}
+
+static int scmi_telemetry_tde_state_set(const struct scmi_protocol_handle *ph,
+ const bool *enable, const bool *tstamp,
+ struct scmi_telemetry_de *de,
+ u32 *sid, u32 *offset, uuid_t *uuid)
+{
+ struct scmi_msg_resp_telemetry_de_configure *resp;
+ struct scmi_msg_telemetry_de_configure *msg;
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct telemetry_de *tde = to_tde(de);
+ struct scmi_xfer *t;
+ int ret;
+
+ /* Stall concurrent configuration request */
+ guard(mutex)(&ti->config_mtx);
+
+ /*
+ * Note that the TDE lock must be taken early on, before the SCMI
+ * configuration command is even issued to avoid a de-sync between the
+ * TDE state and the effective HW state enforced by the platform in
+ * response to our request.
+ */
+ guard(mutex)(&tde->mtx);
+
+ /* Initialize return values with current TDE in case of error */
+ scmi_telemetry_tde_location_update(tde, sid, offset, uuid);
+
+ if (!scmi_telemetry_remote_state_update_needed(ti, enable, &de->enabled,
+ tstamp, &de->tstamp_enabled,
+ false, de))
+ return 0;
+
+ ret = ph->xops->xfer_get_init(ph, TELEMETRY_DE_CONFIGURE,
+ sizeof(*msg), sizeof(*resp), &t);
+ if (ret)
+ return ret;
+
+ msg = t->tx.buf;
+ scmi_telemetry_state_message_prepare(msg, de->info->id, false,
+ enable, &de->enabled,
+ tstamp, &de->tstamp_enabled);
+
+ resp = t->rx.buf;
+ ret = ph->xops->do_xfer(ph, t);
+ if (!ret) {
+ /*
+ * Update cached state on command success anyway, despite any
+ * possible later response validation issue: once the command
+ * has succedeed keep our state in sync with HW.
+ */
+ scmi_telemetry_state_cache_update(ti, enable, &de->enabled,
+ tstamp, &de->tstamp_enabled, de);
+ /* Process response, update DEs location tracking */
+ ret = scmi_telemetry_tde_state_set_resp_process(ti, de, resp);
+ if (!ret)
+ scmi_telemetry_tde_location_update(tde, sid, offset, uuid);
+ else
+ dev_err(ph->dev,
+ "Could NOT process DE ID 0x%08X state response - ret:%d\n",
+ tde->de.info->id, ret);
+ }
+
+ ph->xops->xfer_put(ph, t);
+
+ return ret;
+}
+
+static int scmi_telemetry_state_get(const struct scmi_protocol_handle *ph,
+ const u32 *id, bool *enabled, bool *tstamp_enabled,
+ u32 *sid, u32 *offset, uuid_t *uuid)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct scmi_telemetry_de *de;
+ struct telemetry_de *tde;
+
+ if (!enabled || !tstamp_enabled)
+ return -EINVAL;
+
+ ti->res_get(ti);
+ if (!id) {
+ guard(mutex)(&ti->des_enabled_mtx);
+ /* Returning the all_des_* state */
+ *enabled =
+ (atomic_read(&ti->des_enabled[ENA_STATE]) == ti->info.base.num_des);
+ *tstamp_enabled =
+ (atomic_read(&ti->des_enabled[ENA_TSTAMP]) == ti->num_des_tstamp);
+
+ return 0;
+ }
+
+ de = xa_load(&ti->xa_des, *id);
+ if (!de)
+ return -ENODEV;
+
+ tde = to_tde(de);
+ guard(mutex)(&tde->mtx);
+
+ *enabled = de->enabled;
+ *tstamp_enabled = de->tstamp_enabled;
+
+ scmi_telemetry_tde_location_update(tde, sid, offset, uuid);
+
+ return 0;
+}
+
+static int scmi_telemetry_state_set(const struct scmi_protocol_handle *ph,
+ bool is_group, u32 id, const bool *enable,
+ const bool *tstamp, u32 *sid, u32 *offset,
+ uuid_t *uuid)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct scmi_telemetry_de *de;
+
+ if (is_group)
+ return -EOPNOTSUPP;
+
+ ti->res_get(ti);
+ de = xa_load(&ti->xa_des, id);
+ if (!de)
+ return -ENODEV;
+
+ /* Filter out unplausible timestamp requests */
+ return scmi_telemetry_tde_state_set(ph, enable,
+ de->tstamp_support ? tstamp : NULL,
+ de, sid, offset, uuid);
+}
+
static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.info_get = scmi_telemetry_info_get,
.de_lookup = scmi_telemetry_de_lookup,
.res_get = scmi_telemetry_resources_get,
+ .state_get = scmi_telemetry_state_get,
+ .state_set = scmi_telemetry_state_set,
};
/**
@@ -2461,6 +3165,7 @@ static int scmi_telemetry_resources_alloc(struct telemetry_info *ti)
ti->tdes = no_free_ptr(tdes);
xa_init(&ti->xa_des);
+ mutex_init(&ti->config_mtx);
rinfo->des = no_free_ptr(des);
rinfo->dei_store = no_free_ptr(dei_store);
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index b828cbc1eb21..ad116978ed47 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -968,6 +968,10 @@ struct scmi_telemetry_info {
* @info_get: get the general Telemetry information.
* @de_lookup: get a specific DE descriptor from the DE id.
* @res_get: get a reference to the Telemetry resources descriptor.
+ * @state_get: retrieve the specific DE state, if NULL returns the
+ * cumulative state of all DEs. Global group state can be looked-up
+ * directly from @scmi_telemetry_info via @info_get.
+ * @state_set: enable/disable the specific DE or GROUP with or without timestamps.
*/
struct scmi_telemetry_proto_ops {
const struct scmi_telemetry_info *(*info_get)
@@ -976,6 +980,13 @@ struct scmi_telemetry_proto_ops {
(const struct scmi_protocol_handle *ph, u32 id);
const struct scmi_telemetry_res_info *(*res_get)
(const struct scmi_protocol_handle *ph);
+ int (*state_get)(const struct scmi_protocol_handle *ph,
+ const u32 *id, bool *enabled, bool *tstamp_enabled,
+ u32 *sid, u32 *offset, uuid_t *uuid);
+ int (*state_set)(const struct scmi_protocol_handle *ph,
+ bool is_group, u32 id, const bool *enable,
+ const bool *tstamp, u32 *sid, u32 *offset,
+ uuid_t *uuid);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 19/34] firmware: arm_scmi: Add Telemetry global configuration support
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (17 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 18/34] firmware: arm_scmi: Add Telemetry DEs configuration support Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 20/34] firmware: arm_scmi: Add Telemetry Single/Bulk DEs reads Cristian Marussi
` (15 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add support to configure Telemetry globally by setting its enabled state
and the requested update interval.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- avoid collection name clashes
- rename update_interval_ms to ename update_interval
v15 --> v16
- fix mode check logic
---
drivers/firmware/arm_scmi/telemetry.c | 69 +++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 15 ++++++
2 files changed, 84 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 140ff1072b99..6e5564c36a2d 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -3087,12 +3087,81 @@ static int scmi_telemetry_state_set(const struct scmi_protocol_handle *ph,
de, sid, offset, uuid);
}
+static int
+scmi_telemetry_collection_configure(const struct scmi_protocol_handle *ph,
+ unsigned int res_id, const bool *enable,
+ const unsigned int *update_interval,
+ const enum scmi_telemetry_collection *mode)
+{
+ enum scmi_telemetry_collection *current_mode, next_mode;
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct scmi_msg_telemetry_config_set *msg;
+ bool tlm_enable, *current_state, is_group;
+ unsigned int *active_update_interval;
+ struct scmi_xfer *t;
+ __le32 interval;
+ u32 control;
+ int ret;
+
+ /* Stall concurrent configuration request */
+ guard(mutex)(&ti->config_mtx);
+
+ if (mode && (*mode != SCMI_TLM_COLLECTION_ONDEMAND))
+ return -EOPNOTSUPP;
+
+ is_group = res_id != SCMI_TLM_GRP_INVALID;
+ if (is_group)
+ return -EOPNOTSUPP;
+
+ active_update_interval = &ti->info.active_update_interval;
+ current_mode = &ti->info.current_mode;
+ current_state = &ti->info.enabled;
+
+ if (!enable && !update_interval && (!mode || *mode == *current_mode))
+ return 0;
+
+ ret = ph->xops->xfer_get_init(ph, TELEMETRY_CONFIG_SET,
+ sizeof(*msg), 0, &t);
+ if (ret)
+ return ret;
+
+ if (!update_interval)
+ interval = cpu_to_le32(*active_update_interval);
+ else
+ interval = cpu_to_le32(*update_interval);
+
+ tlm_enable = enable ? *enable : *current_state;
+ next_mode = mode ? *mode : *current_mode;
+
+ msg = t->tx.buf;
+ msg->group_identifier = cpu_to_le32(res_id);
+ control = tlm_enable ? TELEMETRY_ENABLE : 0;
+ control |= !is_group ? TELEMETRY_SET_SELECTOR_ALL : TELEMETRY_SET_SELECTOR_GROUP;
+ control |= TELEMETRY_MODE_SET(next_mode);
+ msg->control = cpu_to_le32(control);
+ msg->sampling_rate = interval;
+ ret = ph->xops->do_xfer(ph, t);
+ if (!ret) {
+ *current_mode = next_mode;
+ *current_state = tlm_enable;
+ ti->streaming_mode =
+ *current_mode == SCMI_TLM_COLLECTION_NOTIFICATION;
+ if (update_interval)
+ *active_update_interval = le32_to_cpu(interval);
+ }
+
+ ph->xops->xfer_put(ph, t);
+
+ return ret;
+}
+
static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.info_get = scmi_telemetry_info_get,
.de_lookup = scmi_telemetry_de_lookup,
.res_get = scmi_telemetry_resources_get,
.state_get = scmi_telemetry_state_get,
.state_set = scmi_telemetry_state_set,
+ .collection_configure = scmi_telemetry_collection_configure,
};
/**
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index ad116978ed47..ba240c8595a8 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -888,6 +888,12 @@ enum scmi_telemetry_compo_type {
#define SCMI_TLM_BUILD_UPDATE_INTERVAL(s, e) \
(FIELD_PREP(GENMASK(20, 5), (s)) | FIELD_PREP(GENMASK(4, 0), (e) & GENMASK(4, 0)))
+enum scmi_telemetry_collection {
+ SCMI_TLM_COLLECTION_ONDEMAND,
+ SCMI_TLM_COLLECTION_NOTIFICATION,
+ SCMI_TLM_COLLECTION_SINGLE_READ,
+};
+
#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF
struct scmi_telemetry_intervals {
@@ -959,6 +965,8 @@ struct scmi_telemetry_info {
struct scmi_telemetry_shmti_info **shmtis;
unsigned int num_uuids;
uuid_t * __rcu *uuids;
+ bool enabled;
+ enum scmi_telemetry_collection current_mode;
};
/**
@@ -972,6 +980,9 @@ struct scmi_telemetry_info {
* cumulative state of all DEs. Global group state can be looked-up
* directly from @scmi_telemetry_info via @info_get.
* @state_set: enable/disable the specific DE or GROUP with or without timestamps.
+ * @collection_configure: choose a sampling rate and enable SHMTI/FC sampling
+ * for on demand collection via @de_data_read or async
+ * notificatioins for all the enabled DEs.
*/
struct scmi_telemetry_proto_ops {
const struct scmi_telemetry_info *(*info_get)
@@ -987,6 +998,10 @@ struct scmi_telemetry_proto_ops {
bool is_group, u32 id, const bool *enable,
const bool *tstamp, u32 *sid, u32 *offset,
uuid_t *uuid);
+ int (*collection_configure)(const struct scmi_protocol_handle *ph,
+ unsigned int res_id, const bool *enable,
+ const unsigned int *update_interval,
+ const enum scmi_telemetry_collection *mode);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 20/34] firmware: arm_scmi: Add Telemetry Single/Bulk DEs reads
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (18 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 19/34] firmware: arm_scmi: Add Telemetry global " Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 21/34] firmware: arm_scmi: Add Telemetry boot-on support Cristian Marussi
` (14 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add support for Telemetry operations needed to read DataEvent values and
timestamps as single entities or all together in a single bulk buffer.
The returned values are effectiely retrieved from the platform only
when strictly needed, i.e. when no fresh recent cached value was already
available.
The DataEvent values are fetched transparently from the platform origins
using the proper synchronization and consistency primitives, directly from
the SHMTIs areas or the FastChannels memory depending on the configuration.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- fix shmti_oveflow WITH LEN
- check liveness on de_snapshot
- remove des_sample and notif structure and logic
- add a comment on bulk read reporting on failures
- return ENODATA when TLM off
v15 --> v16
- removed SINGLE SAMPLE delayed response reads capabilities
- removed groups references
- check liveness in de_snapshot
- add shmti_overflow check on snap
v14 --> v15
- use IS_TDE_TS_LINE macro
- use NOT IS_DATA_LINE in tdcf_de_payld_get
- use better startm/endm initial markers in tdcf_de_payld_get
- retry scmi_telemetry_tdcf_de_parse on error too
- trigger resource enumeration also on de_data_read
- skip unitialized FCs on reads and return -EPROTO
- introduce localized single DE snapshot on single DE data read
- widen tde->mtx locking in msg_payld_process
- use unsigned int samples
- better des_sample_get sanity checks
- refactor msg_payld_process
v13 --> v14
- report scmi_telemetry_line_data_parse errors
- add traces for CACHED_DE_UPDATED
- moved some definitions from earlier patches
- using a less regular time interval for TDCF scan retries
- renamed some fields to more strictly adhere to spec nomenclature
v12 --> v13
- use new RAW macros in line_data_parse and check for line validity
- handle PARTIAL_ERROR by logging and carrying on
- relocate tde->mtx guards higher up and add lockdep assertions
- better tdcf/eplg init within tde->mtx
- do not cache tde->base between DE read retries
- retry scan on payld sanity errors
v11 --> v12
- using new TDCF_INVALID_MATCH_SEQ macro
- reporting transient DATA_INVALID lines as -EINVAL
- added cacheable param to msg_payld_process
v10 --> v11
- split out and relocate unrelated code to previous patches
- add TDE mutex_lock when getting payload address
- check LINE_TYPE while parsing payloads
- check overflows and boundary on single sample replies
v8 -->v9
- Fix Sashiko reports for v8
- check if TS supported on FC line before using it
- check for unexpected shorter reply on sample_get
- use non-LE accessors in msg_payld where appropriate
v7 --> v8
- Fixes for some Sashiko issues on v7
- added CACHED_DE_READ trace
- reworked locking around line handling: blkts/uuid
v6 -->v7
- usse proper le32 accesors, fix sparse warns
v5 --> v6
- fixed some Sashiko review
- add new macros parse TDCF-like content on regular ram (non IO)
https://sashiko.dev/#/patchset/20260703123601.381275-1-cristian.marussi%40arm.com?part=10
- fix retry loop in DE parse to avoid exiting on transient errors
- use proper non-IO accessors for msg payld parsing
- add msg payld boundary check
v4 --> v5
- removed UAPI structures dependency
- cleaned up sample_get logic around groups
- fixes sparse warnings
v3 --> v4
- fix typos in comments
- extend TDE cache helpers with cache lookup routine and use cumulative
scmi_telemetry_de_sample param
v2 --> v3
- split from monolithic Telemetry patch
- simplity using a few assignement using ternary ops
- remove useless ts param from scanning function
- use a compound literal to simplify samples init
- add a missing __must_check on telemetry_ops
- changed errno on DE read troubles:
- ENODEV/ENOENT: DE is UNKNOWN
- EINVAL: DE is marked as DATA_INVALID
- ENODATA: TLM susbsystem or the specific DE is OFF
---
drivers/firmware/arm_scmi/telemetry.c | 529 ++++++++++++++++++++++++--
include/linux/scmi_protocol.h | 17 +
2 files changed, 511 insertions(+), 35 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 6e5564c36a2d..34389ac82b4f 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -383,6 +383,12 @@ struct tdcf {
#define TDCF_END_SIGNATURE(e) (_I(&((e)->sign_end)))
#define TDCF_BAD_END_SEQ GENMASK(31, 0)
+struct telemetry_de_snapshot {
+ u32 magic;
+ struct payload payld;
+ struct payload bts_payld;
+};
+
struct telemetry_shmti;
struct shmti_snap {
@@ -710,8 +716,9 @@ scmi_telemetry_tde_cache_stale(struct telemetry_de *tde, const u32 magic)
}
static void
-scmi_telemetry_tde_cache_update(struct telemetry_de *tde, u64 val,
- u64 *tstamp, const u32 *magic)
+scmi_telemetry_tde_cache_update(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample,
+ const u32 *magic)
{
lockdep_assert_held(&tde->mtx);
@@ -723,10 +730,37 @@ scmi_telemetry_tde_cache_update(struct telemetry_de *tde, u64 val,
return;
tde->last_magic = magic ? *magic : TDCF_BAD_END_SEQ;
- tde->last_val = val;
- tde->last_ts = tstamp && scmi_tde_has_tstamp(tde) ? *tstamp : 0;
- if (tstamp)
- *tstamp = tde->last_ts;
+ tde->last_val = sample->val;
+ tde->last_ts = scmi_tde_has_tstamp(tde) ? sample->tstamp : 0;
+ /* Update returned value too */
+ sample->tstamp = tde->last_ts;
+
+ trace_scmi_tlm_collect(tde->last_ts, tde->de.info->id,
+ tde->last_val, "CACHED_DE_UPDATED");
+}
+
+static int
+scmi_telemetry_tde_cache_lookup(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample,
+ u32 *magic)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ /*
+ * Bailout without providing any sample when the cache is
+ * stale, i.e. last_magic < *magic
+ */
+ if (magic && scmi_telemetry_tde_cache_stale(tde, *magic))
+ return -EAGAIN;
+
+ sample->id = tde->de.info->id;
+ sample->val = tde->last_val;
+ sample->tstamp = tde->last_ts;
+
+ trace_scmi_tlm_collect(sample->tstamp, sample->id,
+ sample->val, "CACHED_DE_READ");
+
+ return 0;
}
struct scmi_tlm_de_priv {
@@ -1984,45 +2018,45 @@ static void scmi_telemetry_tdcf_blkts_parse(struct telemetry_info *ti,
static inline int
scmi_telemetry_line_data_parse(struct telemetry_de *tde, struct shmti_snap *snap,
- u64 *val, u64 *tstamp, struct payload *payld,
- struct payload *bts_payld, u32 magic)
+ struct scmi_telemetry_de_sample *sample,
+ struct payload *payld, struct payload *bts_payld,
+ u32 magic)
{
u32 meta = PAYLD_META_RAW(payld);
lockdep_assert_held(&tde->mtx);
/* Clear returned values in case of errors */
- *val = 0;
-
- if (tstamp) {
- *tstamp = 0;
- if (USE_BLK_TS_RAW(meta)) {
- /* Read out the actual BLK_TS */
- *tstamp = scmi_telemetry_blkts_read(magic, tde,
- bts_payld, snap);
- } else if (LINE_TS_VALID_RAW(meta)) {
- /*
- * Note that LINE_TS_VALID implies HAS_LINE_EXT and that
- * the per DE line_ts_rate is advertised in the DE
- * descriptor, BUT we check anyway against buggy FW.
- */
- if (!HAS_LINE_EXT_RAW(meta)) {
- trace_scmi_tlm_access(tde->de.info->id,
- "SHMTI_BAD_TS_LINE", 0, 0);
- return -EPROTO;
- }
-
- *tstamp = LINE_TSTAMP_GET_RAW(&payld->tsl);
+ sample->tstamp = 0;
+ sample->val = 0;
+
+ if (USE_BLK_TS_RAW(meta)) {
+ /* Read out the actual BLK_TS */
+ sample->tstamp = scmi_telemetry_blkts_read(magic, tde,
+ bts_payld, snap);
+ } else if (LINE_TS_VALID_RAW(meta)) {
+ /*
+ * Note that LINE_TS_VALID implies HAS_LINE_EXT and that
+ * the per DE line_ts_rate is advertised in the DE
+ * descriptor, BUT we check anyway against buggy FW.
+ */
+ if (!HAS_LINE_EXT_RAW(meta)) {
+ trace_scmi_tlm_access(tde->de.info->id,
+ "SHMTI_BAD_TS_LINE", 0, 0);
+ return -EPROTO;
}
+
+ sample->tstamp = LINE_TSTAMP_GET_RAW(&payld->tsl);
}
/* Data is always valid since we are NOT handling BLK TS lines here */
- *val = LINE_DATA_GET_RAW(&payld->l);
+ sample->val = LINE_DATA_GET_RAW(&payld->l);
- trace_scmi_tlm_collect(tstamp ? *tstamp : 0, tde->de.info->id,
- *val, "SHMTI_DE_READ");
+ /* Trace originally read tstamp */
+ trace_scmi_tlm_collect(sample->tstamp, tde->de.info->id,
+ sample->val, "SHMTI_DE_READ");
- scmi_telemetry_tde_cache_update(tde, *val, tstamp, &magic);
+ scmi_telemetry_tde_cache_update(tde, sample, &magic);
return 0;
}
@@ -2128,11 +2162,11 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
struct telemetry_uuid *active_uuid)
{
struct telemetry_shmti *shmti = snap->shmti;
+ struct scmi_telemetry_de_sample sample = {};
struct device *dev = ti->ph->dev;
u32 meta = PAYLD_META_RAW(payld);
bool use_blk_ts = USE_BLK_TS_RAW(meta);
struct telemetry_de *tde;
- u64 val, tstamp = 0;
u32 de_id;
de_id = PAYLD_ID_RAW(payld);
@@ -2225,7 +2259,7 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
* Parse data words ... only for the sake of updating TDE cache,
* any error is logged but ignored.
*/
- if (scmi_telemetry_line_data_parse(tde, snap, &val, &tstamp, payld,
+ if (scmi_telemetry_line_data_parse(tde, snap, &sample, payld,
NULL, snap->magic))
dev_err(dev, "Malformed DATA line.\n");
}
@@ -3155,6 +3189,429 @@ scmi_telemetry_collection_configure(const struct scmi_protocol_handle *ph,
return ret;
}
+static inline int
+scmi_telemetry_de_data_fc_read(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample)
+{
+ struct fc_tsline __iomem *fc;
+
+ lockdep_assert_held(&tde->mtx);
+
+ /*
+ * A FastChannel DE that was discovered as enabled at boot but later
+ * failed to be enumerated cannot be accessed.
+ */
+ if (!tde->base)
+ return -EPROTO;
+
+ fc = tde->base + tde->offset;
+ sample->val = LINE_DATA_GET(fc);
+ sample->tstamp = IS_TDE_TS_LINE(tde) ? LINE_TSTAMP_GET(fc) : 0;
+
+ /* Trace originally read tstamp */
+ trace_scmi_tlm_collect(sample->tstamp, tde->de.info->id, sample->val,
+ "FC_READ");
+
+ /* Cache anyway since TDE caches are on the read path anyway */
+ scmi_telemetry_tde_cache_update(tde, sample, NULL);
+
+ return 0;
+}
+
+static void scmi_telemetry_scan_update(struct telemetry_info *ti)
+{
+ struct telemetry_de *tde;
+
+ /* Scan all SHMTIs ... */
+ for (int id = 0; id < ti->num_shmti; id++) {
+ int ret;
+
+ ret = scmi_telemetry_shmti_scan(ti, id, SCAN_LOOKUP);
+ if (ret)
+ dev_warn(ti->ph->dev,
+ "Failed update-scan of SHMTI ID:%d - ret:%d\n",
+ id, ret);
+ }
+
+ if (!ti->info.fc_support)
+ return;
+
+ /* Need to enumerate resources to access fastchannels */
+ ti->res_get(ti);
+ list_for_each_entry(tde, &ti->fcs_des, item) {
+ struct scmi_telemetry_de_sample sample = {};
+
+ guard(mutex)(&tde->mtx);
+ if (!tde->de.enabled)
+ continue;
+
+ /*
+ * Only for the sake of updating TDE cache.
+ * Ignoring errors deriving from malformed FCs.
+ */
+ scmi_telemetry_de_data_fc_read(tde, &sample);
+ }
+}
+
+/*
+ * TDCF and TS Line Management Notes
+ * ---------------------------------
+ *
+ * TDCF Payload Metadata notable bits:
+ * - Bit[3]: USE BLK Tstamp
+ * - Bit[2]: Line-Extension Field present (LineTstamp)
+ * - Bit[1]: Tstamp VALID
+ * - Bit[0]: Data INVALID
+ *
+ * CASE_1:
+ * -------
+ * + A DE is enabled with timestamp disabled, so the TS fields are
+ * NOT present
+ * -> BIT[3:0] = 0000b
+ *
+ * - 1/A LINE_TSTAMP
+ * ------------------
+ * + that DE is then 're-enabled' with TS: so it was ON, it remains
+ * ON but using DE_CONFIGURE I now enabled also TS, so the
+ * platform relocates it at the end of the SHMTI and return the
+ * new offset
+ * -> BIT[3:0] = 0110b
+ *
+ * - 1/B BLK_TSTAMP
+ * ------------------
+ * + that DE is then 're-enabled' with BLK TS: so it was ON, it
+ * remains ON but using DE_CONFIGURE, we now also enabled the TS,
+ * so the platform will:
+ * - IF a preceding BLK_TS line exist (with same clk rate)
+ * it relocates the DE at the end of the SHMTI and return the
+ * new offset (if there is enough room, if not in another SHMTI)
+ * - IF a preceding BLK_TS line DOES NOT exist (with same clk rate)
+ * it creates a new BLK_TS line at the end of the SHMTI and then
+ * relocates the DE after the new BLK_TS and return the
+ * new offset (if there is enough room, if not in another SHMTI)
+ * -> BIT[3:1] = 1010b
+ *
+ * + the hole left from the relocated DE can be reused by the platform
+ * to fit another equally sized DE. (i.e. without shuffling around any
+ * other enabled DE, since that would cause a change of the known offset)
+ * anyway it will be marked as:
+ * -> BIT[3:0] = 0101b iff it was a LINE_TSTAMP
+ * -> BIT[3:0] = 0001b iff it was a BLK_TSTAMP
+ *
+ * CASE_2:
+ * -------
+ * + A DE is enabled with LINE timestamp enabled, so the TS_Line is there
+ * -> BIT[3:0] = 0110b
+ * + that DE has its timestamp disabled: again, you can do this without
+ * disabling it fully but just disabling the TS, so now that TS_line
+ * fields are still physically there but NOT valid
+ * -> BIT[3:0] = 0100b
+ * + the hole from the timestamp remain there unused until
+ * - you enable again the TS so the hole is used again
+ * -> BIT[3:0] = 0110b
+ * OR
+ * - you disable fully the DE and then re-enable it with the TS
+ * -> potentially CASE_1 the DE is relocated on enable
+ * + same kind of dynamic applies if the DE had a BLK_TS line
+ */
+static int scmi_telemetry_tdcf_de_payld_check(struct telemetry_de *tde,
+ struct payload *payld)
+{
+ u32 meta = PAYLD_META_RAW(payld);
+
+ lockdep_assert_held(&tde->mtx);
+
+ if (DATA_INVALID_RAW(meta)) {
+ trace_scmi_tlm_access(PAYLD_ID_RAW(payld), "DE_DATA_INVALID", 0, 0);
+ return -EINVAL;
+ }
+
+ if (!IS_DATA_LINE_RAW(meta)) {
+ trace_scmi_tlm_access(tde->de.info->id, "BAD_DE_LINE_TYPE", 0, 0);
+ return -EPROTO;
+ }
+
+ if (PAYLD_ID_RAW(payld) != tde->de.info->id) {
+ trace_scmi_tlm_access(tde->de.info->id, "DE_ID_MISMATCH", 0, 0);
+ return -ENODEV;
+ }
+
+ /*
+ * A valid line using BLK_TS should have been initialized with the
+ * related BLK_TS when enabled, unless is a torn read.
+ */
+ if ((USE_BLK_TS_RAW(meta) && !tde->bts)) {
+ trace_scmi_tlm_access(tde->de.info->id, "BAD_USE_BLK_TS", 0, 0);
+ return -EPROTO;
+ }
+
+ return 0;
+}
+
+static int scmi_telemetry_de_snapshot(struct tdcf __iomem *tdcf,
+ struct telemetry_de *tde,
+ struct telemetry_de_snapshot *snap)
+{
+ int retries = SCMI_TLM_TDCF_MAX_RETRIES;
+ u32 startm, endm, live = 0;
+ int err;
+
+ lockdep_assert_held(&tde->mtx);
+
+ do {
+ struct payload __iomem *payld, *bts_payld;
+ ssize_t len;
+
+ /* A bit of exponential backoff between retries */
+ fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP_US);
+
+ err = 0;
+ startm = TDCF_START_SEQ_GET(tdcf);
+ /* Build a default endm certainly different from startm */
+ endm = startm + 1;
+ if (IS_BAD_START_SEQ(startm)) {
+ trace_scmi_tlm_access(tde->de.info->id, "MSEQ_BADSTART_DE",
+ startm, 0);
+ continue;
+ }
+
+ /*
+ * In this context any anomaly detected on the payload could
+ * due to a torn read, and it is worth retrying a snapshot.
+ *
+ * Note that TDE base/offset have been sanitized when
+ * initialized and cannot have changed: accessing them here
+ * is safe.
+ */
+ payld = tde->base + tde->offset;
+ /* Sanitize length before snapshot */
+ len = scmi_telemetry_shmti_overflow(tde->base, tde->eplg, payld);
+ if (len < 0)
+ continue;
+ memcpy_fromio(&snap->payld, payld, len);
+ err = scmi_telemetry_tdcf_de_payld_check(tde, &snap->payld);
+ if (err)
+ continue;
+
+ /*
+ * Here the associated BLK_TS line, if any, has been verified
+ * as sane when linked/bound: no need of further validation.
+ */
+ if (tde->bts) {
+ bts_payld = tde->base + tde->bts->line.offset;
+ /* Sanitize length before snapshot */
+ len = scmi_telemetry_shmti_overflow(tde->base,
+ tde->eplg, bts_payld);
+ if (len < 0)
+ continue;
+ memcpy_fromio(&snap->bts_payld, bts_payld, len);
+ }
+
+ /* Assure the snapshot has been fully taken before checking seqnum */
+ rmb();
+
+ /* Used to detect changes while copying */
+ live = TDCF_START_SEQ_GET(tdcf);
+ endm = TDCF_END_SEQ_GET(tde->eplg);
+ if (startm != endm || startm != live)
+ trace_scmi_tlm_access(tde->de.info->id, "MSEQ_MISMATCH_DE",
+ startm, startm != endm ? endm : live);
+ } while ((startm != endm || startm != live || err) && --retries);
+
+ if (startm != endm || startm != live || err) {
+ trace_scmi_tlm_access(tde->de.info->id, "TDCF_SNAPSHOT_DE_FAIL",
+ startm, startm != endm ? endm : live);
+ return err ?: -EPROTO;
+ }
+
+ /* Ensure any snapshot is visible before updating magic */
+ smp_store_release(&snap->magic, startm);
+
+ trace_scmi_tlm_access(tde->de.info->id, "TDCF_SNAPSHOT_DE_OK",
+ startm, endm);
+
+ return 0;
+}
+
+static int
+scmi_telemetry_tdcf_de_sample_cached(struct tdcf __iomem *tdcf,
+ struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ u32 startm;
+
+ /* If updating cannot be cached */
+ startm = TDCF_START_SEQ_GET(tdcf);
+ if (IS_BAD_START_SEQ(startm))
+ return -EAGAIN;
+
+ return scmi_telemetry_tde_cache_lookup(tde, sample, &startm);
+}
+
+/**
+ * scmi_telemetry_tdcf_de_parse - Helper to read one SHMTI/DE
+ * @tde: A reference to the DE descriptor to read
+ * @sample: A reference to the sample used as a container for output
+ *
+ * This helper can be used to retrieve the data value of a SHMTI based DE
+ * and any related BLK_TS line: unless the values are found already available
+ * and up to date in the TDE cache, a limited snapshot of the involved lines
+ * is taken at first so that all the subsequent DE parsing and processing can
+ * happen on a stable in RAM copy.
+ *
+ * Taking a limited snapshot does not carry relevant overhead and has the
+ * benefit of allowing the reuse of the same RAM-baased data accessors used
+ * during a full scan and related full snapshot, instead of a set of dedicated
+ * __iomem helpers.
+ *
+ * NOTE that since each TDE has its own cache, which is accessed and updated
+ * both during a single DE read and while performing a bulk read, while
+ * caching consistency assures to provide always the 'freshest' value
+ * retrieved, mixing single and bulk reads can lead to the values produced by
+ * the bulk reads to contain 'fresher' late values derived from concurrent
+ * single DE reads: IOW mixing single and bulk reads can produce a set of bulk
+ * DEs values that are NOT all from the same moment in time.
+ *
+ * Moreover in general, since no assumption can be made about the TDCF sequence
+ * numbers across different SHMTIs and related optional DE timestamps, it is NOT
+ * possible to guarantee anyway that data values retrieved from DEs located in
+ * distinct SHMTIs areas comes from the same platform update cycle at all.
+ */
+static int
+scmi_telemetry_tdcf_de_parse(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample)
+{
+ struct telemetry_de_snapshot snap = {};
+ struct tdcf __iomem *tdcf;
+ int ret;
+
+ lockdep_assert_held(&tde->mtx);
+
+ /*
+ * TDEs can be relocated by platform only during enable phase,
+ * thing that cannot happen until tde->mtx is released.
+ */
+ tdcf = tde->base;
+ if (!tdcf)
+ return -ENODEV;
+
+ if (!scmi_telemetry_tdcf_de_sample_cached(tdcf, tde, sample))
+ return 0;
+
+ /* Attempt a localized DE snapshot: DATA_LINE + optionally BLK_TS */
+ ret = scmi_telemetry_de_snapshot(tdcf, tde, &snap);
+ if (ret)
+ return ret;
+
+ /* Now parse the stable line snapshot */
+ return scmi_telemetry_line_data_parse(tde, NULL, sample, &snap.payld,
+ &snap.bts_payld, snap.magic);
+}
+
+static int scmi_telemetry_de_access(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ if (!tde->de.fc_support)
+ return scmi_telemetry_tdcf_de_parse(tde, sample);
+
+ return scmi_telemetry_de_data_fc_read(tde, sample);
+}
+
+static int scmi_telemetry_de_collect(struct telemetry_info *ti,
+ struct scmi_telemetry_de *de,
+ struct scmi_telemetry_de_sample *sample)
+{
+ struct telemetry_de *tde = to_tde(de);
+
+ guard(mutex)(&tde->mtx);
+
+ if (!de->enabled)
+ return -ENODATA;
+
+ return scmi_telemetry_de_access(tde, sample);
+}
+
+static int scmi_telemetry_de_data_read(const struct scmi_protocol_handle *ph,
+ struct scmi_telemetry_de_sample *sample)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct scmi_telemetry_de *de;
+
+ if (!ti->info.enabled || !sample)
+ return -ENODATA;
+
+ ti->res_get(ti);
+ de = xa_load(&ti->xa_des, sample->id);
+ if (!de)
+ return -ENOENT;
+
+ return scmi_telemetry_de_collect(ti, de, sample);
+}
+
+static int
+scmi_telemetry_samples_collect(struct telemetry_info *ti, int grp_id,
+ unsigned int *num_samples,
+ struct scmi_telemetry_de_sample *samples)
+{
+ struct scmi_telemetry_res_info *rinfo;
+ unsigned int max_samples;
+
+ if (grp_id != SCMI_TLM_GRP_INVALID)
+ return -EOPNOTSUPP;
+
+ max_samples = *num_samples;
+ *num_samples = 0;
+
+ rinfo = ti->res_get(ti);
+ for (int i = 0; i < rinfo->num_des; i++) {
+ struct telemetry_de *tde = to_tde(rinfo->des[i]);
+
+ guard(mutex)(&tde->mtx);
+
+ /* Skip disabled DEs */
+ if (!tde->de.enabled)
+ continue;
+
+ /* Must be checked here to skip disabled and non selected */
+ if (*num_samples == max_samples)
+ return -ENOSPC;
+
+ /* Note that this cannot fail when passing a NULL magic */
+ scmi_telemetry_tde_cache_lookup(tde, &samples[(*num_samples)++], NULL);
+ }
+
+ return 0;
+}
+
+static int scmi_telemetry_des_bulk_read(const struct scmi_protocol_handle *ph,
+ int grp_id, unsigned int *num_samples,
+ struct scmi_telemetry_de_sample *samples)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+
+ if (!ti->info.enabled)
+ return -ENODATA;
+
+ if (!num_samples || !samples)
+ return -EINVAL;
+
+ /* Trigger a full SHMTIs & FCs scan */
+ scmi_telemetry_scan_update(ti);
+
+ /*
+ * Collect all last cached values.
+ * Note that on any read failure this reports the latest successfully
+ * collected values: timestamp, where available, can be used to spot
+ * stale values. Single DE reads and related batch requests can be used
+ * to attain a more fine grained access with detailed error reporting.
+ */
+ return scmi_telemetry_samples_collect(ti, grp_id, num_samples, samples);
+}
+
static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.info_get = scmi_telemetry_info_get,
.de_lookup = scmi_telemetry_de_lookup,
@@ -3162,6 +3619,8 @@ static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.state_get = scmi_telemetry_state_get,
.state_set = scmi_telemetry_state_set,
.collection_configure = scmi_telemetry_collection_configure,
+ .de_data_read = scmi_telemetry_de_data_read,
+ .des_bulk_read = scmi_telemetry_des_bulk_read,
};
/**
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index ba240c8595a8..f0b091e6709f 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -969,6 +969,13 @@ struct scmi_telemetry_info {
enum scmi_telemetry_collection current_mode;
};
+struct scmi_telemetry_de_sample {
+ unsigned int id;
+ unsigned int pad;
+ unsigned long long tstamp;
+ unsigned long long val;
+};
+
/**
* struct scmi_telemetry_proto_ops - represents the various operations provided
* by SCMI Telemetry Protocol
@@ -983,6 +990,11 @@ struct scmi_telemetry_info {
* @collection_configure: choose a sampling rate and enable SHMTI/FC sampling
* for on demand collection via @de_data_read or async
* notificatioins for all the enabled DEs.
+ * @de_data_read: on-demand read of a single DE and related optional timestamp:
+ * the value will be retrieved at the proper SHMTI offset OR
+ * from the dedicated FC area (if supported by that DE).
+ * @des_bulk_read: on-demand read of all the currently enabled DEs, or just
+ * the ones belonging to a specific group when provided.
*/
struct scmi_telemetry_proto_ops {
const struct scmi_telemetry_info *(*info_get)
@@ -1002,6 +1014,11 @@ struct scmi_telemetry_proto_ops {
unsigned int res_id, const bool *enable,
const unsigned int *update_interval,
const enum scmi_telemetry_collection *mode);
+ int __must_check (*de_data_read)(const struct scmi_protocol_handle *ph,
+ struct scmi_telemetry_de_sample *sample);
+ int __must_check (*des_bulk_read)(const struct scmi_protocol_handle *ph,
+ int grp_id, unsigned int *num_samples,
+ struct scmi_telemetry_de_sample *samples);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 21/34] firmware: arm_scmi: Add Telemetry boot-on support
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (19 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 20/34] firmware: arm_scmi: Add Telemetry Single/Bulk DEs reads Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 22/34] firmware: arm_scmi: Invoke proper protocol cleanup on failure Cristian Marussi
` (13 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add the initialization and discovery logic needed to detect when the
platform SCMI server is configured with telemetry enabled at boot and
perform all the needed resource enumerations to keep the kernel telemetry
subsystem state aligned with the platform boot-on configurations.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 -->v17
- bailout early on unplausible FC_SUPPORT in DE enumeration
- avoid collection naming clashes
- do NOT use tde_register to finalize pre-discovered (enabled) DE/FC
v15 --> 16
- removed group support at boot-on
- discard DE/FC at enumeration time that were found in SHMTI at boot
- align DE local/global TS state after enumeration
v14 --> v15
- use IS_TDE_TS_ macro
- retrieve collection_mode at boot-on and force ignore groups
- count only successfully initialized groups at boot-on
v13 --> v14
- moved some definitions from earlier split patch
- renamed some fields to more strictly adhere to spec nomenclature
v12 --> v13
- greb tde->mtx on DE_ENABLED_LIST
v11 --> v12
- fixed TS_RATE determination at boot-on
v10 --> v11
- fix non-conformancy on CONFIG_GET when groups NON configurable
- use SCAN_UPDATE also on BOOT-ON initial scan (NO discovery)
- avoid 'naked' ti->rinfo accesses
v8 -- > v9
- remove useless initial state lookup after RESET
- properly set de->tstamp_support on DE_ENABLED_LIST
v5 --> v6
- reporte properly an error on scmi_telemetry_config_lookup() failure
(Sashiko)
v4 --> v5
- use renamed grp_id instead of old id in group descriptors
- fixes sparse warnings
v3 --> v4
- bail-out FW_BUG errors
- fix typos in comments
- track boot-on Telemetry DE states
v2 --> v3
- split from monolithic telemetry protocol patch
- swap logic in scmi_telemetry_initial_state_lookup
---
drivers/firmware/arm_scmi/telemetry.c | 295 +++++++++++++++++++++++++-
1 file changed, 284 insertions(+), 11 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 34389ac82b4f..a3f446b98cb4 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -133,6 +133,21 @@ struct scmi_msg_resp_telemetry_update_intervals {
__le32 intervals[];
};
+struct scmi_msg_telemetry_de_enabled_list {
+ __le32 index;
+ __le32 flags;
+};
+
+struct scmi_enabled_de_desc {
+ __le32 id;
+ __le32 mode;
+};
+
+struct scmi_msg_resp_telemetry_de_enabled_list {
+ __le32 flags;
+ struct scmi_enabled_de_desc array[];
+};
+
struct scmi_msg_telemetry_de_configure {
__le32 identifier;
__le32 flags;
@@ -176,6 +191,20 @@ struct scmi_msg_telemetry_config_set {
__le32 sampling_rate;
};
+struct scmi_msg_telemetry_config_get {
+ __le32 group_identifier;
+ __le32 flags;
+#define TELEMETRY_GET_SELECTOR(x) (FIELD_PREP(GENMASK(3, 0), (x)))
+#define TELEMETRY_GET_SELECTOR_ORPHANS TELEMETRY_GET_SELECTOR(TLM_ORPHANS)
+#define TELEMETRY_GET_SELECTOR_GROUP TELEMETRY_GET_SELECTOR(TLM_GROUP)
+#define TELEMETRY_GET_SELECTOR_ALL TELEMETRY_GET_SELECTOR(TLM_ALL)
+};
+
+struct scmi_msg_resp_telemetry_config_get {
+ __le32 control;
+ __le32 sampling_rate;
+};
+
/* TDCF */
#define _I(__a) (ioread32((void __iomem *)(__a)))
@@ -501,6 +530,8 @@ static inline bool scmi_tde_has_tstamp(struct telemetry_de *t)
return t->de.tstamp_support && t->de.tstamp_enabled;
}
+#define DE_ENABLED_WITH_TSTAMP 2
+
enum de_state {
ENA_STATE,
ENA_TSTAMP,
@@ -677,12 +708,6 @@ static int scmi_telemetry_tde_register(struct telemetry_info *ti,
if (ret)
goto err;
- /* Add to FastChannels list */
- if (tde->de.fc_support) {
- scmi_telemetry_uuid_link(tde, ti->primary_uuid);
- list_add(&tde->item, &ti->fcs_des);
- }
-
/* ... and in the general array */
rinfo->des[rinfo->num_des] = &tde->de;
/* Make sure the freshly registered DE is visible before the index update */
@@ -948,6 +973,7 @@ static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti,
const struct scmi_de_desc *desc = *next;
struct device *dev = ti->ph->dev;
size_t payld_sz = sizeof(*desc);
+ bool fc_support;
int ret;
lockdep_assert_held(&tde->mtx);
@@ -955,6 +981,12 @@ static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti,
if (*rx_len < payld_sz)
return -ENOSPC;
+ fc_support = IS_FC_SUPPORTED(desc);
+ /* Bail-out if already discovered as a SHMTI-based DE */
+ if (fc_support && tde->base)
+ return -EPROTO;
+
+ tde->de.fc_support = fc_support;
tde->de.info->id = le32_to_cpu(desc->de_id);
tde->de.info->grp_id = le32_to_cpu(desc->group_id);
tde->de.info->data_sz = le32_to_cpu(desc->de_data_sz);
@@ -967,7 +999,6 @@ static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti,
tde->de.info->persistent = IS_PERSISTENT(desc);
tde->ts_type = TSTAMP_SUPPORT(desc);
tde->de.tstamp_support = !!tde->ts_type;
- tde->de.fc_support = IS_FC_SUPPORTED(desc);
tde->de.name_support = IS_NAME_SUPPORTED(desc);
/* Update DE_DESCRIPTOR size for the next iteration */
*next += sizeof(*desc);
@@ -984,8 +1015,13 @@ static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti,
/* Variably sized depending on TS support */
*next += sizeof(*line_ts_rate);
} else if (IS_TDE_TS_BLK(tde)) {
- /* Setup default BLK TS value at first */
- tde->de.info->ts_rate = ti->default_blk_ts_rate;
+ /*
+ * Setup default BLK TS rate value at first, if it has NOT
+ * already been picked from the BLK_TS line by the boot-on
+ * SCAN_UPDATE.
+ */
+ if (!tde->de.info->ts_rate)
+ tde->de.info->ts_rate = ti->default_blk_ts_rate;
} else if (!IS_TDE_TS_NONE(tde)) {
dev_warn(dev, "Unknown TDE timestamp type %u\n", tde->ts_type);
return -EPROTO;
@@ -1074,12 +1110,234 @@ static int iter_de_descr_process_response(const struct scmi_protocol_handle *ph,
}
}
+ /* Add to FastChannels list */
+ if (tde->de.fc_support) {
+ scmi_telemetry_uuid_link(tde, ti->primary_uuid);
+ list_add(&tde->item, &ti->fcs_des);
+ }
+
+ /* TS is NOT supported clear any enabled state possibly at boot-on */
+ if (!tde->de.tstamp_support)
+ scmi_telemetry_de_state_update(ti, ENA_TSTAMP,
+ &tde->de.tstamp_enabled, false);
+
/* Count timestamped DEs */
ti->num_des_tstamp += !!tde->de.tstamp_support;
return 0;
}
+static int scmi_telemetry_config_lookup(struct telemetry_info *ti,
+ unsigned int grp_id, bool *enabled,
+ unsigned int *active_update_interval,
+ enum scmi_telemetry_collection *mode,
+ bool ignore_groups)
+{
+ const struct scmi_protocol_handle *ph = ti->ph;
+ struct scmi_msg_telemetry_config_get *msg;
+ struct scmi_msg_resp_telemetry_config_get *resp;
+ bool is_group = grp_id != SCMI_TLM_GRP_INVALID;
+ struct scmi_xfer *t;
+ u32 flags;
+ int ret;
+
+ ret = ph->xops->xfer_get_init(ph, TELEMETRY_CONFIG_GET,
+ sizeof(*msg), sizeof(*resp), &t);
+ if (ret)
+ return ret;
+
+ msg = t->tx.buf;
+ msg->group_identifier = cpu_to_le32(grp_id);
+
+ /* Spec mandates the ALL selector when per-group config not available */
+ if (!ti->info.per_group_config_support || ignore_groups)
+ flags = TELEMETRY_GET_SELECTOR_ALL;
+ else
+ flags = !is_group ? TELEMETRY_GET_SELECTOR_ORPHANS :
+ TELEMETRY_GET_SELECTOR_GROUP;
+
+ msg->flags = cpu_to_le32(flags);
+
+ resp = t->rx.buf;
+ ret = ph->xops->do_xfer(ph, t);
+ if (!ret) {
+ *enabled = le32_get_bits(resp->control, TELEMETRY_ENABLE);
+ *mode = le32_get_bits(resp->control, TELEMETRY_MODE);
+ if (!is_group)
+ ti->streaming_mode =
+ *mode == SCMI_TLM_COLLECTION_NOTIFICATION;
+ *active_update_interval =
+ SCMI_TLM_GET_UPDATE_INTERVAL(le32_to_cpu(resp->sampling_rate));
+ }
+
+ ph->xops->xfer_put(ph, t);
+
+ return ret;
+}
+
+static void iter_enabled_list_prepare_message(void *message,
+ unsigned int desc_index,
+ const void *priv)
+{
+ struct scmi_msg_telemetry_de_enabled_list *msg = message;
+
+ msg->index = cpu_to_le32(desc_index);
+ msg->flags = 0;
+}
+
+static int iter_enabled_list_update_state(struct scmi_iterator_state *st,
+ const void *response, void *priv)
+{
+ const struct scmi_msg_resp_telemetry_de_enabled_list *r = response;
+
+ st->num_returned = le32_get_bits(r->flags, GENMASK(15, 0));
+ st->num_remaining = le32_get_bits(r->flags, GENMASK(31, 16));
+
+ if (st->rx_len < (sizeof(*r) + sizeof(r->array[0]) * st->num_returned))
+ return -EINVAL;
+
+ /*
+ * total enabled is not declared previously anywhere so we
+ * assume it's returned+remaining on first call.
+ */
+ if (!st->max_resources)
+ st->max_resources = st->num_returned + st->num_remaining;
+
+ return 0;
+}
+
+static int
+iter_enabled_list_process_response(const struct scmi_protocol_handle *ph,
+ const void *response,
+ struct scmi_iterator_state *st, void *priv)
+{
+ const struct scmi_msg_resp_telemetry_de_enabled_list *r = response;
+ const struct scmi_enabled_de_desc *desc;
+ struct telemetry_info *ti = priv;
+ struct telemetry_de *tde;
+ u32 de_id;
+ int ret;
+
+ desc = &r->array[st->loop_idx];
+ de_id = le32_to_cpu(desc->id);
+ if (scmi_telemetry_tde_lookup(ti, de_id)) {
+ dev_err(ph->dev,
+ "Found INVALID DE with DUPLICATED ID:0x%08X\n", de_id);
+ return -EINVAL;
+ }
+
+ tde = scmi_telemetry_tde_get(ti, de_id);
+ if (IS_ERR(tde))
+ return PTR_ERR(tde);
+
+ guard(mutex)(&tde->mtx);
+ tde->de.info->id = de_id;
+ tde->de.enabled = true;
+ tde->de.tstamp_enabled = le32_to_cpu(desc->mode) == DE_ENABLED_WITH_TSTAMP;
+ /*
+ * Guessing that, if a DE is reported as enabled with timestamp, it has
+ * to support timestamp: NOTE THAT the timestamp type (tde->ts_type) can
+ * ONLY be discovered later by DE_DESCRIPTION.
+ */
+ tde->de.tstamp_support = tde->de.tstamp_enabled;
+
+ ret = scmi_telemetry_tde_register(ti, tde);
+ if (ret) {
+ scmi_telemetry_free_tde_put(ti, tde);
+ return ret;
+ }
+
+ scmi_telemetry_de_state_update(ti, ENA_STATE, NULL, true);
+ if (tde->de.tstamp_enabled)
+ scmi_telemetry_de_state_update(ti, ENA_TSTAMP, NULL, true);
+
+ dev_dbg(ph->dev, "Registered new ENABLED DE with ID:0x%08X\n",
+ tde->de.info->id);
+
+ return 0;
+}
+
+static int scmi_telemetry_enumerate_des_enabled_list(struct telemetry_info *ti)
+{
+ const struct scmi_protocol_handle *ph = ti->ph;
+ struct scmi_telemetry_res_info *rinfo;
+ struct scmi_iterator_ops ops = {
+ .prepare_message = iter_enabled_list_prepare_message,
+ .update_state = iter_enabled_list_update_state,
+ .process_response = iter_enabled_list_process_response,
+ };
+ void *iter;
+ int ret;
+
+ iter = ph->hops->iter_response_init(ph, &ops, 0,
+ TELEMETRY_DE_ENABLED_LIST,
+ sizeof(u32) * 2, ti);
+ if (IS_ERR(iter))
+ return PTR_ERR(iter);
+
+ ret = ph->hops->iter_response_run(iter);
+ if (ret)
+ return ret;
+
+ /* Get rinfo as it is...without triggering an enumeration */
+ rinfo = __scmi_telemetry_resources_get(ti);
+
+ dev_info(ti->ph->dev, "Found %u enabled DEs.\n", rinfo->num_des);
+
+ return 0;
+}
+
+static int scmi_telemetry_initial_state_lookup(struct telemetry_info *ti)
+{
+ struct device *dev = ti->ph->dev;
+ int ret;
+
+ /* Discover the initial state ONLY for DEs, ignoring groups */
+ ret = scmi_telemetry_config_lookup(ti, SCMI_TLM_GRP_INVALID,
+ &ti->info.enabled,
+ &ti->info.active_update_interval,
+ &ti->info.current_mode, true);
+ if (ret)
+ return ret;
+
+ if (!ti->info.enabled)
+ return 0;
+
+ /*
+ * When Telemetry is found already enabled on the platform, proceed with
+ * passive discovery using DE_ENABLED_LIST and TDCF scanning: note that
+ * this CAN only discover DEs exposed via SHMTIs.
+ * FastChannel DEs need a proper DE_DESCRIPTION enumeration, while, even
+ * though incoming Notifications could be used for passive discovery too,
+ * it would carry a considerable risk of assimilating trash as DEs.
+ */
+ dev_info(dev,
+ "Telemetry found enabled with update interval %lux10^%d mode:%u\n",
+ SCMI_TLM_GET_UPDATE_INTERVAL_SECS(ti->info.active_update_interval),
+ SCMI_TLM_GET_UPDATE_INTERVAL_EXP(ti->info.active_update_interval),
+ ti->info.current_mode);
+ /*
+ * Query enabled DEs list: collect states. It will include DEs from any
+ * interface. Enabled groups still NOT enumerated.
+ */
+ ret = scmi_telemetry_enumerate_des_enabled_list(ti);
+ if (ret) {
+ dev_err(dev, FW_BUG "Cannot query enabled DE list. Abort.\n");
+ return ret;
+ }
+
+ /* Discover DEs on SHMTis: collect states/offsets/values */
+ for (int id = 0; id < ti->num_shmti; id++) {
+ ret = scmi_telemetry_shmti_scan(ti, id, SCAN_UPDATE);
+ if (ret)
+ dev_warn(dev,
+ "Failed discovery-scan of SHMTI ID:%d - ret:%d\n",
+ id, ret);
+ }
+
+ return 0;
+}
+
static void scmi_telemetry_tdes_cleanup(void *arg)
{
struct telemetry_info *ti = arg;
@@ -2225,8 +2483,17 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
trace_scmi_tlm_access(de_id, "DE_LOCATION_UPDATED", 0, 0);
- dev_dbg(dev, "TDCF-updated DE_ID:0x%08X - shmti:%pK offset:%u\n",
- tde->de.info->id, tde->base, tde->offset);
+ /* Just in case it was passively discovered boot-on */
+ if (IS_TDE_TS_NONE(tde)) {
+ if (use_blk_ts)
+ tde->ts_type = TSTAMP_BLK;
+ else if (LINE_TS_VALID_RAW(meta))
+ tde->ts_type = TSTAMP_LINE;
+ }
+
+ dev_dbg(dev,
+ "TDCF-updated DE_ID:0x%08X - shmti:%pK offset:%u ts_type:%d\n",
+ tde->de.info->id, tde->base, tde->offset, tde->ts_type);
}
/*
@@ -3960,6 +4227,12 @@ static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
return ret;
}
+ ret = scmi_telemetry_initial_state_lookup(ti);
+ if (ret) {
+ dev_err(dev, FW_BUG "Cannot retrieve initial state. Abort.\n");
+ return ret;
+ }
+
ti->info.base.version = ph->version;
return ph->set_priv(ph, ti);
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 22/34] firmware: arm_scmi: Invoke proper protocol cleanup on failure
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (20 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 21/34] firmware: arm_scmi: Add Telemetry boot-on support Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 23/34] firmware: arm_scmi: Refactor protocol allocation Cristian Marussi
` (12 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi, Sashiko
After a failure in the protocol initialization phase, invoke if provided
the protocol deinitialization routines.
Fixes: 48dc16e2e52c ("firmware: arm_scmi: Extend protocol registration interfaces")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-12cc0795-af8e-4d0c-8f6a-780a6728de7a
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
drivers/firmware/arm_scmi/driver.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 00a3484006d9..123050335969 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -2353,7 +2353,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
GFP_KERNEL);
if (ret != proto->id)
- goto clean;
+ goto deinit;
/*
* Warn but ignore events registration errors since we do not want
@@ -2374,6 +2374,9 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
return pi;
+deinit:
+ if (pi->proto->instance_deinit)
+ pi->proto->instance_deinit(&pi->ph);
clean:
/* Take care to put the protocol module's owner before releasing all */
scmi_protocol_put(proto);
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 23/34] firmware: arm_scmi: Refactor protocol allocation
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (21 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 22/34] firmware: arm_scmi: Invoke proper protocol cleanup on failure Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 24/34] firmware: arm_scmi: Add common per-protocol debugfs support Cristian Marussi
` (11 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Simplify protocol initialization and finalization using cleanup handlers.
Relocate the protocol put operations out of the setup and cleanup code so
as to be executed strictly after the protocol devres groups have been
fully released.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
drivers/firmware/arm_scmi/driver.c | 53 +++++++++++++-----------------
1 file changed, 22 insertions(+), 31 deletions(-)
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 123050335969..54d9780b7e87 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -2306,24 +2306,21 @@ static void scmi_protocol_version_initialize(struct device *dev,
*
* Context: Assumes to be called with @protocols_mtx already acquired.
* Return: A reference to a freshly allocated and initialized protocol instance
- * or ERR_PTR on failure. On failure the @proto reference is at first
- * put using @scmi_protocol_put() before releasing all the devres group.
+ * or ERR_PTR on failure.
*/
static struct scmi_protocol_instance *
scmi_alloc_init_protocol_instance(struct scmi_info *info,
const struct scmi_protocol *proto)
{
+ const struct scmi_handle *handle = &info->handle;
+ struct scmi_protocol_instance *pi;
int ret = -ENOMEM;
void *gid;
- struct scmi_protocol_instance *pi;
- const struct scmi_handle *handle = &info->handle;
/* Protocol specific devres group */
gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
- if (!gid) {
- scmi_protocol_put(proto);
- goto out;
- }
+ if (!gid)
+ return ERR_PTR(ret);
pi = devm_kzalloc_obj(handle->dev, *pi);
if (!pi)
@@ -2378,10 +2375,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
if (pi->proto->instance_deinit)
pi->proto->instance_deinit(&pi->ph);
clean:
- /* Take care to put the protocol module's owner before releasing all */
- scmi_protocol_put(proto);
devres_release_group(handle->dev, gid);
-out:
return ERR_PTR(ret);
}
@@ -2401,25 +2395,25 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
static struct scmi_protocol_instance * __must_check
scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
{
- struct scmi_protocol_instance *pi;
struct scmi_info *info = handle_to_scmi_info(handle);
+ struct scmi_protocol_instance *pi;
+ const struct scmi_protocol *proto;
- mutex_lock(&info->protocols_mtx);
+ guard(mutex)(&info->protocols_mtx);
pi = idr_find(&info->protocols, protocol_id);
-
if (pi) {
refcount_inc(&pi->users);
- } else {
- const struct scmi_protocol *proto;
-
- /* Fails if protocol not registered on bus */
- proto = scmi_protocol_get(protocol_id, &info->version);
- if (proto)
- pi = scmi_alloc_init_protocol_instance(info, proto);
- else
- pi = ERR_PTR(-EPROBE_DEFER);
+ return pi;
}
- mutex_unlock(&info->protocols_mtx);
+
+ /* Fails if protocol not registered on bus */
+ proto = scmi_protocol_get(protocol_id, &info->version);
+ if (!proto)
+ return ERR_PTR(-EPROBE_DEFER);
+
+ pi = scmi_alloc_init_protocol_instance(info, proto);
+ if (IS_ERR(pi))
+ scmi_protocol_put(proto);
return pi;
}
@@ -2452,12 +2446,13 @@ void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
struct scmi_info *info = handle_to_scmi_info(handle);
struct scmi_protocol_instance *pi;
- mutex_lock(&info->protocols_mtx);
+ guard(mutex)(&info->protocols_mtx);
pi = idr_find(&info->protocols, protocol_id);
if (WARN_ON(!pi))
- goto out;
+ return;
if (refcount_dec_and_test(&pi->users)) {
+ const struct scmi_protocol *proto = pi->proto;
void *gid = pi->gid;
if (pi->proto->events)
@@ -2468,15 +2463,11 @@ void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
idr_remove(&info->protocols, protocol_id);
- scmi_protocol_put(pi->proto);
-
devres_release_group(handle->dev, gid);
+ scmi_protocol_put(proto);
dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
protocol_id);
}
-
-out:
- mutex_unlock(&info->protocols_mtx);
}
void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 24/34] firmware: arm_scmi: Add common per-protocol debugfs support
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (22 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 23/34] firmware: arm_scmi: Refactor protocol allocation Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 25/34] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Cristian Marussi
` (10 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Allow interested SCMI protocols to register their own specific debugfs
entries under a common per-instance and per-protocol subtree rooted
at /sys/kernel/debug/scmi/<N>/protocols/<PROTO_ID>/
Expose a helper to enable protocol initialization code to get access to
such per-protocol/per-instance dentries in order to be able to install
their own dedicated debugfs entries.
Per-protocol debugfs support is configurable and default off.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- fix leak and NULL deref on exit path
v15 --> v16
- cleanup protocol dbg root after devres group closure
v14 --> v15
- fix debugfs_remove_recursive call-site...was too early
v8 --> v9
- avoid NULL deref on DBG cleanup path
v5 --> v6
- bail out of dentry_get helper when SCMI debug susbsytem failed to init
- remove stale SCMI protocol operations
- add a preoper clean up of debugfs when protocol init fails
---
drivers/firmware/arm_scmi/Kconfig | 14 +++++++++
drivers/firmware/arm_scmi/common.h | 2 ++
drivers/firmware/arm_scmi/driver.c | 41 ++++++++++++++++++++++++++-
drivers/firmware/arm_scmi/protocols.h | 6 ++++
4 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/arm_scmi/Kconfig b/drivers/firmware/arm_scmi/Kconfig
index 783c24a20e29..f506c4d1d96a 100644
--- a/drivers/firmware/arm_scmi/Kconfig
+++ b/drivers/firmware/arm_scmi/Kconfig
@@ -69,6 +69,20 @@ config ARM_SCMI_DEBUG_COUNTERS
such useful debug counters. This can be helpful for debugging and
SCMI monitoring.
+config ARM_SCMI_DEBUG_PROTOCOLS
+ bool "Enable SCMI protocols debug"
+ select ARM_SCMI_NEED_DEBUGFS
+ depends on DEBUG_FS
+ default n
+ help
+ Enables per-protocol specific debug features, where available.
+ When provided, such per-protocol debugfs entries are grouped
+ inside a common a subtree named by the protocol number and rooted
+ under a per-instance 'protocols' directory.
+
+ Such per-protocol entries subtree structure is freely defined
+ within the related protocol code.
+
config ARM_SCMI_QUIRKS
bool "Enable SCMI Quirks framework"
depends on JUMP_LABEL || COMPILE_TEST
diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi/common.h
index cdd6f887fae7..ed1623da6563 100644
--- a/drivers/firmware/arm_scmi/common.h
+++ b/drivers/firmware/arm_scmi/common.h
@@ -307,6 +307,7 @@ enum debug_counters {
/**
* struct scmi_debug_info - Debug common info
* @top_dentry: A reference to the top debugfs dentry
+ * @protos: A reference to the top debugfs protocols subdirectory
* @name: Name of this SCMI instance
* @type: Type of this SCMI instance
* @is_atomic: Flag to state if the transport of this instance is atomic
@@ -314,6 +315,7 @@ enum debug_counters {
*/
struct scmi_debug_info {
struct dentry *top_dentry;
+ struct dentry *protos;
const char *name;
const char *type;
bool is_atomic;
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 54d9780b7e87..9dd7becb9b1b 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -19,6 +19,7 @@
#include <linux/acpi.h>
#include <linux/bitmap.h>
#include <linux/cleanup.h>
+#include <linux/dcache.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/export.h>
@@ -101,6 +102,8 @@ struct scmi_xfers_info {
* has completed.
* @ph: An embedded protocol handle that will be passed down to protocol
* initialization code to identify this instance.
+ * @dbg: An optional reference to this protocol top debugfs directory; it will
+ * be automatically recursively removed on protocol de-initialization.
*
* Each protocol is initialized independently once for each SCMI platform in
* which is defined by fwnode and implemented by the SCMI server fw.
@@ -114,6 +117,7 @@ struct scmi_protocol_instance {
unsigned int version;
unsigned int negotiated_version;
struct scmi_protocol_handle ph;
+ struct dentry *dbg;
};
#define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
@@ -2154,6 +2158,25 @@ static void scmi_common_fastchannel_db_ring(struct scmi_fc_db_info *db)
SCMI_PROTO_FC_RING_DB(64);
}
+static struct dentry *
+scmi_debugfs_proto_dentry_get(const struct scmi_protocol_handle *ph)
+{
+ struct scmi_protocol_instance *pi = ph_to_pi(ph);
+ struct scmi_info *info = handle_to_scmi_info(pi->handle);
+
+ if (!IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS) || !info->dbg)
+ return ERR_PTR(-ENODEV);
+
+ if (!pi->dbg) {
+ char proto_dir[8];
+
+ snprintf(proto_dir, 8, "0x%02X", pi->proto->id);
+ pi->dbg = debugfs_create_dir(proto_dir, info->dbg->protos);
+ }
+
+ return pi->dbg;
+}
+
static const struct scmi_proto_helpers_ops helpers_ops = {
.extended_name_get = scmi_common_extended_name_get,
.get_max_msg_size = scmi_common_get_max_msg_size,
@@ -2164,6 +2187,7 @@ static const struct scmi_proto_helpers_ops helpers_ops = {
.protocol_msg_check = scmi_protocol_msg_check,
.fastchannel_init = scmi_common_fastchannel_init,
.fastchannel_db_ring = scmi_common_fastchannel_db_ring,
+ .debugfs_proto_dentry_get = scmi_debugfs_proto_dentry_get,
};
/**
@@ -2314,6 +2338,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
{
const struct scmi_handle *handle = &info->handle;
struct scmi_protocol_instance *pi;
+ struct dentry *dbg = NULL;
int ret = -ENOMEM;
void *gid;
@@ -2345,7 +2370,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
/* proto->init is assured NON NULL by scmi_protocol_register */
ret = pi->proto->instance_init(&pi->ph);
if (ret)
- goto clean;
+ goto clean_dbg;
ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
GFP_KERNEL);
@@ -2374,8 +2399,12 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
deinit:
if (pi->proto->instance_deinit)
pi->proto->instance_deinit(&pi->ph);
+clean_dbg:
+ dbg = pi->dbg;
clean:
devres_release_group(handle->dev, gid);
+ /* Protocol debugfs de-allocation MUST run after devres group release */
+ debugfs_remove_recursive(dbg);
return ERR_PTR(ret);
}
@@ -2453,6 +2482,7 @@ void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
if (refcount_dec_and_test(&pi->users)) {
const struct scmi_protocol *proto = pi->proto;
+ struct dentry *dbg = pi->dbg;
void *gid = pi->gid;
if (pi->proto->events)
@@ -2464,6 +2494,12 @@ void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
idr_remove(&info->protocols, protocol_id);
devres_release_group(handle->dev, gid);
+ /*
+ * Remove per-protocol top dir only after protocol release in
+ * order to allow any protocol specific cleanup on the pi->dbg
+ * subtree.
+ */
+ debugfs_remove_recursive(dbg);
scmi_protocol_put(proto);
dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
protocol_id);
@@ -3218,6 +3254,9 @@ static struct scmi_debug_info *scmi_debugfs_common_setup(struct scmi_info *info)
if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS))
scmi_debugfs_counters_setup(dbg, trans);
+ if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS))
+ dbg->protos = debugfs_create_dir("protocols", top_dentry);
+
dbg->top_dentry = top_dentry;
if (devm_add_action_or_reset(info->dev,
diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_scmi/protocols.h
index 1aa3ee1fb2b0..c39ef77770d1 100644
--- a/drivers/firmware/arm_scmi/protocols.h
+++ b/drivers/firmware/arm_scmi/protocols.h
@@ -11,6 +11,7 @@
#include <linux/bitfield.h>
#include <linux/completion.h>
+#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/kernel.h>
@@ -292,6 +293,9 @@ struct scmi_fc_info {
* gathering FC descriptions from the SCMI platform server.
* @fastchannel_db_ring: A common helper to ring a FC doorbell.
* @get_max_msg_size: A common helper to get the maximum message size.
+ * @debugfs_proto_dentry_get: A common helper to get a per-protocol debugfs top
+ * directory to use as a root. It will be
+ * recursively removed on protocol de-initialization.
*/
struct scmi_proto_helpers_ops {
int (*extended_name_get)(const struct scmi_protocol_handle *ph,
@@ -315,6 +319,8 @@ struct scmi_proto_helpers_ops {
u32 *rate_limit);
void (*fastchannel_db_ring)(struct scmi_fc_db_info *db);
int (*get_max_msg_size)(const struct scmi_protocol_handle *ph);
+ struct dentry *(*debugfs_proto_dentry_get)
+ (const struct scmi_protocol_handle *ph);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 25/34] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (23 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 24/34] firmware: arm_scmi: Add common per-protocol debugfs support Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 26/34] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Cristian Marussi
` (9 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Expose one debugfs entry for each discovered SHMTI that can be used to
dump the related SHMTI in binary form from TBGN up to TEND markers.
No processing is done kernel side, beside using proper accessors for
device memory.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v15 --> v16
- initialize/destroy debugFS around SHMTIs init
v14 --> v15
- cleanup local debugfs subtree with debugfs_remove_recursive on remove
v12 --> v13
- get inode lock on time update
v11 --> v12
- fix debugfs shmti dump file to use the SID as name
v8 --> v9
- use kvzalloc/kfree
- use file_update_time to update debugfs file ts
v5 --> v6
- added mutex to protect binary dump buffer during reads (Sashiko)
- using debugfs_create_file_size() (Sashiko)
---
drivers/firmware/arm_scmi/telemetry.c | 103 ++++++++++++++++++++++++++
1 file changed, 103 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index a3f446b98cb4..ddfa27b71ee4 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -9,9 +9,11 @@
#include <linux/bitfield.h>
#include <linux/compiler_types.h>
#include <linux/completion.h>
+#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
+#include <linux/fs.h>
#include <linux/hash.h>
#include <linux/hashtable.h>
#include <linux/jhash.h>
@@ -445,6 +447,14 @@ struct telemetry_shmti {
#define TDCF_OFFS(_b, _p) \
((unsigned long)((__force void *)(_p) - (__force void *)(_b)))
+struct scmi_telemetry_dbg_shmti {
+ struct telemetry_shmti *shmti;
+ size_t buf_sz;
+ void *buf;
+ /* Protect this descriptor from concurrent readers using it */
+ struct mutex mtx;
+};
+
#define SHMTI_EPLG(s) \
({ \
struct telemetry_shmti *_s = (s); \
@@ -554,6 +564,7 @@ struct telemetry_info {
unsigned int default_blk_ts_rate;
const struct scmi_protocol_handle *ph;
struct telemetry_shmti *shmti;
+ struct dentry *shmti_top;
struct telemetry_de *tdes;
struct xarray xa_des;
struct telemetry_uuid *primary_uuid;
@@ -1358,6 +1369,10 @@ static void scmi_telemetry_tdes_cleanup(void *arg)
scmi_telemetry_de_unlink(&tde->de);
scmi_telemetry_free_tde_put(ti, tde);
}
+
+ /* Safe removal of SHMTI debugfs tree before freeing underlying data */
+ if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS))
+ debugfs_remove_recursive(ti->shmti_top);
}
static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti)
@@ -1535,6 +1550,91 @@ scmi_telemetry_enumerate_common_intervals(struct telemetry_info *ti)
ti->info.intervals);
}
+static int scmi_telemetry_dbg_shmti_open(struct inode *inode, struct file *filp)
+{
+ struct scmi_telemetry_dbg_shmti *sblob;
+ struct telemetry_shmti *shmti;
+
+ if (!inode->i_private)
+ return -ENODEV;
+
+ shmti = inode->i_private;
+ sblob = kzalloc_obj(*sblob);
+ if (!sblob)
+ return -ENOMEM;
+
+ sblob->shmti = shmti;
+ sblob->buf = kvzalloc(shmti->info.len, GFP_KERNEL);
+ if (!sblob->buf) {
+ kfree(sblob);
+ return -ENOMEM;
+ }
+
+ mutex_init(&sblob->mtx);
+
+ filp->private_data = sblob;
+
+ return 0;
+}
+
+static ssize_t scmi_telemetry_dbg_shmti_read(struct file *filp, char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ struct scmi_telemetry_dbg_shmti *sblob = filp->private_data;
+
+ /* Dump on first read and again after each rewind to start pos */
+ guard(mutex)(&sblob->mtx);
+ if (!sblob->buf_sz || *ppos == 0) {
+ struct inode *ino = file_inode(filp);
+
+ memcpy_fromio(sblob->buf, sblob->shmti->base,
+ sblob->shmti->info.len);
+ sblob->buf_sz = sblob->shmti->info.len;
+ /* update inode timestamps */
+ inode_lock(ino);
+ file_update_time(filp);
+ inode_unlock(ino);
+ }
+
+ return simple_read_from_buffer(buf, count, ppos, sblob->buf, sblob->buf_sz);
+}
+
+static int scmi_telemetry_dbg_shmti_release(struct inode *inode, struct file *filp)
+{
+ struct scmi_telemetry_dbg_shmti *sblob = filp->private_data;
+
+ kvfree(sblob->buf);
+ kfree(sblob);
+
+ return 0;
+}
+
+static const struct file_operations scmi_telemetry_dbg_shmti_fops = {
+ .open = scmi_telemetry_dbg_shmti_open,
+ .release = scmi_telemetry_dbg_shmti_release,
+ .read = scmi_telemetry_dbg_shmti_read,
+ .llseek = generic_file_llseek,
+ .owner = THIS_MODULE,
+};
+
+static void scmi_telemetry_debugfs_initialize(struct telemetry_info *ti)
+{
+ const struct scmi_protocol_handle *ph = ti->ph;
+ struct dentry *top;
+
+ top = ph->hops->debugfs_proto_dentry_get(ph);
+ ti->shmti_top = debugfs_create_dir("shmtis", top);
+
+ for (unsigned int i = 0; i < ti->num_shmti; i++) {
+ char id[16];
+
+ snprintf(id, 16, "%u", ti->shmti[i].info.sid);
+ debugfs_create_file_size(id, 0444, ti->shmti_top, &ti->shmti[i],
+ &scmi_telemetry_dbg_shmti_fops,
+ ti->shmti[i].info.len);
+ }
+}
+
static int iter_shmti_update_state(struct scmi_iterator_state *st,
const void *response, void *priv)
{
@@ -1750,6 +1850,9 @@ static int scmi_telemetry_enumerate_shmti(struct telemetry_info *ti)
/* Expose the fully initialized SHMTI infos */
smp_store_release(&ti->info.base.num_shmtis, ti->num_shmti);
+ if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS))
+ scmi_telemetry_debugfs_initialize(ti);
+
return devm_add_action_or_reset(ph->dev, scmi_telemetry_tdes_cleanup, ti);
}
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 26/34] firmware: arm_scmi: Add Telemetry debugfs ABI documentation
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (24 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 25/34] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 27/34] firmware: arm_scmi: Expose per-instance identifier Cristian Marussi
` (8 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add description of the debugfs SCMI Telemetry protocol ABI.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v15 --> v16
- fix Kernel version/date
v5 --> v6
- fixed typos
---
Documentation/ABI/testing/debugfs-scmi | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/Documentation/ABI/testing/debugfs-scmi b/Documentation/ABI/testing/debugfs-scmi
index ee7179ab2edf..b3e09142fa08 100644
--- a/Documentation/ABI/testing/debugfs-scmi
+++ b/Documentation/ABI/testing/debugfs-scmi
@@ -68,3 +68,25 @@ Description: Max number of concurrently allowed in-flight SCMI messages for
the currently configured SCMI transport for instance <n> on the
RX channels.
Users: Debugging, any userspace test suite
+
+What: /sys/kernel/debug/scmi/<n>/protocols/0x<m>/
+Date: Feb 2027
+KernelVersion: 7.5
+Contact: cristian.marussi@arm.com
+Description: A subdirectory grouping debug entries related to protocol <m>
+ for instance <n>. Each protocol owns and defines the subtree
+ of entries rooted under this directory.
+Users: Debugging, any userspace test suite
+
+What: /sys/kernel/debug/scmi/<n>/protocols/0x1B/shmtis/<m>
+Date: Feb 2027
+KernelVersion: 7.5
+Contact: cristian.marussi@arm.com
+Description: A set of RO files exposed by the Telemetry protocol (0x1B) in
+ order to dump the latest snapshot of each SHMTI memory area in
+ binary format: each file is named by its SHMTI id.
+ File is seekable and a seek to position zero on an open file
+ causes the SHMTI snapshot to be refreshed; file timestamps are
+ updated after each snapshot.
+ This directory reports SHMTIs for instance <n>.
+Users: Debugging, any userspace test suite
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 27/34] firmware: arm_scmi: Expose per-instance identifier
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (25 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 26/34] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 28/34] uapi: Add ARM SCMI Telemetry definitions Cristian Marussi
` (7 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
When multiple SCMI instances are configured, the SCMI stack probes and
it is initialized multiple times: one core stack for each defined instance.
Each istance is simply identified internally with a number and such number
is already exposed to the user to name the root of the per-instance debugfs
subtree, if enabled.
Expose such unique identifier to the SCMI drivers so that they can use
that same identifier when in need to name their per-instance resources.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v7 --> v8
- fixed Doxygen 'non-negative' integer
---
drivers/firmware/arm_scmi/driver.c | 1 +
include/linux/scmi_protocol.h | 5 +++++
2 files changed, 6 insertions(+)
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 9dd7becb9b1b..4429144392b7 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -3445,6 +3445,7 @@ static int scmi_probe(struct platform_device *pdev)
idr_init(&info->rx_idr);
handle = &info->handle;
+ handle->id = info->id;
handle->dev = info->dev;
handle->version = &info->version;
handle->devm_protocol_acquire = scmi_devm_protocol_acquire;
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index f0b091e6709f..ab2fdf55eb85 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -1081,6 +1081,10 @@ struct scmi_notify_ops {
/**
* struct scmi_handle - Handle returned to ARM SCMI clients for usage.
*
+ * @id: A unique non-negative natural integer identifying the SCMI Instance
+ * associated with this handle to be used across all drivers for
+ * naming purposes: same identifier used internally as the root for
+ * the debugfs per-instance tree.
* @dev: pointer to the SCMI device
* @version: pointer to the structure containing SCMI version information
* @devm_protocol_acquire: devres managed method to get hold of a protocol,
@@ -1101,6 +1105,7 @@ struct scmi_notify_ops {
* @notify_ops: pointer to set of notifications related operations
*/
struct scmi_handle {
+ unsigned int id;
struct device *dev;
struct scmi_base_info *version;
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 28/34] uapi: Add ARM SCMI Telemetry definitions
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (26 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 27/34] firmware: arm_scmi: Expose per-instance identifier Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 29/34] firmware: arm_scmi: Add System Telemetry driver Cristian Marussi
` (6 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add a number of structures and ioctls definitions used by the ARM
SCMI Telemetry protocol.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v15 --> v16
- added GROUPS/SINGLE_SAMPLE abi_features flags definitions
v14 --> v15
- a couple of clarification in doxygen
v11 --> v12
- fixed documentation around event lifetime cycle to align with
the new implemented logic
v10 --> v11
- typos fixing and comments fixup
- more docs on BATCH IOCTLs and structures
- more docs on SHMTI_LIST
- more docs on EVENT_REGISTER
- more docs on GET_INTRVS upper bounds and zero-query
- more docs on BULK/SINGLE upper bounds and zero-query
v8 --> v9
- Simplified ABI related to _LIST IOCTL, substituting all custom
container structures in favour of the usage of scmi_tlm_batch,
i.e. dropping:
- scmi_tlm_des_list
- scmi_tlm_uuid_list
- scmi_tlm_grps_list
- scmi_tlm_shmtis_list
- Updated related _LIST documentation
v7 --> v8
- Fixes for some Sashiko issues on v7
- fix typos and wrong struct reference in SCMI_TLM_BATCH_READ
- move a couple of IOCTL to be _IOW
- add a few more comments on usage and behaviour of BATCH ops
v6 --> v7
- added per-DE UUID/SID/OFFS
- IOCTL EVENTS: generation counter
- generic BATCH support (scmi_tlm_batch)
- BATCH READ with perf-DE status
- BATCH DE GET/SET CONFIG
- added reserved fields for grow
- added more ABI features flags
- better Doxygen desciption for IN/OUT params
v5 --> v6
- Fixed interval exp to be __s32 as documented
- Using a more unique name for IOCTL MAGIC define
- Renamed a few IOCTLs to be consistent
- redefine primary UUID as an array of u8
- add GET_ABI_INFO to gather ABI version and instance info
- add RESET support
- fixed typos in kdoc
v4 --> v5
- Added more structure padding
- Removed frowned upon flexible array in favour of u64 pointers to external
userspace provided buffer
- Added UAPI support to access SHMTIs from userspace
- Cleaned up Doxygen
v2 --> v3
- Change tstamp_exp tp ts_rate
- renamed num -> num_intervals in scmi_tlm_interval
- added padding in scmi_tlm_de_sample to avoid packing issues on 32bit
v1 --> v2
- Added proper __counted_by marks
- Fixed a few dox comments
- Renamed reserved[] fields to pad[]
---
MAINTAINERS | 1 +
include/uapi/linux/scmi.h | 533 ++++++++++++++++++++++++++++++++++++++
2 files changed, 534 insertions(+)
create mode 100644 include/uapi/linux/scmi.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1eb2b8..418c53f2ddd3 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -27338,6 +27338,7 @@ F: drivers/reset/reset-scmi.c
F: include/linux/device-id/scmi.h
F: include/linux/sc[mp]i_protocol.h
F: include/trace/events/scmi.h
+F: include/uapi/linux/scmi.h
F: include/uapi/linux/virtio_scmi.h
SYSTEM CONTROL MANAGEMENT INTERFACE (SCMI) i.MX Extension Message Protocol drivers
diff --git a/include/uapi/linux/scmi.h b/include/uapi/linux/scmi.h
new file mode 100644
index 000000000000..726989fbfc76
--- /dev/null
+++ b/include/uapi/linux/scmi.h
@@ -0,0 +1,533 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright (C) 2026 ARM Ltd.
+ */
+#ifndef _UAPI_LINUX_SCMI_H
+#define _UAPI_LINUX_SCMI_H
+
+/*
+ * Userspace interface SCMI Telemetry
+ */
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+#define SCMI_TLM_ABI_VERSION_V1 1
+#define SCMI_TLM_CURRENT_ABI_VERSION SCMI_TLM_ABI_VERSION_V1
+#define SCMI_TLM_DE_IMPL_UUID_SZ 16
+
+#define SCMI_TLM_SHMTI_ID_INVALID 0xFFFFFFFF
+
+/**
+ * struct scmi_tlm_abi_info - Basic information on the ABI/instance
+ *
+ * SCMI Telemetry characteristics like number of DEs, Groups, Intervals and
+ * SHMTIs are statically determined by the platform and do not change within
+ * one boot/session; they are reported here so that they can be used to properly
+ * size the requests on other IOCTLs, without having to discover dynamically
+ * those sizes by issuing a zero-sized IOCTL request at first.
+ *
+ * @size: sizeof this structure - IN
+ * @abi_version: ABI Version - OUT
+ * @abi_features: ABI capabilities bitmap - OUT
+ * @primary_de_impl_version: SCMI Telemetry DE implementation revision - OUT
+ * @num_des: Number of defined DEs - OUT
+ * @num_groups: Number of defined DEs groups - OUT
+ * @num_intervals: Number of available update intervals (instance-level) - OUT
+ * @num_shmtis: Number of discovered SHMTI areas - OUT
+ * @features: Instance specific SCMI feature-support bitmap - OUT
+ * @reserved: Some room for future expansion without the need to resize.
+ * Must be zero.
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_ABI_INFO
+ */
+struct scmi_tlm_abi_info {
+ __u32 size;
+ __u32 abi_version;
+ __u32 abi_features;
+#define SCMI_TLM_ABI_FEAT_RESET (1 << 0)
+#define SCMI_TLM_ABI_FEAT_EVENT (1 << 1)
+#define SCMI_TLM_ABI_FEAT_UUID_LIST (1 << 2)
+#define SCMI_TLM_ABI_FEAT_BATCH_STATE (1 << 3)
+#define SCMI_TLM_ABI_FEAT_BATCHED_CFG (1 << 4)
+#define SCMI_TLM_ABI_FEAT_DE_TRACKING (1 << 5)
+#define SCMI_TLM_ABI_FEAT_GROUPS (1 << 6)
+#define SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE (1 << 7)
+ __u8 primary_de_impl_version[SCMI_TLM_DE_IMPL_UUID_SZ];
+ __u32 num_des;
+ __u32 num_groups;
+ __u32 num_intervals;
+ __u32 num_shmtis;
+ __u32 features;
+#define SCMI_TLM_SCMI_SUPPORT_RESET (1 << 0)
+#define SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE (1 << 1)
+#define SCMI_TLM_SCMI_SUPPORT_GROUP_CONFIG (1 << 2)
+#define SCMI_TLM_SCMI_SUPPORT_UPDATE_NOTIFICATION (1 << 3)
+ __u64 reserved;
+};
+
+/**
+ * struct scmi_tlm_update_interval - Update interval descriptor
+ *
+ * @secs: integer representing seconds - OUT
+ * @exp: signed integer representing the base 10 exponent used as a multiplier
+ * with @secs to represent the interval - OUT
+ *
+ * The resulting update interval is calculated as: <secs> * 10 ^ <exp>
+ */
+struct scmi_tlm_update_interval {
+ __u32 secs;
+ __s32 exp;
+};
+
+/**
+ * struct scmi_tlm_config - Whole instance or group configuration
+ *
+ * @enable: Get/Set Telemetry enabled state for the whole instance or the
+ * group specified in @grp_id - IN/OUT
+ * @t_enable: Get/Set timestamp state for the group specified in @grp_id - IN/OUT
+ * @flags: Bitmask to represent special characteristics - IN
+ * @pad: Padding fields to enforce alignment
+ * @grp_id: Identifier of the target group upon which this configuration will
+ * be applied: ignored if not marked as a group in @flags - IN/OUT
+ * @active: Get/Set currently active update interval for the whole instance
+ * or the group specified in @grp_id - IN/OUT
+ * @reserved: Some room for future expansion. Must be zero.
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_CFG
+ * WO - SCMI_TLM_SET_CFG
+ */
+struct scmi_tlm_config {
+ __u8 enable;
+ __u8 t_enable;
+ __u8 flags;
+#define SCMI_TLM_CONFIG_GROUP (1 << 0)
+#define SCMI_TLM_CONFIG_FLAGS (SCMI_TLM_CONFIG_GROUP)
+#define SCMI_TLM_CONFIG_IS_GROUP(f) ((f) & SCMI_TLM_CONFIG_GROUP)
+ __u8 pad;
+ __u32 grp_id;
+ struct scmi_tlm_update_interval active;
+ __u64 reserved;
+};
+
+/**
+ * struct scmi_tlm_intervals - Update intervals descriptor
+ *
+ * @grp_id: Identifier of the target group upon which this configuration
+ * will be applied: ignored if not marked as a group in @flags - IN
+ * @flags: Bitmask to represent special characteristics. When the interval
+ * is NOT marked as SCMI_TLM_INTERV_DISCRETE, @intervals will
+ * contain a triplet: min/max/step - OUT
+ * @pad: Padding fields to enforce alignment
+ * @num_intervals: Number of entries of @intervals. On input this represents the
+ * size of @intervals, while on output carries the effective
+ * number of entries filled-in.
+ * On input this has an upper-bound determined as:
+ * - @scmi_tlm_abi_info.num_intervals if NOT referring a group
+ * OR
+ * - @scmi_tlm_grp_info.num_intervals if referring a group
+ * When provided as zero it will return the upper-bound itself
+ * leaving @intervals untouched - IN/OUT
+ * @pad2: Padding fields to enforce alignment
+ * @reserved: Some room for future expansion. Must be zero.
+ * @intervals: A pointer to an array of struct scmi_tlm_update_interval - IN/OUT
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_INTRVS
+ */
+struct scmi_tlm_intervals {
+ __u32 grp_id;
+ __u8 flags;
+#define SCMI_TLM_INTERV_GROUP (1 << 0)
+#define SCMI_TLM_INTERV_DISCRETE (1 << 1)
+#define SCMI_TLM_INTERV_FLAGS \
+ (SCMI_TLM_INTERV_GROUP | SCMI_TLM_INTERV_DISCRETE)
+#define SCMI_TLM_INTERV_IS_GROUP(f) ((f) & SCMI_TLM_INTERV_GROUP)
+ __u8 pad[3];
+ __u32 num_intervals;
+ __u32 pad2;
+ __u64 reserved;
+#define SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW 0
+#define SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH 1
+#define SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP 2
+ __u64 intervals;
+};
+
+/**
+ * struct scmi_tlm_de_config - DE configuration
+ *
+ * This descriptor is used directly by the aggregate SCMI_TLM_GET_ALL_CFG and
+ * SCMI_TLM_SET_ALL_CFG IOCTLs, or embedded into an @scmi_tlm_batch request for
+ * SCMI_TLM_GET_DE_CFG and SCMI_TLM_SET_DE_CFG IOCTLs.
+ *
+ * @id: Identifier of the DE, ignored by SCMI_TLM_GET/SET_ALL_CFG - IN
+ * @enable: Get/Set the enabled state of this single DE or the cumulative ALL
+ * state - IN/OUT
+ * @t_enable: Get/Set the timestamp state of this single DE (if supported) or
+ * the cumulative ALL state - IN/OUT
+ * @sid: An integer representing the SHMTI ID that contains this DE. Valid
+ * ONLY when the related DE is enabled - OUT
+ * @offset: An integer representing the offset in the SHMTI @sid that identifies
+ * the start of the TDCF DataLine containing this DE.
+ * Valid ONLY when the related DE is enabled - OUT
+ * @pad: Padding fields to enforce alignment
+ * @uuid: An array containing the UUID, in BigEndian format, associated to
+ * this DE. Valid ONLY when the related DE is enabled: this UUID can
+ * be the primary UUID or one of the secondary UUIDs if any exist.
+ * Note that this UUID/DE association is permanent within the same
+ * boot/session - OUT
+ * @reserved: Some room for future expansion. Must be zero.
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_ALL_CFG
+ * WO - SCMI_TLM_SET_ALL_CFG
+ * RW - SCMI_TLM_GET_DE_CFG (within a BATCH)
+ * RW - SCMI_TLM_SET_DE_CFG (within a BATCH)
+ */
+struct scmi_tlm_de_config {
+ __u32 id;
+ __u32 enable;
+ __u32 t_enable;
+ __u32 sid;
+ __u32 offset;
+ __u32 pad;
+ __u8 uuid[SCMI_TLM_DE_IMPL_UUID_SZ];
+ __u64 reserved;
+};
+
+/**
+ * struct scmi_tlm_de_info - DataEvent Descriptor
+ *
+ * @id: DE identifier - IN/OUT
+ * @grp_id: Identifier of the group which this DE belongs to; valid only if
+ * this DE is marked as belonging to a group in @flags - IN/OUT
+ * @data_sz: DE data size in bytes - OUT
+ * @type: DE type - OUT
+ * @unit: DE unit of measurements - OUT
+ * @unit_exp: Power-of-10 multiplier for DE unit - OUT
+ * @ts_rate: Clock rate in kHz used to generate the DE timestamp - OUT
+ * @instance_id: DE instance ID - OUT
+ * @compo_instance_id: DE component instance ID - OUT
+ * @compo_type: Type of component which is associated to this DE - OUT
+ * @persistent: Data value for this DE survives reboot (non-cold ones) - OUT
+ * @flags: Bitmask to represent special characteristics - IN
+ * @pad: Padding fields to enforce alignment
+ * @pad2: Padding fields to enforce alignment
+ * @name: Name of this DE - OUT
+ * @reserved: Some room for future expansion. Must be zero.
+ *
+ * Used to get the full description of a DE: it reflects DE Descriptors
+ * definitions in SCMI V4.0 specification at 3.12.4.6.
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_DE_INFO
+ * RW - SCMI_TLM_GET_DE_LIST (within a @scmi_tlm_batch)
+ */
+struct scmi_tlm_de_info {
+ __u32 id;
+ __u32 grp_id;
+ __u32 data_sz;
+ __u32 type;
+ __u32 unit;
+ __s32 unit_exp;
+ __u32 ts_rate;
+ __u32 instance_id;
+ __u32 compo_instance_id;
+ __u32 compo_type;
+ __u8 persistent;
+ __u8 flags;
+#define SCMI_TLM_DEINFO_GROUP (1 << 0)
+#define SCMI_TLM_DEINFO_FLAGS (SCMI_TLM_DEINFO_GROUP)
+#define SCMI_TLM_DEINFO_HAS_GROUP(f) ((f) & SCMI_TLM_DEINFO_GROUP)
+ __u8 pad[2];
+ __u32 pad2;
+ __u8 name[16];
+ __u64 reserved;
+};
+
+/**
+ * struct scmi_tlm_de_sample - A DataEvent reading
+ *
+ * @id: DE identifier - IN
+ * @pad: Padding fields to enforce alignment.
+ * @tstamp: DE reading timestamp (0 if timestamp NOT supported) - OUT
+ * @val: Reading of the DE data value - OUT
+ *
+ * Used by:
+ * RW - SCMI_TLM_DE_READ
+ * RW - SCMI_TLM_BATCH_READ (within an @scmi_tlm_batch)
+ */
+struct scmi_tlm_de_sample {
+ __u32 id;
+ __u32 pad;
+ __u64 tstamp;
+ __u64 val;
+};
+
+/**
+ * struct scmi_tlm_data_read - Bulk read of a number of DataEvents
+ *
+ * @grp_id: Optional group ID number, ignored if not marked as a group request
+ * in @flags and not supported by SCMI_TLM_BATCH_READ - IN
+ * @flags: Bitmask to represent special characteristics - IN
+ * @pad: Padding fields to enforce alignment
+ * @pad2: Padding fields to enforce alignment
+ * @num_samples: Number of entries in @samples. In input represents the size of
+ * @samples, while in output carries the effective number of items
+ * filled-in.
+ * On input this has an upper-bound determined as:
+ * - @scmi_tlm_abi_info.num_des if NOT referring a group
+ * OR
+ * - @scmi_tlm_grp_info.num_des if referring a group
+ * When provided as zero it will return the upper-bound itself
+ * leaving @samples untouched - IN/OUT
+ * @samples: A reference to an array of struct scmi_tlm_de_sample containing
+ * an entry for each DE - IN/OUT
+ *
+ * Used by:
+ * RW - SCMI_TLM_SINGLE_READ
+ * RW - SCMI_TLM_BULK_READ
+ */
+struct scmi_tlm_data_read {
+ __u32 grp_id;
+ __u8 flags;
+#define SCMI_TLM_READ_GROUP (1 << 0)
+#define SCMI_TLM_READ_FLAGS (SCMI_TLM_READ_GROUP)
+#define SCMI_TLM_READ_IS_GROUP(f) ((f) & SCMI_TLM_READ_GROUP)
+ __u8 pad[3];
+ __u32 pad2;
+ __u32 num_samples;
+ __u64 samples;
+};
+
+/**
+ * struct scmi_tlm_batch - A wrapper structure for BATCH operations
+ *
+ * This structure is used as an opaque container for a set of objects of the
+ * same kind: it is used by a number of different IOCTLs and it is up to the
+ * specific IOCTL implementation to enforce a proper upper-bound on the
+ * maximum amount of items that can be specified in @num_items.
+ *
+ * Exceeding the IOCTL-specific upper bound in @num_items leads to an error
+ * (-EINVAL), while issuing an IOCTL requesting in @num_items less than the
+ * related maximum upper-bound it will succeed and simply return only the
+ * required @num_items.
+ *
+ * It is possible to discover each specific upper-bound in a couple of
+ * alternative ways:
+ *
+ * - by issuing a ZERO sized query IOCTl at first (as detailed down below)
+ * for any of the IOCTLs that use the @scmi_tlm_batch parameter.
+ *
+ * - by querying in advance, once for all, the platform to retrieve all the
+ * statically defined resource counters with SCMI_TLM_GET_ABI_INFO, when
+ * supported by the IOCTL.
+ *
+ * The per-IOCTL statically defined upper-bounds are defined as follows:
+ *
+ * - SCMI_TLM_BATCH_READ: scmi_tlm_abi_info.num_des
+ * - SCMI_TLM_GET_DE_CFG: scmi_tlm_abi_info.num_des
+ * - SCMI_TLM_SET_DE_CFG: scmi_tlm_abi_info.num_des
+ * - SCMI_TLM_GET_DE_LIST: scmi_tlm_abi_info.num_des
+ * - SCMI_TLM_GET_GRP_LIST: scmi_tlm_abi_info.num_groups
+ * - SCMI_TLM_GET_SHMTI_LIST: scmi_tlm_abi_info.num_shmtis
+ *
+ * NOTE that the values returned by SCMI_TLM_GET_UUID_LIST vary dynamically at
+ * runtime depending on the actual configuration: as a consequence its most up
+ * to date uppper bound can only be discovered dynamically through zero-sized
+ * querying.
+ *
+ * The @states field is optional and when it is NULL it is up to the specific
+ * IOCTL implementation to decide which policy to enforce in case of failures
+ * in the middle of the batch processing: as of now each BATCHED IOCTLs fails
+ * as a whole as soon as one of the batched requests fails, without any kind
+ * of roll-back of the previously succeeded requests.
+ *
+ * When this container structure is used as parameter for one the _LIST
+ * IOCTLs listed below, @states is ignored in any case, and the @num_items
+ * field can be set to zero in input to query the system about the maximum
+ * number of available resources.
+ * Note that this idiomatic query-style is made available as an alternative
+ * usage pattern, but somr of this information is usually statically defined
+ * within the same session/boot and available by other means (IOCTLs).
+ *
+ * The field @item_sz MUST match the expected payload size for the specific
+ * IOCTL and it is supposed to be meaningfully set ALSO when @num_items is
+ * zero.
+ *
+ * @num_items: Number of items in @items and @states - IN/OUT
+ * @item_sz: Size of a single item contained in @items - IN
+ * @reserved: Some room for future expansion. Must be zero.
+ * @states: An optional reference to an array of s32 items representing the
+ * outcome of the requests for each single item in @items: these are
+ * ordered in the same order as the @items. - OUT
+ * @items: A reference to an array of batched requests - IN/OUT
+ *
+ * Used by:
+ * RW - SCMI_TLM_BATCH_READ - container of @scmi_tlm_de_sample
+ * RW - SCMI_TLM_GET_DE_CFG - container of @scmi_tlm_de_config
+ * RW - SCMI_TLM_SET_DE_CFG - container of @scmi_tlm_de_config
+ * RW - SCMI_TLM_GET_DE_LIST - container of @scmi_tlm_de_info
+ * RW - SCMI_TLM_GET_UUID_LIST - container of @scmi_tlm_uuid
+ * RW - SCMI_TLM_GET_GRP_LIST - container of @scmi_tlm_grp_info
+ * RW - SCMI_TLM_GET_SHMTI_LIST - container of @scmi_tlm_shmti_info
+ */
+struct scmi_tlm_batch {
+ __u32 num_items;
+ __u32 item_sz;
+ __u64 reserved;
+ __u64 states;
+ __u64 items;
+};
+
+/**
+ * struct scmi_tlm_grp_info - Group info descriptor
+ *
+ * @grp_id: Group ID number - IN
+ * @num_des: Number of DEs part of this group - OUT
+ * @num_intervals: Number of update intervals supported. Zero if group does not
+ * support per-group update interval configuration. - OUT
+ * @pad: Padding fields to enforce alignment
+ * @reserved: Some room for future expansion. Must be zero.
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_GRP_INFO
+ * RW - SCMI_TLM_GET_GRP_LIST (within a BATCH)
+ */
+struct scmi_tlm_grp_info {
+ __u32 grp_id;
+ __u32 num_des;
+ __u32 num_intervals;
+ __u32 pad;
+ __u64 reserved;
+};
+
+/**
+ * struct scmi_tlm_grp_desc - Group descriptor
+ *
+ * @grp_id: Group ID number - IN
+ * @num_des: Number of DEs composing this group. In input represents the size
+ * of @composing_des, while in output carries the effective number
+ * of items filled-in - IN/OUT
+ * @composing_des: A reference to an array of __u32 elements containing the
+ * DataEvent IDs composing this group. - IN/OUT
+ * @reserved: Some room for future expansion. Must be zero.
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_GRP_DESC
+ */
+struct scmi_tlm_grp_desc {
+ __u32 grp_id;
+ __u32 num_des;
+ __u64 composing_des;
+ __u64 reserved;
+};
+
+/**
+ * struct scmi_tlm_shmti_info - SHMTI descriptor
+ *
+ * @sid: SHMTI ID - IN
+ * @fd: Associated opened file descriptor to use for mmap. This file descriptor
+ * can only be used to mmap the SHMTIs to read-only pages and it is
+ * implicitly marked as O_CLOEXEC. The lifetime of any associated mapping
+ * obtained from @fd is independent from it and from the file descriptor
+ * used to issue the related IOCTL - OUT
+ * @len: Effective size of the accessible area of the SHMTI that will be mapped
+ * using @fd. Note that, since mmap allows mapping length of multiple of
+ * PAGE_SIZE, this @len implicitly sets the upper-bound of the number of
+ * PAGE_SIZE pages that you are allowed to request - OUT
+ * @offset: Offset in the obtained mmap where the specified SHMTI start - OUT
+ * @reserved: Some room for future expansion. Must be zero.
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_SHMTI_LIST (within an @scmi_tlm_batch)
+ */
+struct scmi_tlm_shmti_info {
+ __u32 sid;
+ __u32 fd;
+ __u32 len;
+ __u32 offset;
+ __u64 reserved;
+};
+
+/**
+ * struct scmi_tlm_uuid - UUID descriptor
+ *
+ * @bytes: An array of bytes containing the UUID in BigEndian order as
+ * per RFC 9562. - OUT
+ *
+ * Used by:
+ * RW - SCMI_TLM_GET_UUID_LIST (within an @scmi_tlm_batch)
+ */
+struct scmi_tlm_uuid {
+ __u8 bytes[SCMI_TLM_DE_IMPL_UUID_SZ];
+};
+
+/**
+ * struct scmi_tlm_event - Event descriptor
+ *
+ * @type: Type of Telemetry event to subscribe/unsubscribe - IN
+ * @efd: An integer representing an open eventfd file descriptor, which will
+ * be used to signal the subscribed event. The lifecycle of the event
+ * subscription is tied to the controlling chardev file descriptor NOT to
+ * the eventfd lifetime, so that if the file descriptor used to issue this
+ * IOCTL is closed all the events registered for that instance are cleared.
+ * Note that, though, if in the meantime the Telemetry driver is unloaded,
+ * the backing event subscription will be removed and the eventfd will
+ * remain orphaned. - IN
+ * @cookie: An integer used to represent a subscription request: ONLY positive
+ * values represents valid subscription.
+ * On a new subscription this must be set to zero in input, to signify
+ * that a new request is being made: if the subscription was
+ * successful a positive integer is returned.
+ * When, instead, @cookie is set to a positive integer in input, it
+ * is interpreted as a reference to an existing subscription to be
+ * cancelled.
+ * NOTE that the cookies are per-session, so anybody with knowledge of
+ * the @cookie can cancel the subscription as long as it has access to
+ * the same session file-descriptor used to subscribe the event related
+ * to that cookie.
+ * @pad: Padding fields to enforce alignment
+ * @reserved: Some room for future expansion. Must be zero.
+ *
+ * Used by:
+ * RW - SCMI_TLM_EVENT_SUBSCRIBE
+ */
+struct scmi_tlm_event {
+#define SCMI_TLM_EVT_GENERATION 0x0U
+#define SCMI_TLM_EVT_LAST (SCMI_TLM_EVT_GENERATION)
+ __u32 type;
+ __u32 efd;
+ __u32 cookie;
+ __u32 pad;
+ __u64 reserved;
+};
+
+#define SCMI_TLM_IOCTL_MAGIC 0xF1
+
+#define SCMI_TLM_GET_ABI_INFO _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x00, struct scmi_tlm_abi_info)
+#define SCMI_TLM_GET_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x01, struct scmi_tlm_config)
+#define SCMI_TLM_SET_CFG _IOW(SCMI_TLM_IOCTL_MAGIC, 0x02, struct scmi_tlm_config)
+#define SCMI_TLM_GET_INTRVS _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x03, struct scmi_tlm_intervals)
+#define SCMI_TLM_GET_DE_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x04, struct scmi_tlm_batch)
+#define SCMI_TLM_SET_DE_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x05, struct scmi_tlm_batch)
+#define SCMI_TLM_GET_DE_INFO _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x06, struct scmi_tlm_de_info)
+#define SCMI_TLM_GET_DE_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x07, struct scmi_tlm_batch)
+#define SCMI_TLM_DE_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x08, struct scmi_tlm_de_sample)
+#define SCMI_TLM_GET_ALL_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x09, struct scmi_tlm_de_config)
+#define SCMI_TLM_SET_ALL_CFG _IOW(SCMI_TLM_IOCTL_MAGIC, 0x0A, struct scmi_tlm_de_config)
+#define SCMI_TLM_GET_GRP_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0B, struct scmi_tlm_batch)
+#define SCMI_TLM_GET_GRP_INFO _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0C, struct scmi_tlm_grp_info)
+#define SCMI_TLM_GET_GRP_DESC _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0D, struct scmi_tlm_grp_desc)
+#define SCMI_TLM_SINGLE_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0E, struct scmi_tlm_data_read)
+#define SCMI_TLM_BULK_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0F, struct scmi_tlm_data_read)
+#define SCMI_TLM_BATCH_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x10, struct scmi_tlm_batch)
+#define SCMI_TLM_GET_SHMTI_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x11, struct scmi_tlm_batch)
+#define SCMI_TLM_RESET _IO(SCMI_TLM_IOCTL_MAGIC, 0x12)
+#define SCMI_TLM_GET_UUID_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x13, struct scmi_tlm_batch)
+#define SCMI_TLM_EVENT_SUBSCRIBE \
+ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x14, struct scmi_tlm_event)
+
+#endif /* _UAPI_LINUX_SCMI_H */
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 29/34] firmware: arm_scmi: Add System Telemetry driver
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (27 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 28/34] uapi: Add ARM SCMI Telemetry definitions Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 30/34] firmware: arm_scmi: Enable autoloading for " Cristian Marussi
` (5 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add an SCMI driver to allow enumeration, configuration and data collection
of SCMI Telemetry data.
The driver exposes one character device for each found SCMI instance and a
set of related IOCTLs can be used to enumerate, configure and collect
Telemetry resources.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
- return OPNOTSUPP on un-implemented IOCTLs instead of -ENOTTY
- use proper IS_GROUP macros to discard GROUP request across IOCTLs
v15 --> v16
- removed group related IOCTLs
- removed single sample read IOCTLs
- removed reset IOCTL suppport
- removed events IOCTL
- remove unsupported features from ABI features
- clear out->flags in uapi_info
- cap ABI INFO usize to PAGE_SIZE
- sanitize input update interval
- add missing pad/reserved checks in input
v14 --> v15
- reject config attempts and intervals query for groups that do
not support per-group config
- harden/simplify batch helpers
- fix shmti mmap size check to include offset
- drop usage of sdev->dev in device_release
- simplify session_unsubscribe cleanup on exit/unbind
- added missing kref.h include
- expose TLM EVENTS capabilities ONLY IF CONFIG_EVENTFD
- mmap: check/clear also VM_EXEC VM_MAYWRITE VM_MAYEXEC
- reject non zero vm_pgoff
- drop usage of guard(srcu) to avoid races with put_device
v13 --> v14
- avoid double free on probe err path
- grab ctx and check if dead when mmapping an anon_fd
- move away from dead_mtx in favour of dead_srcu and extend srcu'ed
section in ioctl handler
v12 --> v13
- pass back E2BIG from copy_struct_from_user to userspace
- remove a few redundant copy_from_user
- fix filedescriptor lifecycle: drop kref in favour of embedded
cdev and use cdev_device_add. Rely on devm_protocol_get for proto_ops
- refactor EVENT session unsubscribe and check for aliveness
- use *alloc_objs
v11 --> v12
- added RCU deref access on UUIDs
- dropped needless copy_from_user on states_batch
- fixed double free on anon ids error path
- reworked eventfd handling using per-session tracking and cleanup
on chardev last release
- properly free also ti->tsp on error path
- properly acquire/release sdev->dev via get/put_device
v10 --> v11
- make SCMI tlm_<N> devices 664 by default
- add 0-sized query support for all the NON _LIST remaining BATCH ops
- add 0-sized query support on GET_INTRVS
- add 0-sized query support on BULOK/SINGLE reads
v8 --> v9
- Fix some Sashiko reports on v8
- use kvmalloc/kvcalloc for all buffers with user/FW generated size
- add a proper upper bounds checks on IOCTL requests
- make SET_ALL_CFG carry on after a failure (reporting last error anyway)
- properly initialize some possibly unused stack vars
- refactor SHMTIs list processing and ANON fds allocation to avoid leaking
fds by premature installing them into process.
- completely rework cdev and scmi_tlm_instance interactions and lifecycle
and drop all devres allocation: reject fops on cdev after driver unbind
and delay data structures freeing till the last reference has been
dropped.
- reworked BATCH helpers to handle ZERO items requests (for IOCTL query)
- reworked all the _LIST IOCTLs to use scmi_tlm_batch as a container
structure instead of custom structs
- add support for 0-items query pattern to all _LIST IOCTLs
v7 --> v8
- Fixes for some Sashiko issues on v7
- refactor batch handling into common batch init/fini routines
- use array_size to check for overflows
- explain cleanup logic of eventfd_ctx (xa_for_each)
- add a mutex to protect events_xa
- introduce per-IOCTL check on batch upper bounds
v6 --> v7
- fixed module_init errors paths
- introduced a check on FMODE to filter-out unprivleged writers
- added UUID list IOCTL
- added UUID/SID/OFFS tp GET/SET config IOCTLs
- added EVENT subscribe IOCTL
- modified BATCH READ with per-DE status
- added BATCHed mode for DE_CFG_SET/GET
- fix setting properly discrete interval flag
- new ABI feats flags\
- new SCMI feats flags
- new reserved checks
v5 --> v6
- drop hunam readable .read capabilities on chardev
- fill-up the new UAPI de_implementation version u8 array
- added compat_ioctl support
- fixed initialization of flags in scmi_tlm_to_uapi_base_info
- fix grp_id bounds check in scmi_tlm_grp_info_get_ioctl
- fix booleans
- fix SHMTI get list to loop on input params
- reworked bounds checks into:
- scmi_tlm_intervals_get_ioctl
- scmi_tlm_des_list_get_ioctl
- scmi_tlm_grp_list_get_ioctl
- scmi_tlm_grp_desc_get_ioctl
- renamed some READ ioctl to be consistent
- reject oversized mmap requests
- support new GET_ABI_INFO IOCTL
- support RESET IOCTL
- add a missing READ_ONCE() to rinfo get
- avoid gaps in the anon fds returned array needed by shmti mmap
- adding missing cdev_del on remove
---
drivers/firmware/arm_scmi/Kconfig | 10 +
drivers/firmware/arm_scmi/Makefile | 1 +
.../firmware/arm_scmi/scmi_system_telemetry.c | 1376 +++++++++++++++++
3 files changed, 1387 insertions(+)
create mode 100644 drivers/firmware/arm_scmi/scmi_system_telemetry.c
diff --git a/drivers/firmware/arm_scmi/Kconfig b/drivers/firmware/arm_scmi/Kconfig
index f506c4d1d96a..e7d8dd9fa907 100644
--- a/drivers/firmware/arm_scmi/Kconfig
+++ b/drivers/firmware/arm_scmi/Kconfig
@@ -113,4 +113,14 @@ config ARM_SCMI_POWER_CONTROL
called scmi_power_control. Note this may be needed early in boot to
catch early shutdown/reboot SCMI requests.
+config ARM_SCMI_SYSTEM_TELEMETRY
+ tristate "SCMI System Telemetry driver"
+ depends on ARM_SCMI_PROTOCOL || (COMPILE_TEST && OF)
+ help
+ This enables SCMI Systemn Telemetry support that allows userspace to
+ retrieve ARM Telemetry data made available via SCMI.
+
+ This driver can also be built as a module. If so, the module will be
+ called scmi_system_telemetry.
+
endmenu
diff --git a/drivers/firmware/arm_scmi/Makefile b/drivers/firmware/arm_scmi/Makefile
index fe55b7aa0707..20f8d55840a5 100644
--- a/drivers/firmware/arm_scmi/Makefile
+++ b/drivers/firmware/arm_scmi/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_ARM_SCMI_PROTOCOL) += scmi-core.o
obj-$(CONFIG_ARM_SCMI_PROTOCOL) += scmi-module.o
obj-$(CONFIG_ARM_SCMI_POWER_CONTROL) += scmi_power_control.o
+obj-$(CONFIG_ARM_SCMI_SYSTEM_TELEMETRY) += scmi_system_telemetry.o
diff --git a/drivers/firmware/arm_scmi/scmi_system_telemetry.c b/drivers/firmware/arm_scmi/scmi_system_telemetry.c
new file mode 100644
index 000000000000..70f73274563c
--- /dev/null
+++ b/drivers/firmware/arm_scmi/scmi_system_telemetry.c
@@ -0,0 +1,1376 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SCMI - System Telemetry Driver
+ *
+ * Copyright (C) 2026 ARM Ltd.
+ */
+
+#include <linux/anon_inodes.h>
+#include <linux/atomic.h>
+#include <linux/ctype.h>
+#include <linux/cdev.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/kref.h>
+#include <linux/list.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/overflow.h>
+#include <linux/rcupdate.h>
+#include <linux/scmi_protocol.h>
+#include <linux/slab.h>
+#include <linux/sprintf.h>
+#include <linux/srcu.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/uuid.h>
+#include <linux/wait.h>
+#include <linux/xarray.h>
+
+#include <uapi/linux/scmi.h>
+
+#define SCMI_TLM_DRIVER "scmi-telemetry-driver"
+#define SCMI_TLM_NUM_DEVS 256
+
+#define MAX_BULK_LINE_CHAR_LENGTH 64
+
+/*
+ * Break if inadvertently struct scmi_telemetry_de_sample deviates from the
+ * UAPI counterpart struct scmi_tlm_de_sample: this allows more performant
+ * single DE reads.
+ */
+#define SCMI_DE_SAMPLE_BUILD_SANITY_CHECK \
+({ \
+ BUILD_BUG_ON_MSG(sizeof(struct scmi_telemetry_de_sample) != \
+ sizeof(struct scmi_tlm_de_sample), \
+ "UAPI DE sample mismatch !"); \
+ ASSERT_STRUCT_OFFSET(struct scmi_telemetry_de_sample, id, \
+ offsetof(struct scmi_tlm_de_sample, id)); \
+ ASSERT_STRUCT_OFFSET(struct scmi_telemetry_de_sample, pad, \
+ offsetof(struct scmi_tlm_de_sample, pad)); \
+ ASSERT_STRUCT_OFFSET(struct scmi_telemetry_de_sample, tstamp, \
+ offsetof(struct scmi_tlm_de_sample, tstamp));\
+ ASSERT_STRUCT_OFFSET(struct scmi_telemetry_de_sample, val, \
+ offsetof(struct scmi_tlm_de_sample, val)); \
+})
+
+static dev_t stlm_devt;
+
+/**
+ * struct scmi_tlm_setup - Telemetry setup descriptor
+ * @sdev: A reference to the related SCMI device
+ * @ph: A reference to the protocol handle to be used with the ops
+ * @ops: A reference to the protocol ops
+ * @rinfo: A reference to the resource info descriptor
+ */
+struct scmi_tlm_setup {
+ struct scmi_device *sdev;
+ struct scmi_protocol_handle *ph;
+ const struct scmi_telemetry_proto_ops *ops;
+ const struct scmi_telemetry_res_info *rinfo;
+};
+
+/**
+ * struct scmi_tlm_instance - Telemetry instance descriptor
+ * @id: Progressive number identifying this probed instance.
+ * @devt: The dev_t descriptor attached to this instance.
+ * @dev: An embedded device structure.
+ * @cdev: An embedded CDEV structure.
+ * @node: A node to link this in the list of all instances.
+ * @tsp: A reference to the telemetry instance setup
+ * @info: A reference to this instance SCMI Telemetry info data.
+ * @dead_srcu: A SRCU to protect @dead
+ * @dead: A boolean to indicate when the device has been unboud
+ */
+struct scmi_tlm_instance {
+ unsigned int id;
+ dev_t devt;
+ struct device dev;
+ struct cdev cdev;
+ struct list_head node;
+ struct scmi_tlm_setup *tsp;
+ const struct scmi_telemetry_info *info;
+ /* Protect dead */
+ struct srcu_struct dead_srcu;
+ bool dead;
+};
+
+#define cdev_to_instance(c) container_of(c, struct scmi_tlm_instance, cdev)
+#define dev_to_instance(c) container_of(c, struct scmi_tlm_instance, dev)
+
+struct scmi_tlm_shmti_ctx {
+ struct scmi_tlm_instance *ti;
+ struct scmi_telemetry_shmti_info *shmti;
+ struct kref ref;
+};
+
+#define to_shmti_ctx(c) container_of(c, struct scmi_tlm_shmti_ctx, ref)
+
+static inline void scmi_tlm_to_uapi_abi_info(struct scmi_tlm_abi_info *out,
+ const struct scmi_telemetry_info *in)
+{
+ out->size = sizeof(*out);
+ out->abi_version = SCMI_TLM_CURRENT_ABI_VERSION;
+ out->abi_features = SCMI_TLM_ABI_FEAT_UUID_LIST |
+ SCMI_TLM_ABI_FEAT_BATCH_STATE | SCMI_TLM_ABI_FEAT_BATCHED_CFG |
+ SCMI_TLM_ABI_FEAT_DE_TRACKING;
+ export_uuid(out->primary_de_impl_version, &in->base.primary_revision);
+ out->num_des = in->base.num_des;
+ out->num_groups = in->base.num_groups;
+ out->num_intervals = in->base.num_intervals;
+ out->num_shmtis = in->base.num_shmtis;
+ out->features = in->reset_support ? SCMI_TLM_SCMI_SUPPORT_RESET : 0;
+ out->features |= in->single_read_support ?
+ SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE : 0;
+ out->features |= in->per_group_config_support ?
+ SCMI_TLM_SCMI_SUPPORT_GROUP_CONFIG : 0;
+ out->features |= in->continuous_update_support ?
+ SCMI_TLM_SCMI_SUPPORT_UPDATE_NOTIFICATION : 0;
+}
+
+static inline void
+scmi_tlm_to_uapi_intervals(struct scmi_tlm_intervals *out,
+ struct scmi_telemetry_intervals *in,
+ struct scmi_tlm_update_interval *out_interv)
+{
+ unsigned int *in_interv = in->update_intervals;
+
+ out->flags = in->discrete ? SCMI_TLM_INTERV_DISCRETE : 0;
+ for (int i = 0; i < out->num_intervals; i++) {
+ out_interv[i].secs =
+ SCMI_TLM_GET_UPDATE_INTERVAL_SECS(in_interv[i]);
+ out_interv[i].exp =
+ SCMI_TLM_GET_UPDATE_INTERVAL_EXP(in_interv[i]);
+ }
+}
+
+static inline void scmi_tlm_to_uapi_de_info(struct scmi_tlm_de_info *out,
+ struct scmi_telemetry_de_info *in)
+{
+ out->id = in->id;
+ out->grp_id = in->grp_id;
+ out->data_sz = in->data_sz;
+ out->type = in->type;
+ out->unit = in->unit;
+ out->unit_exp = in->unit_exp;
+ out->ts_rate = in->ts_rate;
+ out->instance_id = in->instance_id;
+ out->compo_instance_id = in->compo_instance_id;
+ out->compo_type = in->compo_type;
+ out->persistent = !!in->persistent;
+ out->flags = in->grp_id != SCMI_TLM_GRP_INVALID ?
+ SCMI_TLM_DEINFO_GROUP : 0;
+ out->pad[0] = 0;
+ out->pad[1] = 0;
+ out->pad2 = 0;
+ memcpy(out->name, in->name, 16);
+}
+
+static inline void
+scmi_tlm_to_uapi_des_items(unsigned int num_des,
+ const struct scmi_telemetry_res_info *in,
+ struct scmi_tlm_de_info *des)
+{
+ for (int i = 0; i < num_des; i++)
+ scmi_tlm_to_uapi_de_info(&des[i], &in->dei_store[i]);
+}
+
+/**
+ * scmi_telemetry_res_info_get - Resources info getter
+ * @tsp: A reference to the telemetry instance setup
+ *
+ * On first call this helper takes care to retrieve and cache all the resources
+ * descriptor from the platform, then, on the following invocations it will
+ * always return the cached value.
+ */
+static inline const struct scmi_telemetry_res_info *
+scmi_telemetry_res_info_get(struct scmi_tlm_setup *tsp)
+{
+ const struct scmi_telemetry_res_info *rinfo;
+
+ if (READ_ONCE(tsp->rinfo))
+ return tsp->rinfo;
+
+ rinfo = tsp->ops->res_get(tsp->ph);
+ /* Cache the retrieved resource info value */
+ smp_store_mb(tsp->rinfo, rinfo);
+
+ return rinfo;
+}
+
+static long
+scmi_tlm_abi_info_get_ioctl(const struct scmi_tlm_instance *ti, unsigned long arg)
+{
+ struct scmi_tlm_abi_info base = {};
+ void __user *uptr = (void __user *)arg;
+ bool ignored_trailing;
+ u32 usize;
+ int err;
+
+ if (get_user(usize, (u32 __user *)arg))
+ return -EFAULT;
+
+ if (usize > PAGE_SIZE)
+ return -E2BIG;
+
+ if (usize < offsetofend(struct scmi_tlm_abi_info, reserved))
+ return -EINVAL;
+
+ err = copy_struct_from_user(&base, sizeof(base), uptr, usize);
+ if (err)
+ return err;
+
+ if (base.reserved)
+ return -EINVAL;
+
+ scmi_tlm_to_uapi_abi_info(&base, ti->info);
+ err = copy_struct_to_user(uptr, usize, &base, sizeof(base),
+ &ignored_trailing);
+ if (err)
+ return err;
+
+ if (ignored_trailing)
+ return -EMSGSIZE;
+
+ return 0;
+}
+
+static long
+scmi_tlm_config_get_ioctl(const struct scmi_tlm_instance *ti, unsigned long arg)
+{
+ unsigned int active = ti->info->active_update_interval;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_config cfg;
+
+ if (copy_from_user(&cfg, uptr, sizeof(cfg)))
+ return -EFAULT;
+
+ if (cfg.pad || cfg.reserved || cfg.flags & ~SCMI_TLM_CONFIG_FLAGS)
+ return -EINVAL;
+
+ if (SCMI_TLM_CONFIG_IS_GROUP(cfg.flags))
+ return -EOPNOTSUPP;
+
+ cfg.enable = !!ti->info->enabled;
+ cfg.active.secs = SCMI_TLM_GET_UPDATE_INTERVAL_SECS(active);
+ cfg.active.exp = SCMI_TLM_GET_UPDATE_INTERVAL_EXP(active);
+
+ if (copy_to_user(uptr, &cfg, sizeof(cfg)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static inline int
+scmi_tlm_build_active_interval(struct scmi_tlm_update_interval *intrv,
+ unsigned int *active)
+{
+ *active = SCMI_TLM_BUILD_UPDATE_INTERVAL(intrv->secs, intrv->exp);
+ if (SCMI_TLM_GET_UPDATE_INTERVAL_SECS(*active) != intrv->secs ||
+ SCMI_TLM_GET_UPDATE_INTERVAL_EXP(*active) != intrv->exp) {
+ *active = 0;
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static long
+scmi_tlm_config_set_ioctl(const struct scmi_tlm_instance *ti, unsigned long arg)
+{
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_setup *tsp = ti->tsp;
+ struct scmi_tlm_config cfg;
+ unsigned int active;
+ bool ena;
+
+ if (copy_from_user(&cfg, uptr, sizeof(cfg)))
+ return -EFAULT;
+
+ if (cfg.pad || cfg.reserved || cfg.flags & ~SCMI_TLM_CONFIG_FLAGS)
+ return -EINVAL;
+
+ if (SCMI_TLM_CONFIG_IS_GROUP(cfg.flags))
+ return -EOPNOTSUPP;
+
+ if (scmi_tlm_build_active_interval(&cfg.active, &active))
+ return -EINVAL;
+
+ ena = !!cfg.enable;
+ cfg.grp_id = SCMI_TLM_GRP_INVALID;
+
+ return tsp->ops->collection_configure(tsp->ph, cfg.grp_id, &ena, &active, NULL);
+}
+
+static long
+scmi_tlm_intervals_get_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ struct scmi_telemetry_intervals *tlm_ivs;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_intervals ivs;
+ size_t ivs_intrv_sz;
+
+ if (copy_from_user(&ivs, uptr, sizeof(ivs)))
+ return -EFAULT;
+
+ if (ivs.pad[0] || ivs.pad[1] || ivs.pad[2] || ivs.pad2 ||
+ ivs.reserved || ivs.flags & ~SCMI_TLM_INTERV_FLAGS)
+ return -EINVAL;
+
+ if (SCMI_TLM_INTERV_IS_GROUP(ivs.flags))
+ return -EOPNOTSUPP;
+
+ tlm_ivs = ti->info->intervals;
+
+ /* Return max interval on zero-query */
+ if (!ivs.num_intervals) {
+ ivs.num_intervals = tlm_ivs->num_intervals;
+
+ if (copy_to_user(uptr, &ivs, sizeof(ivs)))
+ return -EFAULT;
+
+ return 0;
+ }
+
+ /* Reject only oversized output buffers */
+ if (ivs.num_intervals > tlm_ivs->num_intervals)
+ return -ENOSPC;
+
+ ivs_intrv_sz = array_size(ivs.num_intervals,
+ sizeof(struct scmi_tlm_update_interval));
+ if (ivs_intrv_sz == SIZE_MAX)
+ return -EOVERFLOW;
+
+ /* A local scratch buffer... */
+ struct scmi_tlm_update_interval *ivs_intrv __free(kvfree) =
+ kvzalloc(ivs_intrv_sz, GFP_KERNEL);
+ if (!ivs_intrv)
+ return -ENOMEM;
+
+ /* Passing the array as a param avoids dancing with uptr->intervals */
+ scmi_tlm_to_uapi_intervals(&ivs, tlm_ivs, ivs_intrv);
+ if (copy_to_user(u64_to_user_ptr(ivs.intervals),
+ (void *)ivs_intrv, ivs_intrv_sz))
+ return -EFAULT;
+
+ if (copy_to_user(uptr, &ivs, sizeof(ivs)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int
+scmi_tlm_batch_initialize(void __user *uptr, struct scmi_tlm_batch *batch,
+ size_t item_sz, unsigned int max_items,
+ void **out_items, size_t *out_batch_sz,
+ int **out_states, size_t *out_states_sz)
+{
+ size_t batch_sz;
+
+ if (copy_from_user(batch, uptr, sizeof(*batch)))
+ return -EFAULT;
+
+ if (batch->reserved || batch->item_sz != item_sz ||
+ batch->num_items > max_items)
+ return -EINVAL;
+
+ /* Sanity check on user invocation */
+ if (!out_items || !out_batch_sz)
+ return -EINVAL;
+
+ *out_items = NULL;
+ *out_batch_sz = 0;
+ if (out_states)
+ *out_states = NULL;
+ if (out_states_sz)
+ *out_states_sz = 0;
+
+ /* Was a size query ? */
+ if (batch->num_items == 0)
+ return 0;
+
+ batch_sz = array_size(batch->num_items, batch->item_sz);
+ if (batch_sz == SIZE_MAX)
+ return -EOVERFLOW;
+
+ void *items __free(kvfree) = kvzalloc(batch_sz, GFP_KERNEL);
+ if (!items)
+ return -ENOMEM;
+
+ /* Read all the requested configs */
+ if (copy_from_user(items, u64_to_user_ptr(batch->items), batch_sz))
+ return -EFAULT;
+
+ /* Is per-item status required ? */
+ if (batch->states && out_states) {
+ size_t states_sz;
+
+ states_sz = array_size(batch->num_items, sizeof(int));
+ if (states_sz == SIZE_MAX)
+ return -EOVERFLOW;
+
+ int *states_arr __free(kvfree) = kvzalloc(states_sz, GFP_KERNEL);
+ if (!states_arr)
+ return -ENOMEM;
+
+ *out_states = no_free_ptr(states_arr);
+ if (out_states_sz)
+ *out_states_sz = states_sz;
+ }
+
+ *out_items = no_free_ptr(items);
+ *out_batch_sz = batch_sz;
+
+ return 0;
+}
+
+static int
+scmi_tlm_batch_finalize(void __user *uptr, struct scmi_tlm_batch *batch,
+ void *items, int *states, size_t batch_sz,
+ size_t states_sz)
+{
+ if (batch_sz) {
+ if (copy_to_user(u64_to_user_ptr(batch->items), items, batch_sz))
+ return -EFAULT;
+
+ if (batch->states && states) {
+ if (copy_to_user(u64_to_user_ptr(batch->states),
+ states, states_sz))
+ return -EFAULT;
+ }
+ }
+
+ if (copy_to_user(uptr, batch, sizeof(*batch)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static long
+scmi_tlm_all_de_config_set_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ const struct scmi_telemetry_res_info *rinfo;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_de_config tcfg = {};
+ bool ena, t_ena;
+ int ret = 0;
+
+ if (copy_from_user(&tcfg, uptr, sizeof(tcfg)))
+ return -EFAULT;
+
+ if (tcfg.pad || tcfg.reserved)
+ return -EINVAL;
+
+ ena = !!tcfg.enable;
+ t_ena = !!tcfg.t_enable;
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ for (int i = 0; i < rinfo->num_des; i++) {
+ int err;
+
+ err = ti->tsp->ops->state_set(ti->tsp->ph, false,
+ rinfo->des[i]->info->id,
+ &ena, &t_ena, NULL, NULL, NULL);
+ if (err) {
+ /* Carry on BUT report last error */
+ ret = err;
+ continue;
+ }
+ }
+
+ return ret;
+}
+
+static long
+scmi_tlm_all_de_config_get_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_de_config tcfg = {};
+ bool ena, t_ena;
+ int ret;
+
+ if (copy_from_user(&tcfg, uptr, sizeof(tcfg)))
+ return -EFAULT;
+
+ if (tcfg.pad || tcfg.reserved)
+ return -EINVAL;
+
+ ena = !!tcfg.enable;
+ t_ena = !!tcfg.t_enable;
+ ret = ti->tsp->ops->state_get(ti->tsp->ph, NULL, &ena, &t_ena,
+ NULL, NULL, NULL);
+ if (ret)
+ return ret;
+
+ tcfg.enable = !!ena;
+ tcfg.t_enable = !!t_ena;
+ if (copy_to_user(uptr, &tcfg, sizeof(tcfg)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static long
+scmi_tlm_de_config_set_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ struct scmi_tlm_de_config *tcfg __free(kvfree) = NULL;
+ int ret, *states __free(kvfree) = NULL;
+ const struct scmi_telemetry_res_info *rinfo;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_batch batch = {};
+ size_t batch_sz, states_sz;
+
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ ret = scmi_tlm_batch_initialize(uptr, &batch, sizeof(*tcfg),
+ rinfo->num_des, (void **)&tcfg, &batch_sz,
+ &states, &states_sz);
+ if (ret)
+ return ret;
+
+ for (int i = 0; i < batch.num_items; i++) {
+ unsigned int sid, offset;
+ bool ena, t_ena;
+ uuid_t uuid;
+ int ret;
+
+ if (tcfg[i].pad || tcfg[i].reserved)
+ return -EINVAL;
+
+ ena = !!tcfg[i].enable;
+ t_ena = !!tcfg[i].t_enable;
+ ret = ti->tsp->ops->state_set(ti->tsp->ph, false,
+ tcfg[i].id, &ena, &t_ena,
+ &sid, &offset, &uuid);
+ if (ret) {
+ if (!states)
+ return ret;
+
+ states[i] = ret;
+ continue;
+ }
+
+ tcfg[i].sid = sid;
+ tcfg[i].offset = offset;
+ export_uuid(tcfg[i].uuid, &uuid);
+ }
+
+ /* Return max batch size on zero query */
+ if (!batch.num_items)
+ batch.num_items = rinfo->num_des;
+
+ return scmi_tlm_batch_finalize(uptr, &batch, tcfg, states, batch_sz,
+ states_sz);
+}
+
+static long
+scmi_tlm_de_config_get_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ struct scmi_tlm_de_config *tcfg __free(kvfree) = NULL;
+ int ret, *states __free(kvfree) = NULL;
+ const struct scmi_telemetry_res_info *rinfo;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_batch batch = {};
+ size_t batch_sz, states_sz;
+
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ ret = scmi_tlm_batch_initialize(uptr, &batch, sizeof(*tcfg),
+ rinfo->num_des, (void **)&tcfg,
+ &batch_sz, &states, &states_sz);
+ if (ret)
+ return ret;
+
+ for (int i = 0; i < batch.num_items; i++) {
+ unsigned int sid = SCMI_TLM_SHMTI_ID_INVALID, offset = 0;
+ bool ena, t_ena;
+ uuid_t uuid = {};
+
+ if (tcfg[i].pad || tcfg[i].reserved)
+ return -EINVAL;
+
+ ena = !!tcfg[i].enable;
+ t_ena = !!tcfg[i].t_enable;
+ ret = ti->tsp->ops->state_get(ti->tsp->ph, &tcfg[i].id,
+ &ena, &t_ena, &sid, &offset, &uuid);
+
+ if (ret) {
+ if (!states)
+ return ret;
+
+ states[i] = ret;
+ continue;
+ }
+
+ tcfg[i].enable = ena;
+ tcfg[i].t_enable = t_ena;
+ tcfg[i].sid = sid;
+ tcfg[i].offset = offset;
+ export_uuid(tcfg[i].uuid, &uuid);
+ }
+
+ /* Return max batch size on zero query */
+ if (!batch.num_items)
+ batch.num_items = rinfo->num_des;
+
+ return scmi_tlm_batch_finalize(uptr, &batch, tcfg, states, batch_sz,
+ states_sz);
+}
+
+static long
+scmi_tlm_de_info_get_ioctl(const struct scmi_tlm_instance *ti, unsigned long arg)
+{
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_setup *tsp = ti->tsp;
+ const struct scmi_telemetry_de *de;
+ struct scmi_tlm_de_info dei;
+
+ if (copy_from_user(&dei, uptr, sizeof(dei)))
+ return -EFAULT;
+
+ /* Flags does NOT make sense in the input param */
+ if (dei.pad[0] || dei.pad[1] || dei.pad2 || dei.flags || dei.reserved)
+ return -EINVAL;
+
+ de = tsp->ops->de_lookup(tsp->ph, dei.id);
+ if (!de)
+ return -EINVAL;
+
+ scmi_tlm_to_uapi_de_info(&dei, de->info);
+ if (copy_to_user(uptr, &dei, sizeof(dei)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static long
+scmi_tlm_des_list_get_ioctl(const struct scmi_tlm_instance *ti, unsigned long arg)
+{
+ struct scmi_tlm_de_info *dei __free(kvfree) = NULL;
+ const struct scmi_telemetry_res_info *rinfo;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_batch batch = {};
+ size_t batch_sz;
+ int ret;
+
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ ret = scmi_tlm_batch_initialize(uptr, &batch, sizeof(*dei),
+ rinfo->num_des, (void **)&dei, &batch_sz,
+ NULL, NULL);
+ if (ret)
+ return ret;
+
+ /* Passing the array as a param avoids dancing with uptr->intervals */
+ if (batch.num_items)
+ scmi_tlm_to_uapi_des_items(batch.num_items, rinfo, dei);
+ else
+ batch.num_items = rinfo->num_des;
+
+ return scmi_tlm_batch_finalize(uptr, &batch, dei, NULL, batch_sz, 0);
+}
+
+static long
+scmi_tlm_de_value_get_ioctl(const struct scmi_tlm_instance *ti, unsigned long arg)
+{
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_setup *tsp = ti->tsp;
+ struct scmi_tlm_de_sample sample;
+ int ret;
+
+ SCMI_DE_SAMPLE_BUILD_SANITY_CHECK;
+
+ if (copy_from_user(&sample, uptr, sizeof(sample)))
+ return -EFAULT;
+
+ if (sample.pad)
+ return -EINVAL;
+
+ ret = tsp->ops->de_data_read(tsp->ph,
+ (struct scmi_telemetry_de_sample *)&sample);
+ if (ret)
+ return ret;
+
+ if (copy_to_user(uptr, &sample, sizeof(sample)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static long scmi_tlm_des_read_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ const struct scmi_telemetry_res_info *rinfo;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_setup *tsp = ti->tsp;
+ struct scmi_tlm_data_read bulk;
+ unsigned int grp_id;
+ int ret;
+
+ if (copy_from_user(&bulk, uptr, sizeof(bulk)))
+ return -EFAULT;
+
+ if (bulk.pad[0] || bulk.pad[1] || bulk.pad[2] || bulk.pad2 ||
+ bulk.flags & ~SCMI_TLM_READ_FLAGS)
+ return -EINVAL;
+
+ if (SCMI_TLM_READ_IS_GROUP(bulk.flags))
+ return -EOPNOTSUPP;
+
+ /* Check that a reasonably sized buffer has been requested */
+ rinfo = scmi_telemetry_res_info_get(tsp);
+ if (bulk.num_samples > rinfo->num_des)
+ return -E2BIG;
+
+ grp_id = SCMI_TLM_GRP_INVALID;
+ /* Return max samples on zero-query */
+ if (!bulk.num_samples) {
+ bulk.num_samples = rinfo->num_des;
+ if (copy_to_user(uptr, &bulk, sizeof(bulk)))
+ return -EFAULT;
+
+ return 0;
+ }
+
+ struct scmi_telemetry_de_sample *samples __free(kvfree) =
+ kvzalloc_objs(*samples, bulk.num_samples);
+ if (!samples)
+ return -ENOMEM;
+
+ ret = tsp->ops->des_bulk_read(tsp->ph, grp_id,
+ &bulk.num_samples, samples);
+ if (ret)
+ return ret;
+
+ if (copy_to_user(u64_to_user_ptr(bulk.samples), samples,
+ bulk.num_samples * sizeof(*samples)))
+ return -EFAULT;
+
+ if (copy_to_user(uptr, &bulk, sizeof(bulk)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static long scmi_tlm_des_batch_read_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ struct scmi_telemetry_de_sample *samples __free(kvfree) = NULL;
+ int ret, *states __free(kvfree) = NULL;
+ const struct scmi_telemetry_res_info *rinfo;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_batch batch = {};
+ size_t batch_sz, states_sz;
+
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ ret = scmi_tlm_batch_initialize(uptr, &batch, sizeof(*samples),
+ rinfo->num_des, (void **)&samples,
+ &batch_sz, &states, &states_sz);
+ if (ret)
+ return ret;
+
+ for (int i = 0; i < batch.num_items; i++) {
+ int ret;
+
+ if (samples[i].pad)
+ return -EINVAL;
+
+ ret = ti->tsp->ops->de_data_read(ti->tsp->ph, &samples[i]);
+ if (ret) {
+ if (!states)
+ return ret;
+
+ states[i] = ret;
+ continue;
+ }
+ }
+
+ /* Return max batch size on zero query */
+ if (!batch.num_items)
+ batch.num_items = rinfo->num_des;
+
+ return scmi_tlm_batch_finalize(uptr, &batch, samples, states, batch_sz,
+ states_sz);
+}
+
+static struct scmi_tlm_shmti_ctx *
+scmi_tlm_shmti_ctx_alloc(struct scmi_tlm_instance *ti,
+ struct scmi_telemetry_shmti_info *shmti)
+{
+ struct scmi_tlm_shmti_ctx *ctx;
+
+ ctx = kzalloc_obj(*ctx);
+ if (!ctx)
+ return ERR_PTR(-ENOMEM);
+
+ ctx->shmti = shmti;
+ kref_init(&ctx->ref);
+
+ /* Get hold of the Telemetry instance when returning anon_fds */
+ get_device(&ti->dev);
+ ctx->ti = ti;
+
+ return ctx;
+}
+
+static void scmi_tlm_shmti_ctx_release(struct kref *kref)
+{
+ struct scmi_tlm_shmti_ctx *ctx = to_shmti_ctx(kref);
+
+ put_device(&ctx->ti->dev);
+
+ kfree(ctx);
+}
+
+static struct scmi_tlm_shmti_ctx *
+scmi_tlm_shmti_ctx_get(struct scmi_tlm_shmti_ctx *ctx)
+{
+ kref_get(&ctx->ref);
+
+ return ctx;
+}
+
+static void scmi_tlm_shmti_ctx_put(struct scmi_tlm_shmti_ctx *ctx)
+{
+ kref_put(&ctx->ref, scmi_tlm_shmti_ctx_release);
+}
+
+static void scmi_tlm_shmti_vma_open(struct vm_area_struct *vma)
+{
+ scmi_tlm_shmti_ctx_get(vma->vm_private_data);
+}
+
+static void scmi_tlm_shmti_vma_close(struct vm_area_struct *vma)
+{
+ scmi_tlm_shmti_ctx_put(vma->vm_private_data);
+}
+
+static const struct vm_operations_struct scmi_tlm_shmti_vm_ops = {
+ .open = scmi_tlm_shmti_vma_open,
+ .close = scmi_tlm_shmti_vma_close,
+};
+
+static int scmi_tlm_shmti_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+ unsigned long npages, expect, needed, req;
+ struct scmi_tlm_shmti_ctx *ctx;
+ phys_addr_t base;
+ size_t map_sz;
+ int ret, idx;
+
+ if (vma->vm_pgoff)
+ return -EINVAL;
+
+ if (vma->vm_flags & (VM_WRITE | VM_EXEC))
+ return -EPERM;
+
+ vm_flags_clear(vma, VM_MAYWRITE | VM_MAYEXEC);
+
+ /* Grab a ctx reference early on */
+ ctx = scmi_tlm_shmti_ctx_get(filp->private_data);
+ /*
+ * Note that any ctx_put potentially release the device and
+ * cleans the SRCU structure...no guards.
+ */
+ idx = srcu_read_lock(&ctx->ti->dead_srcu);
+ if (READ_ONCE(ctx->ti->dead)) {
+ srcu_read_unlock(&ctx->ti->dead_srcu, idx);
+ scmi_tlm_shmti_ctx_put(ctx);
+ return -ENODEV;
+ }
+
+ needed = ctx->shmti->len + ctx->shmti->offset;
+ req = vma->vm_end - vma->vm_start;
+ if (req < needed) {
+ srcu_read_unlock(&ctx->ti->dead_srcu, idx);
+ scmi_tlm_shmti_ctx_put(ctx);
+ return -EINVAL;
+ }
+
+ /* Align & reject oversized requests */
+ base = ctx->shmti->phys & PAGE_MASK;
+ expect = DIV_ROUND_UP(needed, PAGE_SIZE);
+ npages = req >> PAGE_SHIFT;
+ if (npages > expect) {
+ srcu_read_unlock(&ctx->ti->dead_srcu, idx);
+ scmi_tlm_shmti_ctx_put(ctx);
+ return -EINVAL;
+ }
+
+ map_sz = PAGE_ALIGN(needed);
+ vma->vm_private_data = ctx;
+ vma->vm_ops = &scmi_tlm_shmti_vm_ops;
+ /* Set appropriate caching attributes */
+ vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+ /* Map physical address range into user space */
+ ret = vm_iomap_memory(vma, base, map_sz);
+ if (ret) {
+ srcu_read_unlock(&ctx->ti->dead_srcu, idx);
+ scmi_tlm_shmti_ctx_put(ctx);
+ return ret;
+ }
+
+ srcu_read_unlock(&ctx->ti->dead_srcu, idx);
+
+ return 0;
+}
+
+static int scmi_tlm_shmti_release(struct inode *ino, struct file *filp)
+{
+ scmi_tlm_shmti_ctx_put(filp->private_data);
+
+ return 0;
+}
+
+static const struct file_operations scmi_tlm_shmti_fops = {
+ .owner = THIS_MODULE,
+ .mmap = scmi_tlm_shmti_mmap,
+ .release = scmi_tlm_shmti_release,
+};
+
+static void
+scmi_tlm_allocate_anon_fds(struct scmi_tlm_instance *ti, unsigned int *num_shmtis,
+ struct scmi_tlm_shmti_info *shinfo,
+ struct file **filps)
+{
+ const struct scmi_telemetry_info *info = ti->info;
+ struct device *dev = &ti->tsp->sdev->dev;
+ int idx = 0;
+
+ for (int i = 0; i < *num_shmtis; i++) {
+ struct scmi_telemetry_shmti_info *shmti = info->shmtis[i];
+ struct scmi_tlm_shmti_ctx *ctx;
+ struct file *filp;
+ int fd;
+
+ fd = get_unused_fd_flags(O_CLOEXEC);
+ if (fd < 0) {
+ dev_err(dev, "Failed GET_UNUSED_FD for SID: %u\n",
+ shmti->sid);
+ continue;
+ }
+
+ ctx = scmi_tlm_shmti_ctx_alloc(ti, shmti);
+ if (IS_ERR(ctx)) {
+ dev_err(dev, "Failed CTX_ALLOC for SID: %u\n", shmti->sid);
+ put_unused_fd(fd);
+ continue;
+ }
+
+ filp = anon_inode_getfile(SCMI_TLM_DRIVER, &scmi_tlm_shmti_fops,
+ ctx, O_RDONLY | O_CLOEXEC);
+ if (IS_ERR(filp)) {
+ scmi_tlm_shmti_ctx_put(ctx);
+ put_unused_fd(fd);
+ dev_err(dev, "Failed ANON GET_FILE for SID: %u\n",
+ shmti->sid);
+ continue;
+ }
+
+ shinfo[idx].sid = shmti->sid;
+ shinfo[idx].fd = fd;
+ shinfo[idx].len = shmti->len;
+ shinfo[idx].offset = shmti->offset;
+
+ filps[idx++] = filp;
+ }
+
+ /* Return effective initialized items */
+ *num_shmtis = idx;
+}
+
+static inline void
+scmi_tlm_anon_fds_free(int num_shmtis, struct scmi_tlm_shmti_info *shinfo,
+ struct file **filps)
+{
+ for (int i = 0; i < num_shmtis; i++) {
+ put_unused_fd(shinfo[i].fd);
+ /* Triggering scmi_tlm_shmti_release */
+ fput(filps[i]);
+ }
+}
+
+static inline void
+scmi_tlm_anon_fds_install(int num_shmtis, struct scmi_tlm_shmti_info *shinfo,
+ struct file **filps)
+{
+ for (int i = 0; i < num_shmtis; i++)
+ fd_install(shinfo[i].fd, filps[i]);
+}
+
+static long
+scmi_tlm_shmtis_list_get_ioctl(struct scmi_tlm_instance *ti, unsigned long arg)
+{
+ struct scmi_tlm_shmti_info *shinfo __free(kvfree) = NULL;
+ unsigned int max_shmtis = ti->info->base.num_shmtis;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_batch batch = {};
+ size_t batch_sz;
+ int ret;
+
+ ret = scmi_tlm_batch_initialize(uptr, &batch, sizeof(*shinfo), max_shmtis,
+ (void **)&shinfo, &batch_sz, NULL, NULL);
+ if (ret)
+ return ret;
+
+ if (batch.num_items == 0) {
+ batch.num_items = max_shmtis;
+
+ return scmi_tlm_batch_finalize(uptr, &batch, shinfo, NULL,
+ batch_sz, 0);
+ }
+
+ struct file **filps __free(kfree) = kzalloc_objs(*filps, batch.num_items);
+ if (!filps)
+ return -ENOMEM;
+
+ /* Passing the array as a param avoids dancing with uptr->items */
+ scmi_tlm_allocate_anon_fds(ti, &batch.num_items, shinfo, filps);
+ ret = scmi_tlm_batch_finalize(uptr, &batch, shinfo, NULL, batch_sz, 0);
+ if (ret) {
+ scmi_tlm_anon_fds_free(batch.num_items, shinfo, filps);
+ return ret;
+ }
+
+ scmi_tlm_anon_fds_install(batch.num_items, shinfo, filps);
+
+ return 0;
+}
+
+static long
+scmi_tlm_uuid_list_get_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ struct scmi_tlm_uuid *uuids __free(kvfree) = NULL;
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_batch batch = {};
+ unsigned int num_uuids;
+ uuid_t **cur_uuids;
+ size_t batch_sz;
+ int ret;
+
+ /*
+ * Note that UUID array can ONLY grow and it has NOT a known upper
+ * bound, so it does not matter if by the time we grab the cur_uuids
+ * via rcu_dereference() the array has grown in size and we end up
+ * here reading a stale/incomplete set of UUIDs, we will simply
+ * read the new UUIDs next-time: this simplifies allocation.
+ */
+ num_uuids = smp_load_acquire(&ti->info->num_uuids);
+ ret = scmi_tlm_batch_initialize(uptr, &batch, sizeof(*uuids),
+ num_uuids, (void **)&uuids, &batch_sz,
+ NULL, NULL);
+ if (ret)
+ return ret;
+
+ rcu_read_lock();
+ cur_uuids = rcu_dereference(ti->info->uuids);
+ for (int j = 0; j < batch.num_items; j++)
+ export_uuid(uuids[j].bytes, cur_uuids[j]);
+ rcu_read_unlock();
+
+ /* Return max batch size on zero query */
+ if (!batch.num_items)
+ batch.num_items = num_uuids;
+
+ return scmi_tlm_batch_finalize(uptr, &batch, uuids, NULL, batch_sz, 0);
+}
+
+static long scmi_tlm_unlocked_ioctl(struct file *filp, unsigned int cmd,
+ unsigned long arg)
+{
+ struct scmi_tlm_instance *ti = filp->private_data;
+ bool writable = filp->f_mode & FMODE_WRITE;
+
+ guard(srcu)(&ti->dead_srcu);
+ if (READ_ONCE(ti->dead))
+ return -ENODEV;
+
+ switch (cmd) {
+ case SCMI_TLM_GET_ABI_INFO:
+ return scmi_tlm_abi_info_get_ioctl(ti, arg);
+ case SCMI_TLM_GET_CFG:
+ return scmi_tlm_config_get_ioctl(ti, arg);
+ case SCMI_TLM_SET_CFG:
+ if (writable)
+ return scmi_tlm_config_set_ioctl(ti, arg);
+ break;
+ case SCMI_TLM_GET_INTRVS:
+ return scmi_tlm_intervals_get_ioctl(ti, arg);
+ case SCMI_TLM_GET_DE_CFG:
+ return scmi_tlm_de_config_get_ioctl(ti, arg);
+ case SCMI_TLM_SET_DE_CFG:
+ if (writable)
+ return scmi_tlm_de_config_set_ioctl(ti, arg);
+ break;
+ case SCMI_TLM_GET_DE_INFO:
+ return scmi_tlm_de_info_get_ioctl(ti, arg);
+ case SCMI_TLM_GET_DE_LIST:
+ return scmi_tlm_des_list_get_ioctl(ti, arg);
+ case SCMI_TLM_DE_READ:
+ return scmi_tlm_de_value_get_ioctl(ti, arg);
+ case SCMI_TLM_GET_ALL_CFG:
+ return scmi_tlm_all_de_config_get_ioctl(ti, arg);
+ case SCMI_TLM_SET_ALL_CFG:
+ if (writable)
+ return scmi_tlm_all_de_config_set_ioctl(ti, arg);
+ break;
+ case SCMI_TLM_GET_GRP_LIST:
+ return -EOPNOTSUPP;
+ case SCMI_TLM_GET_GRP_INFO:
+ return -EOPNOTSUPP;
+ case SCMI_TLM_GET_GRP_DESC:
+ return -EOPNOTSUPP;
+ case SCMI_TLM_SINGLE_READ:
+ return -EOPNOTSUPP;
+ case SCMI_TLM_BULK_READ:
+ return scmi_tlm_des_read_ioctl(ti, arg);
+ case SCMI_TLM_BATCH_READ:
+ return scmi_tlm_des_batch_read_ioctl(ti, arg);
+ case SCMI_TLM_GET_SHMTI_LIST:
+ return scmi_tlm_shmtis_list_get_ioctl(ti, arg);
+ case SCMI_TLM_RESET:
+ return -EOPNOTSUPP;
+ case SCMI_TLM_GET_UUID_LIST:
+ return scmi_tlm_uuid_list_get_ioctl(ti, arg);
+ case SCMI_TLM_EVENT_SUBSCRIBE:
+ return -EOPNOTSUPP;
+ default:
+ return -ENOTTY;
+ }
+
+ return -EPERM;
+}
+
+static int scmi_tlm_open(struct inode *ino, struct file *filp)
+{
+ struct scmi_tlm_instance *ti = cdev_to_instance(ino->i_cdev);
+
+ guard(srcu)(&ti->dead_srcu);
+ if (READ_ONCE(ti->dead))
+ return -ENODEV;
+
+ get_device(&ti->dev);
+
+ filp->private_data = ti;
+
+ return nonseekable_open(ino, filp);
+}
+
+static int scmi_tlm_release(struct inode *ino, struct file *filp)
+{
+ struct scmi_tlm_instance *ti = filp->private_data;
+ int idx;
+
+ idx = srcu_read_lock(&ti->dead_srcu);
+ if (READ_ONCE(ti->dead)) {
+ srcu_read_unlock(&ti->dead_srcu, idx);
+ put_device(&ti->dev);
+ return -ENODEV;
+ }
+ srcu_read_unlock(&ti->dead_srcu, idx);
+
+ put_device(&ti->dev);
+
+ return 0;
+}
+
+static const struct file_operations stlm_fops = {
+ .owner = THIS_MODULE,
+ .open = scmi_tlm_open,
+ .release = scmi_tlm_release,
+ .unlocked_ioctl = scmi_tlm_unlocked_ioctl,
+ .compat_ioctl = compat_ptr_ioctl,
+};
+
+static struct scmi_tlm_instance *
+scmi_tlm_instance_init(struct scmi_device *sdev,
+ const struct scmi_telemetry_proto_ops *ops,
+ struct scmi_protocol_handle *ph, unsigned int instance_id)
+{
+ int ret;
+
+ struct scmi_tlm_instance *ti __free(kfree) = kzalloc_obj(*ti);
+ if (!ti)
+ return ERR_PTR(-ENOMEM);
+
+ struct scmi_tlm_setup *tsp __free(kfree) = kzalloc_obj(*tsp);
+ if (!tsp)
+ return ERR_PTR(-ENOMEM);
+
+ ti->info = ops->info_get(ph);
+ if (!ti->info)
+ return dev_err_ptr_probe(&sdev->dev,
+ -EINVAL, "invalid Telemetry info !\n");
+
+ ret = init_srcu_struct(&ti->dead_srcu);
+ if (ret)
+ return ERR_PTR(ret);
+
+ tsp->sdev = sdev;
+ tsp->ops = ops;
+ tsp->ph = ph;
+ ti->tsp = no_free_ptr(tsp);
+
+ ti->id = instance_id;
+
+ return no_free_ptr(ti);
+}
+
+static char *scmi_tlm_devnode(const struct device *dev, umode_t *mode)
+{
+ if (mode)
+ *mode = 0664;
+
+ return kasprintf(GFP_KERNEL, "scmi/%s", dev_name(dev));
+}
+
+static struct class stlm_class = {
+ .name = "stlm",
+ .devnode = scmi_tlm_devnode,
+};
+
+static void scmi_tlm_device_release(struct device *dev)
+{
+ struct scmi_tlm_instance *ti = dev_to_instance(dev);
+
+ dev_dbg(&ti->dev, "Finalizing Telemetry instance %u\n", ti->id);
+
+ cleanup_srcu_struct(&ti->dead_srcu);
+ kfree(ti->tsp);
+ kfree(ti);
+}
+
+static int scmi_telemetry_probe(struct scmi_device *sdev)
+{
+ const struct scmi_handle *handle = sdev->handle;
+ struct scmi_protocol_handle *ph;
+ struct scmi_tlm_instance *ti;
+ unsigned int instance;
+ const void *ops;
+ int ret;
+
+ if (!handle)
+ return -ENODEV;
+
+ instance = handle->id;
+ if (instance >= SCMI_TLM_NUM_DEVS)
+ return -ENOSPC;
+
+ ops = handle->devm_protocol_get(sdev, sdev->protocol_id, &ph);
+ if (IS_ERR(ops))
+ return dev_err_probe(&sdev->dev, PTR_ERR(ops),
+ "Cannot access protocol:0x%X\n",
+ sdev->protocol_id);
+
+ ti = scmi_tlm_instance_init(sdev, ops, ph, instance);
+ if (IS_ERR(ti))
+ return PTR_ERR(ti);
+
+ /*
+ * Setting the platform device as parent to enforce proper ordering
+ * at shutdown and to avoid the need for explicit get/put_device
+ */
+ ti->dev.parent = &sdev->dev;
+ ti->dev.devt = MKDEV(MAJOR(stlm_devt), MINOR(stlm_devt) + instance);
+ ti->dev.class = &stlm_class;
+ ti->dev.release = scmi_tlm_device_release;
+ device_initialize(&ti->dev);
+
+ ret = dev_set_name(&ti->dev, "tlm_%d", ti->id);
+ if (ret)
+ goto err_dev;
+
+ cdev_init(&ti->cdev, &stlm_fops);
+ ti->cdev.owner = THIS_MODULE;
+
+ ret = cdev_device_add(&ti->cdev, &ti->dev);
+ if (ret)
+ goto err_dev;
+
+ dev_set_drvdata(&sdev->dev, ti);
+
+ return 0;
+
+err_dev:
+ put_device(&ti->dev);
+
+ return ret;
+}
+
+static void scmi_telemetry_remove(struct scmi_device *sdev)
+{
+ struct scmi_tlm_instance *ti;
+
+ ti = dev_get_drvdata(&sdev->dev);
+ if (!ti)
+ return;
+
+ /* Stop ongoing opens and wait on in-flight operation to drain */
+ WRITE_ONCE(ti->dead, true);
+ synchronize_srcu(&ti->dead_srcu);
+
+ /* Inhibit future opens */
+ cdev_device_del(&ti->cdev, &ti->dev);
+
+ put_device(&ti->dev);
+}
+
+static const struct scmi_device_id scmi_id_table[] = {
+ { SCMI_PROTOCOL_TELEMETRY, "telemetry" },
+ { }
+};
+MODULE_DEVICE_TABLE(scmi, scmi_id_table);
+
+static struct scmi_driver scmi_telemetry_driver = {
+ .name = SCMI_TLM_DRIVER,
+ .probe = scmi_telemetry_probe,
+ .remove = scmi_telemetry_remove,
+ .id_table = scmi_id_table,
+};
+
+static int __init scmi_telemetry_init(void)
+{
+ int ret;
+
+ ret = alloc_chrdev_region(&stlm_devt, 0, SCMI_TLM_NUM_DEVS,
+ SCMI_TLM_DRIVER);
+ if (ret)
+ return ret;
+
+ ret = class_register(&stlm_class);
+ if (ret)
+ goto err_class;
+
+ ret = scmi_register(&scmi_telemetry_driver);
+ if (ret)
+ goto err_scmi;
+
+ return 0;
+
+err_scmi:
+ class_unregister(&stlm_class);
+
+err_class:
+ unregister_chrdev_region(stlm_devt, SCMI_TLM_NUM_DEVS);
+
+ return ret;
+}
+module_init(scmi_telemetry_init);
+
+static void __exit scmi_telemetry_exit(void)
+{
+ scmi_unregister(&scmi_telemetry_driver);
+ unregister_chrdev_region(stlm_devt, SCMI_TLM_NUM_DEVS);
+ class_unregister(&stlm_class);
+}
+module_exit(scmi_telemetry_exit);
+
+MODULE_AUTHOR("Cristian Marussi <cristian.marussi@arm.com>");
+MODULE_DESCRIPTION("ARM SCMI Telemetry Driver");
+MODULE_LICENSE("GPL");
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 30/34] firmware: arm_scmi: Enable autoloading for System Telemetry driver
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (28 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 29/34] firmware: arm_scmi: Add System Telemetry driver Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 31/34] docs: ioctl-number: Add SCMI Ioctls Cristian Marussi
` (4 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add the SCMI System Telemetry driver and related protocols to the list of
standard protocols whosw devices are created automatically, so enabling
module autoloading.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
drivers/firmware/arm_scmi/bus.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/firmware/arm_scmi/bus.c b/drivers/firmware/arm_scmi/bus.c
index 25197197db8e..32002fd9d843 100644
--- a/drivers/firmware/arm_scmi/bus.c
+++ b/drivers/firmware/arm_scmi/bus.c
@@ -546,6 +546,7 @@ static const struct scmi_device_id scmi_std_id_table[] = {
{ SCMI_PROTOCOL_POWERCAP, "powercap" },
{ SCMI_PROTOCOL_PINCTRL, "pinctrl" },
{ SCMI_PROTOCOL_PINCTRL, "pinctrl-imx" },
+ { SCMI_PROTOCOL_TELEMETRY, "telemetry" },
{ },
};
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 31/34] docs: ioctl-number: Add SCMI Ioctls
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (29 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 30/34] firmware: arm_scmi: Enable autoloading for " Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 32/34] [RFC] Documentation: Add SCMI System Telemetry documentation Cristian Marussi
` (3 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi, Jonathan Corbet, Shuah Khan
Add SCMI Subsystem IOCTLs numbers
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: linux-doc@vger.kernel.org
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
Documentation/userspace-api/ioctl/ioctl-number.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
index dbe462040999..53e970fc750e 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -403,6 +403,7 @@ Code Seq# Include File Comments
0xEC 00-01 linux/platform_data/cros_ec_chardev.h ChromeOS EC driver
0xEE 00-09 uapi/linux/pfrut.h Platform Firmware Runtime Update and Telemetry
0xEF 00-0F uapi/linux/ntfs.h NTFS
+0xF1 00-FF uapi/linux/scmi.h ARM SCMI Subsystem
0xF3 00-3F drivers/usb/misc/sisusbvga/sisusb.h sisfb (in development)
<mailto:thomas@winischhofer.net>
0xF6 all LTTng Linux Trace Toolkit Next Generation
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 32/34] [RFC] Documentation: Add SCMI System Telemetry documentation
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (30 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 31/34] docs: ioctl-number: Add SCMI Ioctls Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 33/34] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases Cristian Marussi
` (2 subsequent siblings)
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Document ARM SCMI Telemetry IOCTLs interface.
Co-developed-by: Philip Radford <philip.radford@arm.com>
Signed-off-by: Philip Radford <philip.radford@arm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
Documentation/userspace-api/index.rst | 1 +
Documentation/userspace-api/stlm.rst | 590 ++++++++++++++++++++++++++
2 files changed, 591 insertions(+)
create mode 100644 Documentation/userspace-api/stlm.rst
diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspace-api/index.rst
index a68b1bea57a8..22daa7ebf78e 100644
--- a/Documentation/userspace-api/index.rst
+++ b/Documentation/userspace-api/index.rst
@@ -54,6 +54,7 @@ Devices and I/O
dcdbas
vduse
isapnp
+ stlm
Everything else
===============
diff --git a/Documentation/userspace-api/stlm.rst b/Documentation/userspace-api/stlm.rst
new file mode 100644
index 000000000000..c2151c20c734
--- /dev/null
+++ b/Documentation/userspace-api/stlm.rst
@@ -0,0 +1,590 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=======================================
+STLM - ARM SCMI Telemetry Userspace API
+=======================================
+
+.. contents::
+
+Overview
+========
+
+ARM SCMI is a System and Configuration Management protocol, based on a
+client-server model, that defines a number of messages that allows a
+client/agent like Linux to discover, configure and make use of services
+provided by the server/platform firmware.
+
+SCMI v4.0 [1] introduced support for System Telemetry, through which an
+agent can dynamically enumerate, configure and collect Telemetry Data
+Events (DE) exposed by the platform.
+
+The SCMI System Telemetry driver exposes one or more dedicated devices,
+named as tlm_<N>, grouped under /dev/scmi/, one for each discovered SCMI
+instance.
+
+An IOCTL based interface is defined in order to enumerate, configure and
+read telemetry data, as succinctly described in the following.
+
+For more details on the uAPI, please see include/uapi/linux/scmi.h
+
+Resources Enumeration
+=====================
+
+ - SCMI_TLM_GET_ABI_INFO: Gather ABI versioning detail and basic SCMI
+ Telemetry features like number of resources and supported features.
+
+ - SCMI_TLM_GET_INTRVS: Enumerate available update intervals.
+
+ - SCMI_TLM_GET_DE_INFO: Retrieve DataEvent description.
+
+ - SCMI_TLM_GET_DE_LIST: Retrieve the list of all the existent DataEvent
+ descriptors.
+
+ - SCMI_TLM_GET_GRP_LIST: Gather a list of descriptors for all defined Groups.
+
+ - SCMI_TLM_GET_GRP_INFO: Gather information for a specific Group.
+
+ - SCMI_TLM_GET_GRP_DESC: Gather detailed group composition information for
+ the specified Group.
+
+ - SCMI_TLM_GET_UUID_LIST: Gather a list of descriptors for all the SCMI
+ Telemetry UUIDs defined on this system.
+
+Enumerating DataEvents
+======================
+
+SCMI_TLM_GET_DE_LIST returns the DataEvent descriptors exposed by the
+Telemetery instance. Users can size the request from
+SCMI_TLM_GET_ABI_INFO.num_des or by issuing a zero-sized batch query.
+
+Example
+-------
+
+.. code-block:: c
+
+ int main(int argc, char **argv)
+ {
+ struct scmi_tlm_abi_info abi = {};
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_info *des;
+ int fd, ret, i;
+
+ fd = open("/dev/scmi/tlm_0", O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ abi.size = sizeof(abi);
+ ret = ioctl(fd, SCMI_TLM_GET_ABI_INFO, &abi);
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+
+ if (!abi.num_des) {
+ close(fd);
+ return ret;
+ }
+
+ des = calloc(abi.num_des, sizeof(*des));
+ if (!des) {
+ close(fd);
+ return -ENOMEM;
+ }
+
+ batch.num_items = abi.num_des;
+ batch.item_sz = sizeof(*des);
+ batch.items = (unsigned long)des;
+
+ ret = ioctl(fd, SCMI_TLM_GET_DE_LIST, &batch);
+ if (ret) {
+ free(des);
+ close(fd);
+ return ret;
+ }
+
+ for (i = 0; i < batch.num_items; i++) {
+ printf("0x%08x: %.*s size:%u type:%u unit:%u", des[i].id, 16,
+ des[i].name, des[i].data_sz, des[i].type, des[i].unit);
+
+ if (des[i].flags & SCMI_TLM_DEINFO_GROUP)
+ printf("group:%u", des[i].grp_id);
+
+ printf("\n");
+ }
+
+ return 0;
+ }
+
+Enumerating Groups
+==================
+
+SCMI_TLM_GET_GRP_LIST returns the groups exposed by the Telemetry Instance.
+Group descriptors report; the group ID, the number of DEs in the group, and
+the number of update intervals supported by the group.
+
+SCMI_TLM_GET_GRP_DESC can then be used to retrieve the DE IDs of a specific
+group.
+
+Example
+-------
+
+.. code-block:: c
+
+ int main(int argc, char **argv)
+ {
+ struct scmi_tlm_abi_info abi = {};
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_grp_info *groups;
+ int fd, ret, i;
+
+ fd = open("/dev/scmi/tlm_0", O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ abi.size = sizeof(abi);
+ ret = ioctl(fd, SCMI_TLM_GET_ABI_INFO, &abi);
+
+ if (ret){
+ free(groups);
+ close(fd);
+ return ret;
+ }
+
+ if (!abi.num_groups) {
+ ret = 0;
+ free(groups);
+ close(fd);
+ return ret;
+ }
+
+ groups = calloc(abi.num_groups, sizeof(*groups));
+ if (!groups) {
+ ret = -ENOMEM;
+ free(groups);
+ close(fd);
+ }
+
+ batch.num_items = abi.num_groups;
+ batch.item_sz = sizeof(*groups);
+ batch.items = (unsigned long)groups;
+
+ ret = ioctl(fd, SCMI_TLM_GET_GRP_LIST, &batch);
+ if (ret) {
+ free(groups);
+ close(fd);
+ return ret;
+ }
+
+ for (i = 0; i < batch.num_items; i++) {
+ struct scmi_tlm_grp_desc desc = {};
+ __u32 *de_ids;
+ int j;
+
+ printf("group:%u des:%u intervals:%u\n", groups[i].grp_id,
+ groups[i].num_des, groups[i].num_intervals);
+
+ if (!groups[i].num_des)
+ continue;
+
+ de_ids = calloc(groups[i].num_des, sizeof(*de_ids));
+ if (!de_ids) {
+ ret = -ENOMEM;
+ free(groups);
+ close(fd);
+ return de_ids;
+ }
+
+ desc.grp_id = groups[i].grp_id;
+ desc.num_des = groups[i].num_des;
+ desc.composing_des = (unsigned long)de_ids;
+
+ ret = ioctl(fd, SCMI_TLM_GET_GRP_DESC, &desc);
+ if (ret) {
+ free(de_ids);
+ free(groups);
+ close(fd);
+ return ret;
+ }
+
+ printf("DataEvents:");
+ for (j = 0; j < desc.num_des; j++)
+ printf(" 0x%08x", de_ids[j]);
+ printf("\n");
+
+ free(de_ids);
+ }
+
+ free(groups);
+ close(fd);
+ return 0;
+ }
+
+Enumerating Update Intervals
+============================
+
+SCMI_TLM_GET_INTRVS returns the update intervals supported by the Telemetry
+instance. Userspace can size the request from SCMI_TLM_GET_ABI_INFO.num_intervals
+or by sending a zero-sized query.
+
+The returned intervals may be discrete values, or a range described as
+low/high/step when SCMI_TLM_INTERV_DISCRETE is not set.
+
+Example
+-------
+
+.. code-block:: c
+
+ int main(int argc, char **argv)
+ {
+ struct scmi_tlm_abi_info abi = {};
+ struct scmi_tlm_intervals ivs = {};
+ struct scmi_tlm_update_interval *intervals;
+ int fd, ret, i;
+
+ fd = open("/dev/scmi/tlm_0", O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ abi.size = sizeof(abi);
+ ret = ioctl(fd, SCMI_TLM_GET_ABI_INFO, &abi);
+ if (ret)
+ goto out_fd;
+
+ if (!abi.num_intervals)
+ goto out_fd;
+
+ intervals = calloc(abi.num_intervals, sizeof(*intervals));
+ if (!intervals) {
+ ret = -ENOMEM;
+ goto out_fd;
+ }
+
+ ivs.num_intervals = abi.num_intervals;
+ ivs.intervals = (unsigned long)intervals;
+
+ ret = ioctl(fd, SCMI_TLM_GET_INTRVS, &ivs);
+ if (ret)
+ goto out_free;
+
+ if (ivs.flags & SCMI_TLM_INTERV_DISCRETE) {
+ for (i = 0; i < ivs.num_intervals; i++)
+ printf("interval[%d]: %u * 10^%d seconds\n", i,
+ intervals[i].secs, intervals[i].exp);
+ } else if (ivs.num_intervals >= 3) {
+ printf("low: %u * 10^%d seconds\n",
+ intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW].secs,
+ intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW].exp);
+ printf("high: %u * 10^%d seconds\n",
+ intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH].secs,
+ intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH].exp);
+ printf("step: %u * 10^%d seconds\n",
+ intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP].secs,
+ intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP].exp);
+ }
+ out_free:
+ free(intervals);
+ out_fd:
+ close(fd);
+
+ return ret ? 1 : 0;
+ }
+
+Telemetry Instance Configuration
+================================
+
+SCMI_TLM_GET_CFG returns the current Telemetry configuration. SCMI_TLM_SET_CFG
+updates the enabled state and active update interval for the whole Telemetry
+instance.
+
+The update interval is described as 'secs * 10 ^ exp' seconds. Userspace
+should choose an interval reported by SCMI_TLM_GET_INTRVS.
+
+Example
+-------
+
+.. code-block:: c
+ int main(int argc, char **argv)
+ {
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_config new_cfg = {};
+ int fd, ret;
+
+ fd = open("/dev/scmi/tlm_0", O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ ret = ioctl(fd, SCMI_TLM_GET_CFG, &old_cfg);
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+
+ printf("enabled:%u timestamps:%u interval:%u * 10^%d seconds\n",
+ old_cfg.enable, old_cfg.t_enable,
+ old_cfg.active.secs, old_cfg.active.exp);
+
+ new_cfg.enable = 1;
+ new_cfg.t_enable = old_cfg.t_enable;
+
+ new_cfg.active.secs = 400;
+ new_cfg.active.exp = -3;
+
+ ret = ioctl(fd, SCMI_TLM_SET_CFG, &new_cfg);
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+
+ close(fd)
+
+ return 0;
+ }
+
+Group Configuration
+-------------------
+
+If group configuration is supported, the same ioctls can operate on a group
+by setting SCMI_TLM_CONFIG_GROUP in flags and setting grp_id to a group ID
+discovered with SCMI_TLM_GET_GRP_LIST.
+
+Configuring DataEvents
+======================
+
+SCMI_TLM_GET_DE_CFG returns the configuration of one or more DataEvents.
+SCMI_TLM_SET_DE_CFG updates the enabled state and timestamp state of one or
+more DataEvents.
+
+Both ioctls use struct scmi_tlm_batch with items pointing to an array of
+struct scmi_tlm_de_config. Each entry must contain the DataEvent ID in id.
+Valid IDs can be discovered with SCMI_TLM_GET_DE_LIST.
+
+When a DataEvent is enabled, SCMI_TLM_GET_DE_CFG also reports the SHMTI sid
+and offset where the DataEvent can be found in the shared memory TDCF data,
+together with the UUID associated with that DataEvent.
+
+Note that in the example below, the cfgs[n].id numbers would normally come
+from SCMI_TLM_GET_DE_LIST.
+
+Example
+-------
+
+.. code-block:: c
+
+ int main(int argc, char **argv)
+ {
+ struct scmi_tlm_de_config cfgs[2] = {};
+ struct scmi_tlm_batch batch = {};
+ int fd, ret, i;
+
+ fd = open("/dev/scmi/tlm_0", O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ cfgs[0].id = 0x00000001;
+ cfgs[1].id = 0x00000002;
+
+ batch.num_items = 2;
+ batch.item_sz = sizeof(cfgs[0]);
+ batch.items = (unsigned long)cfgs;
+
+ ret = ioctl(fd, SCMI_TLM_GET_DE_CFG, &batch);
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+
+ for (i = 0; i < batch.num_items; i++) {
+ printf("DE 0x%08x: enabled:%u timestamps:%u\n",
+ cfgs[i].id, cfgs[i].enable, cfgs[i].t_enable);
+ }
+
+ for (i = 0; i < batch.num_items; i++) {
+ cfgs[i].enable = 1;
+ cfgs[i].e_enable = 1;
+ }
+
+ ret = ioctl(fd, SCMI_TLM_SET_DE_CFG, &batch);
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+
+ ret = ioctl(fd, SCMI_TLM_GET_DE_CFG, &batch);
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+
+ for (i = 0; i < batch.num_items; i++) {
+ printf("DE 0x%08x: enabled:%u timestamps:%u sid:%u offset:%u\n",
+ cfgs[i].id, cfgs[i].enable, cfgs[i].t_enable,
+ cfgs[i].sid, cfgs[i].offset);
+ }
+
+ close(fd);
+ return 0;
+ }
+
+Further Configuration
+=====================
+
+ - SCMI_TLM_RESET: Reset the whole Telemetry configuration server side.
+
+ - SCMI_TLM_EVENT_SUBSCRIBE: Subscribe/unsubscribe to a Telemetry event.
+ Lets userspace ask the kernel to notify it when a generation event happens
+ by siganling the eventfd provided.
+
+Event Notification
+------------------
+
+Example
+-------
+
+ - Subscribe to the Telemetry generation event and wait for a notification
+ through an eventfd. Event support is optional so this example checks
+ SCMI_TLM_ABI_FEAT_EVENT.
+
+.. code-block:: c
+
+ int main(int argc, char **argv)
+ {
+ struct scmi_tlm_abi_info abi = {};
+ struct scmi_tlm_event evt = {};
+ eventfd_t count;
+ int fd, efd, ret;
+
+ fd = open("/dev/scmi/tlm_0", O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+
+ abi.size = sizeof(abi);
+ evt.type = SCMI_TLM_EVT_GENERATION;
+
+ ret = ioctl(fd, SCMI_TLM_GET_ABI_INFO, &abi);
+ if (ret)
+ return ret;
+
+ if (!(abi.abi_features & SCMI_TLM_ABI_FEAT_EVENT)) {
+ close(fd);
+ return -EOPNOTSUPP;
+ }
+
+ efd = eventfd(0, EFD_CLOEXEC);
+ if (efd < 0)
+ return efd;
+
+ evt.efd = efd;
+ ret = ioctl(fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt);
+ if (ret)
+ return ret;
+
+ struct pollfd pfd = {
+ .fd = efd,
+ .events = POLLIN,
+ };
+
+ while (1) {
+ ret = poll(&pfd, 1, -1);
+ if (ret < 0)
+ break;
+
+ if (pfd.revents & POLLIN) {
+ ret = eventfd_read(efd, &count);
+ if (ret)
+ break;
+
+ printf("telelmetry generation changed: %llu\n",
+ (unsigned long long)count);
+ }
+ }
+
+ /* Re-issue returned cookie to remove subscription */
+ if (evt.cookie)
+ ioctl(fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt);
+
+ close(efd);
+ close(fd);
+
+ return 0;
+ }
+
+Data Collection
+===============
+
+ - SCMI_TLM_DE_READ: Report the last updated sample for the specified
+ DataEvent.
+
+ - SCMI_TLM_BULK_READ: Report the last samples for all the currently enabled
+ DataEvents.
+
+ - SCMI_TLM_BATCH_READ: Report the last samples for the DataEvents IDs
+ specified within the samples input params.
+
+ - SCMI_TLM_SINGLE_READ: Trigger an immediate platform-side DataEvent
+ update and report the collected samples.
+
+Memory Mapped Raw Access
+------------------------
+
+ - SCMI_TLM_GET_SHMTI_LIST: Gather a list of open file descriptors, one for
+ each SHMTI memory area defined for this instance, that can be used to
+ memory-map such areas in the calling process address space so as to be
+ able to directly access the SCMI Telemetry SHMTI areas shared by the
+ platform firmware, and parse the TDCF format.
+ The SCMI Telemetry TDCF format is defined in the specification at [1].
+
+
+Example
+-------
+
+.. code-block:: c
+
+ int main(int argc, char **argv)
+ {
+ struct scmi_tlm_de_config des_cfg = {};
+ struct scmi_tlm_config cfg = {};
+ struct scmi_tlm_de_sample samples[3] = {};
+ struct scmi_tlm_data_read data = {};
+ int fd, ret;
+
+ fd = open("/dev/scmi/tlm_0", O_RDWR);
+ if (fd < 0)
+ return fd;
+
+ /* Enable ALL Data Events with timestamps*/
+ des_cfg.enable = 1;
+ des_cfg.t_enable = 1;
+ ret = ioctl(fd, SCMI_TLM_SET_ALL_CFG, &des_cfg);
+ if (ret)
+ return ret;
+
+ /* Enable Telemetry as a whole, set a 400ms update interval */
+ cfg.enable = 1;
+ cfg.active.secs = 400;
+ cfg.active.exp = -3;
+
+ ret = ioctl(fd, SCMI_TLM_SET_CFG, &cfg);
+ if (ret)
+ return ret;
+
+ /* Read a selection of DEs */
+ samples[0].id = 0xA001;
+ samples[1].id = 0x0002;
+ samples[2].id = 0x1010;
+ data.num_samples = 3;
+ data.samples = (unsigned long)samples;
+
+ ret = ioctl(fd, SCMI_TLM_BATCH_READ, &data);
+ if (ret)
+ return ret;
+
+ for (int i = 0; i < 3; i++)
+ fprintf(stdout, "%llu: 0x%08X -> %llu\n",
+ samples[i].tstamp, samples[i].id, samples[i].val);
+
+ return 0;
+ }
+
+References
+==========
+[1]: https://developer.arm.com/documentation/den0056/latest/
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 33/34] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (31 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 32/34] [RFC] Documentation: Add SCMI System Telemetry documentation Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 34/34] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool Cristian Marussi
2026-11-24 22:53 ` [PATCH v17 00/34] Introduce SCMI Telemetry support Jonathan Cameron
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
From: Philip Radford <philip.radford@arm.com>
Add support to test SCMI Telemetry UAPI compliance testcases.
Signed-off-by: Philip Radford <philip.radford@arm.com>
Co-developed-by: Cristian Marussi <cristian.marussi@arm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
tools/testing/selftests/arm64/Makefile | 2 +-
tools/testing/selftests/arm64/scmi/Makefile | 12 +
tools/testing/selftests/arm64/scmi/config | 2 +
.../arm64/scmi/test_scmi_telemetry_uapi.c | 2034 +++++++++++++++++
4 files changed, 2049 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/arm64/scmi/Makefile
create mode 100644 tools/testing/selftests/arm64/scmi/config
create mode 100644 tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c
diff --git a/tools/testing/selftests/arm64/Makefile b/tools/testing/selftests/arm64/Makefile
index e456f3b62fa1..f3beae2ae164 100644
--- a/tools/testing/selftests/arm64/Makefile
+++ b/tools/testing/selftests/arm64/Makefile
@@ -4,7 +4,7 @@
ARCH ?= $(shell uname -m 2>/dev/null || echo not)
ifneq (,$(filter $(ARCH),aarch64 arm64))
-ARM64_SUBTARGETS ?= tags signal pauth fp mte bti abi gcs
+ARM64_SUBTARGETS ?= tags signal pauth fp mte bti abi gcs scmi
else
ARM64_SUBTARGETS :=
endif
diff --git a/tools/testing/selftests/arm64/scmi/Makefile b/tools/testing/selftests/arm64/scmi/Makefile
new file mode 100644
index 000000000000..3d9576c7a3e8
--- /dev/null
+++ b/tools/testing/selftests/arm64/scmi/Makefile
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 ARM Limited
+
+# Additional include paths needed by kselftest.h and local headers
+CFLAGS += -std=gnu99 -I.
+LDFLAGS += -static
+
+# Generated binaries to be installed by top KSFT script
+TEST_GEN_PROGS := test_scmi_telemetry_uapi
+
+# Include KSFT lib.mk.
+include ../../lib.mk
diff --git a/tools/testing/selftests/arm64/scmi/config b/tools/testing/selftests/arm64/scmi/config
new file mode 100644
index 000000000000..63cd7e67ab55
--- /dev/null
+++ b/tools/testing/selftests/arm64/scmi/config
@@ -0,0 +1,2 @@
+CONFIG_ARM_SCMI_SYSTEM_TELEMETRY=y
+
diff --git a/tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c b/tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c
new file mode 100644
index 000000000000..12dd44a5c72d
--- /dev/null
+++ b/tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c
@@ -0,0 +1,2034 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2026 ARM Limited
+
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/eventfd.h>
+#include <sys/ioctl.h>
+
+#include "kselftest_harness.h"
+
+#include <../../../../../include/uapi/linux/scmi.h>
+
+#define TLM_0 "tlm_0"
+
+struct scmi_ksft_session {
+ bool initialized;
+ struct scmi_tlm_abi_info info;
+ struct scmi_tlm_config *original_cfg;
+ struct scmi_tlm_intervals available;
+ struct scmi_tlm_update_interval *intervals;
+ struct scmi_tlm_de_info *de_list;
+ unsigned int num_des;
+
+} ksft_session;
+
+static int scmi_tlm_batch_init(struct scmi_tlm_batch *batch,
+ unsigned int num_items,
+ size_t item_sz,
+ bool with_states)
+{
+ void *items = NULL;
+ int *states = NULL;
+
+ bzero(batch, sizeof(*batch));
+
+ if (num_items) {
+ items = malloc(item_sz * num_items);
+ if (!items)
+ return -1;
+
+ bzero(items, item_sz * num_items);
+ }
+
+ if (with_states && num_items) {
+ states = malloc(sizeof(*states) * num_items);
+ if (!states) {
+ free(items);
+ return -1;
+ }
+
+ bzero(states, sizeof(*states) * num_items);
+ }
+
+ batch->num_items = num_items;
+ batch->item_sz = item_sz;
+ batch->items = (__u64)items;
+ batch->states = (__u64)states;
+
+ return 0;
+}
+
+static void scmi_tlm_batch_free(struct scmi_tlm_batch *batch)
+{
+ free((void *)batch->items);
+ free((void *)batch->states);
+ bzero(batch, sizeof(*batch));
+}
+
+static __attribute__((constructor)) void scmi_tlm_initialize(void)
+{
+ struct scmi_tlm_batch batch = {};
+ int fd, ret;
+
+ fd = open("/dev/scmi/" TLM_0, O_RDWR);
+ if (fd < 0) {
+ fprintf(stderr, "INIT: device not available\n");
+ return;
+ }
+
+ ksft_session.info.size = sizeof(struct scmi_tlm_abi_info);
+ ret = ioctl(fd, SCMI_TLM_GET_ABI_INFO, &ksft_session.info);
+ if (ret) {
+ fprintf(stderr, "INIT: failed to get ABI info\n");
+ goto out;
+ }
+
+ if (ksft_session.info.abi_version != SCMI_TLM_ABI_VERSION_V1) {
+ fprintf(stderr, "INIT: ABI Version %u NOT supported",
+ ksft_session.info.abi_version);
+ goto out;
+ }
+
+ if (ksft_session.info.num_des) {
+ ret = scmi_tlm_batch_init(&batch, ksft_session.info.num_des,
+ sizeof(*ksft_session.de_list), false);
+ if (!ret) {
+ ret = ioctl(fd, SCMI_TLM_GET_DE_LIST, &batch);
+ if (!ret) {
+ ksft_session.de_list = (struct scmi_tlm_de_info *)batch.items;
+ ksft_session.num_des = batch.num_items;
+ } else {
+ scmi_tlm_batch_free(&batch);
+ }
+ }
+ }
+
+ if (ksft_session.info.num_intervals) {
+ int num_intervals = ksft_session.info.num_intervals;
+ struct scmi_tlm_update_interval *intervals;
+
+ intervals = malloc(sizeof(*intervals) * num_intervals);
+ if (!intervals) {
+ fprintf(stderr, "INIT: Failed to allocate intervals");
+ goto out;
+ }
+
+ bzero(intervals, sizeof(*intervals) * num_intervals);
+
+ ksft_session.intervals = intervals;
+ ksft_session.available.intervals = (__u64)intervals;
+ ksft_session.available.num_intervals = num_intervals;
+
+ ret = ioctl(fd, SCMI_TLM_GET_INTRVS, &ksft_session.available);
+ if (ret) {
+ fprintf(stderr, "INIT: Failed to retrieve intervals");
+ free(intervals);
+ goto out;
+ }
+ }
+
+ fprintf(stdout, "INIT: Found ABI version %u\n",
+ ksft_session.info.abi_version);
+ fprintf(stdout, "INIT: Discovered %u global update intervals\n",
+ ksft_session.available.num_intervals);
+
+ ksft_session.original_cfg = malloc(sizeof(*ksft_session.original_cfg));
+ if (ksft_session.original_cfg) {
+ bzero(ksft_session.original_cfg, sizeof(*ksft_session.original_cfg));
+
+ ret = ioctl(fd, SCMI_TLM_GET_CFG, ksft_session.original_cfg);
+ if (ret) {
+ free(ksft_session.original_cfg);
+ ksft_session.original_cfg = NULL;
+ }
+ }
+
+ ksft_session.initialized = true;
+
+out:
+ close(fd);
+}
+
+TEST(scmi_tlm_config_struct_layout) {
+ ASSERT_EQ(24, sizeof(struct scmi_tlm_config));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_config, enable));
+ ASSERT_EQ(1, offsetof(struct scmi_tlm_config, t_enable));
+ ASSERT_EQ(2, offsetof(struct scmi_tlm_config, flags));
+ ASSERT_EQ(3, offsetof(struct scmi_tlm_config, pad));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_config, grp_id));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_config, active));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_config, reserved));
+}
+
+TEST(scmi_tlm_abi_info_struct_layout) {
+ ASSERT_EQ(56, sizeof(struct scmi_tlm_abi_info));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_abi_info, size));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_abi_info, abi_version));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_abi_info, abi_features));
+ ASSERT_EQ(12, offsetof(struct scmi_tlm_abi_info, primary_de_impl_version));
+ ASSERT_EQ(28, offsetof(struct scmi_tlm_abi_info, num_des));
+ ASSERT_EQ(32, offsetof(struct scmi_tlm_abi_info, num_groups));
+ ASSERT_EQ(36, offsetof(struct scmi_tlm_abi_info, num_intervals));
+ ASSERT_EQ(40, offsetof(struct scmi_tlm_abi_info, num_shmtis));
+ ASSERT_EQ(44, offsetof(struct scmi_tlm_abi_info, features));
+ ASSERT_EQ(48, offsetof(struct scmi_tlm_abi_info, reserved));
+}
+
+TEST(scmi_tlm_update_interval_struct_layout) {
+ ASSERT_EQ(8, sizeof(struct scmi_tlm_update_interval));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_update_interval, secs));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_update_interval, exp));
+}
+
+TEST(scmi_tlm_intervals_struct_layout) {
+ ASSERT_EQ(32, sizeof(struct scmi_tlm_intervals));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_intervals, grp_id));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_intervals, flags));
+ ASSERT_EQ(5, offsetof(struct scmi_tlm_intervals, pad));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_intervals, num_intervals));
+ ASSERT_EQ(12, offsetof(struct scmi_tlm_intervals, pad2));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_intervals, reserved));
+ ASSERT_EQ(24, offsetof(struct scmi_tlm_intervals, intervals));
+}
+
+TEST(scmi_tlm_de_config_struct_layout) {
+ ASSERT_EQ(48, sizeof(struct scmi_tlm_de_config));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_de_config, id));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_de_config, enable));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_de_config, t_enable));
+ ASSERT_EQ(12, offsetof(struct scmi_tlm_de_config, sid));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_de_config, offset));
+ ASSERT_EQ(20, offsetof(struct scmi_tlm_de_config, pad));
+ ASSERT_EQ(24, offsetof(struct scmi_tlm_de_config, uuid));
+ ASSERT_EQ(40, offsetof(struct scmi_tlm_de_config, reserved));
+}
+
+TEST(scmi_tlm_de_info_struct_layout) {
+ ASSERT_EQ(72, sizeof(struct scmi_tlm_de_info));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_de_info, id));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_de_info, grp_id));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_de_info, data_sz));
+ ASSERT_EQ(12, offsetof(struct scmi_tlm_de_info, type));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_de_info, unit));
+ ASSERT_EQ(20, offsetof(struct scmi_tlm_de_info, unit_exp));
+ ASSERT_EQ(24, offsetof(struct scmi_tlm_de_info, ts_rate));
+ ASSERT_EQ(28, offsetof(struct scmi_tlm_de_info, instance_id));
+ ASSERT_EQ(32, offsetof(struct scmi_tlm_de_info, compo_instance_id));
+ ASSERT_EQ(36, offsetof(struct scmi_tlm_de_info, compo_type));
+ ASSERT_EQ(40, offsetof(struct scmi_tlm_de_info, persistent));
+ ASSERT_EQ(41, offsetof(struct scmi_tlm_de_info, flags));
+ ASSERT_EQ(42, offsetof(struct scmi_tlm_de_info, pad));
+ ASSERT_EQ(44, offsetof(struct scmi_tlm_de_info, pad2));
+ ASSERT_EQ(48, offsetof(struct scmi_tlm_de_info, name));
+ ASSERT_EQ(64, offsetof(struct scmi_tlm_de_info, reserved));
+}
+
+TEST(scmi_tlm_de_sample_struct_layout) {
+ ASSERT_EQ(24, sizeof(struct scmi_tlm_de_sample));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_de_sample, id));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_de_sample, pad));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_de_sample, tstamp));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_de_sample, val));
+}
+
+TEST(scmi_tlm_data_read_struct_layout) {
+ ASSERT_EQ(24, sizeof(struct scmi_tlm_data_read));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_data_read, grp_id));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_data_read, flags));
+ ASSERT_EQ(5, offsetof(struct scmi_tlm_data_read, pad));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_data_read, pad2));
+ ASSERT_EQ(12, offsetof(struct scmi_tlm_data_read, num_samples));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_data_read, samples));
+}
+
+TEST(scmi_tlm_batch_struct_layout) {
+ ASSERT_EQ(32, sizeof(struct scmi_tlm_batch));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_batch, num_items));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_batch, item_sz));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_batch, reserved));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_batch, states));
+ ASSERT_EQ(24, offsetof(struct scmi_tlm_batch, items));
+}
+
+TEST(scmi_tlm_grp_info_struct_layout) {
+ ASSERT_EQ(24, sizeof(struct scmi_tlm_grp_info));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_grp_info, grp_id));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_grp_info, num_des));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_grp_info, num_intervals));
+ ASSERT_EQ(12, offsetof(struct scmi_tlm_grp_info, pad));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_grp_info, reserved));
+}
+
+TEST(scmi_tlm_grp_desc_struct_layout) {
+ ASSERT_EQ(24, sizeof(struct scmi_tlm_grp_desc));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_grp_desc, grp_id));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_grp_desc, num_des));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_grp_desc, composing_des));
+}
+
+TEST(scmi_tlm_shmti_info_struct_layout) {
+ ASSERT_EQ(24, sizeof(struct scmi_tlm_shmti_info));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_shmti_info, sid));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_shmti_info, fd));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_shmti_info, len));
+ ASSERT_EQ(12, offsetof(struct scmi_tlm_shmti_info, offset));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_shmti_info, reserved));
+}
+
+TEST(scmi_tlm_uuid_struct_layout) {
+ ASSERT_EQ(16, sizeof(struct scmi_tlm_uuid));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_uuid, bytes));
+}
+
+TEST(scmi_tlm_event_struct_layout) {
+ ASSERT_EQ(24, sizeof(struct scmi_tlm_event));
+ ASSERT_EQ(0, offsetof(struct scmi_tlm_event, type));
+ ASSERT_EQ(4, offsetof(struct scmi_tlm_event, efd));
+ ASSERT_EQ(8, offsetof(struct scmi_tlm_event, cookie));
+ ASSERT_EQ(12, offsetof(struct scmi_tlm_event, pad));
+ ASSERT_EQ(16, offsetof(struct scmi_tlm_event, reserved));
+}
+
+FIXTURE(stlm) {
+ int fd;
+};
+
+FIXTURE_SETUP(stlm) {
+ if (!ksft_session.initialized)
+ SKIP(return, "initialization failed");
+
+ self->fd = open("/dev/scmi/" TLM_0, O_RDWR);
+ if (self->fd < 0)
+ SKIP(return, "device not available");
+
+ TH_LOG("Testing ABI Version %u on device %s",
+ ksft_session.info.abi_version, TLM_0);
+}
+
+FIXTURE_TEARDOWN(stlm) {
+ close(self->fd);
+}
+
+TEST_F(stlm, tlm_get_cfg) {
+ struct scmi_tlm_config cfg = {};
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg));
+ ASSERT_EQ(0, cfg.pad);
+ ASSERT_EQ(0, cfg.reserved);
+ ASSERT_EQ(0, cfg.flags & ~SCMI_TLM_CONFIG_FLAGS);
+
+ TH_LOG("SCMI_TLM_GET_CFG - ena:%u t_ena:%u secs:%u exp:%d",
+ cfg.enable, cfg.t_enable, cfg.active.secs, cfg.active.exp);
+
+ for (int i = 0; i < ksft_session.info.num_groups; i++) {
+ cfg.flags = SCMI_TLM_CONFIG_GROUP;
+ cfg.grp_id = i;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg));
+ ASSERT_EQ(0, cfg.pad);
+ ASSERT_EQ(0, cfg.reserved);
+ ASSERT_EQ(SCMI_TLM_CONFIG_GROUP, cfg.flags & SCMI_TLM_CONFIG_FLAGS);
+ ASSERT_EQ(i, cfg.grp_id);
+
+ TH_LOG("SCMI_TLM_GET_CFG - ena:%u t_ena:%u grp_id:%u secs:%u exp:%d",
+ cfg.enable, cfg.t_enable, cfg.grp_id, cfg.active.secs, cfg.active.exp);
+ }
+
+ /* Invalid GRP ID should be ignored when proper flags missing */
+ cfg.grp_id = ksft_session.info.num_groups;
+ cfg.flags = 0x0;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg));
+ ASSERT_EQ(0, cfg.pad);
+ ASSERT_EQ(0, cfg.reserved);
+}
+
+TEST_F(stlm, tlm_get_cfg_bad_group) {
+ struct scmi_tlm_config cfg = {};
+
+ cfg.flags = SCMI_TLM_CONFIG_GROUP;
+ cfg.grp_id = ksft_session.info.num_groups;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg));
+ ASSERT_EQ(0, cfg.pad);
+ ASSERT_EQ(0, cfg.reserved);
+ ASSERT_EQ(0, cfg.flags & ~SCMI_TLM_CONFIG_FLAGS);
+}
+
+TEST_F(stlm, tlm_get_cfg_null_ptr_rejected) {
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, NULL));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_get_cfg_invalid_request) {
+ struct scmi_tlm_config cfg = { };
+
+ cfg.pad = 0x1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg));
+ EXPECT_EQ(EINVAL, errno);
+
+ cfg.pad = 0x0;
+ cfg.reserved = 0x1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg));
+ EXPECT_EQ(EINVAL, errno);
+
+ cfg.reserved = 0x0;
+ cfg.flags = ~SCMI_TLM_CONFIG_FLAGS;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_set_cfg) {
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_config new_cfg = {};
+ struct scmi_tlm_config check_cfg = {};
+
+ if (!ksft_session.original_cfg)
+ SKIP(return, "original config not available");
+
+ old_cfg = *ksft_session.original_cfg;
+
+ new_cfg = old_cfg;
+ new_cfg.enable = !old_cfg.enable;
+
+ if (ksft_session.info.num_intervals > 0) {
+ int num_intervals = ksft_session.info.num_intervals;
+ struct scmi_tlm_update_interval *intervals;
+ bool need_valid_interval;
+
+ intervals = (struct scmi_tlm_update_interval *)
+ ksft_session.available.intervals;
+
+ need_valid_interval = new_cfg.enable && old_cfg.active.secs == 0 &&
+ old_cfg.active.exp == 0;
+
+ for (int i = 0; i < num_intervals; i++) {
+ if (!need_valid_interval &&
+ intervals[i].secs == old_cfg.active.secs &&
+ intervals[i].exp == old_cfg.active.exp)
+ continue;
+
+ /* pick another valid interval */
+ new_cfg.active.secs = intervals[i].secs;
+ new_cfg.active.exp = intervals[i].exp;
+ break;
+ }
+ }
+
+ TH_LOG("SCMI_TLM_SET_CFG - ena:%u t_ena:%u secs:%u exp:%d",
+ new_cfg.enable, new_cfg.t_enable, new_cfg.active.secs, new_cfg.active.exp);
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_CFG, &new_cfg));
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_CFG, &check_cfg));
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_CFG, &old_cfg));
+
+ ASSERT_EQ(new_cfg.enable, check_cfg.enable);
+ ASSERT_EQ(new_cfg.active.secs, check_cfg.active.secs);
+ ASSERT_EQ(new_cfg.active.exp, check_cfg.active.exp);
+ ASSERT_EQ(0, check_cfg.pad);
+ ASSERT_EQ(0, check_cfg.reserved);
+}
+
+TEST_F(stlm, tlm_get_intrvs) {
+ struct scmi_tlm_update_interval *intervals;
+ struct scmi_tlm_intervals ivs = {};
+ int num_intervals = ksft_session.info.num_intervals;
+
+ if (!num_intervals)
+ SKIP(return, "No update intervals available");
+
+ intervals = malloc(sizeof(*intervals) * num_intervals);
+ ASSERT_NE(NULL, intervals);
+ bzero(intervals, sizeof(*intervals) * num_intervals);
+
+ ivs.num_intervals = num_intervals;
+ ivs.intervals = (__u64)intervals;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs));
+
+ ASSERT_EQ(num_intervals, ivs.num_intervals);
+ ASSERT_EQ(0, ivs.pad[0]);
+ ASSERT_EQ(0, ivs.pad[1]);
+ ASSERT_EQ(0, ivs.pad[2]);
+ ASSERT_EQ(0, ivs.pad2);
+ ASSERT_EQ(0, ivs.reserved);
+ ASSERT_EQ(0, ivs.flags & ~SCMI_TLM_INTERV_FLAGS);
+
+ for (int i = 0; i < num_intervals; i++) {
+ TH_LOG("SCMI_TLM_GET_INTRVS - [%d] secs:%u exp:%d", i,
+ intervals[i].secs, intervals[i].exp);
+ }
+
+ free(intervals);
+}
+
+TEST_F(stlm, tlm_get_intrvs_invalid_request) {
+ struct scmi_tlm_update_interval interval = {};
+ struct scmi_tlm_intervals ivs = {};
+
+ ivs.num_intervals = 1;
+ ivs.intervals = (__u64)&interval;
+ ivs.pad[0] = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&ivs, sizeof(ivs));
+ ivs.num_intervals = 1;
+ ivs.intervals = (__u64)&interval;
+ ivs.pad2 = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&ivs, sizeof(ivs));
+ ivs.num_intervals = 1;
+ ivs.intervals = (__u64)&interval;
+ ivs.reserved = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&ivs, sizeof(ivs));
+ ivs.num_intervals = 1;
+ ivs.intervals = (__u64)&interval;
+ ivs.flags = ~SCMI_TLM_INTERV_FLAGS;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs));
+}
+
+TEST_F(stlm, tlm_get_intrvs_oversized) {
+ struct scmi_tlm_update_interval interval = {};
+ struct scmi_tlm_intervals ivs = {};
+
+ ivs.num_intervals = ksft_session.info.num_intervals + 1;
+ ivs.intervals = (__u64)&interval;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs));
+ EXPECT_EQ(ENOSPC, errno);
+}
+
+TEST_F(stlm, tlm_get_de_list)
+{
+ struct scmi_tlm_de_info *des;
+ struct scmi_tlm_batch batch = {};
+
+ if (!ksft_session.info.num_des)
+ SKIP(return, "no DEs available");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, ksft_session.info.num_des,
+ sizeof(*des), false));
+
+ des = (struct scmi_tlm_de_info *)batch.items;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch));
+
+ ASSERT_LE(batch.num_items, ksft_session.info.num_des);
+
+ for (int i = 0; i < batch.num_items; i++) {
+ ASSERT_EQ(0, des[i].reserved);
+ ASSERT_EQ(0, des[i].pad[0]);
+ ASSERT_EQ(0, des[i].pad[1]);
+ ASSERT_EQ(0, des[i].pad2);
+ ASSERT_EQ(0, des[i].flags & ~SCMI_TLM_DEINFO_FLAGS);
+
+ TH_LOG("SCMI_TLM_GET_DE_LIST - [%d] id:0x%x name:%s", i, des[i].id,
+ des[i].name);
+ }
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_get_de_list_zero_size_query)
+{
+ struct scmi_tlm_batch batch = {};
+
+ batch.num_items = 0;
+ batch.item_sz = sizeof(struct scmi_tlm_de_info);
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch));
+ ASSERT_EQ(ksft_session.info.num_des, batch.num_items);
+ ASSERT_EQ(sizeof(struct scmi_tlm_de_info), batch.item_sz);
+ ASSERT_EQ(0, batch.reserved);
+ ASSERT_EQ(0, batch.items);
+}
+
+TEST_F(stlm, tlm_get_de_list_zero_size_query_bad_item_sz)
+{
+ struct scmi_tlm_batch batch = {};
+
+ batch.num_items = 0;
+ batch.item_sz = sizeof(struct scmi_tlm_de_info) + 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_de_list_invalid_request)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_info de = {};
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(de) + 1;
+ batch.items = (__u64)&de;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = 1;
+ batch.item_sz = sizeof(de);
+ batch.reserved = 1;
+ batch.items = (__u64)&de;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = ksft_session.info.num_des + 1;
+ batch.item_sz = sizeof(de);
+ batch.items = (__u64)&de;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_de_info)
+{
+ struct scmi_tlm_de_info info = {};
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ info.id = ksft_session.de_list[0].id;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info));
+ ASSERT_EQ(0, info.reserved);
+ ASSERT_EQ(0, info.pad[0]);
+ ASSERT_EQ(0, info.pad[1]);
+ ASSERT_EQ(0, info.pad2);
+ ASSERT_EQ(0, info.flags & ~SCMI_TLM_DEINFO_FLAGS);
+
+ TH_LOG("SCMI_TLM_GET_DE_INFO - id:0x%x name:%s data_sz:%u",
+ info.id, info.name, info.data_sz);
+}
+
+TEST_F(stlm, tlm_get_de_info_invalid_request)
+{
+ struct scmi_tlm_de_info info = {};
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ info.id = ksft_session.de_list[0].id;
+ info.flags = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&info, sizeof(info));
+ info.id = ksft_session.de_list[0].id;
+ info.pad[0] = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&info, sizeof(info));
+ info.id = ksft_session.de_list[0].id;
+ info.reserved = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_de_info_bad_id)
+{
+ struct scmi_tlm_de_info info = {};
+
+ info.id = 0xffffffff;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_de_cfg)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config *cfg;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), false));
+
+ cfg = (struct scmi_tlm_de_config *)batch.items;
+ cfg[0].id = ksft_session.de_list[0].id;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch));
+ ASSERT_EQ(1, batch.num_items);
+ ASSERT_EQ(ksft_session.de_list[0].id, cfg[0].id);
+ ASSERT_EQ(0, cfg[0].reserved);
+
+ TH_LOG("SCMI_TLM_GET_DE_CFG - id:0x%x ena:%u sid:%u offset:%u",
+ cfg[0].id, cfg[0].enable, cfg[0].sid,
+ cfg[0].offset);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_get_de_cfg_invalid_request)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config cfg = {};
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(cfg) + 1;
+ batch.items = (__u64)&cfg;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = ksft_session.info.num_des + 1;
+ batch.item_sz = sizeof(cfg);
+ batch.items = (__u64)&cfg;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = 1;
+ batch.item_sz = sizeof(cfg);
+ batch.items = 0;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_get_de_cfg_bad_id)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config *cfg;
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), false));
+
+ cfg = (struct scmi_tlm_de_config *)batch.items;
+ cfg[0].id = 0xffffffff;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch));
+ EXPECT_EQ(ENODEV, errno);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_get_de_cfg_bad_id_with_states)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config *cfg;
+ int *states;
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), true));
+
+ cfg = (struct scmi_tlm_de_config *)batch.items;
+ states = (int *)batch.states;
+
+ cfg[0].id = 0xffffffff;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch));
+ ASSERT_LT(states[0], 0);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+static int scmi_tlm_get_one_de_cfg(int fd, __u32 id,
+ struct scmi_tlm_de_config *cfg)
+{
+ struct scmi_tlm_batch batch = {};
+
+ bzero(cfg, sizeof(*cfg));
+ cfg->id = id;
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(*cfg);
+ batch.items = (__u64)cfg;
+
+ return ioctl(fd, SCMI_TLM_GET_DE_CFG, &batch);
+}
+
+static int scmi_tlm_set_one_de_cfg(int fd, struct scmi_tlm_de_config *cfg)
+{
+ struct scmi_tlm_batch batch = {};
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(*cfg);
+ batch.items = (__u64)cfg;
+
+ return ioctl(fd, SCMI_TLM_SET_DE_CFG, &batch);
+}
+
+static int scmi_tlm_enable_for_read(int fd, __u32 de_id,
+ struct scmi_tlm_config *old_cfg,
+ struct scmi_tlm_de_config *old_de_cfg)
+{
+ struct scmi_tlm_config new_cfg;
+ struct scmi_tlm_de_config new_de_cfg;
+
+ if (ioctl(fd, SCMI_TLM_GET_CFG, old_cfg))
+ return -1;
+
+ if (scmi_tlm_get_one_de_cfg(fd, de_id, old_de_cfg))
+ return -1;
+
+ new_cfg = *old_cfg;
+ new_cfg.enable = 1;
+
+ if (new_cfg.active.secs == 0 && new_cfg.active.exp == 0) {
+ struct scmi_tlm_update_interval *intervals;
+
+ if (!ksft_session.info.num_intervals)
+ return -1;
+
+ intervals = (struct scmi_tlm_update_interval *)
+ ksft_session.available.intervals;
+
+ new_cfg.active = intervals[0];
+ }
+
+ if (ioctl(fd, SCMI_TLM_SET_CFG, &new_cfg)) {
+ ioctl(fd, SCMI_TLM_SET_CFG, old_cfg);
+ return -1;
+ }
+
+ new_de_cfg = *old_de_cfg;
+ new_de_cfg.enable = 1;
+
+ if (scmi_tlm_set_one_de_cfg(fd, &new_de_cfg)) {
+ ioctl(fd, SCMI_TLM_SET_CFG, old_cfg);
+ return -1;
+ }
+
+ return 0;
+}
+
+static void scmi_tlm_restore_after_read(int fd,
+ struct scmi_tlm_config *old_cfg,
+ struct scmi_tlm_de_config *old_de_cfg)
+{
+ scmi_tlm_set_one_de_cfg(fd, old_de_cfg);
+ ioctl(fd, SCMI_TLM_SET_CFG, old_cfg);
+}
+
+TEST_F(stlm, tlm_de_read)
+{
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_de_config old_de_cfg = {};
+ struct scmi_tlm_de_sample sample = {};
+ __u32 de_id;
+ int ret;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list availalbe");
+
+ de_id = ksft_session.de_list[0].id;
+
+ if (scmi_tlm_enable_for_read(self->fd, de_id, &old_cfg, &old_de_cfg))
+ SKIP(return, "failed to enable telemetry for read");
+
+ sample.id = de_id;
+ ret = ioctl(self->fd, SCMI_TLM_DE_READ, &sample);
+
+ scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg);
+
+ ASSERT_EQ(0, ret);
+ ASSERT_EQ(de_id, sample.id);
+ ASSERT_EQ(0, sample.pad);
+
+ TH_LOG("SCMI_TLM_DE_READ - id:0x%x tstamp:%llu val:%llu", sample.id,
+ sample.tstamp, sample.val);
+}
+
+TEST_F(stlm, tlm_de_read_invalid_request)
+{
+ struct scmi_tlm_de_sample sample = {};
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ sample.id = ksft_session.de_list[0].id;
+ sample.pad = 1;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_DE_READ, &sample));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_de_read_null_ptr_rejected)
+{
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_DE_READ, NULL));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_de_read_bad_id)
+{
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_de_config old_de_cfg = {};
+ struct scmi_tlm_de_sample sample = {};
+ int ret, saved_errno;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ if (scmi_tlm_enable_for_read(self->fd, ksft_session.de_list[0].id,
+ &old_cfg, &old_de_cfg))
+ SKIP(return, "failed to enable telemetry for read");
+
+ sample.id = 0xffffffff;
+
+ ret = ioctl(self->fd, SCMI_TLM_DE_READ, &sample);
+ saved_errno = errno;
+
+ scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg);
+
+ ASSERT_EQ(-1, ret);
+ EXPECT_EQ(ENOENT, saved_errno);
+}
+
+TEST_F(stlm, tlm_single_read)
+{
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_de_config old_de_cfg = {};
+ struct scmi_tlm_de_sample *samples;
+ struct scmi_tlm_data_read read = {};
+ int num_samples = ksft_session.num_des;
+ int ret;
+ __u32 de_id;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ de_id = ksft_session.de_list[0].id;
+
+ if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE) ||
+ !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE))
+ SKIP(return, "single sample read not supported");
+
+ if (scmi_tlm_enable_for_read(self->fd, de_id, &old_cfg, &old_de_cfg))
+ SKIP(return, "failed to enable telemetry for read");
+
+ samples = malloc(sizeof(*samples) * num_samples);
+ ASSERT_NE(NULL, samples);
+ bzero(samples, sizeof(*samples) * num_samples);
+
+ read.num_samples = num_samples;
+ read.samples = (__u64)samples;
+
+ ret = ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read);
+ scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg);
+
+ ASSERT_EQ(0, ret);
+ ASSERT_LE(read.num_samples, num_samples);
+ ASSERT_EQ(0, read.pad[0]);
+ ASSERT_EQ(0, read.pad[1]);
+ ASSERT_EQ(0, read.pad[2]);
+ ASSERT_EQ(0, read.pad2);
+ ASSERT_EQ(0, read.flags & ~SCMI_TLM_READ_FLAGS);
+
+ for (int i = 0; i < read.num_samples; i++) {
+ ASSERT_EQ(0, samples[i].pad);
+
+ TH_LOG("SCMI_TLM_SINGE_READ = [%d] id:0x%x tstamp:%llu val:%llu", i,
+ samples[i].id, samples[i].tstamp, samples[i].val);
+ }
+
+ free(samples);
+}
+
+TEST_F(stlm, tlm_single_read_invalid_request)
+{
+ struct scmi_tlm_de_sample sample = {};
+ struct scmi_tlm_data_read read = {};
+
+ if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE) ||
+ !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE))
+ SKIP(return, "single sample read not supported");
+
+ read.num_samples = 1;
+ read.samples = (__u64)&sample;
+ read.pad[0] = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&read, sizeof(read));
+ read.num_samples = 1;
+ read.samples = (__u64)&sample;
+ read.pad2 = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&read, sizeof(read));
+ read.num_samples = 1;
+ read.samples = (__u64)&sample;
+ read.flags = ~SCMI_TLM_READ_FLAGS;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_single_read_oversized)
+{
+ struct scmi_tlm_de_sample sample = {};
+ struct scmi_tlm_data_read read = {};
+
+ if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE) ||
+ !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE))
+ SKIP(return, "single sample read not supported");
+
+ read.num_samples = ksft_session.info.num_des + 1;
+ read.samples = (__u64)&sample;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read));
+ EXPECT_EQ(E2BIG, errno);
+}
+
+TEST_F(stlm, tlm_single_read_null_ptr_rejected)
+{
+ if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE) ||
+ !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE))
+ SKIP(return, "single sample read not supported");
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, NULL));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_bulk_read)
+{
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_de_config old_de_cfg = {};
+ struct scmi_tlm_de_sample *samples;
+ struct scmi_tlm_data_read read = {};
+ int num_samples = ksft_session.num_des;
+ int i, ret;
+ __u32 de_id;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ de_id = ksft_session.de_list[0].id;
+
+ if (scmi_tlm_enable_for_read(self->fd, de_id, &old_cfg, &old_de_cfg))
+ SKIP(return, "failed to enable telemetry for read");
+
+ samples = malloc(sizeof(*samples) * num_samples);
+ ASSERT_NE(NULL, samples);
+ bzero(samples, sizeof(*samples) * num_samples);
+
+ read.num_samples = num_samples;
+ read.samples = (__u64)samples;
+
+ ret = ioctl(self->fd, SCMI_TLM_BULK_READ, &read);
+ scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg);
+
+ ASSERT_EQ(0, ret);
+ ASSERT_LE(read.num_samples, num_samples);
+ ASSERT_EQ(0, read.pad[0]);
+ ASSERT_EQ(0, read.pad[1]);
+ ASSERT_EQ(0, read.pad[2]);
+ ASSERT_EQ(0, read.pad2);
+ ASSERT_EQ(0, read.flags & ~SCMI_TLM_READ_FLAGS);
+
+ for (i = 0; i < read.num_samples; i++) {
+ ASSERT_EQ(0, samples[i].pad);
+
+ TH_LOG("SCMI_TLM_BULK_READ - [%d] id:0x%x tstamp:%llu val:%llu", i,
+ samples[i].id, samples[i].tstamp, samples[i].val);
+ }
+
+ free(samples);
+}
+
+TEST_F(stlm, tlm_bulk_read_invalid_request)
+{
+ struct scmi_tlm_de_sample sample = {};
+ struct scmi_tlm_data_read read = {};
+
+ read.num_samples = 1;
+ read.samples = (__u64)&sample;
+ read.pad[0] = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, &read));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&read, sizeof(read));
+ read.num_samples = 1;
+ read.samples = (__u64)&sample;
+ read.pad2 = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, &read));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&read, sizeof(read));
+ read.num_samples = 1;
+ read.samples = (__u64)&sample;
+ read.flags = ~SCMI_TLM_READ_FLAGS;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, &read));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_bulk_read_oversized)
+{
+ struct scmi_tlm_de_sample sample = {};
+ struct scmi_tlm_data_read read = {};
+
+ read.num_samples = ksft_session.info.num_des + 1;
+ read.samples = (__u64)&sample;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, &read));
+ EXPECT_EQ(E2BIG, errno);
+}
+
+TEST_F(stlm, tlm_bulk_read_null_ptr_rejected)
+{
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, NULL));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_batch_read)
+{
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_de_config old_de_cfg = {};
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_sample *samples;
+ int ret;
+ __u32 de_id;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ de_id = ksft_session.de_list[0].id;
+
+ if (scmi_tlm_enable_for_read(self->fd, de_id, &old_cfg, &old_de_cfg))
+ SKIP(return, "failed to enable telemetry for read");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*samples),
+ false));
+
+ samples = (struct scmi_tlm_de_sample *)batch.items;
+ samples[0].id = de_id;
+
+ ret = ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch);
+
+ scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg);
+ ASSERT_EQ(0, ret);
+
+ ASSERT_EQ(1, batch.num_items);
+ ASSERT_EQ(ksft_session.de_list[0].id, samples[0].id);
+ ASSERT_EQ(0, samples[0].pad);
+
+ TH_LOG("SCMI_TLM_BATCH_READ - id:0x%x tstamp:%llu val:%llu", samples[0].id,
+ samples[0].tstamp, samples[0].val);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_batch_read_invalid_request)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_sample sample = {};
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(sample) + 1;
+ batch.items = (__u64)&sample;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = 1;
+ batch.item_sz = sizeof(sample);
+ batch.reserved = 1;
+ batch.items = (__u64)&sample;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = ksft_session.info.num_des + 1;
+ batch.item_sz = sizeof(sample);
+ batch.items = (__u64)&sample;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_batch_read_bad_items_ptr)
+{
+ struct scmi_tlm_batch batch = {};
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(struct scmi_tlm_de_sample);
+ batch.items = 0;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_batch_read_bad_id)
+{
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_de_config old_de_cfg = {};
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_sample *samples;
+ int ret, saved_errno;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ if (scmi_tlm_enable_for_read(self->fd, ksft_session.de_list[0].id,
+ &old_cfg, &old_de_cfg))
+ SKIP(return, "failed to enable telemetry for read");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*samples), false));
+
+ samples = (struct scmi_tlm_de_sample *)batch.items;
+ samples[0].id = 0xffffffff;
+
+ ret = ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch);
+ saved_errno = errno;
+
+ scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg);
+
+ ASSERT_EQ(-1, ret);
+ EXPECT_EQ(ENOENT, saved_errno);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_batch_read_bad_id_with_states)
+{
+ struct scmi_tlm_config old_cfg = {};
+ struct scmi_tlm_de_config old_de_cfg = {};
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_sample *samples;
+ int *states;
+ int ret;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ if (scmi_tlm_enable_for_read(self->fd, ksft_session.de_list[0].id,
+ &old_cfg, &old_de_cfg))
+ SKIP(return, "failed to enable telemetry for read");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*samples), true));
+
+ samples = (struct scmi_tlm_de_sample *)batch.items;
+ states = (int *)batch.states;
+
+ samples[0].id = 0xffffffff;
+
+ ret = ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch);
+
+ scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg);
+
+ ASSERT_EQ(0, ret);
+ ASSERT_EQ(-ENOENT, states[0]);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_get_grp_list_zero_size_query)
+{
+ struct scmi_tlm_batch batch = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ batch.num_items = 0;
+ batch.item_sz = sizeof(struct scmi_tlm_grp_info);
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch));
+ ASSERT_EQ(ksft_session.info.num_groups, batch.num_items);
+ ASSERT_EQ(sizeof(struct scmi_tlm_grp_info), batch.item_sz);
+ ASSERT_EQ(0, batch.reserved);
+}
+
+TEST_F(stlm, tlm_get_grp_list)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_grp_info *groups;
+ int i;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ if (!ksft_session.info.num_groups)
+ SKIP(return, "no groups available");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, ksft_session.info.num_groups,
+ sizeof(*groups), false));
+
+ groups = (struct scmi_tlm_grp_info *)batch.items;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch));
+ ASSERT_LE(batch.num_items, ksft_session.info.num_groups);
+
+ for (i = 0; i < batch.num_items; i++) {
+ ASSERT_EQ(i, groups[i].grp_id);
+ ASSERT_EQ(0, groups[i].pad);
+ ASSERT_EQ(0, groups[i].reserved);
+
+ TH_LOG("SCMI_TLM_GRP_LIST - [%d] grp:%u num_des:%u num_intervals:%u",
+ i, groups[i].grp_id, groups[i].num_des, groups[i].num_intervals);
+ }
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_get_grp_list_invalid_request)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_grp_info group = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(group) + 1;
+ batch.items = (__u64)&group;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = 1;
+ batch.item_sz = sizeof(group);
+ batch.reserved = 1;
+ batch.items = (__u64)&group;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = ksft_session.info.num_groups + 1;
+ batch.item_sz = sizeof(group);
+ batch.items = (__u64)&group;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_grp_info)
+{
+ struct scmi_tlm_grp_info group = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ if (!ksft_session.info.num_groups)
+ SKIP(return, "no groups available");
+
+ group.grp_id = 0;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group));
+ ASSERT_EQ(0, group.grp_id);
+ ASSERT_EQ(0, group.pad);
+ ASSERT_EQ(0, group.reserved);
+
+ TH_LOG("SCMI_TLM_GET_GRP_INFO - grp:%u num_des:%u num_intervals:%u",
+ group.grp_id, group.num_des, group.num_intervals);
+}
+
+TEST_F(stlm, tlm_get_grp_info_invalid_request)
+{
+ struct scmi_tlm_grp_info group = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ group.grp_id = 0;
+ group.pad = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&group, sizeof(group));
+ group.grp_id = 0;
+ group.reserved = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_grp_info_bad_group)
+{
+ struct scmi_tlm_grp_info group = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ group.grp_id = ksft_session.info.num_groups;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_grp_desc)
+{
+ struct scmi_tlm_grp_info group = {};
+ struct scmi_tlm_grp_desc desc = {};
+ int i;
+ __u32 *de_ids;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ if (!ksft_session.info.num_groups)
+ SKIP(return, "no groups available");
+
+ group.grp_id = 0;
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group));
+
+ if (!group.num_des)
+ SKIP(return, "group had no DEs");
+
+ de_ids = malloc(sizeof(*de_ids) * group.num_des);
+ ASSERT_NE(NULL, de_ids);
+ bzero(de_ids, sizeof(*de_ids) * group.num_des);
+
+ desc.grp_id = group.grp_id;
+ desc.num_des = group.num_des;
+ desc.composing_des = (__u64)de_ids;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc));
+ ASSERT_EQ(group.grp_id, desc.grp_id);
+ ASSERT_LE(desc.num_des, group.num_des);
+ ASSERT_EQ(0, desc.reserved);
+
+ for (i = 0; i < desc.num_des; i++)
+ TH_LOG("SCMI_TLM_GET_GRP_DESC - grp:%u de[%d]:0x%x", desc.grp_id,
+ i, de_ids[i]);
+
+ free(de_ids);
+}
+
+TEST_F(stlm, tlm_get_grp_desc_invalid_request)
+{
+ struct scmi_tlm_grp_desc desc = {};
+ __u32 de_id = 0;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ if (!ksft_session.info.num_groups)
+ SKIP(return, "no groups available");
+
+ desc.grp_id = 0;
+ desc.num_des = 1;
+ desc.composing_des = (__u64)&de_id;
+ desc.reserved = 1;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_grp_desc_bad_group)
+{
+ struct scmi_tlm_grp_desc desc = {};
+ __u32 de_id = 0;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ desc.grp_id = ksft_session.info.num_groups;
+ desc.num_des = 1;
+ desc.composing_des = (__u64)&de_id;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_grp_desc_oversized)
+{
+ struct scmi_tlm_grp_info group = {};
+ struct scmi_tlm_grp_desc desc = {};
+ __u32 de_id = 0;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ if (!ksft_session.info.num_groups)
+ SKIP(return, "no groups available");
+
+ group.grp_id = 0;
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group));
+ desc.grp_id = 0;
+ desc.num_des = group.num_des + 1;
+ desc.composing_des = (__u64)&de_id;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc));
+ EXPECT_EQ(ENOSPC, errno);
+}
+
+TEST_F(stlm, tlm_get_grp_desc_bad_pointer)
+{
+ struct scmi_tlm_grp_info group = {};
+ struct scmi_tlm_grp_desc desc = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS))
+ SKIP(return, "GROUPS not supported");
+
+ if (!ksft_session.info.num_groups)
+ SKIP(return, "no groups available");
+
+ group.grp_id = 0;
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group));
+
+ if (!group.num_des)
+ SKIP(return, "group has no DEs");
+
+ desc.grp_id = 0;
+ desc.num_des = group.num_des;
+ desc.composing_des = 0;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_get_all_cfg)
+{
+ struct scmi_tlm_de_config cfg = {};
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, &cfg));
+ ASSERT_EQ(0, cfg.reserved);
+ ASSERT_LE(cfg.enable, 1);
+ ASSERT_LE(cfg.t_enable, 1);
+
+ TH_LOG("SCMI_TLM_GET_ALL_CFG - ena:%u t_ena:%u", cfg.enable, cfg.t_enable);
+}
+
+TEST_F(stlm, tlm_get_all_cfg_invalid_request)
+{
+ struct scmi_tlm_de_config cfg = {};
+
+ cfg.reserved = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, &cfg));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_all_cfg_null_ptr_rejected)
+{
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, NULL));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_set_de_cfg)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config old_cfg = {};
+ struct scmi_tlm_de_config new_cfg = {};
+ struct scmi_tlm_de_config check_cfg = {};
+ struct scmi_tlm_de_config *cfg;
+ int set_ret;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), false));
+
+ cfg = (struct scmi_tlm_de_config *)batch.items;
+ cfg[0].id = ksft_session.de_list[0].id;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch));
+ old_cfg = cfg[0];
+
+ new_cfg = old_cfg;
+ new_cfg.enable = !old_cfg.enable;
+
+ cfg[0] = new_cfg;
+ set_ret = ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch);
+ ASSERT_EQ(0, set_ret);
+
+ cfg[0].id = old_cfg.id;
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch));
+ check_cfg = cfg[0];
+
+ cfg[0] = old_cfg;
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch));
+
+ ASSERT_EQ(new_cfg.id, check_cfg.id);
+ ASSERT_EQ(!!new_cfg.enable, !!check_cfg.enable);
+ ASSERT_EQ(!!new_cfg.t_enable, !!check_cfg.t_enable);
+ ASSERT_EQ(0, check_cfg.reserved);
+
+ TH_LOG("SCMI_TLM_SET_DE_CFG = id:0x%x ena:%u t_ena:%u", check_cfg.id,
+ check_cfg.enable, check_cfg.t_enable);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_set_de_cfg_invalid_request)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config cfg = {};
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(cfg) + 1;
+ batch.items = (__u64)&cfg;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = ksft_session.info.num_des + 1;
+ batch.item_sz = sizeof(cfg);
+ batch.items = (__u64)&cfg;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = 1;
+ batch.item_sz = sizeof(cfg);
+ batch.items = 0;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_set_de_cfg_read_only_rejected)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config cfg = {};
+ int fd;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ fd = open("/dev/scmi/" TLM_0, O_RDONLY);
+ ASSERT_GE(fd, 0);
+
+ cfg.id = ksft_session.de_list[0].id;
+ batch.num_items = 1;
+ batch.item_sz = sizeof(cfg);
+ batch.items = (__u64)&cfg;
+
+ ASSERT_EQ(-1, ioctl(fd, SCMI_TLM_SET_DE_CFG, &batch));
+ EXPECT_EQ(EPERM, errno);
+
+ close(fd);
+}
+
+TEST_F(stlm, tlm_set_de_cfg_bad_id)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config *cfg;
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), false));
+
+ cfg = (struct scmi_tlm_de_config *)batch.items;
+ cfg[0].id = 0xffffffff;
+ cfg[0].enable = 1;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch));
+ EXPECT_EQ(ENODEV, errno);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_set_de_cfg_bad_id_with_states)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_config *cfg;
+ int *states;
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), true));
+
+ cfg = (struct scmi_tlm_de_config *)batch.items;
+ states = (int *)batch.states;
+
+ cfg[0].id = 0xffffffff;
+ cfg[0].enable = 1;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch));
+ ASSERT_LT(states[0], 0);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_set_all_cfg)
+{
+ struct scmi_tlm_batch saved_batch = {};
+ struct scmi_tlm_de_config *saved;
+ struct scmi_tlm_de_config old_all = {};
+ struct scmi_tlm_de_config new_all = {};
+ struct scmi_tlm_de_config check_all = {};
+ int i, ret;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&saved_batch, ksft_session.num_des,
+ sizeof(*saved), false));
+
+ saved = (struct scmi_tlm_de_config *)saved_batch.items;
+
+ for (i = 0; i < ksft_session.num_des; i++)
+ saved[i].id = ksft_session.de_list[i].id;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &saved_batch));
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, &old_all));
+
+ new_all.enable = !old_all.enable;
+ new_all.t_enable = 0;
+
+ ret = ioctl(self->fd, SCMI_TLM_SET_ALL_CFG, &new_all);
+ if (ret) {
+ ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &saved_batch);
+ scmi_tlm_batch_free(&saved_batch);
+ ASSERT_EQ(0, ret);
+ }
+
+ ret = ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, &check_all);
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &saved_batch));
+
+ ASSERT_EQ(0, ret);
+ ASSERT_EQ(!!new_all.enable, !!check_all.enable);
+ ASSERT_EQ(0, check_all.reserved);
+
+ TH_LOG("SCMI_TLM_SET_ALL_CFG - ena:%u t_ena:%u", check_all.enable,
+ check_all.t_enable);
+
+ scmi_tlm_batch_free(&saved_batch);
+}
+
+TEST_F(stlm, tlm_set_all_cfg_invalid_request)
+{
+ struct scmi_tlm_de_config cfg = {};
+
+ cfg.reserved = 1;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_ALL_CFG, &cfg));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_set_all_cfg_null_ptr_rejected)
+{
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_ALL_CFG, NULL));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_set_all_cfg_read_only_rejected)
+{
+ struct scmi_tlm_de_config cfg = {};
+ int fd;
+
+ fd = open("/dev/scmi/" TLM_0, O_RDONLY);
+ ASSERT_GE(fd, 0);
+ ASSERT_EQ(-1, ioctl(fd, SCMI_TLM_SET_ALL_CFG, &cfg));
+ EXPECT_EQ(EPERM, errno);
+
+ close(fd);
+}
+
+TEST_F(stlm, tlm_get_shmti_list_zero_size_query)
+{
+ struct scmi_tlm_batch batch = {};
+
+ batch.num_items = 0;
+ batch.item_sz = sizeof(struct scmi_tlm_shmti_info);
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch));
+ ASSERT_EQ(ksft_session.info.num_shmtis, batch.num_items);
+ ASSERT_EQ(sizeof(struct scmi_tlm_shmti_info), batch.item_sz);
+ ASSERT_EQ(0, batch.reserved);
+ ASSERT_EQ(0, batch.items);
+}
+
+TEST_F(stlm, tlm_get_shmti_list)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_shmti_info *shmtis;
+ int i;
+
+ if (!ksft_session.info.num_shmtis)
+ SKIP(return, "no SHMTIs available");
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, ksft_session.info.num_shmtis,
+ sizeof(*shmtis), false));
+
+ shmtis = (struct scmi_tlm_shmti_info *)batch.items;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch));
+ ASSERT_LE(batch.num_items, ksft_session.info.num_shmtis);
+
+ for (i = 0; i < batch.num_items; i++) {
+ EXPECT_EQ(0, shmtis[i].reserved);
+ EXPECT_GT(shmtis[i].len, 0);
+ EXPECT_NE(SCMI_TLM_SHMTI_ID_INVALID, shmtis[i].sid);
+
+ EXPECT_NE(-1, fcntl(shmtis[i].fd, F_GETFD));
+ EXPECT_EQ(FD_CLOEXEC, fcntl(shmtis[i].fd, F_GETFD) & FD_CLOEXEC);
+
+ TH_LOG("SCMI_TLM_GET_SHMTI_LIST - [%d] sid:%u fd:%u len:%u offset:%u",
+ i, shmtis[i].sid, shmtis[i].fd, shmtis[i].len, shmtis[i].offset);
+
+ close(shmtis[i].fd);
+ }
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_get_shmti_list_invalid_request)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_shmti_info shmti = {};
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(shmti) + 1;
+ batch.items = (__u64)&shmti;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = 1;
+ batch.item_sz = sizeof(shmti);
+ batch.reserved = 1;
+ batch.items = (__u64)&shmti;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_shmti_list_bad_items_ptr)
+{
+ struct scmi_tlm_batch batch = {};
+
+ if (!ksft_session.info.num_shmtis)
+ SKIP(return, "no SHMTIs available");
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(struct scmi_tlm_shmti_info);
+ batch.items = 0;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_get_uuid_list_zero_size_query)
+{
+ struct scmi_tlm_batch batch = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST))
+ SKIP(return, "UUID list not supported");
+
+ batch.num_items = 0;
+ batch.item_sz = sizeof(struct scmi_tlm_uuid);
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch));
+ ASSERT_EQ(sizeof(struct scmi_tlm_uuid), batch.item_sz);
+ ASSERT_EQ(0, batch.reserved);
+ ASSERT_EQ(0, batch.items);
+
+ TH_LOG("SCMI_TLM_GET_UUID_LIST - num_uuids:%u", batch.num_items);
+}
+
+TEST_F(stlm, tlm_get_uuid_list)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_uuid *uuids;
+ int i, j;
+ bool nonzero;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST))
+ SKIP(return, "UUID list not supported");
+
+ batch.num_items = 0;
+ batch.item_sz = sizeof(*uuids);
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch));
+
+ if (!batch.num_items)
+ SKIP(return, "no UUIDs available");
+
+ uuids = malloc(sizeof(*uuids) * batch.num_items);
+ ASSERT_NE(NULL, uuids);
+ bzero(uuids, sizeof(*uuids) * batch.num_items);
+
+ batch.items = (__u64)uuids;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch));
+ ASSERT_GT(batch.num_items, 0);
+ ASSERT_EQ(sizeof(*uuids), batch.item_sz);
+ ASSERT_EQ(0, batch.reserved);
+
+ for (i = 0; i < batch.num_items; i++) {
+ nonzero = false;
+
+ for (j = 0; j < SCMI_TLM_DE_IMPL_UUID_SZ; j++) {
+ if (uuids[i].bytes[j]) {
+ nonzero = true;
+ break;
+ }
+ }
+
+ EXPECT_TRUE(nonzero);
+
+ TH_LOG("SCMI_TLM_GET_UUID_LIST - [%d] %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+ i, uuids[i].bytes[0], uuids[i].bytes[1],
+ uuids[i].bytes[2], uuids[i].bytes[3],
+ uuids[i].bytes[4], uuids[i].bytes[5],
+ uuids[i].bytes[6], uuids[i].bytes[7],
+ uuids[i].bytes[8], uuids[i].bytes[9],
+ uuids[i].bytes[10], uuids[i].bytes[11],
+ uuids[i].bytes[12], uuids[i].bytes[13],
+ uuids[i].bytes[14], uuids[i].bytes[15]);
+ }
+
+ free(uuids);
+}
+
+TEST_F(stlm, tlm_get_uuid_list_invalid_request)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_uuid uuid = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST))
+ SKIP(return, "UUID list not support");
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(uuid) + 1;
+ batch.items = (__u64)&uuid;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&batch, sizeof(batch));
+ batch.num_items = 1;
+ batch.item_sz = sizeof(uuid);
+ batch.reserved = 1;
+ batch.items = (__u64)&uuid;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_F(stlm, tlm_get_uuid_list_bad_items_ptr)
+{
+ struct scmi_tlm_batch batch = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST))
+ SKIP(return, "UUID list not supported");
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(struct scmi_tlm_uuid);
+ batch.items = 0;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_reset)
+{
+ if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_RESET) ||
+ !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_RESET))
+ SKIP(return, "reset not supported");
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_RESET));
+
+ TH_LOG("SCMI_TLM_RESET");
+}
+
+TEST_F(stlm, tlm_reset_read_only_rejected)
+{
+ int fd;
+
+ if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_RESET) ||
+ !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_RESET))
+ SKIP(return, "reset not supported");
+
+ fd = open("/dev/scmi/" TLM_0, O_RDONLY);
+ ASSERT_GE(fd, 0);
+ ASSERT_EQ(-1, ioctl(fd, SCMI_TLM_RESET));
+ EXPECT_EQ(EPERM, errno);
+
+ close(fd);
+}
+
+TEST_F(stlm, tlm_event_subscribe)
+{
+ struct scmi_tlm_event evt = {};
+ int efd;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT))
+ SKIP(return, "events not supported");
+
+ efd = eventfd(0, EFD_CLOEXEC);
+ ASSERT_GE(efd, 0);
+
+ evt.type = SCMI_TLM_EVT_GENERATION;
+ evt.efd = efd;
+ evt.cookie = 0;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt));
+ ASSERT_GT(evt.cookie, 0);
+ ASSERT_EQ(0, evt.pad);
+ ASSERT_EQ(0, evt.reserved);
+
+ TH_LOG("SCMI_TLM_EVENT_SUBSCRIBE - type:%u cookie:%u", evt.type,
+ evt.cookie);
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt));
+
+ close(efd);
+}
+
+TEST_F(stlm, tlm_event_subscribe_invalid_request)
+{
+ struct scmi_tlm_event evt = {};
+ int efd;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT))
+ SKIP(return, "events not supported");
+
+ efd = eventfd(0, EFD_CLOEXEC);
+ ASSERT_GE(efd, 0);
+
+ evt.type = SCMI_TLM_EVT_GENERATION;
+ evt.efd = efd;
+ evt.pad = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt));
+ EXPECT_EQ(EINVAL, errno);
+
+ bzero(&evt, sizeof(evt));
+ evt.type = SCMI_TLM_EVT_GENERATION;
+ evt.efd = efd;
+ evt.reserved = 1;
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt));
+ EXPECT_EQ(EINVAL, errno);
+
+ close(efd);
+}
+
+TEST_F(stlm, tlm_event_subscribe_bad_type)
+{
+ struct scmi_tlm_event evt = {};
+ int efd;
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT))
+ SKIP(return, "events not supported");
+
+ efd = eventfd(0, EFD_CLOEXEC);
+ ASSERT_GE(efd, 0);
+
+ evt.type = SCMI_TLM_EVT_LAST + 1;
+ evt.efd = efd;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt));
+ EXPECT_EQ(ENODEV, errno);
+
+ close(efd);
+}
+
+TEST_F(stlm, tlm_event_subscribe_bad_eventfd)
+{
+ struct scmi_tlm_event evt = {};
+
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT))
+ SKIP(return, "events not supported");
+
+ evt.type = SCMI_TLM_EVT_GENERATION;
+ evt.efd = -1;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt));
+ EXPECT_EQ(EBADF, errno);
+}
+
+TEST_F(stlm, tlm_event_subscribe_null_ptr_rejected)
+{
+ if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT))
+ SKIP(return, "events not supported");
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, NULL));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_HARNESS_MAIN /* generates main() */
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v17 34/34] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (32 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 33/34] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases Cristian Marussi
@ 2026-11-24 19:48 ` Cristian Marussi
2026-11-24 22:53 ` [PATCH v17 00/34] Introduce SCMI Telemetry support Jonathan Cameron
34 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-11-24 19:48 UTC (permalink / raw)
To: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc
Cc: sudeep.holla, james.quinlan, f.fainelli, vincent.guittot,
etienne.carriere, peng.fan, michal.simek, d-gole,
jonathan.cameron, elif.topuz, lukasz.luba, philip.radford, david,
souvik.chakravarty, leitao, kas, puranjay, usama.arif,
kernel-team, Cristian Marussi
Add a testing tool that exercises the SCMI ioctls UAPI interface: as of
now the tool simply queries the initial state of the SCMI Telemetry
subsystem, tries to enable all the existent Data Events and dumps all
the Telemetry data.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v15v --> v16
- handle new abi_features GROUPS/SINGLE_SAMPLE
---
MAINTAINERS | 1 +
tools/testing/scmi/Makefile | 25 +
tools/testing/scmi/stlm.c | 1698 ++++++++++++++++++++++++++++++
tools/testing/scmi/stlm_decode.c | 252 +++++
tools/testing/scmi/stlm_decode.h | 7 +
5 files changed, 1983 insertions(+)
create mode 100644 tools/testing/scmi/Makefile
create mode 100644 tools/testing/scmi/stlm.c
create mode 100644 tools/testing/scmi/stlm_decode.c
create mode 100644 tools/testing/scmi/stlm_decode.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 418c53f2ddd3..aa87f805e448 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -27340,6 +27340,7 @@ F: include/linux/sc[mp]i_protocol.h
F: include/trace/events/scmi.h
F: include/uapi/linux/scmi.h
F: include/uapi/linux/virtio_scmi.h
+F: tools/testing/scmi/
SYSTEM CONTROL MANAGEMENT INTERFACE (SCMI) i.MX Extension Message Protocol drivers
M: Peng Fan <peng.fan@nxp.com>
diff --git a/tools/testing/scmi/Makefile b/tools/testing/scmi/Makefile
new file mode 100644
index 000000000000..790ff8086489
--- /dev/null
+++ b/tools/testing/scmi/Makefile
@@ -0,0 +1,25 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+CC=$(CROSS_COMPILE)gcc
+OBJS = stlm.o stlm_decode.o
+
+CFLAGS=-Wall -static -std=gnu11 -I ../../../include/uapi/
+ifneq ($(DEBUG), )
+ CFLAGS+=-O0 -g -ggdb
+else
+ CFLAGS+=-static
+endif
+
+all: stlm
+
+stlm: $(OBJS)
+ $(CC) $(CFLAGS) $^ -o $@
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c $<
+
+clean:
+ rm -f *.o
+ rm -f stlm
+
+.PHONY: clean
diff --git a/tools/testing/scmi/stlm.c b/tools/testing/scmi/stlm.c
new file mode 100644
index 000000000000..76f41cb170ec
--- /dev/null
+++ b/tools/testing/scmi/stlm.c
@@ -0,0 +1,1698 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SCMI Telemetry Test Tool
+ *
+ * Copyright (C) 2026 ARM Ltd.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/eventfd.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/signalfd.h>
+
+#include <unistd.h>
+
+#include <linux/scmi.h>
+
+#include "stlm_decode.h"
+
+#define IOCTL_ERR_STR(_ioctl) "IOCTL:" #_ioctl
+#define MIN_TDCF_SZ_V1 24
+
+#define USAGE_STRING \
+ "\t[OPTIONS]\n" \
+ "\t -i <instance_path>\t\tUse this device instance path. Default: /dev/scmi/tlm_0\n" \
+ "\t[COMMANDS]\n" \
+ "\t info [full] - Display summarized (or full) configuration and status data\n" \
+ "\t query - Query ALL possible IOCTLs for resources (item=0)\n" \
+ "\t uuids - UUIDs dump\n" \
+ "\t monitor [detach] - Monitor Generation counter\n" \
+ "\t shmti [check|dump [<sid>]|decode [<sid>]] - SHMTI operations\n" \
+ "\t\t check - Sanity check of all SHMTis\n" \
+ "\t\t dump [<sid>] - Binary dump of <sid> or all SHMTI areas\n" \
+ "\t\t decode [<sid>] - Line-by-line decode of <sid> or all SHMTI areas\n" \
+ "\t des [info|data] [<de_id>] - Display detailed DEs info, config or data\n" \
+ "\t groups - Display detailed GROUPS info and config data\n" \
+ "\t cfg [SUB-COMMANDS] - Configure Telemetry\n" \
+ "\t\t tlm on|off - Enable or disable Telemetry as a whole\n" \
+ "\t\t update <msecs> - Set global update interval\n" \
+ "\t\t all_des [[on|off]:[on|off]] - Enable or disable ALL DEs with or wout TS\n" \
+ "\t\t de [<de_id> [[on|off]:[on|off]]] - Get/Set configuration of one or ALL de\n" \
+ "\t\t group [<grp_id>] [[on|off]:[on|off]:[<update_msecs>]] - Get/Set GROUP configuration\n" \
+ "\t single - Perform a single sample read\n" \
+ "\t bulk - Perform a bulk read\n" \
+ "\t batch <de_id> [de_id [de_id]... ] - Perform a batch read of the DE IDs listed in input\n" \
+ "\t reset - Perform a Telemetry subsytem reset platform-side\n"
+
+struct tlm_de {
+ struct scmi_tlm_de_info *info;
+ struct scmi_tlm_de_config cfg;
+ struct scmi_tlm_de_sample sample;
+};
+
+struct tlm_group {
+ int fd;
+ struct scmi_tlm_grp_info *info;
+ struct scmi_tlm_grp_desc *desc;
+ struct scmi_tlm_intervals *ivs;
+};
+
+struct tlm_state {
+ int fd;
+ const char *path;
+ struct scmi_tlm_abi_info info;
+ struct scmi_tlm_config cfg;
+ struct scmi_tlm_intervals *ivs;
+ struct scmi_tlm_batch *shmtis;
+ unsigned int num_des;
+ struct tlm_de *des;
+ unsigned int num_groups;
+ struct tlm_group *grps;
+};
+
+struct parsed_args {
+ const char *instance;
+ const char *cmd;
+ unsigned int cnt;
+ char **opts;
+};
+
+static const char *argv0;
+
+static void gather_tlm_res_state(struct tlm_state *st);
+
+static inline void dump_des_info(struct tlm_state *st, uint32_t *de_id)
+{
+ gather_tlm_res_state(st);
+
+ fprintf(stdout, "\n+ %s #%d DEs:\n",
+ !de_id ? "Found" : "Lookup",
+ !de_id ? st->num_des : 1);
+
+ for (int i = 0; i < st->num_des; i++) {
+ if (de_id && st->des[i].info->id != *de_id)
+ continue;
+
+ fprintf(stdout, "\t[0x%08X][%s]\n",
+ st->des[i].info->id, st->des[i].info->name);
+ fprintf(stdout, "\t\tGRP_ID:0x%08X SZ:%u TYPE:%u UNIT:%u UNIT_EXP:%d TS_RATE:%d\n",
+ st->des[i].info->grp_id, st->des[i].info->data_sz,
+ st->des[i].info->type, st->des[i].info->unit,
+ st->des[i].info->unit_exp, st->des[i].info->ts_rate);
+ fprintf(stdout, "\t\tINST_ID:%u COMPO_ID:%u COMPO_TYPE:%u PERSIST:%u\n\n",
+ st->des[i].info->instance_id, st->des[i].info->compo_instance_id,
+ st->des[i].info->compo_type, st->des[i].info->persistent);
+ }
+
+ fprintf(stdout, "\n");
+}
+
+static void dump_single_de_data(struct tlm_state *st, uint32_t *de_id)
+{
+ struct tlm_de de = {};
+
+ de.sample.id = *de_id;
+ if (ioctl(st->fd, SCMI_TLM_DE_READ, &de.sample)) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_DE_READ));
+ return;
+ }
+
+ fprintf(stdout, "\t[0x%08X]\t\t -->> TS:%16llu %016llX\n",
+ de.sample.id, de.sample.tstamp, de.sample.val);
+}
+
+static void dump_des_data(struct tlm_state *st, uint32_t *de_id)
+{
+ if (de_id)
+ return dump_single_de_data(st, de_id);
+
+ gather_tlm_res_state(st);
+
+ for (int i = 0; i < st->num_des; i++) {
+ struct tlm_de *de = &st->des[i];
+ int ret;
+
+ de->sample.id = de->info->id;
+ ret = ioctl(st->fd, SCMI_TLM_DE_READ, &de->sample);
+ if (ret) {
+ if (errno != ENODATA) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_DE_READ));
+ continue;
+ }
+ }
+
+ fprintf(stdout, "\t[0x%08X][%16s] --- %s/%s\n\t\t-->> TS:%16llu %016llX\n",
+ de->info->id, de->info->name,
+ de->cfg.enable ? "ON" : "OFF",
+ de->cfg.t_enable ? "TS_ON " : "TS_OFF",
+ de->sample.tstamp, de->sample.val);
+ }
+
+ fprintf(stdout, "\n");
+}
+
+static void dump_des(struct tlm_state *st, const char *action, uint32_t *de_id)
+{
+ if (!action) {
+ dump_des_info(st, de_id);
+ dump_des_data(st, de_id);
+ } else if (!strncmp(action, "info", 4)) {
+ dump_des_info(st, de_id);
+ } else if (!strncmp(action, "data", 4)) {
+ dump_des_data(st, de_id);
+ }
+}
+
+static inline void usage(const char *program_name)
+{
+ printf("Usage: %s [OPTIONS] [CMD [CMD_OPTS]...]\n", program_name);
+ printf("%s", USAGE_STRING);
+}
+
+static int cmd_unknown(const char *cmd)
+{
+ fprintf(stderr, "Unknown command '%s'\n", cmd);
+ usage(argv0);
+
+ return EXIT_FAILURE;
+}
+
+static inline int cmd_dump_des(struct tlm_state *st, struct parsed_args *args)
+{
+ uint32_t id, *de = NULL;
+ const char *action = NULL;
+
+ if (args && args->cnt) {
+ const char *idstr = NULL;
+
+ if (!strncmp(args->opts[0], "info", 4) ||
+ !strncmp(args->opts[0], "data", 4))
+ action = args->opts[0];
+
+ idstr = !action ? args->opts[0] : ((args->cnt > 1) ? args->opts[1] : NULL);
+ if (idstr) {
+ id = strtoul(idstr, NULL, 0);
+ if (id == ULONG_MAX || errno == EINVAL) {
+ fprintf(stderr, "\n+ BAD DE_ID: %s\n", idstr);
+ return 0;
+ }
+ de = &id;
+ }
+ }
+
+ dump_des(st, action, de);
+
+ return 0;
+}
+
+static inline int cmd_dump_groups(struct tlm_state *st)
+{
+ gather_tlm_res_state(st);
+
+ fprintf(stdout, "\n+ Found %d GRPs: ", st->num_groups);
+ for (int i = 0; i < st->num_groups; i++) {
+ struct scmi_tlm_update_interval *grp_intrvs;
+ struct tlm_group *grp = &st->grps[i];
+ uint32_t *composing_des;
+
+ fprintf(stdout, "\n\tGRP_ID:%d DES#:%d INTRVS#:%d %s\n",
+ grp->info->grp_id, grp->info->num_des,
+ grp->info->num_intervals,
+ !grp->ivs ? "(---)" :
+ ((grp->ivs->flags & SCMI_TLM_INTERV_DISCRETE) ? "(DISCRETE)" : ""));
+
+ fprintf(stdout, "\tGRP Intervals:\n");
+ if (grp->ivs) {
+ grp_intrvs = (struct scmi_tlm_update_interval *)grp->ivs->intervals;
+ for (int j = 0; j < grp->ivs->num_intervals; j++)
+ fprintf(stdout, "\t[%d]::%u,%d\n", j,
+ grp_intrvs[j].secs, grp_intrvs[j].exp);
+ }
+
+ composing_des = (uint32_t *)grp->desc->composing_des;
+ fprintf(stdout, "\tCOMPOSING_DES:");
+ for (int j = 0; j < grp->desc->num_des; j++)
+ fprintf(stdout, "0x%08X ", composing_des[j]);
+ }
+ fprintf(stdout, "\n");
+
+ return 0;
+}
+
+static void print_uuid(const char *prefix, void *arg)
+{
+ unsigned char *uuid = arg;
+
+ fprintf(stdout, "%s", prefix);
+ for (int i = 0; i < SCMI_TLM_DE_IMPL_UUID_SZ; i++) {
+ fprintf(stdout, "%02X", uuid[i]);
+ switch (i) {
+ case 3:
+ case 5:
+ case 7:
+ case 9:
+ fprintf(stdout, "%c", '-');
+ break;
+ case 15:
+ fprintf(stdout, "%c", '\n');
+ break;
+ }
+ }
+}
+
+static inline int cmd_dump_state(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_update_interval *intervals;
+ struct scmi_tlm_shmti_info *shinfo;
+
+ fprintf(stdout, "- SYSTEM TELEMETRY @instance: %s\n\n", st->path);
+ fprintf(stdout, "+ ABI Version: %u\n", st->info.abi_version);
+ fprintf(stdout, "+ ABI Features: 0x%04X\n", st->info.abi_features);
+ fprintf(stdout, "\t (RESET:%u",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_RESET));
+ fprintf(stdout, " EVENT:%u",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_EVENT));
+ fprintf(stdout, " UUID_LIST:%u",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST));
+ fprintf(stdout, " BATCH_STATE:%u",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_BATCH_STATE));
+ fprintf(stdout, " BATCHED_CFG:%u",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_BATCHED_CFG));
+ fprintf(stdout, " DE_TRACKING:%u",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_DE_TRACKING));
+ fprintf(stdout, " GROUPS:%u",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS));
+ fprintf(stdout, " SINGLE_SAMPLE:%u)\n",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE));
+ fprintf(stdout, "+ SCMI Features: 0x%04X\n", st->info.features);
+ fprintf(stdout, "\t (RESET:%u",
+ !!(st->info.features & SCMI_TLM_SCMI_SUPPORT_RESET));
+ fprintf(stdout, " SINGLE_SAMPLE:%u",
+ !!(st->info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE));
+ fprintf(stdout, " GROUP_CONFIG:%u",
+ !!(st->info.features & SCMI_TLM_SCMI_SUPPORT_GROUP_CONFIG));
+ fprintf(stdout, " UPDATE_NOTIFICATIONS:%u)\n",
+ !!(st->info.features & SCMI_TLM_SCMI_SUPPORT_UPDATE_NOTIFICATION));
+ fprintf(stdout, "+ DEs#: %d\n", st->info.num_des);
+ fprintf(stdout, "+ GRPS#: %d\n", st->info.num_groups);
+ fprintf(stdout, "+ INTRV#: %d %s\n", st->info.num_intervals,
+ (st->ivs->flags & SCMI_TLM_INTERV_DISCRETE) ? "(DISCRETE)" : "");
+ fprintf(stdout, "+ SHMTIS#: %d\n", st->info.num_shmtis);
+
+ print_uuid("+ Primary UUID: ", &st->info.primary_de_impl_version[0]);
+ fprintf(stdout, "\n+ TLM_ENABLED: %d\n", st->cfg.enable);
+ fprintf(stdout, "+ CURRENT_UPDATE_INTERVAL: %u,%d\n",
+ st->cfg.active.secs, st->cfg.active.exp);
+
+ fprintf(stdout, "\n+ Found #%u Global Update Intervals\n",
+ st->ivs->num_intervals);
+ intervals = (struct scmi_tlm_update_interval *)st->ivs->intervals;
+ for (int i = 0; i < st->ivs->num_intervals; i++)
+ fprintf(stdout, "\t[%d]::%u,%d\n", i,
+ intervals[i].secs, intervals[i].exp);
+
+ fprintf(stdout, "\n+ Found #%u SHMTI areas\n",
+ st->shmtis ? st->shmtis->num_items : 0);
+ if (st->shmtis) {
+ shinfo = (struct scmi_tlm_shmti_info *)st->shmtis->items;
+ for (int i = 0; i < st->shmtis->num_items; i++)
+ fprintf(stdout, "\t[%d]:: FD:%u LEN:%u\n",
+ shinfo[i].sid, shinfo[i].fd, shinfo[i].len);
+ }
+
+ if (args && args->cnt && args->opts &&
+ !strncmp(args->opts[0], "full", strlen(args->opts[0]))) {
+ cmd_dump_des(st, NULL);
+ cmd_dump_groups(st);
+
+ if (st->info.num_des != st->num_des) {
+ fprintf(stdout, "\n++++++ DES NOT FULLY_ENUMERATED ++++++\n");
+ fprintf(stdout, "+++ DECLARED:%u ENUMERATED:%u +++\n",
+ st->info.num_des, st->num_des);
+ }
+ }
+
+ return 0;
+}
+
+static int discover_base_info(int fd, struct scmi_tlm_abi_info *info)
+{
+ int ret;
+
+ info->size = sizeof(*info);
+ ret = ioctl(fd, SCMI_TLM_GET_ABI_INFO, info);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_ABI_INFO));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int
+query_items(int fd, unsigned long ioctl_op, size_t item_sz, const char *tag)
+{
+ struct scmi_tlm_batch query = {};
+ int ret;
+
+ query.item_sz = item_sz;
+ ret = ioctl(fd, ioctl_op, &query);
+ if (ret < 0) {
+ perror(tag);
+ return -errno;
+ }
+
+ fprintf(stdout, "--- [QUERY]:[%s] -> NUM_ITEMS:%u\n",
+ tag, query.num_items);
+
+ return 0;
+}
+
+static inline struct scmi_tlm_batch *
+batch_buffer_alloc(int num_items, size_t item_sz, bool need_status)
+{
+ struct scmi_tlm_batch *batch;
+ size_t batch_sz, all_items_sz;
+ void *items;
+
+ batch_sz = sizeof(*batch);
+ batch = malloc(batch_sz);
+ if (!batch)
+ return NULL;
+
+ bzero(batch, batch_sz);
+ batch->num_items = num_items;
+
+ batch->item_sz = item_sz;
+ all_items_sz = batch->num_items * batch->item_sz;
+ items = malloc(all_items_sz);
+ if (!items) {
+ free(batch);
+ return NULL;
+ }
+
+ bzero(items, all_items_sz);
+ batch->items = (uint64_t)items;
+
+ if (need_status) {
+ int *states;
+ size_t states_sz = sizeof(*states) * batch->num_items;
+
+ states = malloc(states_sz);
+ if (!states) {
+ free(items);
+ free(batch);
+ return NULL;
+ }
+
+ bzero(states, states_sz);
+ batch->states = (uint64_t)states;
+ }
+
+ return batch;
+}
+
+static struct scmi_tlm_batch *scmi_get_des_list(int fd, int num_des)
+{
+ struct scmi_tlm_de_info *dei;
+ struct scmi_tlm_batch *batch;
+ int ret;
+
+ batch = batch_buffer_alloc(num_des, sizeof(*dei), false);
+ if (!batch)
+ return NULL;
+
+ ret = ioctl(fd, SCMI_TLM_GET_DE_LIST, batch);
+ if (ret) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_LIST));
+ free(batch);
+ return NULL;
+ }
+
+ return batch;
+}
+
+static struct tlm_de *enumerate_des(struct tlm_state *st)
+{
+ struct scmi_tlm_batch *des_batch;
+ struct tlm_de *des;
+ int num_items;
+
+ des_batch = scmi_get_des_list(st->fd, st->info.num_des);
+ if (!des_batch)
+ return NULL;
+
+ num_items = des_batch->num_items;
+ des = malloc(sizeof(*des) * num_items);
+ if (!des) {
+ free(des_batch);
+ return NULL;
+ }
+
+ bzero(des, sizeof(*des) * num_items);
+
+ for (int i = 0; i < num_items; i++) {
+ struct tlm_de *de = &des[i];
+ struct scmi_tlm_batch batch = {};
+ int ret;
+
+ de->info = &(((struct scmi_tlm_de_info *)des_batch->items)[i]);
+ de->cfg.id = de->info->id;
+
+ batch.num_items = 1;
+ batch.item_sz = sizeof(de->cfg);
+ batch.items = (uint64_t)&de->cfg;
+
+ ret = ioctl(st->fd, SCMI_TLM_GET_DE_CFG, &batch);
+ if (ret) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_CFG));
+ free(des);
+ free(des_batch);
+ return NULL;
+ }
+ }
+
+ /* On failure st->num_des remains zero */
+ st->num_des = num_items;
+
+ return des;
+}
+
+static int get_current_config(int fd, struct scmi_tlm_config *cfg)
+{
+ int ret;
+
+ ret = ioctl(fd, SCMI_TLM_GET_CFG, cfg);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_CFG));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static struct scmi_tlm_batch *scmi_get_grps_list(int fd, int num_groups)
+{
+ struct scmi_tlm_batch *batch;
+ int ret;
+
+ batch = batch_buffer_alloc(num_groups, sizeof(struct scmi_tlm_grp_info),
+ false);
+ if (!batch)
+ return NULL;
+
+ ret = ioctl(fd, SCMI_TLM_GET_GRP_LIST, batch);
+ if (ret) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_GRP_LIST));
+ free(batch);
+ return NULL;
+ }
+
+ return batch;
+}
+
+static struct scmi_tlm_intervals *
+enumerate_intervals(int fd, int num_intervals, unsigned int *grp_id)
+{
+ struct scmi_tlm_update_interval *intervs;
+ struct scmi_tlm_intervals *ivs;
+ size_t sz, i_sz;
+ int ret;
+
+ sz = sizeof(*ivs);
+ ivs = malloc(sz);
+ if (!ivs)
+ return NULL;
+
+ bzero(ivs, sz);
+
+ i_sz = sizeof(*intervs) * num_intervals;
+ intervs = malloc(i_sz);
+ if (!intervs) {
+ free(ivs);
+ return NULL;
+ }
+
+ bzero(intervs, i_sz);
+
+ ivs->num_intervals = num_intervals;
+ ivs->intervals = (uint64_t)intervs;
+ if (grp_id) {
+ ivs->grp_id = *grp_id;
+ ivs->flags = SCMI_TLM_INTERV_GROUP;
+ }
+
+ ret = ioctl(fd, SCMI_TLM_GET_INTRVS, ivs);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_INTRVS));
+ free(ivs);
+ free(intervs);
+ return NULL;
+ }
+
+ return ivs;
+}
+
+static struct scmi_tlm_batch *enumerate_shmtis(struct tlm_state *st)
+{
+ struct scmi_tlm_batch *batch;
+ int ret;
+
+ batch = batch_buffer_alloc(st->info.num_shmtis,
+ sizeof(struct scmi_tlm_shmti_info), false);
+ if (!batch)
+ return NULL;
+
+ ret = ioctl(st->fd, SCMI_TLM_GET_SHMTI_LIST, batch);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_SHMTI_LIST));
+ free(batch);
+ return NULL;
+ }
+
+ return batch;
+}
+
+static struct tlm_group *enumerate_groups(struct tlm_state *st)
+{
+ struct scmi_tlm_batch *grps_batch;
+ struct tlm_group *grps;
+ int num_items;
+
+ grps_batch = scmi_get_grps_list(st->fd, st->info.num_groups);
+ if (!grps_batch)
+ return NULL;
+
+ num_items = grps_batch->num_items;
+ grps = malloc(sizeof(*grps) * num_items);
+ if (!grps) {
+ free(grps_batch);
+ return NULL;
+ }
+
+ bzero(grps, sizeof(*grps) * num_items);
+ for (int i = 0; i < num_items; i++) {
+ struct tlm_group *grp = &grps[i];
+ size_t size, composing_sz;
+ uint32_t *composing_des;
+ int ret;
+
+ grp->info = &(((struct scmi_tlm_grp_info *)grps_batch->items)[i]);
+ size = sizeof(*grp->desc);
+ grp->desc = malloc(size);
+ if (!grp->desc) {
+ free(grps);
+ free(grps_batch);
+ return NULL;
+ }
+
+ bzero(grp->desc, size);
+ composing_sz = sizeof(uint32_t) * grp->info->num_des;
+ composing_des = malloc(composing_sz);
+ if (!composing_des) {
+ free(grp->desc);
+ free(grps);
+ free(grps_batch);
+ return NULL;
+ }
+
+ grp->desc->grp_id = grp->info->grp_id;
+ grp->desc->num_des = grp->info->num_des;
+ grp->desc->composing_des = (uint64_t)composing_des;
+ ret = ioctl(st->fd, SCMI_TLM_GET_GRP_DESC, grp->desc);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_GRP_DESC));
+ free(composing_des);
+ free(grp->desc);
+ free(grps);
+ free(grps_batch);
+ return NULL;
+ }
+
+ grp->ivs = enumerate_intervals(st->fd, grp->info->num_intervals,
+ &grp->info->grp_id);
+ }
+
+ /* On failure st->num_groups remains zero */
+ st->num_groups = num_items;
+
+ return grps;
+}
+
+static void gather_tlm_res_state(struct tlm_state *st)
+{
+ if (st->info.num_des && !st->des) {
+ st->des = enumerate_des(st);
+ if (!st->des)
+ fprintf(stderr, "Failed to enumerate DES\n");
+ }
+
+ /* Enumerate even though GROUPS NOT supported */
+ if (st->info.num_groups && !st->grps) {
+ st->grps = enumerate_groups(st);
+ if (!st->grps)
+ fprintf(stderr, "Failed to enumerate GROUPS\n");
+ }
+}
+
+static int gather_tlm_state(struct tlm_state *st)
+{
+ int ret;
+
+ ret = discover_base_info(st->fd, &st->info);
+ if (ret)
+ return ret;
+
+ st->ivs = enumerate_intervals(st->fd, st->info.num_intervals, NULL);
+ if (!st->ivs) {
+ fprintf(stderr, "Failed to enumerate Global Intervals. Abort.\n");
+ return -1;
+ }
+
+ ret = get_current_config(st->fd, &st->cfg);
+ if (ret)
+ return ret;
+
+ if (st->info.num_shmtis) {
+ st->shmtis = enumerate_shmtis(st);
+ if (!st->shmtis)
+ fprintf(stderr, "Failed to enumerate SHMTIS\n");
+ }
+
+ return 0;
+}
+
+static struct tlm_state *open_session(const char *path)
+{
+ struct tlm_state *st;
+
+ st = malloc(sizeof(*st));
+ if (!st)
+ return NULL;
+
+ bzero(st, sizeof(*st));
+ st->fd = open(path, O_RDWR);
+ if (st->fd < 0) {
+ perror("open");
+ free(st);
+ return NULL;
+ }
+
+ st->path = path;
+
+ return st;
+}
+
+static ssize_t tdcf_dump(const void *tdcf, size_t len)
+{
+ ssize_t read_bytes = 0;
+
+ do {
+ ssize_t bytes;
+
+ bytes = write(1, tdcf + read_bytes, len - read_bytes);
+ if (bytes < 0) {
+ read_bytes = -1;
+ break;
+ }
+ read_bytes += bytes;
+ } while (read_bytes < len);
+
+ return read_bytes;
+}
+
+static int dump_shmti(struct scmi_tlm_shmti_info *shmti, bool decode)
+{
+ ssize_t read_bytes = 0;
+ void *tdcf;
+ unsigned int len;
+
+ if (shmti->len < MIN_TDCF_SZ_V1) {
+ fprintf(stderr,
+ "Skipping malformed SHMTI - SID:%u LEN:%u OFFSET:0x%X\n",
+ shmti->sid, shmti->len, shmti->offset);
+ return -1;
+ }
+
+ len = shmti->len + shmti->offset;
+ tdcf = mmap(NULL, len, PROT_READ, MAP_SHARED, shmti->fd, 0);
+ if (tdcf == MAP_FAILED) {
+ fprintf(stderr, "%s\n", strerror(errno));
+ return -1;
+ }
+
+ if (decode)
+ read_bytes = tdcf_decode(tdcf + shmti->offset, shmti->len);
+ else
+ read_bytes = tdcf_dump(tdcf + shmti->offset, shmti->len);
+
+ munmap(tdcf, len);
+
+ return read_bytes;
+}
+
+static int shmti_dump(struct tlm_state *st, struct parsed_args *args, bool decode)
+{
+ struct scmi_tlm_shmti_info *shmti;
+ unsigned long first, last;
+
+ if (!st->shmtis || !st->shmtis->num_items)
+ return 0;
+
+ if (args->cnt > 1) {
+ first = last = strtoul(args->opts[1], NULL, 0);
+ if (first == ULONG_MAX || errno == EINVAL ||
+ first >= st->shmtis->num_items) {
+ fprintf(stderr, "Invalid SHMTI ID: %lu\n", first);
+ return -1;
+ }
+ } else {
+ first = 0;
+ last = st->shmtis->num_items - 1;
+ }
+
+ for (int sid = first; sid <= last; sid++) {
+ int ret;
+
+ shmti = &(((struct scmi_tlm_shmti_info *)(st->shmtis->items))[sid]);
+ fprintf(stderr, "\nBinary %s of SHMTI[%u] LEN:%u\n",
+ decode ? "DECODE" : "DUMP", shmti->sid, shmti->len);
+
+ ret = dump_shmti(shmti, decode);
+ if (!ret)
+ fprintf(stderr, "...FAILED DUMP of SHMTI[%u] !!!\n", sid);
+ }
+
+ return 0;
+}
+
+static int shmti_check(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_shmti_info *shmtis;
+
+ if (!st->shmtis)
+ return 0;
+
+ shmtis = (struct scmi_tlm_shmti_info *)st->shmtis->items;
+
+ fprintf(stdout, "\n- SHMTI mmap SANITY CHECK -\n-------------------\n");
+
+ for (int i = 0; i < st->shmtis->num_items; i++) {
+ unsigned long *start, *end;
+ unsigned int len;
+ void *tdcf;
+ char *buf;
+
+ if (shmtis[i].len < MIN_TDCF_SZ_V1) {
+ fprintf(stdout,
+ "Skipping malformed SHMTI[%u] - SID:%u LEN:%u OFFSET:0x%X\n",
+ i, shmtis[i].sid, shmtis[i].len, shmtis[i].offset);
+ continue;
+ }
+
+ fprintf(stdout, "Mapping SHMTI[%u] - SID:%u LEN:%u FD:%u OFFS:0x%X\n",
+ i, shmtis[i].sid, shmtis[i].len, shmtis[i].fd, shmtis[i].offset);
+
+ len = shmtis[i].len + shmtis[i].offset;
+ tdcf = mmap(NULL, len, PROT_READ, MAP_SHARED, shmtis[i].fd, 0);
+ if (tdcf == MAP_FAILED) {
+ fprintf(stderr, "%s\n", strerror(errno));
+ return -1;
+ }
+
+ start = tdcf + shmtis[i].offset;
+ end = (void *)start + shmtis[i].len;
+
+ fprintf(stdout, "-----------------------------------------------------\n");
+ fprintf(stdout, "Mapped SHMTI - SID:%u %u@%p\n",
+ shmtis[i].sid, shmtis[i].offset, tdcf);
+
+ buf = (char *)start;
+ for (int j = 0; j < 4; j++)
+ fprintf(stdout, "%c ", buf[j]);
+
+ fprintf(stdout, " ===> ");
+
+ buf = ((char *)end) - 4;
+ for (int j = 0 ; j < 4; j++)
+ fprintf(stdout, "%c ", buf[j]);
+ fprintf(stdout, "\n-----------------------------------------------------\n");
+
+ munmap(tdcf, len);
+ }
+
+ return 0;
+}
+
+static int cmd_shmti_ops(struct tlm_state *st, struct parsed_args *args)
+{
+ if (!args->cnt || !args->opts)
+ return cmd_unknown("_");
+
+ if (!strncmp(args->opts[0], "dump", strlen(args->opts[0])))
+ return shmti_dump(st, args, false);
+ if (!strncmp(args->opts[0], "check", strlen(args->opts[0])))
+ return shmti_check(st, args);
+ if (!strncmp(args->opts[0], "decode", strlen(args->opts[0])))
+ return shmti_dump(st, args, true);
+
+ return cmd_unknown(args->opts[0]);
+}
+
+static int parse_options(int argc, char **argv, struct parsed_args *args)
+{
+ int opt = 0;
+
+ /* Set defaults */
+ args->instance = "/dev/scmi/tlm_0";
+ while ((opt = getopt(argc, argv, "i:h")) != -1) {
+ switch (opt) {
+ case 'i':
+ args->instance = optarg;
+ break;
+ case 'h':
+ default:
+ return -EINVAL;
+ }
+ }
+
+ if (optind <= argc) {
+ if (optind < argc - 1) {
+ args->cnt = argc - 1 - optind;
+ args->opts = malloc(sizeof(*args->opts) * args->cnt);
+ if (!args->opts)
+ return -1;
+ }
+
+ args->cmd = argv[optind];
+ for (int i = 0, j = optind + 1; j < argc; i++, j++)
+ args->opts[i] = argv[j];
+ }
+
+ return 0;
+}
+
+static inline struct scmi_tlm_data_read *bulk_buffer_alloc(struct tlm_state *st)
+{
+ struct scmi_tlm_de_sample *samples;
+ struct scmi_tlm_data_read *bulk;
+ size_t bulk_sz, samples_sz;
+
+ bulk_sz = sizeof(*bulk);
+ bulk = malloc(bulk_sz);
+ if (!bulk)
+ return NULL;
+
+ bzero(bulk, bulk_sz);
+ samples_sz = st->info.num_des * sizeof(*samples);
+ samples = malloc(samples_sz);
+ if (!samples) {
+ free(bulk);
+ return NULL;
+ }
+
+ bzero(samples, samples_sz);
+ bulk->samples = (uint64_t)samples;
+ bulk->num_samples = st->info.num_des;
+
+ return bulk;
+}
+
+static int cmd_single_read(struct tlm_state *st)
+{
+ struct scmi_tlm_de_sample *samples;
+ struct scmi_tlm_data_read *bulk;
+ int ret;
+
+ bulk = bulk_buffer_alloc(st);
+ if (!bulk)
+ return -1;
+
+ ret = ioctl(st->fd, SCMI_TLM_SINGLE_READ, bulk);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_SINGLE_READ));
+ return -errno;
+ }
+
+ fprintf(stdout, "\n- Single ASYNC read -\n-------------------\n");
+ samples = (struct scmi_tlm_de_sample *)bulk->samples;
+ for (int i = 0; i < bulk->num_samples; i++)
+ fprintf(stdout, "0x%08X %016llu %016llX\n",
+ samples[i].id, samples[i].tstamp, samples[i].val);
+
+ return 0;
+}
+
+static int cmd_bulk_read(struct tlm_state *st)
+{
+ struct scmi_tlm_de_sample *samples;
+ struct scmi_tlm_data_read *bulk;
+ int ret;
+
+ bulk = bulk_buffer_alloc(st);
+ if (!bulk)
+ return -1;
+
+ ret = ioctl(st->fd, SCMI_TLM_BULK_READ, bulk);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_BULK_READ));
+ return -errno;
+ }
+
+ fprintf(stdout, "\n- BULK read -\n-------------------\n");
+ samples = (struct scmi_tlm_de_sample *)bulk->samples;
+ for (int i = 0; i < bulk->num_samples; i++)
+ fprintf(stdout, "0x%08X %016llu %016llX\n",
+ samples[i].id, samples[i].tstamp, samples[i].val);
+
+ return 0;
+}
+
+static int cmd_tlm_reset(struct tlm_state *st)
+{
+ int ret;
+
+ ret = ioctl(st->fd, SCMI_TLM_RESET);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_RESET));
+ return -errno;
+ }
+
+ fprintf(stdout, "\n--- TLM RESET ISSUED\n");
+
+ return 0;
+}
+
+static int cmd_batch_read(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_de_sample *samples;
+ struct scmi_tlm_batch *batch;
+ int ret;
+
+ if (!args->opts || !args->cnt)
+ return -EINVAL;
+
+ if (args->cnt > st->info.num_des)
+ return -ENOSPC;
+
+ /* Allocate for full size with status */
+ batch = batch_buffer_alloc(st->info.num_des, sizeof(*samples), true);
+ if (!batch)
+ return -1;
+
+ samples = (struct scmi_tlm_de_sample *)batch->items;
+ batch->num_items = args->cnt;
+ for (int i = 0; i < batch->num_items; i++) {
+ unsigned long val;
+
+ val = strtoul(args->opts[i], NULL, 0);
+ if (val == ULONG_MAX || errno == EINVAL)
+ continue;
+
+ samples[i].id = (unsigned int)val;
+ }
+
+ ret = ioctl(st->fd, SCMI_TLM_BATCH_READ, batch);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_BATCH_READ));
+ return -errno;
+ }
+
+ fprintf(stdout, "\n- BATCH read -\n-------------------\n");
+ for (int i = 0; i < batch->num_items; i++)
+ fprintf(stdout, "STATUS[%d] => 0x%08X %016llu %016llX\n",
+ ((uint32_t *)batch->states)[i],
+ samples[i].id, samples[i].tstamp, samples[i].val);
+
+ return 0;
+}
+
+/*
+ * <ena>:<t_ena>[:<update_msecs>]
+ */
+static void
+parse_item_cfg(char *str, bool *ena, bool *t_ena, unsigned int *update)
+{
+ char *tok;
+
+ if (!ena)
+ return;
+
+ *ena = false;
+ tok = strtok(str, ":");
+ if (tok)
+ *ena = strncmp(tok, "on", strlen(tok)) == 0;
+
+ if (!t_ena)
+ return;
+
+ *t_ena = false;
+ tok = strtok(NULL, ":");
+ if (tok)
+ *t_ena = strncmp(tok, "on", strlen(tok)) == 0;
+
+ if (!update)
+ return;
+
+ tok = strtok(NULL, ":");
+ if (tok) {
+ unsigned long val;
+
+ val = strtoul(tok, NULL, 0);
+ if (val == ULONG_MAX || errno == EINVAL)
+ return;
+
+ *update = (unsigned int)val;
+ }
+}
+
+static int configure_all_des(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_de_config de_cfg = {};
+ bool ena, t_ena;
+ int ret;
+
+ if (!args->opts)
+ return -1;
+
+ ret = ioctl(st->fd, SCMI_TLM_GET_ALL_CFG, &de_cfg);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_ALL_CFG));
+ return -errno;
+ }
+
+ fprintf(stdout, "--- [GET] ALL DEs ON:%d T_ON:%d\n",
+ de_cfg.enable, de_cfg.t_enable);
+
+ if (args->cnt < 2)
+ return 0;
+
+ parse_item_cfg(args->opts[1], &ena, &t_ena, NULL);
+ de_cfg.enable = !!ena;
+ de_cfg.t_enable = !!t_ena;
+
+ fprintf(stdout, "--- [SET] ALL DEs ON:%d T_ON:%d\n",
+ de_cfg.enable, de_cfg.t_enable);
+
+ ret = ioctl(st->fd, SCMI_TLM_SET_ALL_CFG, &de_cfg);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_SET_ALL_CFG));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int de_config_batch_get(struct tlm_state *st, struct scmi_tlm_batch *batch)
+{
+ int ret;
+
+ /* Get current config */
+ ret = ioctl(st->fd, SCMI_TLM_GET_DE_CFG, batch);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_CFG));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int configure_de(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_de_config *de_cfg;
+ struct scmi_tlm_batch *batch;
+ bool ena, t_ena;
+ unsigned long val;
+ int ret, *states;
+
+ if (!args->opts)
+ return -1;
+
+ gather_tlm_res_state(st);
+
+ if (args->cnt < 2) {
+ /*
+ * Bail-out to avoid issuing a query-0 which is not what
+ * intended here
+ */
+ if (!st->num_des)
+ return 0;
+
+ batch = batch_buffer_alloc(st->num_des, sizeof(*de_cfg), true);
+ if (!batch)
+ return -1;
+
+ /* Dump all DE configs */
+ de_cfg = (struct scmi_tlm_de_config *)batch->items;
+ for (int i = 0; i < st->num_des; i++)
+ de_cfg[i].id = st->des[i].info->id;
+
+ ret = de_config_batch_get(st, batch);
+ if (ret)
+ return ret;
+
+ states = (int *)batch->states;
+ for (int i = 0; i < batch->num_items; i++) {
+ fprintf(stdout,
+ "--- [GET] STATUS[%d] -> DE:0x%08X ON:%d T_ON:%d SID:%u OFFS:0x%X",
+ states[i], de_cfg[i].id, de_cfg[i].enable,
+ de_cfg[i].t_enable, de_cfg[i].sid, de_cfg[i].offset);
+ print_uuid(" UUID: ", &de_cfg[i].uuid[0]);
+ fprintf(stdout, "\n");
+ }
+
+ return 0;
+ }
+
+ /*Dump one DE config */
+ batch = batch_buffer_alloc(1, sizeof(*de_cfg), true);
+ if (!batch)
+ return -1;
+
+ states = (int *)batch->states;
+ de_cfg = (struct scmi_tlm_de_config *)batch->items;
+ val = strtoul(args->opts[1], NULL, 0);
+ if (val == ULONG_MAX || errno == EINVAL)
+ return -1;
+
+ de_cfg[0].id = (unsigned int)val;
+ ret = de_config_batch_get(st, batch);
+ if (ret)
+ return ret;
+
+ fprintf(stdout, "--- [GET] STATUS[%d] -> DE:0x%08X ON:%d T_ON:%d SID:%u OFFS:0x%X",
+ states[0], de_cfg[0].id, de_cfg[0].enable,
+ de_cfg[0].t_enable, de_cfg[0].sid, de_cfg[0].offset);
+ print_uuid(" UUID: ", &de_cfg[0].uuid[0]);
+ fprintf(stdout, "\n");
+
+ if (args->cnt < 3)
+ return 0;
+
+ /* Configure one DE */
+ parse_item_cfg(args->opts[2], &ena, &t_ena, NULL);
+ de_cfg[0].enable = !!ena;
+ de_cfg[0].t_enable = !!t_ena;
+
+ ret = ioctl(st->fd, SCMI_TLM_SET_DE_CFG, batch);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_SET_DE_CFG));
+ return -errno;
+ }
+
+ fprintf(stdout, "--- [SET] STATUS[%d] -> DE:0x%08X ON:%d T_ON:%d SID:%u OFFS:0x%X",
+ states[0], de_cfg[0].id, de_cfg[0].enable,
+ de_cfg[0].t_enable, de_cfg[0].sid, de_cfg[0].offset);
+ print_uuid(" UUID: ", &de_cfg[0].uuid[0]);
+ fprintf(stdout, "\n");
+
+ return 0;
+}
+
+static int grp_config_get(struct tlm_state *st, struct scmi_tlm_config *cfg)
+{
+ int ret;
+
+ /* Get current config */
+ ret = ioctl(st->fd, SCMI_TLM_GET_CFG, cfg);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_CFG));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int configure_group(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_config cfg = {};
+ bool ena, t_ena;
+ unsigned long val;
+ int ret;
+
+ if (!args->opts)
+ return -1;
+
+ gather_tlm_res_state(st);
+
+ if (args->cnt < 2) {
+ /* Dump all GRPS configs */
+ for (int i = 0; i < st->num_groups; i++) {
+ cfg.grp_id = st->grps[i].info->grp_id;
+ cfg.active.exp = -3;
+ cfg.flags = SCMI_TLM_CONFIG_GROUP;
+ ret = grp_config_get(st, &cfg);
+ if (!ret)
+ fprintf(stdout,
+ "--- [GET] -> GROUP:0x%08X ON:%d T_ON:%d INTRV:%u,%d\n",
+ cfg.grp_id, cfg.enable, cfg.t_enable,
+ cfg.active.secs, cfg.active.exp);
+ }
+
+ return 0;
+ }
+
+ /*Dump one GROUP config */
+ val = strtoul(args->opts[1], NULL, 0);
+ if (val == ULONG_MAX || errno == EINVAL)
+ return -1;
+
+ cfg.grp_id = (unsigned int)val;
+ cfg.flags = SCMI_TLM_CONFIG_GROUP;
+ cfg.active.exp = -3;
+ ret = grp_config_get(st, &cfg);
+ if (ret)
+ return ret;
+
+ fprintf(stdout, "--- [GET] -> GROUP:0x%08X ON:%d T_ON:%d INTRV:%u,%d\n",
+ cfg.grp_id, cfg.enable, cfg.t_enable, cfg.active.secs, cfg.active.exp);
+
+ if (args->cnt < 3)
+ return 0;
+
+ /* Configure one GROUP */
+ parse_item_cfg(args->opts[2], &ena, &t_ena, &cfg.active.secs);
+ cfg.enable = !!ena;
+ cfg.t_enable = !!t_ena;
+
+ fprintf(stdout, "--- [SET] -> GROUP:0x%08X ON:%d T_ON:%d INTRV:%u,%d\n",
+ cfg.grp_id, cfg.enable, cfg.t_enable,
+ cfg.active.secs, cfg.active.exp);
+
+ ret = ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int configure_telemetry(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_config cfg = st->cfg;
+ bool val;
+ int ret;
+
+ if (args->cnt < 2 || !args->opts)
+ return -1;
+
+ val = strncmp(args->opts[1], "on", strlen(args->opts[1])) == 0;
+
+ cfg.enable = !!val;
+ cfg.flags = 0;
+
+ fprintf(stdout, "\n---%sabling Telemetry\n", val ? "En" : "Dis");
+ ret = ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int configure_update(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_config cfg = st->cfg;
+ unsigned long val;
+ int ret;
+
+ if (args->cnt < 2 || !args->opts)
+ return -1;
+
+ val = strtoul(args->opts[1], NULL, 0);
+ if (val == ULONG_MAX || errno == EINVAL)
+ return -1;
+
+ cfg.flags = 0;
+ if (!val) {
+ cfg.enable = val;
+ fprintf(stdout, "\n---Disabling Telemetry\n");
+ } else {
+ cfg.enable = !!val;
+ cfg.active.secs = val;
+ cfg.active.exp = -3;
+ fprintf(stdout, "\n--- Setting update interval to %lu\n", val);
+ }
+
+ ret = ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int cmd_configure(struct tlm_state *st, struct parsed_args *args)
+{
+ if (!args->cnt || !args->opts)
+ return cmd_unknown("_");
+
+ if (!strncmp(args->opts[0], "all_des", strlen(args->opts[0])))
+ return configure_all_des(st, args);
+ if (!strncmp(args->opts[0], "tlm", strlen(args->opts[0])))
+ return configure_telemetry(st, args);
+ if (!strncmp(args->opts[0], "update", strlen(args->opts[0])))
+ return configure_update(st, args);
+ if (!strncmp(args->opts[0], "de", strlen(args->opts[0])))
+ return configure_de(st, args);
+ if (!strncmp(args->opts[0], "group", strlen(args->opts[0])))
+ return configure_group(st, args);
+
+ return cmd_unknown(args->opts[0]);
+}
+
+static inline int cmd_full_scan_and_dump(struct tlm_state *st)
+{
+ cmd_dump_state(st, NULL);
+ cmd_dump_des(st, NULL);
+ cmd_dump_groups(st);
+
+ return 0;
+}
+
+static int cmd_uuids(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_uuid *uuids;
+ size_t u_sz;
+ int ret;
+
+ batch.item_sz = sizeof(*uuids);
+ /* Get size at first */
+ ret = ioctl(st->fd, SCMI_TLM_GET_UUID_LIST, &batch);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_UUID_LIST));
+ return -errno;
+ }
+
+ u_sz = sizeof(*uuids) * batch.num_items;
+ uuids = malloc(u_sz);
+ if (!uuids)
+ return -ENOMEM;
+
+ bzero(uuids, u_sz);
+ batch.items = (uint64_t)uuids;
+ ret = ioctl(st->fd, SCMI_TLM_GET_UUID_LIST, &batch);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_GET_UUID_LIST));
+ return -errno;
+ }
+
+ fprintf(stdout, "--- [GET] -> NUM_UUIDS: %u\n", batch.num_items);
+ for (int i = 0; i < batch.num_items; i++) {
+ print_uuid(" UUID: ",
+ &(((struct scmi_tlm_uuid *)(batch.items))[i]));
+ fprintf(stdout, "\n");
+ }
+
+ return 0;
+}
+
+static int cmd_monitor(struct tlm_state *st, struct parsed_args *args)
+{
+ struct scmi_tlm_event evt = {};
+ struct pollfd fds[2];
+ sigset_t mask;
+ int ret, efd, sfd;
+
+ efd = eventfd(0, EFD_CLOEXEC);
+ if (efd < 0)
+ return -errno;
+
+ sigemptyset(&mask);
+ sigaddset(&mask, SIGTERM);
+ sigaddset(&mask, SIGINT);
+ sigaddset(&mask, SIGQUIT);
+ ret = sigprocmask(SIG_BLOCK, &mask, NULL);
+ if (ret < 0) {
+ close(efd);
+ return -errno;
+ }
+
+ sfd = signalfd(-1, &mask, 0);
+ if (sfd < 0) {
+ close(efd);
+ return -errno;
+ }
+
+ fds[0].fd = sfd;
+ fds[0].events = POLLIN;
+
+ evt.type = SCMI_TLM_EVT_GENERATION;
+ evt.efd = efd;
+ fprintf(stdout, "==>> Registering for events of type %u\n", evt.type);
+ ret = ioctl(st->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_EVENT_SUBSCRIBE));
+ return -errno;
+ }
+
+ fds[1].fd = evt.efd;
+ fds[1].events = POLLIN;
+
+ if (args->cnt) {
+ fprintf(stdout, "==>> Closing FD on TLM device...\n");
+ close(st->fd);
+ }
+
+ fprintf(stdout, "==>> Waiting for events of type %u on cookie:%u\n",
+ evt.type, evt.cookie);
+ while (1) {
+ ret = poll(fds, 2, -1);
+ if (ret < 0)
+ return ret;
+
+ /* Break on SIGINT */
+ if (fds[0].revents & POLLIN) {
+ struct signalfd_siginfo fdsi;
+
+ read(fds[0].fd, &fdsi, sizeof(fdsi));
+ if (fdsi.ssi_signo) {
+ fprintf(stderr, "Got signal :%u\n", fdsi.ssi_signo);
+ break;
+ }
+ }
+
+ if (fds[1].revents & POLLIN) {
+ uint64_t val = 0;
+
+ read(fds[1].fd, &val, sizeof(val));
+ fprintf(stdout, "==>> EVENT READ on EFD:%d - VAL:%lu!\n",
+ evt.efd, val);
+ }
+ }
+
+ if (!args->cnt) {
+ fprintf(stdout, "==>> DE-Registering for events of type %u on cookie:%u\n",
+ evt.type, evt.cookie);
+ ret = ioctl(st->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt);
+ if (ret < 0) {
+ perror(IOCTL_ERR_STR(SCMI_TLM_EVENT_SUBSCRIBE));
+ return -errno;
+ }
+ }
+
+ close(sfd);
+ close(efd);
+
+ return 0;
+}
+
+static int query_intervals(struct tlm_state *st)
+{
+ struct scmi_tlm_intervals tlm_ivs = {};
+ int ret;
+
+ /* Global interval */
+ ret = ioctl(st->fd, SCMI_TLM_GET_INTRVS, &tlm_ivs);
+ if (ret < 0) {
+ perror("SCMI_TLM_GET_INTRVS");
+ return -errno;
+ }
+
+ fprintf(stdout, "--- [QUERY]:[SCMI_TLM_GET_INTRVS] -> NUM_ITEMS:%u\n",
+ tlm_ivs.num_intervals);
+
+ for (int i = 0; i < st->num_groups; i++) {
+ struct scmi_tlm_intervals tlm_ivs = {};
+
+ tlm_ivs.grp_id = st->grps[i].info->grp_id;
+ tlm_ivs.flags = SCMI_TLM_INTERV_GROUP;
+ ret = ioctl(st->fd, SCMI_TLM_GET_INTRVS, &tlm_ivs);
+ if (ret < 0) {
+ perror("SCMI_TLM_GET_INTRVS");
+ return -errno;
+ }
+
+ fprintf(stdout,
+ "--- [QUERY][GRP%u]:[SCMI_TLM_GET_INTRVS] -> NUM_ITEMS:%u\n",
+ tlm_ivs.grp_id, tlm_ivs.num_intervals);
+ }
+
+ return 0;
+}
+
+static int query_samples(struct tlm_state *st, unsigned long ioctl_op,
+ const char *tag)
+{
+ struct scmi_tlm_data_read data = {};
+ int ret;
+
+ /* Global interval */
+ ret = ioctl(st->fd, ioctl_op, &data);
+ if (ret < 0) {
+ perror(tag);
+ return -errno;
+ }
+
+ fprintf(stdout, "--- [QUERY]:[%s] -> NUM_ITEMS:%u\n",
+ tag, data.num_samples);
+
+ for (int i = 0; i < st->num_groups; i++) {
+ struct scmi_tlm_data_read data = {};
+
+ data.grp_id = st->grps[i].info->grp_id;
+ data.flags = SCMI_TLM_READ_GROUP;
+ ret = ioctl(st->fd, ioctl_op, &data);
+ if (ret < 0) {
+ perror(tag);
+ return -errno;
+ }
+
+ fprintf(stdout,
+ "--- [QUERY][GRP%u]:[%s] -> NUM_ITEMS:%u\n",
+ data.grp_id, tag, data.num_samples);
+ }
+
+ return 0;
+}
+
+static int cmd_query_ioctls(struct tlm_state *st)
+{
+ int ret;
+
+ gather_tlm_res_state(st);
+
+ ret = query_items(st->fd, SCMI_TLM_GET_DE_LIST,
+ sizeof(struct scmi_tlm_de_info),
+ "SCMI_TLM_GET_DE_LIST");
+ if (ret)
+ return ret;
+
+ /*Bailoout ONLY whenm GROUPS not supported */
+ ret = query_items(st->fd, SCMI_TLM_GET_GRP_LIST,
+ sizeof(struct scmi_tlm_grp_info),
+ "SCMI_TLM_GET_GRP_LIST");
+ if (ret && st->info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)
+ return ret;
+
+ ret = query_items(st->fd, SCMI_TLM_GET_SHMTI_LIST,
+ sizeof(struct scmi_tlm_shmti_info),
+ "SCMI_TLM_GET_SHMTI_LIST");
+ if (ret)
+ return ret;
+
+ ret = query_items(st->fd, SCMI_TLM_GET_UUID_LIST,
+ sizeof(struct scmi_tlm_uuid),
+ "SCMI_TLM_GET_UUID_LIST");
+ if (ret)
+ return ret;
+
+ ret = query_items(st->fd, SCMI_TLM_GET_DE_CFG,
+ sizeof(struct scmi_tlm_de_config),
+ "SCMI_TLM_GET_DE_CFG");
+ if (ret)
+ return ret;
+
+ ret = query_items(st->fd, SCMI_TLM_SET_DE_CFG,
+ sizeof(struct scmi_tlm_de_config),
+ "SCMI_TLM_SET_DE_CFG");
+ if (ret)
+ return ret;
+
+ ret = query_items(st->fd, SCMI_TLM_BATCH_READ,
+ sizeof(struct scmi_tlm_de_sample),
+ "SCMI_TLM_BATCH_READ");
+ if (ret)
+ return ret;
+
+ ret = query_intervals(st);
+ if (ret)
+ return ret;
+
+ ret = query_samples(st, SCMI_TLM_BULK_READ, "SCMI_TLM_BULK_READ");
+ if (ret)
+ return ret;
+
+ ret = query_samples(st, SCMI_TLM_SINGLE_READ, "SCMI_TLM_SINGLE_READ");
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ struct parsed_args args = {};
+ struct tlm_state *st;
+ int ret;
+
+ argv0 = argv[0];
+
+ ret = parse_options(argc, argv, &args);
+ if (ret < 0) {
+ usage(argv0);
+ return EXIT_FAILURE;
+ }
+
+ st = open_session(args.instance);
+ if (!st)
+ return EXIT_FAILURE;
+
+ ret = gather_tlm_state(st);
+ if (ret)
+ return EXIT_FAILURE;
+
+ if (!args.cmd)
+ return cmd_full_scan_and_dump(st);
+ else if (!strncmp(args.cmd, "info", strlen(args.cmd)))
+ return cmd_dump_state(st, &args);
+ else if (!strncmp(args.cmd, "uuids", strlen(args.cmd)))
+ return cmd_uuids(st, &args);
+ else if (!strncmp(args.cmd, "monitor", strlen(args.cmd)))
+ return cmd_monitor(st, &args);
+ else if (!strncmp(args.cmd, "shmti", strlen(args.cmd)))
+ return cmd_shmti_ops(st, &args);
+ else if (!strncmp(args.cmd, "des", strlen(args.cmd)))
+ return cmd_dump_des(st, &args);
+ else if (!strncmp(args.cmd, "groups", strlen(args.cmd)))
+ return cmd_dump_groups(st);
+ else if (!strncmp(args.cmd, "cfg", strlen(args.cmd)))
+ return cmd_configure(st, &args);
+ else if (!strncmp(args.cmd, "single", strlen(args.cmd)))
+ return cmd_single_read(st);
+ else if (!strncmp(args.cmd, "bulk", strlen(args.cmd)))
+ return cmd_bulk_read(st);
+ else if (!strncmp(args.cmd, "batch", strlen(args.cmd)))
+ return cmd_batch_read(st, &args);
+ else if (!strncmp(args.cmd, "reset", strlen(args.cmd)))
+ return cmd_tlm_reset(st);
+ else if (!strncmp(args.cmd, "query", strlen(args.cmd)))
+ return cmd_query_ioctls(st);
+
+ return cmd_unknown(args.cmd);
+}
diff --git a/tools/testing/scmi/stlm_decode.c b/tools/testing/scmi/stlm_decode.c
new file mode 100644
index 000000000000..186964710184
--- /dev/null
+++ b/tools/testing/scmi/stlm_decode.c
@@ -0,0 +1,252 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SCMI Telemetry Test Tool Decoder
+ *
+ * Copyright (C) 2026 ARM Ltd.
+ */
+
+/*
+ * Userspace decoder for a snapshot of an SCMI v4.0 Telemetry SHMTI area
+ * (TDCF format). libc only, no alignment or endianness assumptions on the
+ * host or on the snapshot buffer.
+ *
+ * Layout (all fields little-endian, except the UUID line payload):
+ *
+ * Prologue (16 bytes)
+ * +0 u32 signature 0x5442474E ("TBGN" as a LE-decoded u32)
+ * +4 u32 match seq LSB set => platform update in progress
+ * +8 u32 num_qwords payload size in 64-bit units
+ * +12 u32 hdr meta [7:0] TDCF revision
+ * Payload (num_qwords * 8 bytes of lines, each 2 or 3 qwords)
+ * +0 u32 meta [7:4] line type: 0 DATA, 1 BLK_TS, 2 UUID
+ * [3] USE_BLK_TS [2] HAS_LINE_EXT
+ * [1] TS_VALID [0] DATA_INVALID
+ * +4 u32 id DATA: DE id BLK_TS: clock rate
+ * +8 DATA : u64 value BLK_TS: u64 timestamp UUID: 16 bytes
+ * +16 u64 line timestamp (only when HAS_LINE_EXT; UUID lines: part
+ * of the 16 bytes above, HAS_LINE_EXT is mandatory for them)
+ * Epilogue (last 8 bytes of the SHMTI)
+ * +0 u32 match seq must equal the prologue one
+ * +4 u32 signature 0x54454E44 ("TEND" as a LE-decoded u32)
+ *
+ * Line length: 16 bytes, or 24 bytes when HAS_LINE_EXT is set.
+ *
+ * Returns 0 on success, -EINVAL on bad size/signature, -EOVERFLOW/-EPROTO
+ * on malformed payload, -EAGAIN if the snapshot looks torn (odd start
+ * sequence or start/end sequence mismatch): the content is still decoded.
+ */
+#include <errno.h>
+#include <inttypes.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#define TDCF_SIG_BEGIN 0x5442474Eu
+#define TDCF_SIG_END 0x54454E44u
+#define TDCF_PRLG_SZ 16u
+#define TDCF_EPLG_SZ 8u
+#define TDCF_MIN_SZ (TDCF_PRLG_SZ + TDCF_EPLG_SZ)
+
+#define TDCF_LINE_DATA 0u
+#define TDCF_LINE_BLK_TS 1u
+#define TDCF_LINE_UUID 2u
+
+#define TDCF_META_TYPE(m) (((m) >> 4) & 0xfu)
+#define TDCF_META_USE_BLK_TS (1u << 3)
+#define TDCF_META_HAS_EXT (1u << 2)
+#define TDCF_META_TS_VALID (1u << 1)
+#define TDCF_META_INVALID (1u << 0)
+
+#define TDCF_LINE_SZ 16u
+#define TDCF_LINE_EXT_SZ 24u
+
+static uint32_t tdcf_le32(const uint8_t *p)
+{
+ return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
+ ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
+}
+
+static uint64_t tdcf_le64(const uint8_t *p)
+{
+ return ((uint64_t)tdcf_le32(p + 4) << 32) | tdcf_le32(p);
+}
+
+static void tdcf_flags(uint32_t m, char out[16])
+{
+ snprintf(out, 16, "%c%c%c%c",
+ (m & TDCF_META_USE_BLK_TS) ? 'B' : '-',
+ (m & TDCF_META_HAS_EXT) ? 'E' : '-',
+ (m & TDCF_META_TS_VALID) ? 'T' : '-',
+ (m & TDCF_META_INVALID) ? 'I' : '-');
+}
+
+/* UUID bytes are in memory order, i.e. 4 x BE dwords: no swapping needed */
+static void tdcf_uuid_str(const uint8_t *u, char out[40])
+{
+ snprintf(out, 40,
+ "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
+ "%02x%02x%02x%02x%02x%02x",
+ u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7],
+ u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15]);
+}
+
+int tdcf_decode(const void *buf, size_t len)
+{
+ const uint8_t *b = buf;
+ uint32_t sig_s, mseq_s, nq, hdr, mseq_e, sig_e;
+ size_t off = TDCF_PRLG_SZ, end, left;
+ /* Payload starts at 16, so offset 0 can safely mean "none" */
+ size_t bts_off = 0, uuid_off = 0;
+ uint64_t bts_ts = 0;
+ uint32_t bts_rate = 0;
+ int torn = 0;
+
+ if (!b || len < TDCF_MIN_SZ) {
+ fprintf(stderr, "TDCF: buffer too small (%zu bytes)\n", len);
+ return -EINVAL;
+ }
+
+ end = len - TDCF_EPLG_SZ; /* payload lives in [off, end) */
+
+ sig_s = tdcf_le32(b);
+ mseq_s = tdcf_le32(b + 4);
+ nq = tdcf_le32(b + 8);
+ hdr = tdcf_le32(b + 12);
+ mseq_e = tdcf_le32(b + end);
+ sig_e = tdcf_le32(b + end + 4);
+
+ if (sig_s != TDCF_SIG_BEGIN || sig_e != TDCF_SIG_END) {
+ fprintf(stderr,
+ "TDCF: bad signature START:0x%08" PRIx32
+ " (exp 0x%08x) END:0x%08" PRIx32 " (exp 0x%08x)\n",
+ sig_s, TDCF_SIG_BEGIN, sig_e, TDCF_SIG_END);
+ return -EINVAL;
+ }
+
+ printf("PRLG @0x0000 sig=TBGN mseq=0x%08" PRIx32 " qwords=%" PRIu32
+ " rev=%" PRIu32 " hdr_meta=0x%08" PRIx32 "\n",
+ mseq_s, nq, hdr & 0xffu, hdr);
+
+ if (mseq_s & 1u) {
+ fprintf(stderr, "TDCF: odd start sequence: update in progress\n");
+ torn = 1;
+ }
+ if (mseq_s != mseq_e) {
+ fprintf(stderr,
+ "TDCF: sequence mismatch start:0x%08" PRIx32
+ " end:0x%08" PRIx32 "\n", mseq_s, mseq_e);
+ torn = 1;
+ }
+
+ left = nq;
+ while (left) {
+ const uint8_t *p = b + off;
+ char fl[16], us[40];
+ uint32_t meta, id;
+ size_t lb;
+
+ if (end - off < 4) {
+ fprintf(stderr, "TDCF: line @0x%zx overflows SHMTI\n", off);
+ return -EOVERFLOW;
+ }
+
+ meta = tdcf_le32(p);
+ lb = (meta & TDCF_META_HAS_EXT) ? TDCF_LINE_EXT_SZ : TDCF_LINE_SZ;
+ if (end - off < lb) {
+ fprintf(stderr, "TDCF: line @0x%zx overflows SHMTI\n", off);
+ return -EOVERFLOW;
+ }
+ if (left < lb / 8) {
+ fprintf(stderr,
+ "TDCF: line @0x%zx exceeds prologue num_qwords\n",
+ off);
+ return -EPROTO;
+ }
+
+ id = tdcf_le32(p + 4);
+ tdcf_flags(meta, fl);
+ printf("LINE @0x%04zx %-6s [%s] ", off,
+ TDCF_META_TYPE(meta) == TDCF_LINE_DATA ? "DATA" :
+ TDCF_META_TYPE(meta) == TDCF_LINE_BLK_TS ? "BLK_TS" :
+ TDCF_META_TYPE(meta) == TDCF_LINE_UUID ? "UUID" : "???",
+ fl);
+
+ if (meta & TDCF_META_INVALID) {
+ /* Disabled/unused slot: not an error */
+ printf("id=0x%08" PRIx32 " <invalid/unused>\n", id);
+ goto next;
+ }
+
+ switch (TDCF_META_TYPE(meta)) {
+ case TDCF_LINE_DATA: {
+ uint64_t val = tdcf_le64(p + 8);
+
+ printf("id=0x%08" PRIx32 " val=0x%016" PRIx64
+ " (%" PRIu64 ") ", id, val, val);
+
+ if (meta & TDCF_META_USE_BLK_TS) {
+ if (bts_off)
+ printf("ts=0x%016" PRIx64
+ " (blk@0x%zx rate=%" PRIu32 ")",
+ bts_ts, bts_off, bts_rate);
+ else
+ printf("ts=<USE_BLK_TS without preceding BLK_TS>");
+ } else if (meta & TDCF_META_TS_VALID) {
+ if (meta & TDCF_META_HAS_EXT)
+ printf("ts=0x%016" PRIx64 " (line)",
+ tdcf_le64(p + 16));
+ else
+ printf("ts=<TS_VALID without EXT: malformed>");
+ } else if (meta & TDCF_META_HAS_EXT) {
+ printf("ts=invalid");
+ } else {
+ printf("ts=none");
+ }
+
+ if (uuid_off)
+ printf(" uuid=line@0x%zx", uuid_off);
+ else
+ printf(" uuid=primary");
+ printf("\n");
+ break;
+ }
+ case TDCF_LINE_BLK_TS:
+ if (meta & (TDCF_META_USE_BLK_TS | TDCF_META_HAS_EXT |
+ TDCF_META_TS_VALID)) {
+ printf("rate=%" PRIu32 " <malformed, ignored>\n", id);
+ break;
+ }
+ bts_off = off;
+ bts_rate = id;
+ bts_ts = tdcf_le64(p + 8);
+ printf("rate=%" PRIu32 "%s ts=0x%016" PRIx64
+ " (%" PRIu64 ")\n", bts_rate,
+ bts_rate ? "" : " (0: protocol default)",
+ bts_ts, bts_ts);
+ break;
+ case TDCF_LINE_UUID:
+ if ((meta & (TDCF_META_TS_VALID | TDCF_META_USE_BLK_TS)) ||
+ !(meta & TDCF_META_HAS_EXT)) {
+ printf("<malformed, ignored>\n");
+ break;
+ }
+ uuid_off = off;
+ tdcf_uuid_str(p + 8, us);
+ printf("id=0x%08" PRIx32 " %s\n", id, us);
+ break;
+ default:
+ printf("id=0x%08" PRIx32 " <unknown line type %u, skipped>\n",
+ id, TDCF_META_TYPE(meta));
+ break;
+ }
+next:
+ off += lb;
+ left -= lb / 8;
+ }
+
+ if (end > off)
+ printf("PAD @0x%04zx %zu unused payload bytes\n", off, end - off);
+ printf("EPLG @0x%04zx mseq=0x%08" PRIx32 " sig=TEND\n", end, mseq_e);
+
+ return torn ? -EAGAIN : len;
+}
diff --git a/tools/testing/scmi/stlm_decode.h b/tools/testing/scmi/stlm_decode.h
new file mode 100644
index 000000000000..740d1528d1f4
--- /dev/null
+++ b/tools/testing/scmi/stlm_decode.h
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __STLM_DECODE_H
+#define __STLM_DECODE_H
+
+int tdcf_decode(const void *buf, size_t len);
+
+#endif
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* Re: [PATCH v17 00/34] Introduce SCMI Telemetry support
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
` (33 preceding siblings ...)
2026-11-24 19:48 ` [PATCH v17 34/34] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool Cristian Marussi
@ 2026-11-24 22:53 ` Jonathan Cameron
34 siblings, 0 replies; 38+ messages in thread
From: Jonathan Cameron @ 2026-11-24 22:53 UTC (permalink / raw)
To: Cristian Marussi
Cc: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc, sudeep.holla,
james.quinlan, f.fainelli, vincent.guittot, etienne.carriere,
peng.fan, michal.simek, d-gole, elif.topuz, lukasz.luba,
philip.radford, david, souvik.chakravarty, leitao, kas, puranjay,
usama.arif, kernel-team
On Tue, 24 Nov 2026 19:48:06 +0000
Cristian Marussi <cristian.marussi@arm.com> wrote:
> Hi all,
>
> [TLDR Summary]
> [V17 highlights]
> - addressed more Sashiko reports
> - added a few more SCMI Core fixes that ease per-protocol logic:
> + added more consistency in the SCMI Core Iterators machinery
> + added centralized check (SCMI Core) for truncated reply
> + Reworked some SCM Clock behaviour to allow for truncated checks
> + SCMI protocol core initialization refactor and fixes
>
> [V18 TODO]
> - address any remaining outstanding Sashiko/reviewers issues
> - address pending human reviews feedback
I'm lost.
Does this mean humans should wait for v18? I'll assume not and
take a look anyway...
Sashiko is moderately easy to run locally as long as your company
isn't too restrictive with tokens (if they are say upstream folk are
complaining and ask for more!). Might not be exactly the same
as the version here but will reduce the noise and versions on list.
Jonathan
^ permalink raw reply [flat|nested] 38+ messages in thread
* Re: [PATCH v17 01/34] firmware: arm_scmi: Harden iterators state parsing
2026-11-24 19:48 ` [PATCH v17 01/34] firmware: arm_scmi: Harden iterators state parsing Cristian Marussi
@ 2026-11-24 22:54 ` Jonathan Cameron
0 siblings, 0 replies; 38+ messages in thread
From: Jonathan Cameron @ 2026-11-24 22:54 UTC (permalink / raw)
To: Cristian Marussi
Cc: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc, sudeep.holla,
james.quinlan, f.fainelli, vincent.guittot, etienne.carriere,
peng.fan, michal.simek, d-gole, elif.topuz, lukasz.luba,
philip.radford, david, souvik.chakravarty, leitao, kas, puranjay,
usama.arif, kernel-team
On Tue, 24 Nov 2026 19:48:07 +0000
Cristian Marussi <cristian.marussi@arm.com> wrote:
> Add checks in the core multi-part iterators support to detect generally
> malformed and inconsistent multi-part replies.
>
> Fixes: 36b6ea0fc6bc ("firmware: arm_scmi: Add iterators for multi-part commands")
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
You answered my question on this some versions back (wrt to where the magic
size came from).
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 38+ messages in thread
* Re: [PATCH v17 02/34] [RFC] firmware: arm_scmi: Additional iterators consistency checks
2026-11-24 19:48 ` [PATCH v17 02/34] [RFC] firmware: arm_scmi: Additional iterators consistency checks Cristian Marussi
@ 2026-11-24 23:08 ` Jonathan Cameron
0 siblings, 0 replies; 38+ messages in thread
From: Jonathan Cameron @ 2026-11-24 23:08 UTC (permalink / raw)
To: Cristian Marussi
Cc: linux-kernel, linux-arm-kernel, arm-scmi, linux-doc, sudeep.holla,
james.quinlan, f.fainelli, vincent.guittot, etienne.carriere,
peng.fan, michal.simek, d-gole, elif.topuz, lukasz.luba,
philip.radford, david, souvik.chakravarty, leitao, kas, puranjay,
usama.arif, kernel-team
On Tue, 24 Nov 2026 19:48:08 +0000
Cristian Marussi <cristian.marussi@arm.com> wrote:
> Add a few more consistency checks around the number of returned and
> remaining items during the life of an iteration: this is useful to spot
> prematurely terminated iterations that would produce shorter than expected
> results.
>
> Handling these anomalies in the platform multi-part replies within the
> core SCMI iterators machinery avoids the need to address such issues
> multiple times at the protocol layer.
>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> ---
> RFC because this is another fix that is potentially disrupting for any
> non compliant SCMI platform deployed in the wild.
Looks sensible to me and cutting out weirdness as early as possible should
make everything more resilient. If there is a broken implementation out there
(and to hit these is not definitely not great) then quirk that when it surfaces.
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
> ---
> drivers/firmware/arm_scmi/driver.c | 36 ++++++++++++++++++++++++++----
> 1 file changed, 32 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
> index 55d9cc487916..6dd249c08f5f 100644
> --- a/drivers/firmware/arm_scmi/driver.c
> +++ b/drivers/firmware/arm_scmi/driver.c
> @@ -1852,7 +1852,7 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
> const struct scmi_protocol_handle *ph;
> struct scmi_iterator_state *st;
> struct scmi_iterator *i;
> - unsigned int n;
> + unsigned int n, i_start, i_max, i_returned;
>
> if (!iter)
> return -EINVAL;
> @@ -1862,10 +1862,15 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
> ph = i->ph;
> st = &i->state;
>
> + /* Initialize iteration counters used to check consistency */
> + i_max = 0;
> + i_returned = 0;
> + i_start = start ? *start : 0;
> +
> /* Reinitialize state for next run */
> st->num_returned = 0;
> st->num_remaining = 0;
> - st->desc_index = start ? *start : 0;
> + st->desc_index = i_start;
>
> do {
> iops->prepare_message(i->msg, st->desc_index, i->priv);
> @@ -1883,11 +1888,34 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
> if (ret)
> return ret;
>
> - if (!st->num_returned && st->num_remaining) {
> - dev_err(ph->dev, "Inconsistent multi-part reply.\n");
> + /*
> + * Initialize max items expectation for this run based on
> + * values returned on first message exchange: during the
> + * following iterations consistency will be validated against
> + * these parameters, so as to spot FW anomalies like sudden
> + * termination or unplausible conditions.
> + */
> + if (!i_max)
> + i_max = i_start + st->num_returned + st->num_remaining;
> +
> + if ((!st->num_returned && st->num_remaining) ||
> + (i_max != i_start + i_returned + st->num_returned + st->num_remaining)) {
> + dev_err(ph->dev,
> + "Inconsistent multi-part reply. max:%u i_max:%u i_start:%u i_returned:%u returned:%u remaining:%u desc_index:%u\n",
> + st->max_resources, i_max, i_start, i_returned,
> + st->num_returned, st->num_remaining,
> + st->desc_index);
> return -EINVAL;
> }
>
> + dev_dbg(ph->dev,
> + "Multi-part - max:%u i_max:%u i_start:%u i_returned:%u returned:%u remaining:%u desc_index:%u\n",
> + st->max_resources, i_max, i_start, i_returned,
> + st->num_returned, st->num_remaining, st->desc_index);
> +
> + /* Account for last batch received */
> + i_returned += st->num_returned;
> +
> if (st->num_returned > st->max_resources - st->desc_index) {
> dev_err(ph->dev,
> "No. of resources can't exceed %d\n",
^ permalink raw reply [flat|nested] 38+ messages in thread
end of thread, other threads:[~2026-11-24 23:08 UTC | newest]
Thread overview: 38+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 01/34] firmware: arm_scmi: Harden iterators state parsing Cristian Marussi
2026-11-24 22:54 ` Jonathan Cameron
2026-11-24 19:48 ` [PATCH v17 02/34] [RFC] firmware: arm_scmi: Additional iterators consistency checks Cristian Marussi
2026-11-24 23:08 ` Jonathan Cameron
2026-11-24 19:48 ` [PATCH v17 03/34] firmware: arm_scmi: Make Clock attributes size deterministic Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 04/34] firmware: arm_scmi: Add centralized detection of truncated replies Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 05/34] firmware: arm_scmi: Introduce a dedicated SCMI Errors header Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 06/34] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 07/34] firmware: arm_scmi: Allow registration of unknown-size events/reports Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 08/34] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 09/34] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 10/34] include: trace: Add Telemetry trace events Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 11/34] firmware: arm_scmi: Add Telemetry basic attributes enumeration Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 12/34] firmware: arm_scmi: Add Telemetry resources enumeration Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 13/34] firmware: arm_scmi: Add Telemetry global update intervals enumeration Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 14/34] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 15/34] firmware: arm_scmi: Add Telemetry TDCF areas parsing support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 16/34] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 17/34] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 18/34] firmware: arm_scmi: Add Telemetry DEs configuration support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 19/34] firmware: arm_scmi: Add Telemetry global " Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 20/34] firmware: arm_scmi: Add Telemetry Single/Bulk DEs reads Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 21/34] firmware: arm_scmi: Add Telemetry boot-on support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 22/34] firmware: arm_scmi: Invoke proper protocol cleanup on failure Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 23/34] firmware: arm_scmi: Refactor protocol allocation Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 24/34] firmware: arm_scmi: Add common per-protocol debugfs support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 25/34] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 26/34] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 27/34] firmware: arm_scmi: Expose per-instance identifier Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 28/34] uapi: Add ARM SCMI Telemetry definitions Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 29/34] firmware: arm_scmi: Add System Telemetry driver Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 30/34] firmware: arm_scmi: Enable autoloading for " Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 31/34] docs: ioctl-number: Add SCMI Ioctls Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 32/34] [RFC] Documentation: Add SCMI System Telemetry documentation Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 33/34] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 34/34] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool Cristian Marussi
2026-11-24 22:53 ` [PATCH v17 00/34] Introduce SCMI Telemetry support Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox