* [PATCH v14 00/32] Introduce SCMI Telemetry support
@ 2026-10-16 18:55 Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 01/32] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
` (31 more replies)
0 siblings, 32 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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]
[V14 highlights]
- Split the 2 bulkiest patches into 5+3 patches
- V14 addressed more reviews feedback
- V14 reworked protocol instance notifier to remove residual races
[V15 TODO]
- Address any remaining V14 outstanding Sashiko/reviewers issues
- [ABI]: more DOCS and examples
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.3-rc7, tested on an emulated setup.
This series is available also at [1].
If you still reading...any feedback welcome :P
Thanks,
Cristian
---
v13 --> v14
- rebased on v7.3-rc7
- [STLM]
+ fix mmmap/munmap calls
+ better NULL checks on failed enumerations
+ harden command parsing and return errors
- [SYS/TLM]
+ 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
- [TLM]
+ renamed some fields to more strictly adhere to spec nomenclature
+ split v13 Basic Telemetry into patches [7-11]
+ split V13 Parse SHMTI into patches [12-14]
+ add a barrier to acquire rinfo->num_des on reset
+ avoid lock inversion on config_mtx
+ report scmi_telemetry_line_data_parse errors
+ add traces for CACHED_DE_UPDATED
+ using a less regular time interval for TDCF scan retries
+ 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
+ fixed AB-BA dealock on blkts_link
+ relocate xa_destroy for xa_lines after uuids cleanup
+ report errors on scmi_telemetry_line_data_parse()
+ fix group unroll loop on groups_init failure
+ remove goto from de_desc_parse since clashes with cleanup guards
+ reworked the helpers used to register protocol notifiers so as to
effectively work during the protocol initialization phase when locks are
enabled
v12 --> v13
- rebased on v7.3-rc5
- [STLM]
+ misc fixes on alloc/free
+ add single DE dump capability to get info/cfg/data
+ added SHMTI/TDCF decoder (by LLM)
+ fix mmap len/offset call to account for bigger pages and
offset
+ fixed SPDX header
- [SYS/TLM]
+ 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 helpers
- [TLM]
+ reworked resourecs cleanup logic
+ use *alloc_objs where possible
+ reworked tde->mtx locks acquisition to improve synchronization between
configuration actions and concurrent readers
+ refactored STATE_SET helpers to keep DE and GROUP handling distinct
+ handle PARTIAL_ERRORs
+ reworked logic around TDE<-->BLK_TS link/unlink
+ review policy around errors while scanning the TDCF: now retries
assuming a torn read
+ harden nearest_line_type scanner
+ harden freeing path on RCU UUIDs
+ better DATA LINE size-validation while parsing
+ reset active lines trackers on scan retries
+ remove dubious macro TO_CPU64
+ rename DE_CONFIGURE defines
+ remove useless READ_ONCE
v11 --> v12
- rebased on v7.3-rc3
- [STLM]
+ renamed command helpers
+ reviewed internal/external errcodes usage
+ fixed dump_shmti read loop
+ fixed shmti_check end address calculation
+ fixed batch read boundary checks
+ fixed group indexing
- [SYS/TLM]
+ added RCU protection to UUIDs
+ reworked eventfd handling: per-session tracking with lifetime tied
to chardev fds
+ dropped needless copy_from_user on batch.states
- [UAPI]: document new eventfd lifecycle
- [TLM]:
+ fix debugfs shmti dump file to use the SID as name
+ reviewed generation counter update logic
+ fixed TS_RATE determination at boot-on
+ updating cache within TDE lock when processing msg_payld
+ using config_mtx to sync with ongoing configurations
+ reporting transient DATA_INVALID lines as -EINVAL
+ 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
+ reject zero as MATCHSEQ
+ reworked locking around line_get/put
+ reworked locking around blkts_read
+ 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
+ dropped counted-by in smhtis
+ reworked barriers around res_get
+ added smp_store_release on TDE registration
v10 --> v11
- rebased on v7.3-rc1
- added initial RFC for KSelfTest
- [STLM]: add more 0-sized query check for new IOCTLs supported
- [SYS/TLM]: make devices 664
- [SYS/TLMM]: add new 0-sized query support for IOCTLs:
GET_INTRVS / BULK / SIMPLE
- [UAPI]: more docs and clarification on upper-bounds
- [UAPI]: more docs and clarification BATCH IOCTLs parameter usage
- [TLM]: move events cleanup into resource_free
- [TLM]: fix non-conformancy on CONFIG_GET when groups NON configurable
- [TLM]: use SCAN_UPDATE also on BOOT-ON initial scan (NO discovery)
- [TLM]: avoid 'naked' ti->rinfo accesses
- [TLM]: link notif DE to primary UUID only when first seen
- [TLM]: protect TDE updates on reset
- [TLM]: review tde->mtx locking
- [TLM]: review line.mtx locking
- [TLM]: refactor reply processing code on DE enable/disable
- [TLM]: add new mutex to protect des cache updates
- [TLM]: add more overflow and boundary check on payload parsing
- [TLM]: fix UUID/BLKTS line refcounting to avoid leaks on free
- [TLM]: cleanup exit path on resource enumeration
- [TLM]: account for ktime_t notification field when checking boundary
v9 --> v10
- rebased on v7.2-rc7
- fixed trivial conflict issue when applying patch 5
v8 --> v9
- [SYS/TLM] reworked ANON fds processing to delay fd_install to the very
last moment when failure is no more possible
- [SYS/TLM] reworked cdev and data lifecycle to cleanly handle driver
unbind while chardev fds are open, avoiding any devres allocation
- [SYS/TLM] added sensible upper-bounds checks on allocations
- [SYS/TLM] used kvmalloc and friends all over when allocation sizes
originates from userspace or FW
- [SYS/TLM] reworked BATCH helpers to support batch.items=0
- [SYS/TLM] reworked ALL the _LIST IOCTLs to use scmi_tlm_batch container
and related helpers
- [SYS/TLM] support 0-items IOCTL idiom
- [ABI] dropped custom container structure in favour of scmi_tlm_batch for
all of the _LIST IOCVTLs
- [TLM] use kvzalloc when size is potentially important
- [TLM] avoid NULL deref on DBG
- [TLM] removed initial state lookup on RESET
- [TLM] fix notification payload boundary and wraps checks
- [TLM] added check on unexpected shorter reply on simpl reads
- [TLM] use non-LE accessors when appropriate for payloads
- [TLM] converted a few macros to inlines
- [TLM] fix DE offsest boundary check logic
- [TLM] bail out if SHMTI not 32-bit aligned
- [TLM] warn if SHMTI not 64-bit aligned
- [DRV] added non-devres protocol operations helpers
v7 --> v8
- Rebased on v7.2-rc6
- [SYS/TLM] refatcor batched IOCTLs handling
- [SYS/TLM] use array_size to check for overflows all over
- [SYS/TLM] add upper bounds checks for batched IOCTL
- [ABI] fix wrong strcut in SCMI_TLM_BATCH_READ
- [ABI] move a few IOCTLs to be _IOW only
- [TLM] reworked locking around TDCF lines tracking
- [TLM] add proper barriers to TDE/UUID binding process
- [TLM] update tde->sid on SCAN_UPDATE too
- [TLM] check wrap-arounds on shmti_de_offset manipulation
- [TLM] remove .all_disable telemetry_operation since unused
- [TLM] add WARN on if a valid SHMTI ID is reported o
enabling a FastChannel/DE
- [TLM] add more safeguards and checks to SHMTI_OVERFLOWS
- [TLM] fix UUIDs copy and allocation
- [TLM] fix UUID reference store and cleanup
- [TLM] add proper msg boundary parsing checks on DE_DESCRIPTION multipart
- [TLM] add proper cleanup/rollback on groups_init err_path
- [TLM] update num_des_tstamp ONLY after a DE is successfully initialized
- [TLM] proper cleanup tde during tde_put (preserving info/mutex bits)
- [TLM] add membarriers in enumerate resources
- [TLM] add a check and a warn on SHMTI phys_addr alignment
- [TLM] use proper FTRACE helpers for variable strings
v6 --> v7
- [ABI] IOCTL EVENTS support: GENERATION COUNTER (stlm monitor)
- [ABI] expose DE tracking: UUID/SHMTI/OFFS
- [ABI] IOCTL BATCHED DE_CFG
- [ABI] added per-DE IOCTL BATCH status
- [ABI] new ABI feats flags
- [ABI] improved description in Doxygen docs
- [ABI] added reserved space to grow
- [STLM] added new ABI feats support (generation, UUIDs, location...)
- [SYS/TLM] fixed module_init error path
- [SYS/TLM] fixed interval DISCRETE flags reporting
- [SYS/TLM] check open FMODE before executing a config change
- [SYS/TLM] fixing DEs data cleanup on RESET
- [SYS/TLM] added _RAW helpers to handle non-MMIO TDCF-like accesses
(like Notification payload)
- [SYS/TLM] added UUID rescan logic at DE enable, when DE/UUID association
- [TLM] reworked UUID internal handling (endianity) and use uuid_t type
- [TLM] added generic Telemetry protocol support for events subscription
unknown
- residual sparse fixes
v5 --> v6
- rebased on v7.2-rc4
- fixed a lot of Sashiko complains
- fixed ABI issues reported by review (Fayssal)
- added IOCTL TLM_RESET
- added ABI versioning
- added compat_ioctl support
- use a new IOCTL magic name
- better handling of UUID endianity
- reworked all bounds checks in UAPI implementation
- reject oversized SHMTI mmap request
- refined 'stlm' testing tool to be more interactive and to
exercise the full spectrum of IOCTLs ('stlm -h' is your manual
for now :P)
- fully reworked per-protocol notification handling
- dropped the last bit of human readable data attached to the
chardev .read
v4 --> v5
- rebased on v7.2-rc1
- dropped FileSystem based driver
- introduced a new simple chardev SCMI driver using Telemetry
- reworked/reviewed the v4 IOCTLs based UAPI
- UAPI: better struct alignment and comments
- UAI: Removed flexible array members
- UAPI: make SCMI Telemetry protocol stack completely independent from
uapi defs
- UAPI: new ioctls support to enable RAW mmap direct access to SCMI SHMTI
areas from userspace
- added new ABI Documentation
- added new SCMI core facility to lookup the current SCMI instance ID
v3 --> v4
- rebased on v7.1-rc7
- updatded doc to detail Concurrency model
- bail out on FW_BUG errors
- make all_des_enable/all_des_tstamp_enable entry readable
- refactored access to TDE values
- refactored common accessors for tlm_priv (FIX WARN on kfree)
- make all files by default world readable and user writable (if needed)
- added uid/god/umask mount options (and docs)
- added generation counter to aid spotting config changes (and docs)
- added DebugFS configurable support to debug/dump SHMTI areas (and docs)
- hide FS entries when NOT supported (like des_simple_sample_read)
- fixed output format of des/<NNN>/value to -> <TS> <VALUE>
- renamed top-dir by_components to by-components
- add a .remove method to SCMI System Telemetry Driver
- use kzalloc_obj
V2 --> V3
- rebased on v7.0-rc5
- ported the firmware interface to SCMI v4.0 BETA
- split the SCMI protocol layer in a lot of small patches
- completd filesystem and ABI documentation
- renamed components subtree to by_components
- fixed uninitialized var in scmi_telemetry_de_subdir_symlink
- renamd tstamp_exp to tstamp_rate
- swap logic in scmi_telemetry_initial_state_lookup
- use memcpy_from_le32 where required
- changed a dfew dev_err into Telemetry traces
- define and use new helper scmi_telemetry_de_unlink
- simplify a few assignments with ternary ops
- added a missing __mmust_check on the internal SCMI API
- reworked and clarified de_data_read returned errno:
ENODATA vs EINVAL vs ENODEV/ENOENT
- removed some risky/unneeded devres allocations
- various checkpatch fixes
- reworked and clarified usage of traces in Telemetry
- added the missing DT binding for protocol 0x1B
- split out unrelated change around notification from patch
adding support for protocol internal notifier
- more comments
V1 --> V2
- rebased on v6.19-rc3
- harden TDCF shared memory areas accesses by using proper accessors
- reworked protocol resources lifecycle to allow lazy enumeration
- using NEW FS mount API
- reworked FS inode allocation to use a std kmem_cache
- fixed a few IOCTLs support routine to support lazy enumeration
- added (RFC) a new FS lazy mount option to support lazily population of
some subtrees of the FS (des/ groups/ components/)
- reworked implementation of components/ alternative FS view to use
symlinks instead of hardlinks
- added a basic simple (RFC) testing tool to exercise UAPI ioctls interface
- hardened Telmetry protocol and driver to support partial out-of-spec FW
lacking some cmds (best effort)
- reworked probing races handling
- reviewed behaviour on unmount/unload
- added support for Boot_ON Telemetry by supporting SCMI Telemetry cmds:
+ DE_ENABLED_LIST
+ CONFIG_GET
- added FS and ABI docs
RFC --> V1
---
- moved from SysFS/chardev to a full fledged FS
- added support for SCMI Telemetry BLK timestamps
[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_V14
Cristian Marussi (31):
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
firmware: arm_scmi: Introduce protocol instance notifiers
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 groups initialization
firmware: arm_scmi: Add Telemetry 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 configuration operations
firmware: arm_scmi: Add Telemetry DataEvent read capabilities
firmware: arm_scmi: Add Telemetry reset logic
firmware: arm_scmi: Add Telemetry notification support
firmware: arm_scmi: Add Telemetry boot-on support
firmware: arm-scmi: Add Telemetry generic event support
firmware: arm_scmi: Add Telemetry generation counter event
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
firmware: arm_scmi: Add un-managed methods to get/put protocols
operations
uapi: Add ARM SCMI Telemetry definitions
firmware: arm_scmi: Add 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 | 148 +
MAINTAINERS | 3 +
drivers/firmware/arm_scmi/Kconfig | 24 +
drivers/firmware/arm_scmi/Makefile | 3 +-
drivers/firmware/arm_scmi/common.h | 30 +
drivers/firmware/arm_scmi/driver.c | 192 +-
drivers/firmware/arm_scmi/notify.c | 252 +-
drivers/firmware/arm_scmi/notify.h | 8 +-
drivers/firmware/arm_scmi/protocols.h | 17 +
.../firmware/arm_scmi/scmi_system_telemetry.c | 1751 +++++++
drivers/firmware/arm_scmi/telemetry.c | 4523 +++++++++++++++++
include/linux/scmi_protocol.h | 266 +-
include/trace/events/scmi.h | 45 +
include/uapi/linux/scmi.h | 522 ++
tools/testing/scmi/Makefile | 25 +
tools/testing/scmi/stlm.c | 1635 ++++++
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 | 1810 +++++++
26 files changed, 11500 insertions(+), 61 deletions(-)
create mode 100644 Documentation/userspace-api/stlm.rst
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 v14 01/32] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-19 20:34 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 02/32] firmware: arm_scmi: Allow registration of unknown-size events/reports Cristian Marussi
` (30 subsequent siblings)
31 siblings, 1 reply; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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, so it needs to be
distinguishable.
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
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/common.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi/common.h
index fe8c22cfb9f7..678258bc7a93 100644
--- a/drivers/firmware/arm_scmi/common.h
+++ b/drivers/firmware/arm_scmi/common.h
@@ -48,6 +48,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[] = {
@@ -63,6 +65,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 v14 02/32] firmware: arm_scmi: Allow registration of unknown-size events/reports
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 01/32] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 03/32] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Cristian Marussi
` (29 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 25 ++++++++++++++++++++-----
drivers/firmware/arm_scmi/notify.h | 8 ++++++--
2 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c
index fb60fb07c0ed..7d1bb424a723 100644
--- a/drivers/firmware/arm_scmi/notify.c
+++ b/drivers/firmware/arm_scmi/notify.c
@@ -595,7 +595,13 @@ 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.
+ */
+ 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;
}
@@ -752,7 +758,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;
@@ -767,6 +773,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;
@@ -779,8 +788,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,
@@ -807,7 +821,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 v14 03/32] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 01/32] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 02/32] firmware: arm_scmi: Allow registration of unknown-size events/reports Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-19 20:46 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 04/32] firmware: arm_scmi: Introduce protocol instance notifiers Cristian Marussi
` (28 subsequent siblings)
31 siblings, 1 reply; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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.
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.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
drivers/firmware/arm_scmi/notify.c | 73 +++++++++++++++++++-----------
1 file changed, 46 insertions(+), 27 deletions(-)
diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c
index 7d1bb424a723..2bc2e03df639 100644
--- a/drivers/firmware/arm_scmi/notify.c
+++ b/drivers/firmware/arm_scmi/notify.c
@@ -890,7 +890,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)
*
@@ -903,8 +902,7 @@ void scmi_deregister_protocol_events(const struct scmi_handle *handle,
* 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;
@@ -914,8 +912,20 @@ scmi_allocate_event_handler(struct scmi_notify_instance *ni, u32 evt_key)
hndl->key = evt_key;
BLOCKING_INIT_NOTIFIER_HEAD(&hndl->chain);
refcount_set(&hndl->users, 1);
- /* New handlers are created pending */
- hash_add(ni->pending_events_handlers, &hndl->hash, hndl->key);
+
+ 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)
+ hash_add(ni->pending_events_handlers, &hndl->hash, hndl->key);
return hndl;
}
@@ -1084,7 +1094,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));
@@ -1092,39 +1103,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 v14 04/32] firmware: arm_scmi: Introduce protocol instance notifiers
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (2 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 03/32] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-19 21:02 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 05/32] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Cristian Marussi
` (27 subsequent siblings)
31 siblings, 1 reply; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 Protocol notifications are typically used by SCMI drivers to detect
and react to particular conditions: this was the assumption and the classic
usage scenario upon which the SCMI notification framework was built.
Some protocols like Telemetry, though, could be interested in enabling and
registering for such notifications: in such a scenario each initialized
protocol instance could want to register a common protocol notifier.
In order to reuse the existing SCMI Notifications machinery, allow
protocols themselves to register for their own notifications and provide
their own notifier callbacks.
Each protocol, at the end of its initialization phase, can now provide one
unique per-protocol-instance notifier block, which will be registered, by
the core SCMI stack, on the related notification chain: such notifier will
be automatically removed during the protocol de-initialization phase.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v13 --> v14
- reworked the helpers used to register protocol notifiers so as to
possibly worked during the protocol initialization phase when locks
are enabled
v12 --> v13
- refactored a few IFs
v10 -->v11
- added a few dev_dbg
v8 --> v9
- Added membarrier comment
v7 --> v8
- Reworked logic around protocol notifier registration to address
possible races as pointed out by Sashiko
- Reviewed commit message
v5 -->v6
- move protocol notifier registration out of proto_init
v2-->v3
- split out unrelated changes on event sizing
v1-->v2
- Fixed multiline comment format
---
drivers/firmware/arm_scmi/common.h | 24 ++++
drivers/firmware/arm_scmi/driver.c | 115 +++++++++++++++----
drivers/firmware/arm_scmi/notify.c | 154 ++++++++++++++++++++++++++
drivers/firmware/arm_scmi/protocols.h | 10 ++
4 files changed, 282 insertions(+), 21 deletions(-)
diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi/common.h
index 678258bc7a93..c31896672b81 100644
--- a/drivers/firmware/arm_scmi/common.h
+++ b/drivers/firmware/arm_scmi/common.h
@@ -18,6 +18,7 @@
#include <linux/list.h>
#include <linux/module.h>
#include <linux/property.h>
+#include <linux/notifier.h>
#include <linux/refcount.h>
#include <linux/scmi_protocol.h>
#include <linux/spinlock.h>
@@ -162,7 +163,9 @@ struct scmi_device *scmi_device_create(struct device_node *np,
void scmi_device_destroy(struct device *parent, int protocol, const char *name);
int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id);
+int scmi_protocol_acquire_unlocked(const struct scmi_handle *handle, u8 protocol_id);
void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id);
+void scmi_protocol_release_unlocked(const struct scmi_handle *handle, u8 protocol_id);
/* SCMI Transport */
/**
@@ -687,8 +690,29 @@ static struct platform_driver __drv = { \
.probe = __tag##_probe, \
}
+struct scmi_protocol_notifier {
+ u8 proto_id;
+ u8 evt_id;
+ const u32 *src_id;
+ struct notifier_block *nb;
+ bool registered;
+};
+
void scmi_notification_instance_data_set(const struct scmi_handle *handle,
void *priv);
void *scmi_notification_instance_data_get(const struct scmi_handle *handle);
+
+int scmi_protocol_notifier_register(const struct scmi_handle *handle,
+ struct scmi_protocol_notifier *pno);
+int scmi_protocol_notifier_unregister(const struct scmi_handle *handle,
+ struct scmi_protocol_notifier *pno);
+
+static inline bool
+scmi_protocol_notifier_registered(struct scmi_protocol_notifier *pno)
+{
+ /* Ensure registered is visible */
+ return smp_load_acquire(&pno->registered);
+}
+
int scmi_inflight_count(const struct scmi_handle *handle);
#endif /* _SCMI_COMMON_H */
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index a666ad39acdf..929f3782ee36 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -91,6 +91,8 @@ struct scmi_xfers_info {
* @gid: A reference for per-protocol devres management.
* @users: A refcount to track effective users of this protocol.
* @priv: Reference for optional protocol private data.
+ * @pno: A protocol instance notifier descriptor: active only when the
+ * included notifier block @nb is non-NULL.
* @version: Protocol version supported by the platform as detected at runtime.
* @negotiated_version: When the platform supports a newer protocol version,
* the agent will try to negotiate with the platform the
@@ -110,6 +112,7 @@ struct scmi_protocol_instance {
void *gid;
refcount_t users;
void *priv;
+ struct scmi_protocol_notifier pno;
unsigned int version;
unsigned int negotiated_version;
struct scmi_protocol_handle ph;
@@ -1656,6 +1659,24 @@ static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
return pi->priv;
}
+static int
+scmi_register_instance_notifier(const struct scmi_protocol_handle *ph, u8 evt_id,
+ const u32 *src_id, struct notifier_block *nb)
+{
+ struct scmi_protocol_instance *pi = ph_to_pi(ph);
+
+ pi->pno.proto_id = pi->proto->id;
+ pi->pno.evt_id = evt_id;
+ pi->pno.src_id = src_id;
+ /*
+ * Ensure the descriptor fields are visibile when the notifier block is
+ * made available
+ */
+ smp_store_release(&pi->pno.nb, nb);
+
+ return 0;
+}
+
static const struct scmi_xfer_ops xfer_ops = {
.xfer_get_init = xfer_get_init,
.reset_rx_to_maxsz = reset_rx_to_maxsz,
@@ -2256,6 +2277,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
pi->ph.hops = &helpers_ops;
pi->ph.set_priv = scmi_set_protocol_priv;
pi->ph.get_priv = scmi_get_protocol_priv;
+ pi->ph.instance_notifier_register = scmi_register_instance_notifier;
refcount_set(&pi->users, 1);
/*
@@ -2317,12 +2339,12 @@ 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;
- mutex_lock(&info->protocols_mtx);
- pi = idr_find(&info->protocols, protocol_id);
+ guard(mutex)(&info->protocols_mtx);
+ pi = idr_find(&info->protocols, protocol_id);
if (pi) {
refcount_inc(&pi->users);
} else {
@@ -2330,16 +2352,44 @@ scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
/* 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);
+ if (!proto)
+ return ERR_PTR(-EPROBE_DEFER);
+
+ pi = scmi_alloc_init_protocol_instance(info, proto);
+ if (IS_ERR(pi))
+ return pi;
+
+ /*
+ * A unique protocol notifier, when present, is registered
+ * after the protocol itself and all of its supported events
+ * have been successfully initialized.
+ */
+ if (smp_load_acquire(&pi->pno.nb) &&
+ scmi_protocol_notifier_register(pi->handle, &pi->pno))
+ dev_warn(handle->dev,
+ "Failed to register protocol [0x%X] notifier\n",
+ proto->id);
}
- mutex_unlock(&info->protocols_mtx);
return pi;
}
+int scmi_protocol_acquire_unlocked(const struct scmi_handle *handle, u8 protocol_id)
+{
+ struct scmi_info *info = handle_to_scmi_info(handle);
+ struct scmi_protocol_instance *pi;
+
+ lockdep_assert_held(&info->protocols_mtx);
+
+ pi = idr_find(&info->protocols, protocol_id);
+ if (WARN_ON(!pi))
+ return -ENODEV;
+
+ refcount_inc(&pi->users);
+
+ return 0;
+}
+
/**
* scmi_protocol_acquire - Protocol acquire
* @handle: A reference to the SCMI platform instance.
@@ -2355,23 +2405,32 @@ int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
}
-/**
- * scmi_protocol_release - Protocol de-initialization helper.
- * @handle: A reference to the SCMI platform instance.
- * @protocol_id: The protocol being requested.
- *
- * Remove one user for the specified protocol and triggers de-initialization
- * and resources de-allocation once the last user has gone.
- */
-void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
+void scmi_protocol_release_unlocked(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);
+ lockdep_assert_held(&info->protocols_mtx);
+
pi = idr_find(&info->protocols, protocol_id);
if (WARN_ON(!pi))
- goto out;
+ return;
+
+ /*
+ * If a protocol notifier was registered and this is the last instance
+ * releasing the protocol, un-register the notifier: note that the
+ * notifier itself counts as one user, as for any other regular notifier,
+ * so if a protocol notifier was registered and there are only 2 users
+ * active we can derive that this is the last protocol instance
+ * de-registering.
+ */
+ if (scmi_protocol_notifier_registered(&pi->pno) &&
+ refcount_read(&pi->users) == 2 &&
+ scmi_protocol_notifier_unregister(pi->handle, &pi->pno))
+ dev_err(handle->dev,
+ "Failed to release protocol [0x%X] notifier\n",
+ protocol_id);
if (refcount_dec_and_test(&pi->users)) {
void *gid = pi->gid;
@@ -2390,9 +2449,23 @@ void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
protocol_id);
}
+}
-out:
- mutex_unlock(&info->protocols_mtx);
+/**
+ * scmi_protocol_release - Protocol de-initialization helper.
+ * @handle: A reference to the SCMI platform instance.
+ * @protocol_id: The protocol being requested.
+ *
+ * Remove one user for the specified protocol and triggers de-initialization
+ * and resources de-allocation once the last user has gone.
+ */
+void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
+{
+ struct scmi_info *info = handle_to_scmi_info(handle);
+
+ guard(mutex)(&info->protocols_mtx);
+
+ scmi_protocol_release_unlocked(handle, protocol_id);
}
void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c
index 2bc2e03df639..c51f48894f15 100644
--- a/drivers/firmware/arm_scmi/notify.c
+++ b/drivers/firmware/arm_scmi/notify.c
@@ -1059,6 +1059,44 @@ static int scmi_register_event_handler(struct scmi_notify_instance *ni,
return ret;
}
+static inline struct scmi_event_handler *
+scmi_create_registered_handler(struct scmi_notify_instance *ni, u32 evt_key)
+{
+ struct scmi_registered_event *r_evt;
+ struct scmi_event_handler *hndl;
+ int ret;
+
+ /* Protocol/events MUST have been registered by now */
+ r_evt = SCMI_GET_REVT(ni, KEY_XTRACT_PROTO_ID(evt_key),
+ KEY_XTRACT_EVT_ID(evt_key));
+
+ if (WARN_ON(!r_evt))
+ return ERR_PTR(-ENODEV);
+
+ if (r_evt->not_supported_by_platform)
+ return ERR_PTR(-EOPNOTSUPP);
+
+ /* Create if still not found and required */
+ hndl = scmi_allocate_event_handler(evt_key);
+ if (!hndl)
+ return ERR_PTR(-ENOMEM);
+
+ ret = scmi_protocol_acquire_unlocked(ni->handle,
+ KEY_XTRACT_PROTO_ID(hndl->key));
+ if (ret) {
+ scmi_put_handler_unlocked(ni, hndl);
+ return ERR_PTR(ret);
+ }
+
+ /* Being created registered it never pass by pending list */
+ hndl->r_evt = r_evt;
+
+ guard(mutex)(&r_evt->proto->registered_mtx);
+ hash_add(r_evt->proto->registered_events_handlers, &hndl->hash, hndl->key);
+
+ return hndl;
+}
+
/**
* __scmi_event_handler_get_ops() - Utility to get or create an event handler
* @ni: A reference to the notification instance to use
@@ -1436,6 +1474,67 @@ static int scmi_notifier_register(const struct scmi_handle *handle,
return ret;
}
+/**
+ * scmi_protocol_notifier_register - Register the protocol notifier
+ * @handle: The handle identifying the platform instance against which the
+ * callback is registered
+ * @pno: The protocol notifier descriptor
+ *
+ * A protocol can optionally register a single protocol notifier callback
+ * during its initialization phase: the notifier_block will be the registered
+ * using the usual common SCMI notification framework.
+ *
+ * This custom wrapper function is used to register such a notifier since, by
+ * construction, when a protocol notifier is registered:
+ * - it is the first notifier to be registered
+ * - the related protocol and its events have been already initialized so this
+ * notifier will never need to transition from the pending events list
+ *
+ * Return: 0 on Success
+ */
+int scmi_protocol_notifier_register(const struct scmi_handle *handle,
+ struct scmi_protocol_notifier *pno)
+{
+ struct scmi_event_handler *hndl;
+ struct scmi_notify_instance *ni;
+ u32 evt_key;
+ int ret;
+
+ ni = scmi_notification_instance_data_get(handle);
+ if (!ni)
+ return -ENODEV;
+
+ /* Ensure registered was visible */
+ if (smp_load_acquire(&pno->registered))
+ return -EBUSY;
+
+ evt_key = MAKE_HASH_KEY(pno->proto_id, pno->evt_id,
+ pno->src_id ? *pno->src_id : SRC_ID_MASK);
+
+ /* This protocol handler notifier cannot exist already at this point */
+ hndl = scmi_create_registered_handler(ni, evt_key);
+ if (IS_ERR(hndl))
+ return PTR_ERR(hndl);
+
+ blocking_notifier_chain_register(&hndl->chain, pno->nb);
+ ret = scmi_event_handler_enable_events(hndl);
+ if (ret) {
+ /* When enable fails throw the handler away */
+ scmi_put_handler_unlocked(ni, hndl);
+ /* Drop protocol refcount acquired after creation */
+ scmi_protocol_release_unlocked(handle, pno->proto_id);
+ return ret;
+ }
+
+ /* Make sure registration is visible */
+ smp_store_release(&pno->registered, true);
+
+ dev_dbg(handle->dev,
+ "Registered SCMI protocol [0x%X] notifier\n", pno->proto_id);
+
+ return 0;
+}
+
/**
* scmi_notifier_unregister() - Unregister a notifier_block for an event
* @handle: The handle identifying the platform instance against which the
@@ -1493,6 +1592,61 @@ static int scmi_notifier_unregister(const struct scmi_handle *handle,
return 0;
}
+int scmi_protocol_notifier_unregister(const struct scmi_handle *handle,
+ struct scmi_protocol_notifier *pno)
+{
+ struct scmi_registered_event *r_evt;
+ struct scmi_event_handler *hndl;
+ struct scmi_notify_instance *ni;
+ u32 evt_key;
+ bool freed;
+
+ ni = scmi_notification_instance_data_get(handle);
+ if (!ni)
+ return -ENODEV;
+
+ evt_key = MAKE_HASH_KEY(pno->proto_id, pno->evt_id,
+ pno->src_id ? *pno->src_id : SRC_ID_MASK);
+
+ r_evt = SCMI_GET_REVT(ni, KEY_XTRACT_PROTO_ID(evt_key),
+ KEY_XTRACT_EVT_ID(evt_key));
+
+ if (WARN_ON(!r_evt))
+ return -ENODEV;
+
+ /* Make sure de-registration is visible BEFORE calling unregister */
+ smp_store_release(&pno->registered, false);
+
+ scoped_guard(mutex, &r_evt->proto->registered_mtx) {
+ hndl = KEY_FIND(r_evt->proto->registered_events_handlers,
+ hndl, evt_key);
+ if (!hndl) {
+ /* Visibly revert registered state on failure */
+ smp_store_release(&pno->registered, true);
+ return -ENOMEM;
+ }
+
+ blocking_notifier_chain_unregister(&hndl->chain, pno->nb);
+
+ freed = scmi_put_handler_unlocked(ni, hndl);
+ /* This must cause the handler to be freed at this point */
+ WARN_ON(!freed);
+ }
+
+ /*
+ * Releasing the protocol for this notifier only here AFTER unlocking
+ * registered_mtx, since releasing the protocol itself can trigger its
+ * de-initialization (ie. including r_evt and registered_mtx)
+ */
+ if (freed)
+ scmi_protocol_release_unlocked(handle, pno->proto_id);
+
+ dev_dbg(handle->dev,
+ "Unregistered SCMI protocol [0x%X] notifier\n", pno->proto_id);
+
+ return 0;
+}
+
struct scmi_notifier_devres {
const struct scmi_handle *handle;
u8 proto_id;
diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_scmi/protocols.h
index 15ad5162e37a..b7f29003a7dc 100644
--- a/drivers/firmware/arm_scmi/protocols.h
+++ b/drivers/firmware/arm_scmi/protocols.h
@@ -166,6 +166,13 @@ struct scmi_proto_helpers_ops;
* can be used by the protocol implementation to generate SCMI messages.
* @set_priv: A method to set protocol private data for this instance.
* @get_priv: A method to get protocol private data previously set.
+ * @instance_notifier_register: A method to register interest for specific
+ * notifications from within a protocol
+ * implementation unit: ONLY one instance notifier
+ * can be registered per-protocol.
+ * The related notifier block will be registered
+ * right after the protocol is initialized and
+ * automatically removed on protocol release.
*
* This structure represents a protocol initialized against specific SCMI
* instance and it will be used as follows:
@@ -185,6 +192,9 @@ struct scmi_protocol_handle {
const struct scmi_proto_helpers_ops *hops;
int (*set_priv)(const struct scmi_protocol_handle *ph, void *priv);
void *(*get_priv)(const struct scmi_protocol_handle *ph);
+ int (*instance_notifier_register)(const struct scmi_protocol_handle *ph,
+ u8 evt_id, const u32 *src_id,
+ struct notifier_block *nb);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 05/32] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (3 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 04/32] firmware: arm_scmi: Introduce protocol instance notifiers Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 06/32] include: trace: Add Telemetry trace events Cristian Marussi
` (26 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 v14 06/32] include: trace: Add Telemetry trace events
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (4 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 05/32] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-19 21:03 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 07/32] firmware: arm_scmi: Add Telemetry basic attributes enumeration Cristian Marussi
` (25 subsequent siblings)
31 siblings, 1 reply; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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.
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 703b7bb68e44..d7c0b2cbd069 100644
--- a/include/trace/events/scmi.h
+++ b/include/trace/events/scmi.h
@@ -180,6 +180,51 @@ TRACE_EVENT(scmi_msg_dump,
__entry->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 v14 07/32] firmware: arm_scmi: Add Telemetry basic attributes enumeration
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (5 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 06/32] include: trace: Add Telemetry trace events Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-19 21:27 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 08/32] firmware: arm_scmi: Add Telemetry resources enumeration Cristian Marussi
` (24 subsequent siblings)
31 siblings, 1 reply; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 148 ++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 33 +++++-
5 files changed, 184 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 929f3782ee36..7b4cb257605d 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -3626,6 +3626,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);
}
@@ -3644,6 +3645,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 b7f29003a7dc..1f9925b8f495 100644
--- a/drivers/firmware/arm_scmi/protocols.h
+++ b/drivers/firmware/arm_scmi/protocols.h
@@ -401,5 +401,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..32f6f8d1106d
--- /dev/null
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -0,0 +1,148 @@
+// 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
+
+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 = le32_to_cpu(resp->groups_num);
+ 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 5ab73b1ab9aa..121c83e1c6d8 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -2,13 +2,14 @@
/*
* 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.h>
#include <linux/notifier.h>
#include <linux/types.h>
@@ -824,6 +825,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 __must_check *(*info_get)
+ (const struct scmi_protocol_handle *ph);
+};
+
/**
* struct scmi_notify_ops - represents notifications' operations provided by
* SCMI core
@@ -930,6 +960,7 @@ enum scmi_std_protocol {
SCMI_PROTOCOL_VOLTAGE = 0x17,
SCMI_PROTOCOL_POWERCAP = 0x18,
SCMI_PROTOCOL_PINCTRL = 0x19,
+ SCMI_PROTOCOL_TELEMETRY = 0x1b,
};
enum scmi_system_events {
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 08/32] firmware: arm_scmi: Add Telemetry resources enumeration
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (6 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 07/32] firmware: arm_scmi: Add Telemetry basic attributes enumeration Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 09/32] firmware: arm_scmi: Add Telemetry groups initialization Cristian Marussi
` (23 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 core Telemetry initialization routines in charge of basic allocations
and DataEvents enumerations.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
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 | 633 ++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 38 ++
2 files changed, 671 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 32f6f8d1106d..87ff75805935 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"
@@ -51,13 +55,189 @@ 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;
+ struct scmi_de_desc desc[];
+};
+
+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;
+ u32 ts_rate;
+ 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(&tde->item);
+
+ return tde;
+}
+
+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);
+
+ guard(mutex)(&ti->free_mtx);
+ /* 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;
+ list_add_tail(&tde->item, &ti->free_des);
+}
+
+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;
+
+ /* Pick a new 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;
+
+ /* ... 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
@@ -99,6 +279,221 @@ 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 += tde->ts_type == TSTAMP_LINE ? 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 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;
+ size_t payld_sz = sizeof(*desc);
+
+ 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 (tde->ts_type == TSTAMP_LINE) {
+ __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 (tde->ts_type == TSTAMP_BLK) {
+ /* Setup default BLK TS value at first */
+ tde->de.info->ts_rate = ti->default_blk_ts_rate;
+ }
+
+ if (tde->de.fc_support) {
+ u32 size;
+ u64 phys_addr;
+ void __iomem *addr;
+ struct de_desc_fastchannel *dfc;
+
+ dfc = *next;
+ phys_addr = le32_to_cpu(dfc->addr_low);
+ phys_addr |= (u64)le32_to_cpu(dfc->addr_high) << 32;
+
+ size = le32_to_cpu(dfc->size);
+ addr = devm_ioremap(ti->ph->dev, 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);
+ }
+
+ 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;
+ }
+
+ /* Count timestamped DEs */
+ ti->num_des_tstamp += !!tde->de.tstamp_support;
+
+ *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 discovered = false;
+ unsigned int de_id;
+ int ret;
+
+ de_id = le32_to_cpu(desc->de_id);
+ /* Check if this DE has already been discovered by other means... */
+ 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);
+
+ discovered = 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 (discovered)
+ scmi_telemetry_free_tde_put(ti, tde);
+ return ret;
+ }
+
+ /* Now fully enumerated */
+ tde->enumerated = true;
+
+ if (discovered) {
+ /* Register if it was not already ... */
+ 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;
+ }
+ }
+
+ /* Add to FastChannels list */
+ if (tde->de.fc_support)
+ list_add(&tde->item, &ti->fcs_des);
+
+ return 0;
+}
+
+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)
{
@@ -107,10 +502,242 @@ scmi_telemetry_info_get(const struct scmi_protocol_handle *ph)
return &ti->info;
}
+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);
+
+ ti->res_get(ti);
+ return xa_load(&ti->xa_des, id);
+}
+
+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_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.
+ *
+ * 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);
+ guard(mutex)(&tdes[i].mtx);
+
+ /* Bind contiguous DE info structures */
+ tdes[i].de.info = &dei_store[i];
+ scmi_telemetry_free_tde_put(ti, &tdes[i]);
+ }
+
+ INIT_LIST_HEAD(&ti->fcs_des);
+
+ ti->tdes = no_free_ptr(tdes);
+
+ 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);
+
+ 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_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);
+ }
+
+ xa_destroy(&ti->xa_des);
+}
+
+static void scmi_telemetry_instance_cleanup(void *arg)
+{
+ struct telemetry_info *ti = arg;
+
+ scmi_telemetry_tdes_cleanup(ti);
+ 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 and Groups, 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 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;
+ }
+
+ ti->rinfo->fully_enumerated = true;
+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;
+
+ /* Allocate and Initialize on first call... */
+ ret = scmi_telemetry_resources_alloc(ti);
+ if (ret)
+ return ret;
+
+ xa_init(&ti->xa_des);
+
+ /* 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;
@@ -132,6 +759,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 121c83e1c6d8..018427daf55f 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -13,6 +13,7 @@
#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
@@ -825,6 +826,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;
+ struct scmi_telemetry_de_info *info;
+ bool enabled;
+ bool tstamp_enabled;
+};
+
+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;
@@ -848,10 +880,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 __must_check *(*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 __must_check *(*res_get)
+ (const struct scmi_protocol_handle *ph);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 09/32] firmware: arm_scmi: Add Telemetry groups initialization
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (7 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 08/32] firmware: arm_scmi: Add Telemetry resources enumeration Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 10/32] firmware: arm_scmi: Add Telemetry update intervals enumeration Cristian Marussi
` (22 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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
Allocate and initialize Telemetry Groups from base DataEvent data.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v13 --> v14
- splitted out of the old Basic Telemetry patch, contains the
groups initialization code.
---
drivers/firmware/arm_scmi/telemetry.c | 149 +++++++++++++++++++++++++-
include/linux/scmi_protocol.h | 20 ++++
2 files changed, 167 insertions(+), 2 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 87ff75805935..9e3dc2c2cc4b 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -123,6 +123,7 @@ struct telemetry_info {
unsigned int default_blk_ts_rate;
const struct scmi_protocol_handle *ph;
struct telemetry_de *tdes;
+ struct scmi_telemetry_group *grps;
struct xarray xa_des;
/* Mutex to protect access to @free_des */
struct mutex free_mtx;
@@ -321,6 +322,7 @@ static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti,
{
const struct scmi_de_desc *desc = *next;
size_t payld_sz = sizeof(*desc);
+ unsigned int grp_id;
lockdep_assert_held(&tde->mtx);
@@ -328,7 +330,22 @@ static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti,
return -ENOSPC;
tde->de.info->id = le32_to_cpu(desc->de_id);
- tde->de.info->grp_id = le32_to_cpu(desc->group_id);
+ grp_id = le32_to_cpu(desc->group_id);
+ if (grp_id != SCMI_TLM_GRP_INVALID) {
+ struct scmi_telemetry_res_info *rinfo;
+
+ /* Get rinfo without triggering a recursive enumeration */
+ rinfo = __scmi_telemetry_resources_get(ti);
+
+ /* Group descriptors are empty but allocated at this point */
+ if (grp_id >= ti->info.base.num_groups)
+ return -EINVAL;
+
+ /* Link to parent group */
+ tde->de.grp = &rinfo->grps[grp_id];
+ }
+
+ tde->de.info->grp_id = grp_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);
@@ -459,6 +476,96 @@ static int iter_de_descr_process_response(const struct scmi_protocol_handle *ph,
if (tde->de.fc_support)
list_add(&tde->item, &ti->fcs_des);
+ /* Account for this DE in group num_de counter */
+ if (tde->de.grp)
+ tde->de.grp->info->num_des++;
+
+ return 0;
+}
+
+static int
+scmi_telemetry_de_groups_init(struct device *dev, struct telemetry_info *ti)
+{
+ struct scmi_telemetry_res_info *rinfo;
+ unsigned int num_groups = 0;
+
+ /* Get rinfo without triggering a recursive enumeration */
+ rinfo = __scmi_telemetry_resources_get(ti);
+
+ /* Allocate all groups DEs IDs arrays at first ... */
+ for (int i = 0; i < ti->info.base.num_groups; i++, num_groups++) {
+ struct scmi_telemetry_group *grp = &rinfo->grps[i];
+ size_t des_str_sz;
+
+ unsigned int *des __free(kfree) =
+ kzalloc_objs(unsigned int, grp->info->num_des);
+ if (!des)
+ break;
+
+ /*
+ * Max size 32bit ID string in Hex: 0xCAFECAFE
+ * - 10 digits + ' '/'\n' = 11 bytes per number
+ * - terminating NUL character
+ */
+ des_str_sz = grp->info->num_des * 11 + 1;
+ char *des_str __free(kfree) = kzalloc_objs(*des_str, des_str_sz);
+ if (!des_str)
+ break;
+
+ grp->des = no_free_ptr(des);
+ grp->des_str = no_free_ptr(des_str);
+ /* Reset group DE counter */
+ grp->info->num_des = 0;
+ }
+
+ /* Unroll on failure... */
+ if (num_groups < ti->info.base.num_groups) {
+ for (int i = num_groups - 1; i >= 0; i--) {
+ kfree(rinfo->grps[i].des);
+ rinfo->grps[i].des = NULL;
+ kfree(rinfo->grps[i].des_str);
+ rinfo->grps[i].des_str = NULL;
+ }
+
+ return -ENOMEM;
+ }
+
+ /* Scan DEs and populate DE IDs arrays for all groups */
+ for (int i = 0; i < rinfo->num_des; i++) {
+ struct scmi_telemetry_group *grp;
+
+ grp = rinfo->des[i]->grp;
+ if (!grp)
+ continue;
+
+ /*
+ * Note that, at this point, num_des is guaranteed to be
+ * sane (in-bounds) by construction.
+ */
+ grp->des[grp->info->num_des++] = i;
+ }
+
+ /* Build composing DES string */
+ for (int i = 0; i < ti->info.base.num_groups; i++) {
+ struct scmi_telemetry_group *grp = &rinfo->grps[i];
+ size_t bufsize = grp->info->num_des * 11 + 1;
+ char *buf = grp->des_str;
+
+ for (int j = 0; j < grp->info->num_des; j++) {
+ char term = j != (grp->info->num_des - 1) ? ' ' : '\0';
+ int len;
+
+ len = scnprintf(buf, bufsize, "0x%08X%c",
+ rinfo->des[grp->des[j]]->info->id, term);
+
+ buf += len;
+ bufsize -= len;
+ }
+ }
+
+ /* Expose all groups once all fully initialized */
+ rinfo->num_groups = num_groups;
+
return 0;
}
@@ -491,7 +598,7 @@ static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti)
if (ret)
return ret;
- return 0;
+ return scmi_telemetry_de_groups_init(ph->dev, ti);
}
static const struct scmi_telemetry_info *
@@ -561,6 +668,18 @@ static int scmi_telemetry_resources_alloc(struct telemetry_info *ti)
if (!dei_store)
return -ENOMEM;
+ /* Array to hold descriptors of discovered GROUPs */
+ struct scmi_telemetry_group *grps __free(kfree) =
+ kzalloc_objs(*grps, ti->info.base.num_groups);
+ if (!grps)
+ return -ENOMEM;
+
+ /* Allocate a set of contiguous Group info descriptors. */
+ struct scmi_telemetry_grp_info *grps_store __free(kfree) =
+ kzalloc_objs(*grps_store, ti->info.base.num_groups);
+ if (!grps_store)
+ return -ENOMEM;
+
struct scmi_telemetry_res_info *rinfo __free(kfree) =
kzalloc_obj(*rinfo);
if (!rinfo)
@@ -577,12 +696,20 @@ static int scmi_telemetry_resources_alloc(struct telemetry_info *ti)
scmi_telemetry_free_tde_put(ti, &tdes[i]);
}
+ for (int i = 0; i < ti->info.base.num_groups; i++) {
+ grps_store[i].grp_id = i;
+ /* Bind contiguous Group info struct */
+ grps[i].info = &grps_store[i];
+ }
+
INIT_LIST_HEAD(&ti->fcs_des);
ti->tdes = no_free_ptr(tdes);
rinfo->des = no_free_ptr(des);
rinfo->dei_store = no_free_ptr(dei_store);
+ rinfo->grps = no_free_ptr(grps);
+ rinfo->grps_store = no_free_ptr(grps_store);
/* Ensure all of the above assignments are visible */
smp_store_release(&ti->rinfo, no_free_ptr(rinfo));
@@ -611,6 +738,9 @@ static void scmi_telemetry_resources_free(struct telemetry_info *ti)
kfree(rinfo->des);
kfree(rinfo->dei_store);
+ kfree(rinfo->grps);
+ kfree(rinfo->grps_store);
+
kfree(rinfo);
dev_dbg(ti->ph->dev, "SCMI Telemetry resources freed for instance\n");
@@ -632,10 +762,25 @@ static void scmi_telemetry_tdes_cleanup(struct telemetry_info *ti)
xa_destroy(&ti->xa_des);
}
+static void scmi_telemetry_groups_free(struct scmi_telemetry_res_info *rinfo)
+{
+ /* Cannot be NULL here by construction, make sure it remains like that */
+ if (WARN_ON(!rinfo))
+ return;
+
+ for (int i = 0; i < rinfo->num_groups; i++) {
+ struct scmi_telemetry_group *grp = &rinfo->grps[i];
+
+ kfree(grp->des);
+ kfree(grp->des_str);
+ }
+}
+
static void scmi_telemetry_instance_cleanup(void *arg)
{
struct telemetry_info *ti = arg;
+ scmi_telemetry_groups_free(ti->rinfo);
scmi_telemetry_tdes_cleanup(ti);
scmi_telemetry_resources_free(ti);
}
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index 018427daf55f..51d4cbfc0969 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -826,6 +826,22 @@ struct scmi_pinctrl_proto_ops {
int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin);
};
+#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF
+
+struct scmi_telemetry_grp_info {
+ unsigned int grp_id;
+ unsigned int num_des;
+ unsigned int num_intervals;
+};
+
+struct scmi_telemetry_group {
+ bool enabled;
+ bool tstamp_enabled;
+ unsigned int *des;
+ char *des_str;
+ struct scmi_telemetry_grp_info *info;
+};
+
struct scmi_telemetry_de_info {
unsigned int id;
unsigned int grp_id;
@@ -846,6 +862,7 @@ struct scmi_telemetry_de {
bool fc_support;
bool name_support;
struct scmi_telemetry_de_info *info;
+ struct scmi_telemetry_group *grp;
bool enabled;
bool tstamp_enabled;
};
@@ -855,6 +872,9 @@ struct scmi_telemetry_res_info {
unsigned int num_des;
struct scmi_telemetry_de **des;
struct scmi_telemetry_de_info *dei_store;
+ unsigned int num_groups;
+ struct scmi_telemetry_group *grps __counted_by_ptr(num_groups);
+ struct scmi_telemetry_grp_info *grps_store;
};
struct scmi_telemetry_base_info {
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 10/32] firmware: arm_scmi: Add Telemetry update intervals enumeration
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (8 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 09/32] firmware: arm_scmi: Add Telemetry groups initialization Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 11/32] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration Cristian Marussi
` (21 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 available Telemetry update intervals.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v13 --> v14
- splitted out of the old Basic Telemetry patch, contains the
update_intervals enumeration routines
---
drivers/firmware/arm_scmi/telemetry.c | 199 ++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 21 +++
2 files changed, 220 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 9e3dc2c2cc4b..b27d09496b53 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -91,6 +91,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[];
+};
+
enum timestamps {
TSTAMP_NONE = 0,
TSTAMP_LINE = 1,
@@ -601,6 +616,177 @@ static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti)
return scmi_telemetry_de_groups_init(ph->dev, ti);
}
+struct scmi_tlm_ivl_priv {
+ struct device *dev;
+ 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;
+ 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 = {
+ .dev = ph->dev,
+ .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 int
+scmi_telemetry_enumerate_groups_intervals(struct telemetry_info *ti)
+{
+ struct scmi_telemetry_res_info *rinfo;
+
+ if (!ti->info.per_group_config_support)
+ return 0;
+
+ /* Get rinfo without triggering a recursive enumeration */
+ rinfo = __scmi_telemetry_resources_get(ti);
+
+ for (int id = 0; id < rinfo->num_groups; id++) {
+ int ret;
+
+ ret = scmi_tlm_enumerate_update_intervals(ti,
+ &rinfo->grps[id].intervals,
+ id, SPECIFIC_GROUP_DES);
+ if (ret)
+ return ret;
+
+ rinfo->grps_store[id].num_intervals =
+ rinfo->grps[id].intervals->num_intervals;
+ }
+
+ 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)
{
@@ -773,6 +959,7 @@ static void scmi_telemetry_groups_free(struct scmi_telemetry_res_info *rinfo)
kfree(grp->des);
kfree(grp->des_str);
+ kfree(grp->intervals);
}
}
@@ -836,6 +1023,12 @@ scmi_telemetry_resources_enumerate(struct telemetry_info *ti)
goto done;
}
+ ret = scmi_telemetry_enumerate_groups_intervals(ti);
+ if (ret) {
+ dev_err(dev, FW_BUG "Cannot fully enumerate group intervals. Degraded system.\n");
+ goto done;
+ }
+
ti->rinfo->fully_enumerated = true;
done:
/* Disable initialization permanently */
@@ -910,6 +1103,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 51d4cbfc0969..26a18698681a 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -826,8 +826,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)))
+
#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_grp_info {
unsigned int grp_id;
unsigned int num_des;
@@ -840,6 +857,8 @@ struct scmi_telemetry_group {
unsigned int *des;
char *des_str;
struct scmi_telemetry_grp_info *info;
+ unsigned int active_update_interval;
+ struct scmi_telemetry_intervals *intervals;
};
struct scmi_telemetry_de_info {
@@ -893,6 +912,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 v14 11/32] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (9 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 10/32] firmware: arm_scmi: Add Telemetry update intervals enumeration Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 12/32] firmware: arm_scmi: Add Telemetry TDCF areas parsing support Cristian Marussi
` (20 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
v13 --> v14
- splitted out of the old Basic Telemetry patch, contains the
SHMTI areas enumeration routines
---
drivers/firmware/arm_scmi/telemetry.c | 208 ++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 59 ++++++++
2 files changed, 267 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index b27d09496b53..f587cd21849c 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -55,6 +55,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;
@@ -106,6 +119,53 @@ struct scmi_msg_resp_telemetry_update_intervals {
__le32 intervals[];
};
+/* TDCF */
+
+#define _I(__a) (ioread32((void __iomem *)(__a)))
+
+struct prlg {
+ u32 sign_start;
+#define SIGNATURE_START 0x5442474E /* TBGN */
+ u32 match_start;
+ u32 num_qwords;
+ u32 hdr_meta_1;
+#define TDCF_REVISION_GET(x) (le32_get_bits((x)->hdr_meta_1, GENMASK(7, 0)))
+};
+
+struct eplg {
+ u32 match_end;
+ u32 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)
+
+#define TDCF_START_SIGNATURE(x) (_I(&((x)->prlg.sign_start)))
+#define TDCF_END_SIGNATURE(e) (_I(&((e)->sign_end)))
+
+struct telemetry_shmti {
+ u32 flags;
+ void __iomem *base;
+ u32 last_magic;
+ struct scmi_telemetry_shmti_info info;
+};
+
+#define SHMTI_EPLG(s) \
+ ({ \
+ struct telemetry_shmti *_s = (s); \
+ struct eplg __iomem *_eplg; \
+ \
+ _eplg = _s->base + _s->info.len - TDCF_EPLG_SZ; \
+ (_eplg); \
+ })
+
enum timestamps {
TSTAMP_NONE = 0,
TSTAMP_LINE = 1,
@@ -119,6 +179,7 @@ struct telemetry_de {
u32 ts_rate;
bool enumerated;
void __iomem *base;
+ struct eplg __iomem *eplg;
u32 sid;
u32 offset;
/* NOTE THAT DE data_sz is registered in scmi_telemetry_de */
@@ -137,6 +198,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 scmi_telemetry_group *grps;
struct xarray xa_des;
@@ -787,6 +849,146 @@ 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:%pK - START:0x%04X END:0x%04X\n",
+ shmti->info.sid, shmti->base, 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;
+ u64 phys_addr;
+ u32 len;
+
+ 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);
+ phys_addr = le32_to_cpu(desc->addr_low);
+ phys_addr |= (u64)le32_to_cpu(desc->addr_high) << 32;
+
+ if (!IS_ALIGNED(phys_addr, 4)) {
+ dev_err(ph->dev,
+ "SHMTI ID %u is NOT 32-bit aligned (paddr:0x%llX). Abort.\n",
+ shmti->info.sid, phys_addr);
+ return -EINVAL;
+ }
+
+ len = le32_to_cpu(desc->length);
+ if (len < SHMTI_MIN_SIZE) {
+ dev_err(ph->dev, "Invalid length for SHMTI ID:%u len:%u\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:0x%llX)\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;
+
+ return scmi_telemetry_shmti_validate(ph->dev, shmti);
+}
+
+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,
+ };
+ void *iter;
+
+ iter = ph->hops->iter_response_init(ph, &ops, ti->num_shmti,
+ TELEMETRY_LIST_SHMTI,
+ sizeof(u32), ti);
+ if (IS_ERR(iter))
+ return PTR_ERR(iter);
+
+ return ph->hops->iter_response_run(iter);
+}
+
+static int scmi_telemetry_enumerate_shmti(struct telemetry_info *ti)
+{
+ const struct scmi_protocol_handle *ph = ti->ph;
+ struct scmi_telemetry_shmti_info **shmtis;
+ int ret;
+
+ if (!ti->num_shmti)
+ return 0;
+
+ ti->shmti = devm_kcalloc(ph->dev, ti->num_shmti, sizeof(*ti->shmti),
+ GFP_KERNEL);
+ if (!ti->shmti)
+ return -ENOMEM;
+
+ ret = scmi_telemetry_shmti_list(ph, ti);
+ if (ret) {
+ dev_err(ph->dev, "Cannot get SHMTI list descriptors");
+ return ret;
+ }
+
+ /* Expose initialized SHMTIs references */
+ shmtis = devm_kcalloc(ph->dev, ti->num_shmti,
+ sizeof(*shmtis), GFP_KERNEL);
+ if (!shmtis)
+ return -ENOMEM;
+
+ for (int i = 0; i < ti->num_shmti; i++)
+ shmtis[i] = &ti->shmti[i].info;
+
+ ti->info.shmtis = shmtis;
+ 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)
{
@@ -1109,6 +1311,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 26a18698681a..3c09c14b66fa 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -826,6 +826,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_USPECIFIED,
+ 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_USPECIFIED,
+ 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))
@@ -905,6 +956,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;
@@ -914,6 +972,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 v14 12/32] firmware: arm_scmi: Add Telemetry TDCF areas parsing support
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (10 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 11/32] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 13/32] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines Cristian Marussi
` (19 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 570 ++++++++++++++++++++++++++
1 file changed, 570 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index f587cd21849c..04481a3df587 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -14,8 +14,10 @@
#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/slab.h>
#include <linux/sprintf.h>
#include <linux/string.h>
@@ -25,9 +27,14 @@
#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 5
+#define SCMI_TLM_TDCF_RETRY_GAP 1234
+
#define SCMI_TLM_DE_IMPL_NUM_DWORDS 4
enum scmi_telemetry_protocol_cmd {
@@ -123,6 +130,129 @@ 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 */
+ SCAN_DISCOVERY /* Update xa_des: allows for new DEs to be discovered */
+};
+
+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(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(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_RATE(p) PAYLD_ID(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 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_BLK_TS_LINE(x) (LINE_TYPE(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 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_BYTES_RAW(m) ((LINE_LENGTH_WORDS_RAW(m)) * sizeof(u32))
+#define LINE_LENGTH_BYTES(x) (LINE_LENGTH_BYTES_RAW(PAYLD_META(x)))
+
struct prlg {
u32 sign_start;
#define SIGNATURE_START 0x5442474E /* TBGN */
@@ -145,10 +275,19 @@ struct tdcf {
unsigned char payld[];
};
+#define QWORDS(_t) (_I(&(_t)->prlg.num_qwords))
+
#define SHMTI_MIN_SIZE (sizeof(struct tdcf) + TDCF_EPLG_SZ)
#define TDCF_START_SIGNATURE(x) (_I(&((x)->prlg.sign_start)))
+#define TDCF_START_SEQ_GET(x) (_I(&((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_SIGNATURE(e) (_I(&((e)->sign_end)))
+#define TDCF_BAD_END_SEQ GENMASK(31, 0)
struct telemetry_shmti {
u32 flags;
@@ -166,6 +305,35 @@ struct telemetry_shmti {
(_eplg); \
})
+struct telemetry_info;
+
+struct telemetry_line {
+ enum tdcf_line_types type;
+ u32 last_magic;
+ struct payload __iomem *payld;
+ refcount_t users;
+ struct telemetry_info *ti;
+ /* 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 {
+ uuid_t uuid;
+ bool published;
+ struct telemetry_line line;
+};
+
+#define to_uuid_from_line(l) container_of(l, struct telemetry_uuid, line)
+#define to_uuid_from_uuid_t(u) container_of(u, struct telemetry_uuid, uuid)
+
enum timestamps {
TSTAMP_NONE = 0,
TSTAMP_LINE = 1,
@@ -184,7 +352,13 @@ 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, p) (!((t)->bts) || (t)->bts->line.payld != (p))
+ struct telemetry_uuid *uuid;
struct scmi_telemetry_de de;
/* Protect last_val/ts/magic accesses - MUST BE KEPT LAST */
struct mutex mtx;
@@ -192,10 +366,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;
@@ -209,6 +397,7 @@ struct telemetry_info {
struct scmi_telemetry_info info;
atomic_t rinfo_initializing;
struct completion rinfo_initdone;
+ #define PAYLD_HASH_BITS 4
struct scmi_telemetry_res_info *rinfo;
struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti);
};
@@ -216,6 +405,25 @@ struct telemetry_info {
static struct scmi_telemetry_res_info *
__scmi_telemetry_resources_get(struct telemetry_info *ti);
+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]:%u\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)
{
@@ -312,6 +520,27 @@ static int scmi_telemetry_tde_register(struct telemetry_info *ti,
return ret;
}
+static bool
+scmi_telemetry_tde_cache_unchanged(struct telemetry_de *tde, u32 magic)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ return tde->last_magic == magic;
+}
+
+static void
+scmi_telemetry_tde_cache_update(struct telemetry_de *tde, u64 val,
+ u64 *tstamp, u32 *magic)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ 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;
@@ -1018,6 +1247,338 @@ scmi_telemetry_resources_get(const struct scmi_protocol_handle *ph)
return res_get(ti);
}
+static u64
+scmi_telemetry_blkts_read(u32 magic, struct telemetry_de *tde)
+{
+ lockdep_assert_held(&tde->mtx);
+
+ return 0;
+}
+
+static inline struct telemetry_de *
+scmi_telemetry_tde_allocate(struct telemetry_info *ti, u32 de_id,
+ struct payload __iomem *payld)
+{
+ struct telemetry_de *tde;
+
+ tde = scmi_telemetry_tde_get(ti, de_id);
+ if (IS_ERR(tde))
+ return NULL;
+
+ guard(mutex)(&tde->mtx);
+ tde->de.info->id = de_id;
+ tde->de.enabled = true;
+ tde->de.tstamp_enabled = LINE_TS_VALID(payld) || USE_BLK_TS(payld);
+
+ if (scmi_telemetry_tde_register(ti, tde)) {
+ scmi_telemetry_free_tde_put(ti, tde);
+ return NULL;
+ }
+
+ 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);
+
+ return tde;
+}
+
+static inline int
+scmi_telemetry_line_data_parse(struct telemetry_de *tde, u64 *val, u64 *tstamp,
+ struct payload __iomem *payld, u32 magic)
+{
+ u32 meta = PAYLD_META(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(&payld->tsl);
+ }
+ }
+
+ /* Data is always valid since we are NOT handling BLK TS lines here */
+ *val = LINE_DATA_GET(&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
+ * @shmti: A reference to the containing SHMTI area
+ * @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.
+ * - SCAN_DISCOVERY: the most advanced scan available which provides all
+ * the SCAN_LOOKUP features plus discovery capabilities:
+ * any DataLine referring to a previously unknown DE leads
+ * to the allocation of a new DE descriptor.
+ * This mode is used on the first scan at init time, ONLY
+ * if Telemetry was found to be already enabled at boot on
+ * the platform side: this helps to maximize gathered
+ * information when dealing with out of spec firmwares.
+ * Any usage of this discovery mode other than in a boot-on
+ * enabled scenario is discouraged since it can easily
+ * lead to spurious DE discoveries.
+ */
+static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
+ struct payload __iomem *payld,
+ struct telemetry_shmti *shmti,
+ enum scan_mode mode,
+ struct telemetry_block_ts *active_bts,
+ struct telemetry_uuid *active_uuid)
+{
+ struct telemetry_de *tde;
+ u64 val, tstamp = 0;
+ u32 de_id;
+
+ de_id = PAYLD_ID(payld);
+ /* Is this DE ID known ? */
+ tde = scmi_telemetry_tde_lookup(ti, de_id);
+ if (!tde) {
+ if (mode != SCAN_DISCOVERY) {
+ trace_scmi_tlm_access(de_id, "DE_INVALID", 0, 0);
+ return;
+ }
+
+ /* In SCAN_DISCOVERY mode we allocate new DEs for unknown IDs */
+ tde = scmi_telemetry_tde_allocate(ti, de_id, payld);
+ if (!tde) {
+ dev_err(ti->ph->dev,
+ "Cannot allocate TDE for ID:0x%08X\n", de_id);
+ return;
+ }
+ }
+
+ guard(mutex)(&tde->mtx);
+ /* Update DE location refs if requested: normally done only on enable */
+ if (mode >= SCAN_UPDATE) {
+ tde->sid = shmti->info.sid;
+ tde->base = shmti->base;
+ tde->eplg = SHMTI_EPLG(shmti);
+ tde->offset = (void __iomem *)payld - (void __iomem *)shmti->base;
+
+ dev_dbg(ti->ph->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 ?*/
+ if (scmi_telemetry_tde_cache_unchanged(tde, shmti->last_magic))
+ return;
+
+ /*
+ * Parse data words ... only for the sake of updating TDE cache,
+ * any error is logged by ignored.
+ */
+ scmi_telemetry_line_data_parse(tde, &val, &tstamp, payld,
+ shmti->last_magic);
+}
+
+/**
+ * scmi_telemetry_tdcf_line_parse - Line parser
+ * @ti: A reference to the Telemetry instance
+ * @payld: A reference to the payload representing the line to parse
+ * @shmti: A reference to the descriptor of the enclosing SHMTI area
+ * @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,
+ struct payload __iomem *payld,
+ struct telemetry_shmti *shmti,
+ enum scan_mode mode,
+ struct telemetry_block_ts **active_bts,
+ struct telemetry_uuid **active_uuid)
+{
+ int used_qwords;
+
+ used_qwords = LINE_LENGTH_QWORDS(payld);
+ /* Invalid lines are not an error, could simply be disabled DEs */
+ if (DATA_INVALID(payld)) {
+ trace_scmi_tlm_access(PAYLD_ID(payld), "TDCF_INVALID", 0, 0);
+ return used_qwords;
+ }
+
+ switch (LINE_TYPE(payld)) {
+ case TDCF_DATA_LINE:
+ scmi_telemetry_tdcf_data_parse(ti, payld, shmti, mode,
+ *active_bts, *active_uuid);
+ break;
+ default:
+ trace_scmi_tlm_access(PAYLD_ID(payld), "TDCF_LINE_UNKNOWN", 0, 0);
+ break;
+ }
+
+ return used_qwords;
+}
+
+static inline bool
+scmi_telemetry_shmti_overflow(struct telemetry_shmti *shmti, void __iomem *next)
+{
+ void __iomem *limit = shmti->base + shmti->info.len;
+
+ /* Is payld->meta accessible ? */
+ if (next >= limit - sizeof(u32))
+ return true;
+
+ next += LINE_LENGTH_BYTES((struct payload __iomem *)next);
+
+ return next >= limit;
+}
+
+/**
+ * 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 tdcf __iomem *tdcf = shmti->base;
+ int retries = SCMI_TLM_TDCF_MAX_RETRIES;
+ struct telemetry_block_ts *active_bts = NULL;
+ struct telemetry_uuid *active_uuid = NULL;
+ u32 startm, endm;
+
+ if (!tdcf)
+ return -ENODEV;
+
+ do {
+ unsigned int qwords;
+ void __iomem *next;
+
+ /* A bit of exponential backoff between retries */
+ fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP);
+
+ /*
+ * Note that during a full SHMTI scan the magic seq numbers are
+ * checked only at the start and at the end of the scan, NOT
+ * between each parsed line and this has these consequences:
+ * - TDCF magic numbers accesses are reduced to 2 reads
+ * - the set of values obtained from a full scan belong all
+ * to the same platform update (same magic number)
+ * - a SHMTI full scan is an all or nothing operation: when
+ * a potentially corrupted read is detected along the way
+ * (MSEQ_MISMATCH) another full scan is triggered.
+ */
+ 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(0, "MSEQ_BADSTART", startm, 0);
+ continue;
+ }
+
+ /* On a BAD_SEQ this will be updated on the next attempt */
+ shmti->last_magic = startm;
+
+ qwords = QWORDS(tdcf);
+ next = tdcf->payld;
+ while (qwords) {
+ int used_qwords;
+
+ if (scmi_telemetry_shmti_overflow(shmti, next)) {
+ trace_scmi_tlm_access(0, "SHMTI_OVERFLOW", startm, 0);
+ /* Retry, it could due to a concurrent update */
+ break;
+ }
+
+ used_qwords = scmi_telemetry_tdcf_line_parse(ti, next,
+ shmti, mode,
+ &active_bts,
+ &active_uuid);
+ if (!used_qwords || qwords < used_qwords) {
+ trace_scmi_tlm_access(PAYLD_ID(next),
+ "BAD_QWORDS", startm, 0);
+ /* Retry, it could due to a concurrent update */
+ break;
+ }
+
+ next += used_qwords * 8;
+ qwords -= used_qwords;
+ }
+
+ endm = TDCF_END_SEQ_GET(SHMTI_EPLG(shmti));
+ if (startm != endm)
+ trace_scmi_tlm_access(0, "MSEQ_MISMATCH", startm, endm);
+ } while (startm != endm && --retries);
+
+ if (startm != endm) {
+ trace_scmi_tlm_access(0, "TDCF_SCAN_FAIL", startm, endm);
+ return -EPROTO;
+ }
+
+ return 0;
+}
+
static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.info_get = scmi_telemetry_info_get,
.de_lookup = scmi_telemetry_de_lookup,
@@ -1139,6 +1700,12 @@ static void scmi_telemetry_tdes_cleanup(struct telemetry_info *ti)
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);
@@ -1268,6 +1835,9 @@ static int scmi_telemetry_instance_init(struct telemetry_info *ti)
xa_init(&ti->xa_des);
+ 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 v14 13/32] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (11 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 12/32] firmware: arm_scmi: Add Telemetry TDCF areas parsing support Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 14/32] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines Cristian Marussi
` (18 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
v13 --> v14
- split out of previous single patch implementing parse SHMTI routines
---
drivers/firmware/arm_scmi/telemetry.c | 288 +++++++++++++++++++++++++-
1 file changed, 287 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 04481a3df587..69bed616fa04 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>
@@ -157,6 +158,11 @@ struct line {
__le32 data_high;
};
+struct blk_tsline {
+ __le32 ts_low;
+ __le32 ts_high;
+};
+
struct tsline {
__le32 data_low;
__le32 data_high;
@@ -234,6 +240,7 @@ struct payload {
union {
struct line l;
struct tsline tsl;
+ struct blk_tsline blk_tsl;
};
};
@@ -390,6 +397,9 @@ struct telemetry_info {
struct telemetry_de *tdes;
struct scmi_telemetry_group *grps;
struct xarray xa_des;
+ /* Mutex to protect @xa_lines */
+ struct mutex lines_mtx;
+ struct xarray xa_lines;
/* Mutex to protect access to @free_des */
struct mutex free_mtx;
struct list_head free_des;
@@ -398,6 +408,7 @@ struct telemetry_info {
atomic_t rinfo_initializing;
struct completion rinfo_initdone;
#define PAYLD_HASH_BITS 4
+ struct mutex payld_locks[1 << PAYLD_HASH_BITS];
struct scmi_telemetry_res_info *rinfo;
struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti);
};
@@ -408,6 +419,12 @@ __scmi_telemetry_resources_get(struct telemetry_info *ti);
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, struct payload __iomem *payld)
+{
+ return &locks[hash_long((__force unsigned long)payld, 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)
@@ -1250,9 +1267,214 @@ scmi_telemetry_resources_get(const struct scmi_protocol_handle *ph)
static u64
scmi_telemetry_blkts_read(u32 magic, struct telemetry_de *tde)
{
+ struct telemetry_block_ts *bts;
+
+ /* Acquiring order is: tde -> line */
lockdep_assert_held(&tde->mtx);
+ if (WARN_ON(!tde->bts))
+ return 0;
- return 0;
+ bts = tde->bts;
+ guard(mutex)(&bts->line.mtx);
+
+ if (bts->line.last_magic == magic)
+ return bts->last_ts;
+
+ /* Note that the bts->last_rate can change ONLY on creation */
+ bts->last_ts = BLK_TS_STAMP(&bts->line.payld->blk_tsl);
+ bts->line.last_magic = magic;
+
+ return bts->last_ts;
+}
+
+static void scmi_telemetry_blkts_update(struct telemetry_info *ti, u32 magic,
+ struct telemetry_block_ts *bts)
+{
+ guard(mutex)(&bts->line.mtx);
+
+ if (bts->line.last_magic != magic) {
+ bts->last_ts = BLK_TS_STAMP(&bts->line.payld->blk_tsl);
+ bts->last_rate = BLK_TS_RATE(bts->line.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;
+ }
+}
+
+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:
+ dev_warn(line->ti->ph->dev,
+ "Unknown LINE type %u\n", line->type);
+ break;
+ }
+}
+
+static void __scmi_telemetry_line_put(struct telemetry_line *line)
+{
+ lockdep_assert_held(&line->ti->lines_mtx);
+
+ mutex_lock(&line->mtx);
+ if (refcount_dec_and_test(&line->users)) {
+ xa_erase(&line->ti->xa_lines, (unsigned long)line->payld);
+
+ /* 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->ti->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);
+ 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_info *ti, struct payload __iomem *payld)
+{
+ struct telemetry_line *line;
+
+ /* Acquiring coarse grain first ... */
+ guard(mutex)(&ti->lines_mtx);
+ line = xa_load(&ti->xa_lines, (unsigned long)payld);
+ if (!line)
+ return NULL;
+
+ guard(mutex)(&line->mtx);
+ refcount_inc(&line->users);
+
+ return line;
+}
+
+static int
+scmi_telemetry_line_init(struct telemetry_info *ti, struct telemetry_line *line,
+ struct payload __iomem *payld, enum tdcf_line_types type)
+{
+ line->type = type;
+ refcount_set(&line->users, 1);
+ line->payld = payld;
+ line->ti = ti;
+ mutex_init(&line->mtx);
+
+ guard(mutex)(&ti->lines_mtx);
+ return xa_insert(&ti->xa_lines, (unsigned long)payld, line, GFP_KERNEL);
+}
+
+static struct telemetry_block_ts *
+scmi_telemetry_blkts_create(struct telemetry_info *ti,
+ struct payload __iomem *payld)
+{
+ struct telemetry_block_ts *bts;
+ int ret;
+
+ bts = kzalloc_obj(*bts);
+ if (!bts)
+ return NULL;
+
+ ret = scmi_telemetry_line_init(ti, &bts->line, payld, TDCF_BLK_TS_LINE);
+ if (ret) {
+ kfree(bts);
+ return NULL;
+ }
+
+ trace_scmi_tlm_collect(0, (__force u64)payld, 0, "SHMTI_NEW_BLKTS");
+
+ return bts;
+}
+
+static struct telemetry_block_ts *
+scmi_telemetry_blkts_get_or_create(struct telemetry_info *ti,
+ struct payload __iomem *payld)
+{
+ struct telemetry_line *line;
+
+ /* Avoid races on line creation for this payld */
+ guard(mutex)(lock_for_payld(ti->payld_locks, payld));
+
+ line = scmi_telemetry_line_get(ti, payld);
+ if (line)
+ return to_blkts(line);
+
+ return scmi_telemetry_blkts_create(ti, payld);
+}
+
+/**
+ * scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser
+ *
+ * @ti: A reference to the telemetry_info descriptor
+ * @payld: TDCF payld line to process
+ * @shmti: SHMTI descriptor inside which the scan is happening
+ * @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.
+ */
+static void scmi_telemetry_tdcf_blkts_parse(struct telemetry_info *ti,
+ struct payload __iomem *payld,
+ struct telemetry_shmti *shmti,
+ struct telemetry_block_ts **active_bts)
+{
+ struct telemetry_block_ts *bts, *previous = *active_bts;
+
+ /* Check for spec compliance */
+ if (BLK_TS_INVALID(payld)) {
+ trace_scmi_tlm_access(0, "BLK_TS_INVALID", 0, PAYLD_META(payld));
+ return;
+ }
+
+ /* A BLK_TS descriptor MUST be returned: it is found or it is created */
+ bts = scmi_telemetry_blkts_get_or_create(ti, payld);
+ if (WARN_ON(!bts))
+ return;
+
+ /* Update the descriptor with the lastest TS */
+ scmi_telemetry_blkts_update(ti, shmti->last_magic, bts);
+
+ /* 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 struct telemetry_de *
@@ -1325,6 +1547,26 @@ scmi_telemetry_line_data_parse(struct telemetry_de *tde, u64 *val, u64 *tstamp,
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
@@ -1378,11 +1620,18 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
struct telemetry_block_ts *active_bts,
struct telemetry_uuid *active_uuid)
{
+ bool use_blk_ts = USE_BLK_TS(payld);
struct telemetry_de *tde;
u64 val, tstamp = 0;
u32 de_id;
de_id = PAYLD_ID(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) {
@@ -1417,6 +1666,10 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
if (scmi_telemetry_tde_cache_unchanged(tde, shmti->last_magic))
return;
+ /* Link the related BTS when needed, it's unlinked on disable */
+ if (use_blk_ts && BLK_TS_CHANGED(tde, active_bts->line.payld))
+ scmi_telemetry_blkts_link(tde, active_bts);
+
/*
* Parse data words ... only for the sake of updating TDE cache,
* any error is logged by ignored.
@@ -1468,6 +1721,9 @@ static int scmi_telemetry_tdcf_line_parse(struct telemetry_info *ti,
scmi_telemetry_tdcf_data_parse(ti, payld, shmti, mode,
*active_bts, *active_uuid);
break;
+ case TDCF_BLK_TS_LINE:
+ scmi_telemetry_tdcf_blkts_parse(ti, payld, shmti, active_bts);
+ break;
default:
trace_scmi_tlm_access(PAYLD_ID(payld), "TDCF_LINE_UNKNOWN", 0, 0);
break;
@@ -1490,6 +1746,21 @@ scmi_telemetry_shmti_overflow(struct telemetry_shmti *shmti, void __iomem *next)
return next >= limit;
}
+static inline void
+scmi_telemetry_reset_active_lines(struct telemetry_block_ts **active_bts,
+ struct telemetry_uuid **active_uuid)
+{
+ if (*active_bts) {
+ scmi_telemetry_line_put(&(*active_bts)->line);
+ *active_bts = NULL;
+ }
+
+ if (*active_uuid) {
+ scmi_telemetry_line_put(&(*active_uuid)->line);
+ *active_uuid = NULL;
+ }
+}
+
/**
* scmi_telemetry_shmti_scan - Full SHMTI scan
* @ti: A reference to the telemetry info descriptor
@@ -1518,6 +1789,9 @@ static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
/* A bit of exponential backoff between retries */
fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP);
+ /* Reset line trackers between attempts */
+ scmi_telemetry_reset_active_lines(&active_bts, &active_uuid);
+
/*
* Note that during a full SHMTI scan the magic seq numbers are
* checked only at the start and at the end of the scan, NOT
@@ -1571,6 +1845,13 @@ static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
trace_scmi_tlm_access(0, "MSEQ_MISMATCH", startm, endm);
} while (startm != endm && --retries);
+ /*
+ * Put the initial creation reference so that lines will be
+ * effectively dropped when there are no more data lines referencing
+ * them.
+ */
+ scmi_telemetry_reset_active_lines(&active_bts, &active_uuid);
+
if (startm != endm) {
trace_scmi_tlm_access(0, "TDCF_SCAN_FAIL", startm, endm);
return -EPROTO;
@@ -1711,6 +1992,7 @@ static void scmi_telemetry_tdes_cleanup(struct telemetry_info *ti)
guard(mutex)(&tde->mtx);
+ scmi_telemetry_de_unlink(&tde->de);
scmi_telemetry_free_tde_put(ti, tde);
}
@@ -1834,6 +2116,10 @@ static int scmi_telemetry_instance_init(struct telemetry_info *ti)
return ret;
xa_init(&ti->xa_des);
+ xa_init(&ti->xa_lines);
+ mutex_init(&ti->lines_mtx);
+ for (int i = 0; i < 1 << PAYLD_HASH_BITS; i++)
+ mutex_init(&ti->payld_locks[i]);
atomic_set(&ti->des_enabled[ENA_STATE], 0);
atomic_set(&ti->des_enabled[ENA_TSTAMP], 0);
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 14/32] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (12 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 13/32] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 15/32] firmware: arm_scmi: Add Telemetry configuration operations Cristian Marussi
` (17 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
v13 --> v14
- split out of previous single patch implementing parse SHMTI routines
---
drivers/firmware/arm_scmi/telemetry.c | 291 +++++++++++++++++++++++++-
include/linux/scmi_protocol.h | 2 +
2 files changed, 292 insertions(+), 1 deletion(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 69bed616fa04..68e272b1106f 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -18,6 +18,7 @@
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/mutex.h>
+#include <linux/rcupdate.h>
#include <linux/refcount.h>
#include <linux/slab.h>
#include <linux/sprintf.h>
@@ -170,6 +171,10 @@ struct tsline {
__le32 ts_high;
};
+struct uuid_line {
+ __be32 dwords[SCMI_TLM_DE_IMPL_NUM_DWORDS];
+};
+
#define LINE_DATA_GET(f) \
({ \
typeof(f) _f = (f); \
@@ -241,6 +246,7 @@ struct payload {
struct line l;
struct tsline tsl;
struct blk_tsline blk_tsl;
+ struct uuid_line uuid_l;
};
};
@@ -388,6 +394,10 @@ 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;
/* Mutex to protect des_enabled_mtx */
struct mutex des_enabled_mtx;
atomic_t des_enabled[ENA_MAX];
@@ -400,6 +410,7 @@ struct telemetry_info {
/* Mutex to protect @xa_lines */
struct mutex lines_mtx;
struct xarray xa_lines;
+ struct telemetry_uuid *primary_uuid;
/* Mutex to protect access to @free_des */
struct mutex free_mtx;
struct list_head free_des;
@@ -419,6 +430,9 @@ __scmi_telemetry_resources_get(struct telemetry_info *ti);
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, struct payload __iomem *payld)
{
@@ -796,8 +810,10 @@ static int iter_de_descr_process_response(const struct scmi_protocol_handle *ph,
}
/* 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);
+ }
/* Account for this DE in group num_de counter */
if (tde->de.grp)
@@ -1304,6 +1320,7 @@ static void scmi_telemetry_blkts_update(struct telemetry_info *ti, u32 magic,
static inline void scmi_telemetry_line_free(struct telemetry_line *line)
{
+ struct telemetry_uuid *uuid;
void *blob;
switch (line->type) {
@@ -1311,6 +1328,18 @@ static inline void scmi_telemetry_line_free(struct telemetry_line *line)
blob = to_blkts(line);
kfree(blob);
break;
+ case TDCF_UUID_LINE:
+ uuid = to_uuid_from_line(line);
+ /*
+ * By construction a UUID line, once published in the UUIDs
+ * global array, will ONLY be freed on cleanup while being
+ * un-published while coping with the needed RCU machinery.
+ * Warn if this invariant is NOT respected.
+ */
+ if (WARN_ON((smp_load_acquire(&uuid->published))))
+ return;
+ kfree(uuid);
+ break;
default:
dev_warn(line->ti->ph->dev,
"Unknown LINE type %u\n", line->type);
@@ -1357,6 +1386,19 @@ 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;
+
+ scmi_telemetry_line_put(&tde->uuid->line);
+ 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);
@@ -1365,6 +1407,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 *
@@ -1436,6 +1479,145 @@ scmi_telemetry_blkts_get_or_create(struct telemetry_info *ti,
return scmi_telemetry_blkts_create(ti, payld);
}
+static int scmi_telemetry_uuids_update(struct telemetry_info *ti,
+ struct telemetry_uuid *uuid)
+{
+ uuid_t **cur_uuids;
+
+ guard(mutex)(&ti->uuids_mtx);
+
+ /* 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);
+ }
+
+ /* Bump refcount on this line ... cannot fail by construction */
+ scmi_telemetry_line_get(ti, uuid->line.payld);
+ /* 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);
+ uuid->published = true;
+ /* Update UUIDs count */
+ smp_store_release(&ti->info.num_uuids, ti->info.num_uuids + 1);
+
+ return 0;
+}
+
+static struct telemetry_uuid *
+scmi_telemetry_uuid_create(struct telemetry_info *ti,
+ struct payload __iomem *payld)
+{
+ __be32 dwords[SCMI_TLM_DE_IMPL_NUM_DWORDS];
+ struct uuid_line __iomem *uuid_l;
+ struct telemetry_uuid *uuid;
+
+ uuid = kzalloc_obj(*uuid);
+ if (!uuid)
+ return NULL;
+
+ /* A NULL payload is used for the unique primary UUID */
+ if (!payld)
+ return uuid;
+
+ uuid_l = &payld->uuid_l;
+ /*
+ * Use proper mem accessors BUT no swapping, maintain UUID
+ * in memory BE layout.
+ */
+ for (int i = 0; i < SCMI_TLM_DE_IMPL_NUM_DWORDS; i++)
+ dwords[i] = (__force __be32)__raw_readl(&uuid_l->dwords[i]);
+
+ /*
+ * Fetch BigEndian in-memory UUID fields as per SCMIv4.0
+ * specification 3.12.2.2
+ */
+ import_uuid(&uuid->uuid, (__force const __u8 *)&dwords[0]);
+
+ return uuid;
+}
+
+static struct telemetry_uuid *
+scmi_telemetry_uuid_register(struct telemetry_info *ti,
+ struct payload __iomem *payld,
+ struct telemetry_uuid *uuid)
+{
+ if (scmi_telemetry_line_init(ti, &uuid->line, payld, TDCF_UUID_LINE)) {
+ kfree(uuid);
+ return NULL;
+ }
+
+ if (scmi_telemetry_uuids_update(ti, uuid)) {
+ scmi_telemetry_line_put(&uuid->line);
+ return NULL;
+ }
+
+ trace_scmi_tlm_collect(0, (__force u64)payld, 0, "SHMTI_NEW_UUID");
+
+ return uuid;
+}
+
+static struct telemetry_uuid *
+scmi_telemetry_uuid_get_or_create(struct telemetry_info *ti,
+ struct payload __iomem *payld)
+{
+ struct telemetry_line *line;
+ struct telemetry_uuid *uuid;
+
+ guard(mutex)(lock_for_payld(ti->payld_locks, payld));
+ line = scmi_telemetry_line_get(ti, payld);
+ if (line)
+ return to_uuid_from_line(line);
+
+ uuid = scmi_telemetry_uuid_create(ti, payld);
+ if (!uuid)
+ return uuid;
+
+ return scmi_telemetry_uuid_register(ti, payld, uuid);
+}
+
+static void scmi_telemetry_tdcf_uuid_parse(struct telemetry_info *ti,
+ struct payload __iomem *payld,
+ struct telemetry_shmti *shmti,
+ struct telemetry_uuid **active_uuid)
+{
+ struct telemetry_uuid *uuid, *previous = *active_uuid;
+
+ if (UUID_INVALID(payld)) {
+ trace_scmi_tlm_access(0, "UUID_INVALID", 0, 0);
+ return;
+ }
+
+ /* A UUID descriptor MUST be returned: it is found or it is created */
+ uuid = scmi_telemetry_uuid_get_or_create(ti, payld);
+ if (WARN_ON(!uuid))
+ return;
+
+ /* Drop one refcount on the previous active since we found a new one */
+ if (previous)
+ scmi_telemetry_line_put(&previous->line);
+
+ *active_uuid = uuid;
+}
+
/**
* scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser
*
@@ -1567,6 +1749,22 @@ 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)
+{
+ /* Acquiring order is: tde -> line */
+ lockdep_assert_held(&tde->mtx);
+ if (tde->uuid)
+ return;
+
+ guard(mutex)(&uuid->line.mtx);
+ refcount_inc(&uuid->line.users);
+
+ /* 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
@@ -1670,6 +1868,9 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
if (use_blk_ts && BLK_TS_CHANGED(tde, active_bts->line.payld))
scmi_telemetry_blkts_link(tde, active_bts);
+ /* 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 by ignored.
@@ -1724,6 +1925,9 @@ static int scmi_telemetry_tdcf_line_parse(struct telemetry_info *ti,
case TDCF_BLK_TS_LINE:
scmi_telemetry_tdcf_blkts_parse(ti, payld, shmti, active_bts);
break;
+ case TDCF_UUID_LINE:
+ scmi_telemetry_tdcf_uuid_parse(ti, payld, shmti, active_uuid);
+ break;
default:
trace_scmi_tlm_access(PAYLD_ID(payld), "TDCF_LINE_UNKNOWN", 0, 0);
break;
@@ -1976,6 +2180,35 @@ 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 = to_uuid_from_uuid_t(cur_uuids[i]);
+
+ /* Ensure un-publishing is visible */
+ smp_store_release(&uuid->published, false);
+ scmi_telemetry_line_put(&uuid->line);
+ }
+ }
+
+ kfree_rcu_mightsleep(cur_uuids);
+}
+
static void scmi_telemetry_tdes_cleanup(struct telemetry_info *ti)
{
struct scmi_telemetry_de *de;
@@ -2020,6 +2253,7 @@ static void scmi_telemetry_instance_cleanup(void *arg)
scmi_telemetry_groups_free(ti->rinfo);
scmi_telemetry_tdes_cleanup(ti);
+ scmi_telemetry_uuids_cleanup(ti);
scmi_telemetry_resources_free(ti);
}
@@ -2093,6 +2327,28 @@ scmi_telemetry_resources_enumerate(struct telemetry_info *ti)
return ti->rinfo;
}
+static int scmi_telemetry_primary_uuid_init(struct telemetry_info *ti)
+{
+ struct telemetry_uuid *uuid;
+
+ /* Primary UUID is stored on key 0 (NULL) */
+ uuid = scmi_telemetry_uuid_create(ti, NULL);
+ if (!uuid)
+ return -ENOMEM;
+
+ uuid_copy(&uuid->uuid, &ti->info.base.primary_revision);
+ /* Make it available in the uuid list once initialized */
+ if (!scmi_telemetry_uuid_register(ti, NULL, 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
@@ -2108,6 +2364,8 @@ 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;
/* Allocate and Initialize on first call... */
@@ -2121,6 +2379,37 @@ static int scmi_telemetry_instance_init(struct telemetry_info *ti)
for (int i = 0; i < 1 << PAYLD_HASH_BITS; i++)
mutex_init(&ti->payld_locks[i]);
+ /*
+ * 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) {
+ xa_destroy(&ti->xa_lines);
+ xa_destroy(&ti->xa_des);
+ 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);
+ xa_destroy(&ti->xa_lines);
+ xa_destroy(&ti->xa_des);
+ 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 3c09c14b66fa..763ba26eb9f0 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -973,6 +973,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 __counted_by_ptr(num_uuids);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 15/32] firmware: arm_scmi: Add Telemetry configuration operations
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (13 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 14/32] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 16/32] firmware: arm_scmi: Add Telemetry DataEvent read capabilities Cristian Marussi
` (16 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 configuration operations to selectively
enable or disable DataEvents monitoring.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
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 | 788 +++++++++++++++++++++++++-
include/linux/scmi_protocol.h | 27 +
2 files changed, 813 insertions(+), 2 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 68e272b1106f..8b5e212197e8 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -128,6 +128,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_SET(x) (FIELD_PREP(GENMASK(4, 1), (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)))
@@ -353,8 +396,6 @@ enum timestamps {
TSTAMP_BLK = 2,
};
-#define SHMTI_ID_INVALID GENMASK(31, 0)
-
struct telemetry_de {
enum timestamps ts_type;
u32 ts_rate;
@@ -415,6 +456,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;
@@ -1618,6 +1661,106 @@ static void scmi_telemetry_tdcf_uuid_parse(struct telemetry_info *ti,
*active_uuid = uuid;
}
+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;
+
+ /* 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)
+ return IOMEM_ERR_PTR(-EPROTO);
+
+ return payld;
+}
+
+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 __iomem *payld,
+ struct payload __iomem *bts_payld)
+{
+ /* Trigger a manual search when no BLK_TS payload offset was provided */
+ if (!bts_payld) {
+ /* Find the BLK_TS immediately preceding this DE payld */
+ bts_payld = scmi_telemetry_nearest_line_by_type(shmti, payld,
+ TDCF_BLK_TS_LINE);
+ if (!bts_payld)
+ return NULL;
+ } else if (!IS_BLK_TS_LINE(bts_payld)) {
+ trace_scmi_tlm_access(0, "BAD_BLK_TS_LINE_OFFSET", 0,
+ PAYLD_META(bts_payld));
+ return NULL;
+ }
+
+ /* Check for spec compliance */
+ if (BLK_TS_INVALID(bts_payld)) {
+ trace_scmi_tlm_access(0, "BLK_TS_INVALID", 0,
+ PAYLD_META(bts_payld));
+ return NULL;
+ }
+
+ return scmi_telemetry_blkts_get_or_create(ti, bts_payld);
+}
+
/**
* scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser
*
@@ -1765,6 +1908,20 @@ 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)
+{
+ struct payload __iomem *uuid;
+
+ uuid = scmi_telemetry_nearest_line_by_type(shmti, payld, TDCF_UUID_LINE);
+ if (!uuid)
+ return NULL;
+
+ /* Here the nearest UUID line just found has been already validated */
+ return scmi_telemetry_uuid_get_or_create(ti, uuid);
+}
+
/**
* scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing
* @ti: A reference to the telemetry info descriptor
@@ -2064,10 +2221,623 @@ static int scmi_telemetry_shmti_scan(struct telemetry_info *ti,
return 0;
}
+static void scmi_telemetry_group_state_update(struct telemetry_info *ti,
+ struct scmi_telemetry_group *grp,
+ const bool *enable, const bool *tstamp)
+{
+ struct scmi_telemetry_res_info *rinfo;
+
+ rinfo = ti->res_get(ti);
+ for (int i = 0; i < grp->info->num_des; i++) {
+ struct scmi_telemetry_de *de = rinfo->des[grp->des[i]];
+
+ guard(mutex)(&(to_tde(de)->mtx));
+ if (enable)
+ scmi_telemetry_de_state_update(ti, ENA_STATE,
+ &de->enabled, *enable);
+
+ if (tstamp && de->tstamp_support)
+ scmi_telemetry_de_state_update(ti, ENA_TSTAMP,
+ &de->tstamp_enabled, *tstamp);
+ }
+}
+
+static void scmi_telemetry_group_disable(struct telemetry_info *ti,
+ struct scmi_telemetry_group *grp)
+{
+ struct scmi_telemetry_res_info *rinfo;
+
+ rinfo = ti->res_get(ti);
+ /*
+ * Unlink the related BLK_TS/UUID lines of DEs composing the group
+ * and invalidate SHMTI reference.
+ */
+ for (int i = 0; i < grp->info->num_des; i++) {
+ struct telemetry_de *tde = to_tde(rinfo->des[grp->des[i]]);
+
+ guard(mutex)(&tde->mtx);
+
+ scmi_telemetry_blkts_unlink(tde);
+
+ tde->sid = SHMTI_ID_INVALID;
+ tde->offset = 0;
+ }
+}
+
+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;
+ struct telemetry_block_ts *bts;
+ struct telemetry_uuid *uuid;
+
+ /* Acquiring order is: tde -> line */
+ 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.
+ */
+ /* Acquiring order is: tde -> line */
+
+ if (!tde->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))
+ return 0;
+
+ if (tde->ts_type != TSTAMP_BLK) {
+ trace_scmi_tlm_access(tde->de.info->id, "BAD_DE_TSTAMP_TYPE", 0, 0);
+ return -EPROTO;
+ }
+
+ bts_payld = NULL;
+ if (bts_offs &&
+ check_add_overflow((__force unsigned long)tde->base, bts_offs,
+ (__force unsigned long *)&bts_payld))
+ return -EPROTO;
+
+ /* Check SHMTI overflow on returned offset */
+ if (bts_payld) {
+ if (scmi_telemetry_shmti_overflow(shmti, bts_payld)) {
+ trace_scmi_tlm_access(0, "SHMTI_OVERFLOW", 0, 0);
+ return -EINVAL;
+ }
+ } else {
+ dev_warn_once(ti->ph->dev,
+ "Received BLK_TS with zero offset.\n");
+ trace_scmi_tlm_access(0, "BLKTS_WITH_ZERO_OFFSET", 0, 0);
+ }
+
+ /* Has associated BLK_TS line changed at all ? */
+ if (!BLK_TS_CHANGED(tde, bts_payld))
+ return 0;
+
+ /* Look for a viable BLK_TS line */
+ bts = scmi_telemetry_blkts_bind(ti, shmti, payld, bts_payld);
+ if (WARN_ON(!bts)) {
+ trace_scmi_tlm_access(tde->de.info->id, "INVALID_USE_BLK_TS", 0, 0);
+ return -EPROTO;
+ }
+
+ /* 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 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;
+
+ 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);
+
+ /* ...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);
+
+ return scmi_telemetry_tde_linked_lines_process(ti, tde, shmti,
+ payld, bts_offs);
+}
+
+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 (IS_SHMTI_ID_VALID(sid)) {
+ u32 de_offs, bts_offs;
+
+ 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) || !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/UUID lines on disable */
+ struct telemetry_de *tde = to_tde(de);
+
+ lockdep_assert_held(&tde->mtx);
+
+ tde->sid = SHMTI_ID_INVALID;
+ tde->offset = 0;
+
+ /* UUID is never unlinked once discovered */
+ scmi_telemetry_blkts_unlink(tde);
+ }
+
+ return 0;
+}
+
+static int
+scmi_telemetry_group_state_set_resp_process(struct telemetry_info *ti,
+ struct scmi_telemetry_group *grp,
+ struct scmi_msg_resp_telemetry_de_configure *r)
+{
+ u32 sid = le32_to_cpu(r->shmti_id);
+ int ret;
+
+ /*
+ * 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 (IS_SHMTI_ID_VALID(sid)) {
+ if (sid >= ti->num_shmti)
+ return -EPROTO;
+
+ /*
+ * A full SHMTI scan is needed when enabling a
+ * group or its timestamps in order to retrieve
+ * offsets: note that when group-timestamp is
+ * enabled for composing DEs a re-scan is needed
+ * since some DEs could have been relocated due
+ * to lack of space in the TDCF.
+ */
+ ret = scmi_telemetry_shmti_scan(ti, sid, SCAN_UPDATE);
+ if (ret) {
+ dev_err(ti->ph->dev,
+ "Failed group-scan of SHMTI ID:%d - ret:%d\n",
+ sid, ret);
+ return ret;
+ }
+ } else {
+ /* Unlink the related BLK_TS/UUID lines on disable */
+ scmi_telemetry_group_disable(ti, grp);
+ }
+
+ 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_group *grp,
+ struct scmi_telemetry_de *de)
+{
+ if ((!grp && !de) || (grp && de))
+ return;
+
+ if (enable) {
+ if (!grp)
+ scmi_telemetry_de_state_update(ti, ENA_STATE,
+ enabled_state,
+ *enable);
+ else
+ *enabled_state = *enable;
+ }
+
+ if (tstamp) {
+ if (!grp) {
+ if (de->tstamp_support)
+ scmi_telemetry_de_state_update(ti, ENA_TSTAMP,
+ tstamp_enabled_state,
+ *tstamp);
+ } else {
+ *tstamp_enabled_state = *tstamp;
+ }
+ }
+
+ if (grp)
+ scmi_telemetry_group_state_update(ti, grp, enable, 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 && !*enabled_state) {
+ scmi_telemetry_state_cache_update(ti, enable,
+ enabled_state, tstamp,
+ tstamp_enabled_state,
+ is_group ? obj : NULL,
+ !is_group ? obj : NULL);
+
+ 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 : 0;
+ 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);
+
+ scoped_guard(mutex, &tde->mtx) {
+ /* Initialize return values in case of error */
+ scmi_telemetry_tde_location_update(NULL, 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) {
+ guard(mutex)(&tde->mtx);
+ /* Update cached state on success */
+ scmi_telemetry_state_cache_update(ti, enable, &de->enabled,
+ tstamp, &de->tstamp_enabled, NULL, 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_group_state_set(const struct scmi_protocol_handle *ph,
+ const bool *enable, const bool *tstamp,
+ struct scmi_telemetry_group *grp)
+{
+ struct scmi_msg_resp_telemetry_de_configure *resp;
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct scmi_msg_telemetry_de_configure *msg;
+ struct scmi_xfer *t;
+ int ret;
+
+ /* Stall concurrent configuration request */
+ guard(mutex)(&ti->config_mtx);
+
+ if (!scmi_telemetry_remote_state_update_needed(ti, enable, &grp->enabled,
+ tstamp, &grp->tstamp_enabled,
+ true, grp))
+ 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, grp->info->grp_id, true,
+ enable, &grp->enabled,
+ tstamp, &grp->tstamp_enabled);
+ resp = t->rx.buf;
+ ret = ph->xops->do_xfer(ph, t);
+ if (!ret) {
+ /* Update cached state on success */
+ scmi_telemetry_state_cache_update(ti, enable, &grp->enabled,
+ tstamp, &grp->tstamp_enabled,
+ grp, NULL);
+ ret = scmi_telemetry_group_state_set_resp_process(ti, grp, resp);
+ if (ret)
+ dev_err(ph->dev,
+ "Could NOT process GROUP %u state response - ret:%d\n",
+ grp->info->grp_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;
+
+ 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_res_info *rinfo;
+
+ rinfo = ti->res_get(ti);
+ if (!is_group) {
+ struct scmi_telemetry_de *de;
+
+ de = xa_load(&ti->xa_des, id);
+ if (!de)
+ return -ENODEV;
+
+ return scmi_telemetry_tde_state_set(ph, enable, tstamp, de,
+ sid, offset, uuid);
+ } else {
+ struct scmi_telemetry_group *grp;
+
+ if (id >= ti->info.base.num_groups)
+ return -EINVAL;
+
+ grp = &rinfo->grps[id];
+ return scmi_telemetry_group_state_set(ph, enable, tstamp, grp);
+ }
+}
+
+static int
+scmi_telemetry_collection_configure(const struct scmi_protocol_handle *ph,
+ unsigned int res_id, const bool *enable,
+ const unsigned int *update_interval_ms,
+ 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_NOTIFICATION &&
+ !ti->info.continuous_update_support)
+ return -EINVAL;
+
+ is_group = res_id != SCMI_TLM_GRP_INVALID;
+ if (is_group && res_id >= ti->info.base.num_groups)
+ return -EINVAL;
+
+ if (!is_group) {
+ active_update_interval = &ti->info.active_update_interval;
+ current_mode = &ti->info.current_mode;
+ current_state = &ti->info.enabled;
+ } else {
+ struct scmi_telemetry_res_info *rinfo;
+
+ rinfo = ti->res_get(ti);
+ active_update_interval =
+ &rinfo->grps[res_id].active_update_interval;
+ current_mode = &rinfo->grps[res_id].current_mode;
+ current_state = &rinfo->grps[res_id].enabled;
+ }
+
+ if (!enable && !update_interval_ms && (!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_ms)
+ interval = cpu_to_le32(*active_update_interval);
+ else
+ interval = cpu_to_le32(*update_interval_ms);
+
+ 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;
+ if (!is_group) {
+ *current_state = tlm_enable;
+ ti->info.notif_enabled =
+ *current_mode == SCMI_TLM_NOTIFICATION;
+ }
+ if (update_interval_ms)
+ *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,
};
/**
@@ -2180,6 +2950,18 @@ 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_lines_cleanup(struct telemetry_info *ti)
+{
+ struct telemetry_line *line;
+ unsigned long idx;
+
+ guard(mutex)(&ti->lines_mtx);
+ /* Drop all remaining currently unbound lines and their containers */
+ xa_for_each(&ti->xa_lines, idx, line)
+ __scmi_telemetry_line_put(line);
+ xa_destroy(&ti->xa_lines);
+}
+
static void scmi_telemetry_uuids_cleanup(struct telemetry_info *ti)
{
uuid_t **cur_uuids;
@@ -2254,6 +3036,7 @@ static void scmi_telemetry_instance_cleanup(void *arg)
scmi_telemetry_groups_free(ti->rinfo);
scmi_telemetry_tdes_cleanup(ti);
scmi_telemetry_uuids_cleanup(ti);
+ scmi_telemetry_lines_cleanup(ti);
scmi_telemetry_resources_free(ti);
}
@@ -2376,6 +3159,7 @@ static int scmi_telemetry_instance_init(struct telemetry_info *ti)
xa_init(&ti->xa_des);
xa_init(&ti->xa_lines);
mutex_init(&ti->lines_mtx);
+ mutex_init(&ti->config_mtx);
for (int i = 0; i < 1 << PAYLD_HASH_BITS; i++)
mutex_init(&ti->payld_locks[i]);
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index 763ba26eb9f0..cadb7610c98f 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -884,6 +884,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)))
+enum scmi_telemetry_collection {
+ SCMI_TLM_ONDEMAND,
+ SCMI_TLM_NOTIFICATION,
+ SCMI_TLM_SINGLE_READ,
+};
+
#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF
struct scmi_telemetry_intervals {
@@ -910,6 +916,7 @@ struct scmi_telemetry_group {
struct scmi_telemetry_grp_info *info;
unsigned int active_update_interval;
struct scmi_telemetry_intervals *intervals;
+ enum scmi_telemetry_collection current_mode;
};
struct scmi_telemetry_de_info {
@@ -975,6 +982,9 @@ struct scmi_telemetry_info {
struct scmi_telemetry_shmti_info **shmtis;
unsigned int num_uuids;
uuid_t * __rcu *uuids __counted_by_ptr(num_uuids);
+ bool enabled;
+ bool notif_enabled;
+ enum scmi_telemetry_collection current_mode;
};
/**
@@ -984,6 +994,12 @@ 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 or GROUP state, if NULL returns the
+ * cumulative state of all DEs.
+ * @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 __must_check *(*info_get)
@@ -992,6 +1008,17 @@ struct scmi_telemetry_proto_ops {
(const struct scmi_protocol_handle *ph, u32 id);
const struct scmi_telemetry_res_info __must_check *(*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);
+ int (*collection_configure)(const struct scmi_protocol_handle *ph,
+ unsigned int res_id, const bool *enable,
+ const unsigned int *update_interval_ms,
+ const enum scmi_telemetry_collection *mode);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 16/32] firmware: arm_scmi: Add Telemetry DataEvent read capabilities
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (14 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 15/32] firmware: arm_scmi: Add Telemetry configuration operations Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 17/32] firmware: arm_scmi: Add Telemetry reset logic Cristian Marussi
` (15 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 578 ++++++++++++++++++++++++--
include/linux/scmi_protocol.h | 24 ++
2 files changed, 569 insertions(+), 33 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 8b5e212197e8..0825a0d38296 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -171,6 +171,11 @@ struct scmi_msg_telemetry_config_set {
__le32 sampling_rate;
};
+struct scmi_msg_resp_telemetry_reading_complete {
+ __le32 num_dwords;
+ __le32 array[] __counted_by_le(num_dwords);
+};
+
/* TDCF */
#define _I(__a) (ioread32((void __iomem *)(__a)))
@@ -218,6 +223,17 @@ struct uuid_line {
__be32 dwords[SCMI_TLM_DE_IMPL_NUM_DWORDS];
};
+#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); \
@@ -229,6 +245,17 @@ struct uuid_line {
_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); \
@@ -400,6 +427,7 @@ struct telemetry_de {
enum timestamps ts_type;
u32 ts_rate;
bool enumerated;
+ bool cached_msg;
void __iomem *base;
struct eplg __iomem *eplg;
u32 sid;
@@ -603,16 +631,40 @@ scmi_telemetry_tde_cache_unchanged(struct telemetry_de *tde, u32 magic)
}
static void
-scmi_telemetry_tde_cache_update(struct telemetry_de *tde, u64 val,
- u64 *tstamp, u32 *magic)
+scmi_telemetry_tde_cache_update(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample,
+ u32 *magic)
{
lockdep_assert_held(&tde->mtx);
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)
+{
+ guard(mutex)(&tde->mtx);
+
+ if (magic && tde->last_magic != *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 {
@@ -1830,7 +1882,8 @@ scmi_telemetry_tde_allocate(struct telemetry_info *ti, u32 de_id,
}
static inline int
-scmi_telemetry_line_data_parse(struct telemetry_de *tde, u64 *val, u64 *tstamp,
+scmi_telemetry_line_data_parse(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample,
struct payload __iomem *payld, u32 magic)
{
u32 meta = PAYLD_META(payld);
@@ -1838,36 +1891,35 @@ scmi_telemetry_line_data_parse(struct telemetry_de *tde, u64 *val, u64 *tstamp,
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;
- }
+ sample->tstamp = 0;
+ sample->val = 0;
- *tstamp = LINE_TSTAMP_GET(&payld->tsl);
+ if (USE_BLK_TS_RAW(meta)) {
+ /* Read out the actual BLK_TS */
+ sample->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;
}
+
+ sample->tstamp = LINE_TSTAMP_GET(&payld->tsl);
}
/* Data is always valid since we are NOT handling BLK TS lines here */
- *val = LINE_DATA_GET(&payld->l);
+ sample->val = LINE_DATA_GET(&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;
}
@@ -1975,9 +2027,9 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
struct telemetry_block_ts *active_bts,
struct telemetry_uuid *active_uuid)
{
+ struct scmi_telemetry_de_sample sample = {};
bool use_blk_ts = USE_BLK_TS(payld);
struct telemetry_de *tde;
- u64 val, tstamp = 0;
u32 de_id;
de_id = PAYLD_ID(payld);
@@ -2032,8 +2084,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 by ignored.
*/
- scmi_telemetry_line_data_parse(tde, &val, &tstamp, payld,
- shmti->last_magic);
+ scmi_telemetry_line_data_parse(tde, &sample, payld, shmti->last_magic);
}
/**
@@ -2831,6 +2882,464 @@ scmi_telemetry_collection_configure(const struct scmi_protocol_handle *ph,
return ret;
}
+static inline void
+scmi_telemetry_de_data_fc_read(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample)
+{
+ struct fc_tsline __iomem *fc = tde->base + tde->offset;
+
+ lockdep_assert_held(&tde->mtx);
+
+ sample->val = LINE_DATA_GET(fc);
+ sample->tstamp = tde->ts_type == TSTAMP_LINE ? LINE_TSTAMP_GET(fc) : 0;
+
+ /* Trace originally read tstamp */
+ trace_scmi_tlm_collect(sample->tstamp, tde->de.info->id, sample->val,
+ "FC_READ");
+
+ scmi_telemetry_tde_cache_update(tde, sample, NULL);
+}
+
+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 */
+ 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 struct payload __iomem *
+scmi_telemetry_tdcf_de_payld_get(struct telemetry_de *tde)
+{
+ struct payload __iomem *payld;
+
+ lockdep_assert_held(&tde->mtx);
+
+ payld = tde->base + tde->offset;
+ if (DATA_INVALID(payld)) {
+ trace_scmi_tlm_access(PAYLD_ID(payld), "DE_DATA_INVALID", 0, 0);
+ return IOMEM_ERR_PTR(-EINVAL);
+ }
+
+ if (IS_BLK_TS_LINE(payld)) {
+ trace_scmi_tlm_access(tde->de.info->id, "BAD_DE_META", 0, 0);
+ return IOMEM_ERR_PTR(-EPROTO);
+ }
+
+ if (PAYLD_ID(payld) != tde->de.info->id) {
+ trace_scmi_tlm_access(tde->de.info->id, "DE_ID_MISMATCH", 0, 0);
+ return IOMEM_ERR_PTR(-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(payld) && !tde->bts)) {
+ trace_scmi_tlm_access(tde->de.info->id, "BAD_USE_BLK_TS", 0, 0);
+ return IOMEM_ERR_PTR(-EPROTO);
+ }
+
+ return payld;
+}
+
+static int
+scmi_telemetry_tdcf_de_parse(struct telemetry_de *tde,
+ struct scmi_telemetry_de_sample *sample)
+{
+ u32 startm = TDCF_INVALID_MATCH_SEQ, endm = TDCF_BAD_END_SEQ;
+ int retries = SCMI_TLM_TDCF_MAX_RETRIES;
+ struct tdcf __iomem *tdcf;
+ int err = 0;
+
+ lockdep_assert_held(&tde->mtx);
+
+ do {
+ struct payload __iomem *payld;
+
+ /* A bit of exponential backoff between retries */
+ fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP);
+
+ /*
+ * Refresh tdcf on each retry, since it could have changed
+ * by the platform while relocating the DE.
+ */
+ tdcf = tde->base;
+ if (!tdcf)
+ continue;
+
+ startm = TDCF_START_SEQ_GET(tdcf);
+ if (IS_BAD_START_SEQ(startm)) {
+ trace_scmi_tlm_access(tde->de.info->id, "MSEQ_BADSTART",
+ startm, 0);
+ continue;
+ }
+
+ /* Return last readings if nothing changed */
+ err = scmi_telemetry_tde_cache_lookup(tde, sample, &startm);
+ if (!err)
+ return 0;
+
+ /*
+ * In this context any anomaly detected on the payload could
+ * due to a torn read, and it is worth a retry.
+ * Unless the entry was marked as DATA_INVALID (-EINVAL) which
+ * indicates an entry referring a DE which has been disabled by
+ * the user or some other sort of error platform side: if we
+ * get all down here, though, it cannot be due to a disabled DE,
+ * so bail-out to avoid retrying on permament/transient errors.
+ */
+ payld = scmi_telemetry_tdcf_de_payld_get(tde);
+ err = PTR_ERR_OR_ZERO(payld);
+ if (!err)
+ err = scmi_telemetry_line_data_parse(tde, sample,
+ payld, startm);
+ else if (err == -EINVAL)
+ return err;
+
+ endm = TDCF_END_SEQ_GET(tde->eplg);
+ if (startm != endm)
+ trace_scmi_tlm_access(tde->de.info->id, "MSEQ_MISMATCH",
+ startm, endm);
+ } while (startm != endm && --retries);
+
+ if (startm != endm) {
+ trace_scmi_tlm_access(tde->de.info->id, "TDCF_DE_FAIL",
+ startm, endm);
+ return -EPROTO;
+ }
+
+ return err;
+}
+
+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);
+
+ scmi_telemetry_de_data_fc_read(tde, sample);
+
+ return 0;
+}
+
+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;
+
+ /*
+ * DE readings returns cached values when:
+ * - DE data value was retrieved via notification/delayed_response
+ */
+ if (tde->cached_msg)
+ return scmi_telemetry_tde_cache_lookup(tde, sample, NULL);
+
+ 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;
+
+ 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,
+ int *num_samples,
+ struct scmi_telemetry_de_sample *samples)
+{
+ struct scmi_telemetry_res_info *rinfo;
+ int max_samples;
+
+ 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 and not belonging to the selected Group */
+ if (!tde->de.enabled ||
+ (grp_id != SCMI_TLM_GRP_INVALID &&
+ (!tde->de.grp || tde->de.grp->info->grp_id != grp_id)))
+ 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, int *num_samples,
+ struct scmi_telemetry_de_sample *samples)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+
+ if (!ti->info.enabled || !num_samples || !samples)
+ return -EINVAL;
+
+ /* Trigger a full SHMTIs & FCs scan */
+ scmi_telemetry_scan_update(ti);
+
+ /* Collect all last cached values */
+ return scmi_telemetry_samples_collect(ti, grp_id, num_samples, samples);
+}
+
+static void
+scmi_telemetry_msg_payld_process(struct telemetry_info *ti,
+ unsigned int num_dwords, __le32 *dwords,
+ size_t payld_bytes, bool cacheable)
+{
+ struct scmi_telemetry_res_info *rinfo;
+ u32 next = 0;
+
+ rinfo = ti->res_get(ti);
+ if (!rinfo->fully_enumerated) {
+ dev_warn_once(ti->ph->dev,
+ "Cannot process DEs in message payload. Drop.\n");
+ return;
+ }
+
+ while (next < num_dwords) {
+ struct payload *payld = (struct payload *)&dwords[next];
+ struct scmi_telemetry_de *de;
+ struct telemetry_de *tde;
+ u32 de_id, meta = le32_to_cpu(payld->meta);
+ unsigned int wlen;
+
+ wlen = LINE_LENGTH_WORDS_RAW(meta);
+ /* Check Boundary */
+ if (next + wlen > payld_bytes / sizeof(u32))
+ return;
+
+ next += wlen;
+ de_id = le32_to_cpu(payld->id);
+ if (DATA_INVALID_RAW(meta)) {
+ trace_scmi_tlm_access(de_id, "MSG_INVALID", 0, 0);
+ continue;
+ }
+
+ if (LINE_TYPE_RAW(meta) != TDCF_DATA_LINE) {
+ trace_scmi_tlm_access(de_id, "MSG_BAD_TYPE", 0, 0);
+ continue;
+ }
+
+ if (USE_BLK_TS_RAW(meta)) {
+ trace_scmi_tlm_access(de_id, "MSG_BAD_USE_BLKTS", 0, 0);
+ continue;
+ }
+
+ de = xa_load(&ti->xa_des, de_id);
+ if (!de || !de->enabled) {
+ dev_err(ti->ph->dev,
+ "MSG - Received INVALID DE - ID:%u enabled:%c\n",
+ de_id, de ? (de->enabled ? 'Y' : 'N') : 'X');
+ continue;
+ }
+
+ tde = to_tde(de);
+ guard(mutex)(&tde->mtx);
+ tde->cached_msg = true;
+ tde->last_val = LINE_DATA_GET_RAW(&payld->tsl);
+ /* TODO BLK_TS in notification payloads */
+ tde->last_ts = HAS_LINE_EXT_RAW(meta) &&
+ LINE_TS_VALID_RAW(meta) ?
+ LINE_TSTAMP_GET_RAW(&payld->tsl) : 0;
+
+ trace_scmi_tlm_collect(tde->last_ts, tde->de.info->id,
+ tde->last_val, "MESSAGE");
+ }
+}
+
+static int scmi_telemetry_des_sample_get(const struct scmi_protocol_handle *ph,
+ int grp_id, int *num_samples,
+ struct scmi_telemetry_de_sample *samples)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct scmi_msg_telemetry_config_set *msg;
+ struct scmi_xfer *t;
+ bool is_group;
+ u32 control;
+ int ret;
+
+ if (!ti->info.enabled || !num_samples || !samples)
+ return -EINVAL;
+
+ is_group = grp_id != SCMI_TLM_GRP_INVALID;
+ if (is_group && grp_id >= ti->info.base.num_groups)
+ return -EINVAL;
+
+ ret = ph->xops->xfer_get_init(ph, TELEMETRY_CONFIG_SET,
+ sizeof(*msg), 0, &t);
+ if (ret)
+ return ret;
+
+ msg = t->tx.buf;
+ msg->group_identifier = cpu_to_le32(grp_id);
+ control = TELEMETRY_ENABLE;
+ control |= !is_group ? TELEMETRY_SET_SELECTOR_ALL :
+ TELEMETRY_SET_SELECTOR_GROUP;
+ control |= TELEMETRY_MODE_SINGLE;
+ msg->control = cpu_to_le32(control);
+ msg->sampling_rate = 0;
+
+ ret = ph->xops->do_xfer_with_response(ph, t);
+ /*
+ * PARTIAL_ERRORs are reported as -EBADMSG and related lines,
+ * marked as DATA_INVALID, will be properly skipped by
+ * scmi_telemetry_msg_payld_process.
+ */
+ if (!ret || ret == -EBADMSG) {
+ struct scmi_msg_resp_telemetry_reading_complete *r = t->rx.buf;
+ u32 num_dwords = le32_to_cpu(r->num_dwords);
+
+ if (ret == -EBADMSG)
+ trace_scmi_tlm_access(0, "BAD_SAMPLE_PARTIAL_ERROR", 0, 0);
+
+ /* Update cached DEs values from payload */
+ if (num_dwords) {
+ size_t payld_sz = 0, dwords_sz;
+
+ if (check_mul_overflow(num_dwords, sizeof(r->array[0]), &dwords_sz) ||
+ check_add_overflow(sizeof(*r), dwords_sz, &payld_sz) ||
+ payld_sz > t->rx.len) {
+ dev_err(ph->dev,
+ "Malformed Telemetry delayed response - num_dwords:%u payld_sz:%zd rx_len:%zd\n",
+ num_dwords, payld_sz, t->rx.len);
+ ret = -EPROTO;
+ goto out;
+ }
+
+ scmi_telemetry_msg_payld_process(ti, num_dwords, r->array,
+ payld_sz - sizeof(*r), false);
+ }
+
+ /* Scan and update SMHTIs and FCs */
+ scmi_telemetry_scan_update(ti);
+
+ /* Collect all last cached values, override any PARTIAL_ERROR */
+ ret = scmi_telemetry_samples_collect(ti, grp_id, num_samples,
+ samples);
+ }
+
+out:
+ 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,
@@ -2838,6 +3347,9 @@ 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,
+ .des_sample_get = scmi_telemetry_des_sample_get,
};
/**
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index cadb7610c98f..52c9031c266b 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -987,6 +987,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
@@ -1000,6 +1007,15 @@ 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.
+ * @des_sample_get: on-demand read of all the currently enabled DEs, or just
+ * the ones belonging to a specific group when provided.
+ * This causes an immediate update platform-side of all the
+ * enabled DEs.
*/
struct scmi_telemetry_proto_ops {
const struct scmi_telemetry_info __must_check *(*info_get)
@@ -1019,6 +1035,14 @@ struct scmi_telemetry_proto_ops {
unsigned int res_id, const bool *enable,
const unsigned int *update_interval_ms,
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, int *num_samples,
+ struct scmi_telemetry_de_sample *samples);
+ int __must_check (*des_sample_get)(const struct scmi_protocol_handle *ph,
+ int grp_id, int *num_samples,
+ struct scmi_telemetry_de_sample *samples);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 17/32] firmware: arm_scmi: Add Telemetry reset logic
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (15 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 16/32] firmware: arm_scmi: Add Telemetry DataEvent read capabilities Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 18/32] firmware: arm_scmi: Add Telemetry notification support Cristian Marussi
` (14 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 request platform to
reset telemetry current configuration and data events values.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v13 --> v14
- add a barrier to acquire rinfo->num_des on reset
- avoid lock inversion on config_mtx
v12 --> v13
- relocate tde->mtx guards higher up and add lockdep assertions
- unlink BLKTS under tde->mtx on local free
v11 --> v12
- using config_mtx to sync with ongoing configurations
- use proper helpers to update cache state
v10 --> v11
- protect TDE updates on reset
- use des_enable mutex when clearing des cache
- zero also active_update_interval
v8 --> v9
- add a commment on why NOT unlinking UUID
v6 --> v7
- add proper cleanup of sid/offs/uuids and caches on RESET
v5 --> v6
- added missing reset of global state
v3 --> v4
- reset tracked Telemetry DE state
v2 --> v3
- split from monolithic Telemetry patch
- use scmi_telemetry_de_unlink
---
drivers/firmware/arm_scmi/telemetry.c | 80 +++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 2 +
2 files changed, 82 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 0825a0d38296..a5ba21fbfed7 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -3340,6 +3340,85 @@ static int scmi_telemetry_des_sample_get(const struct scmi_protocol_handle *ph,
return ret;
}
+static void scmi_telemetry_local_resources_reset(struct telemetry_info *ti)
+{
+ struct scmi_telemetry_res_info *rinfo;
+ unsigned int num_des;
+
+ ti->info.enabled = false;
+ ti->info.notif_enabled = false;
+ ti->info.current_mode = SCMI_TLM_ONDEMAND;
+ ti->info.active_update_interval = 0;
+
+ /* Get rinfo without triggering an enumeration, cannot be NULL anyway */
+ rinfo = __scmi_telemetry_resources_get(ti);
+ /* Make sure to see the latest num_des */
+ num_des = smp_load_acquire(&rinfo->num_des);
+ /* Clear all local state...*/
+ for (int i = 0; i < num_des; i++) {
+ struct telemetry_de *tde = to_tde(rinfo->des[i]);
+
+ scoped_guard(mutex, &tde->mtx) {
+ tde->last_ts = 0;
+ tde->last_val = 0;
+ tde->last_magic = 0;
+ if (!tde->de.fc_support) {
+ tde->sid = SHMTI_ID_INVALID;
+ tde->base = NULL;
+ tde->offset = 0;
+ tde->eplg = NULL;
+ }
+
+ scmi_telemetry_de_state_update(ti, ENA_STATE,
+ &tde->de.enabled, false);
+
+ if (tde->de.tstamp_support)
+ scmi_telemetry_de_state_update(ti, ENA_TSTAMP,
+ &tde->de.tstamp_enabled,
+ false);
+
+ /*
+ * Unlinking ONLY BLK_TS lines since, per the SCMI spec,
+ * the UUID/DE associations are permanent within the same
+ * session boot: after a reset has cleared the server-side
+ * configuration the next time SHMTIs will be re-populated,
+ * these same UUID/DE associations will be re-instated.
+ */
+ scmi_telemetry_blkts_unlink(tde);
+ }
+ }
+
+ for (int i = 0; i < rinfo->num_groups; i++) {
+ rinfo->grps[i].enabled = false;
+ rinfo->grps[i].tstamp_enabled = false;
+ rinfo->grps[i].current_mode = SCMI_TLM_ONDEMAND;
+ rinfo->grps[i].active_update_interval = 0;
+ }
+}
+
+static int scmi_telemetry_reset(const struct scmi_protocol_handle *ph)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct scmi_xfer *t;
+ int ret;
+
+ /* Stall concurrent configuration request */
+ guard(mutex)(&ti->config_mtx);
+
+ ret = ph->xops->xfer_get_init(ph, TELEMETRY_RESET, sizeof(u32), 0, &t);
+ if (ret)
+ return ret;
+
+ put_unaligned_le32(0, t->tx.buf);
+ ret = ph->xops->do_xfer(ph, t);
+ if (!ret)
+ scmi_telemetry_local_resources_reset(ti);
+
+ 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,
@@ -3350,6 +3429,7 @@ static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.de_data_read = scmi_telemetry_de_data_read,
.des_bulk_read = scmi_telemetry_des_bulk_read,
.des_sample_get = scmi_telemetry_des_sample_get,
+ .reset = scmi_telemetry_reset,
};
/**
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index 52c9031c266b..7656db822f30 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -1016,6 +1016,7 @@ struct scmi_telemetry_de_sample {
* the ones belonging to a specific group when provided.
* This causes an immediate update platform-side of all the
* enabled DEs.
+ * @reset: reset configuration and telemetry data.
*/
struct scmi_telemetry_proto_ops {
const struct scmi_telemetry_info __must_check *(*info_get)
@@ -1043,6 +1044,7 @@ struct scmi_telemetry_proto_ops {
int __must_check (*des_sample_get)(const struct scmi_protocol_handle *ph,
int grp_id, int *num_samples,
struct scmi_telemetry_de_sample *samples);
+ int (*reset)(const struct scmi_protocol_handle *ph);
};
/**
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 18/32] firmware: arm_scmi: Add Telemetry notification support
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (16 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 17/32] firmware: arm_scmi: Add Telemetry reset logic Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 19/32] firmware: arm_scmi: Add Telemetry boot-on support Cristian Marussi
` (13 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 notifications to Telemetry protocol and register an
internal notifier during protocol initialization: any DE value received
inside a notification payload will be cached for future user consumption.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v13 --> v14
- moved some definitions from earlier split patch
v12 --> v13
- handle PARTIAL_ERROR: log and ignore
- unlink UUID under mutex
v11 --> v12
- marking notifs TDE as cacheable
- updating cache within TDE lock when processing msg_payld
v10 --> v11
- link DE received by notif to the primary UUID when first seen
v8 --> v9
- fix notif boundary and wraparound checks
- rollback notif report payload in __le32 format before parsing
to align with single read payload parsing
v6 --> v7
- fix sparse warn on notof payload handling
v5 -> v6
- use protocol instance notifier interface
- use new RAW macros to access payload
- add proper boundary checks on notification payload
- fix mutex scope in msg payld parsing
- add notification payload consistency check (Sahiko)
v3 -> v4
- use TDE cache to save DEs received via msg payload
v2 --> v3
- changed a few dev_err into traces
- split from monolithic telemetry protocol patch
- use memcpy_from_le32
---
drivers/firmware/arm_scmi/telemetry.c | 192 ++++++++++++++++++++++++--
include/linux/scmi_protocol.h | 9 ++
2 files changed, 187 insertions(+), 14 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index a5ba21fbfed7..71d885a326eb 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -64,6 +64,13 @@ struct scmi_msg_resp_telemetry_protocol_attributes {
__le32 default_blk_ts_rate;
};
+struct scmi_telemetry_update_notify_payld {
+ __le32 agent_id;
+ __le32 status;
+ __le32 num_dwords;
+ __le32 array[] __counted_by_le(num_dwords);
+};
+
struct scmi_shmti_desc {
__le32 id;
__le32 addr_low;
@@ -487,6 +494,7 @@ struct telemetry_info {
/* Protect global and per-group configuration data */
struct mutex config_mtx;
struct scmi_telemetry_info info;
+ struct notifier_block telemetry_nb;
atomic_t rinfo_initializing;
struct completion rinfo_initdone;
#define PAYLD_HASH_BITS 4
@@ -495,6 +503,9 @@ struct telemetry_info {
struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti);
};
+#define telemetry_nb_to_info(x) \
+ container_of(x, struct telemetry_info, telemetry_nb)
+
static struct scmi_telemetry_res_info *
__scmi_telemetry_resources_get(struct telemetry_info *ti);
@@ -652,7 +663,7 @@ scmi_telemetry_tde_cache_lookup(struct telemetry_de *tde,
struct scmi_telemetry_de_sample *sample,
u32 *magic)
{
- guard(mutex)(&tde->mtx);
+ lockdep_assert_held(&tde->mtx);
if (magic && tde->last_magic != *magic)
return -EAGAIN;
@@ -3214,6 +3225,7 @@ scmi_telemetry_msg_payld_process(struct telemetry_info *ti,
while (next < num_dwords) {
struct payload *payld = (struct payload *)&dwords[next];
+ struct scmi_telemetry_de_sample sample = {};
struct scmi_telemetry_de *de;
struct telemetry_de *tde;
u32 de_id, meta = le32_to_cpu(payld->meta);
@@ -3243,23 +3255,27 @@ scmi_telemetry_msg_payld_process(struct telemetry_info *ti,
de = xa_load(&ti->xa_des, de_id);
if (!de || !de->enabled) {
- dev_err(ti->ph->dev,
- "MSG - Received INVALID DE - ID:%u enabled:%c\n",
- de_id, de ? (de->enabled ? 'Y' : 'N') : 'X');
+ trace_scmi_tlm_access(de_id, de ? "MSG_DE_DISABLED" :
+ "MSG_DE_UNKNOWN", 0, 0);
continue;
}
tde = to_tde(de);
- guard(mutex)(&tde->mtx);
- tde->cached_msg = true;
- tde->last_val = LINE_DATA_GET_RAW(&payld->tsl);
- /* TODO BLK_TS in notification payloads */
- tde->last_ts = HAS_LINE_EXT_RAW(meta) &&
- LINE_TS_VALID_RAW(meta) ?
- LINE_TSTAMP_GET_RAW(&payld->tsl) : 0;
+ scoped_guard(mutex, &tde->mtx) {
+ tde->cached_msg = cacheable;
+ sample.val = LINE_DATA_GET_RAW(&payld->tsl);
+ sample.tstamp = HAS_LINE_EXT_RAW(meta) &&
+ LINE_TS_VALID_RAW(meta) ?
+ LINE_TSTAMP_GET_RAW(&payld->tsl) : 0;
+
+ /* Update the cache holding the TDE mutex */
+ scmi_telemetry_tde_cache_update(tde, &sample, NULL);
+
+ scmi_telemetry_uuid_link(tde, ti->primary_uuid);
+ }
- trace_scmi_tlm_collect(tde->last_ts, tde->de.info->id,
- tde->last_val, "MESSAGE");
+ trace_scmi_tlm_collect(sample.tstamp, de->info->id, sample.val,
+ "MESSAGE");
}
}
@@ -3432,6 +3448,133 @@ static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.reset = scmi_telemetry_reset,
};
+static bool
+scmi_telemetry_notify_supported(const struct scmi_protocol_handle *ph,
+ u8 evt_id, u32 src_id)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+
+ return ti->info.continuous_update_support;
+}
+
+static int
+scmi_telemetry_set_notify_enabled(const struct scmi_protocol_handle *ph,
+ u8 evt_id, u32 src_id, bool enable)
+{
+ return 0;
+}
+
+static void *
+scmi_telemetry_fill_custom_report(const struct scmi_protocol_handle *ph,
+ u8 evt_id, ktime_t timestamp,
+ const void *payld, size_t payld_sz,
+ void *report, u32 *src_id)
+{
+ const struct scmi_telemetry_update_notify_payld *p = payld;
+ struct scmi_telemetry_update_report *r = report;
+ size_t dwords_sz, msg_payld_sz;
+
+ /* At least sized as an empty notification */
+ if (payld_sz < sizeof(*p))
+ return NULL;
+
+ r->timestamp = timestamp;
+ r->agent_id = le32_to_cpu(p->agent_id);
+ r->status = le32_to_cpu(p->status);
+ r->num_dwords = le32_to_cpu(p->num_dwords);
+
+ /*Check boundaries and wraparounds */
+ if (check_mul_overflow(r->num_dwords, sizeof(r->dwords[0]), &dwords_sz))
+ return NULL;
+
+ if (check_add_overflow(sizeof(*p), dwords_sz, &msg_payld_sz))
+ return NULL;
+
+ if (msg_payld_sz > payld_sz)
+ return NULL;
+
+ /*
+ * Allocated dwords and report are sized as max_msg_size, so as
+ * to allow for the maximum payload permitted by the configured
+ * transport. Overflow is not possible since out-of-size messages
+ * are dropped at the transport layer.
+ */
+ if (r->num_dwords)
+ memcpy_from_le32(r->dwords, p->array, r->num_dwords);
+
+ *src_id = 0;
+
+ return r;
+}
+
+static const struct scmi_event tlm_events[] = {
+ {
+ .id = SCMI_EVENT_TELEMETRY_UPDATE,
+ .max_payld_sz = 0,
+ .max_report_sz = 0,
+ },
+};
+
+static const struct scmi_event_ops tlm_event_ops = {
+ .is_notify_supported = scmi_telemetry_notify_supported,
+ .set_notify_enabled = scmi_telemetry_set_notify_enabled,
+ .fill_custom_report = scmi_telemetry_fill_custom_report,
+};
+
+static const struct scmi_protocol_events tlm_protocol_events = {
+ .queue_sz = SCMI_PROTO_QUEUE_SZ,
+ .ops = &tlm_event_ops,
+ .evts = tlm_events,
+ .num_events = ARRAY_SIZE(tlm_events),
+ .num_sources = 1,
+};
+
+static int scmi_telemetry_notifier(struct notifier_block *nb,
+ unsigned long event, void *data)
+{
+ struct scmi_telemetry_update_report *er = data;
+ struct telemetry_info *ti = telemetry_nb_to_info(nb);
+
+ /*
+ * PARTIAL_ERRORs are reported as -EBADMSG and related lines,
+ * marked as DATA_INVALID, will be properly skipped by
+ * scmi_telemetry_msg_payld_process.
+ */
+ if (er->status && er->status != -EBADMSG) {
+ trace_scmi_tlm_access(0, "BAD_NOTIF_MSG", 0, 0);
+ return NOTIFY_DONE;
+ } else if (er->status == -EBADMSG) {
+ trace_scmi_tlm_access(0, "BAD_NOTIF_PARTIAL_ERROR", 0, 0);
+ }
+
+ trace_scmi_tlm_access(0, "TLM_UPDATE_MSG", 0, 0);
+ /* Lookup the embedded DEs in the notification payload ... */
+ if (er->num_dwords) {
+ size_t payld_bytes = sizeof(er->dwords[0]) * er->num_dwords;
+
+ __le32 *dwords __free(kvfree) =
+ kvzalloc_objs(*dwords, er->num_dwords);
+ if (!dwords)
+ return NOTIFY_DONE;
+
+ /* The report payload size was bound checked on rx */
+ memcpy_to_le32(dwords, er->dwords, er->num_dwords);
+ /*
+ * Note that the DEs received via Notifications can only be
+ * updated that way, and as such are cached at each reception
+ * and never read on-demand.
+ */
+ scmi_telemetry_msg_payld_process(ti, er->num_dwords, dwords,
+ payld_bytes, true);
+ }
+
+ /* ...scan the SHMTI/FCs for any other DE updates. */
+ if (ti->streaming_mode)
+ scmi_telemetry_scan_update(ti);
+
+ return NOTIFY_OK;
+}
+
/**
* scmi_telemetry_resources_alloc - Resources allocation
* @ti: A reference to the telemetry info descriptor for this instance
@@ -3840,7 +3983,27 @@ static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
ti->info.base.version = ph->version;
- return ph->set_priv(ph, ti);
+ ret = ph->set_priv(ph, ti);
+ if (ret)
+ return ret;
+
+ /*
+ * Register a notifier anyway straight upon protocol initialization
+ * since there could be some DEs that are ONLY reported by notifications
+ * even though the chosen collection method was SHMTI/FCs.
+ */
+ if (ti->info.continuous_update_support) {
+ ti->telemetry_nb.notifier_call = &scmi_telemetry_notifier;
+ ret = ph->instance_notifier_register(ph, SCMI_EVENT_TELEMETRY_UPDATE,
+ NULL, &ti->telemetry_nb);
+ if (ret) {
+ dev_err(ph->dev,
+ "Could NOT register Telemetry notifier\n");
+ return ret;
+ }
+ }
+
+ return 0;
}
static const struct scmi_protocol scmi_telemetry = {
@@ -3848,6 +4011,7 @@ static const struct scmi_protocol scmi_telemetry = {
.owner = THIS_MODULE,
.instance_init = &scmi_telemetry_protocol_init,
.ops = &tlm_proto_ops,
+ .events = &tlm_protocol_events,
.supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
};
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index 7656db822f30..8328ba9b592b 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -1252,6 +1252,7 @@ enum scmi_notification_events {
SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER = 0x0,
SCMI_EVENT_POWERCAP_CAP_CHANGED = 0x0,
SCMI_EVENT_POWERCAP_MEASUREMENTS_CHANGED = 0x1,
+ SCMI_EVENT_TELEMETRY_UPDATE = 0x0,
};
struct scmi_power_state_changed_report {
@@ -1339,4 +1340,12 @@ struct scmi_powercap_meas_changed_report {
unsigned int domain_id;
unsigned int power;
};
+
+struct scmi_telemetry_update_report {
+ ktime_t timestamp;
+ unsigned int agent_id;
+ int status;
+ unsigned int num_dwords;
+ unsigned int dwords[];
+};
#endif /* _LINUX_SCMI_PROTOCOL_H */
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 19/32] firmware: arm_scmi: Add Telemetry boot-on support
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (17 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 18/32] firmware: arm_scmi: Add Telemetry notification support Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 20/32] firmware: arm-scmi: Add Telemetry generic event support Cristian Marussi
` (12 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 270 +++++++++++++++++++++++++-
1 file changed, 266 insertions(+), 4 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 71d885a326eb..08250372a282 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -135,6 +135,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;
@@ -183,6 +198,20 @@ struct scmi_msg_resp_telemetry_reading_complete {
__le32 array[] __counted_by_le(num_dwords);
};
+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)))
@@ -460,6 +489,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,
@@ -816,8 +847,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 (tde->ts_type == TSTAMP_BLK) {
- /* 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;
}
if (tde->de.fc_support) {
@@ -928,6 +964,215 @@ static int iter_de_descr_process_response(const struct scmi_protocol_handle *ph,
return 0;
}
+static int scmi_telemetry_config_lookup(struct telemetry_info *ti,
+ unsigned int grp_id, bool *enabled,
+ unsigned int *active_update_interval)
+{
+ const struct scmi_protocol_handle *ph = ti->ph;
+ struct scmi_msg_telemetry_config_get *msg;
+ struct scmi_msg_resp_telemetry_config_get *resp;
+ 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)
+ flags = TELEMETRY_GET_SELECTOR_ALL;
+ else
+ flags = grp_id == SCMI_TLM_GRP_INVALID ?
+ 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);
+ *active_update_interval =
+ SCMI_TLM_GET_UPDATE_INTERVAL(le32_to_cpu(resp->sampling_rate));
+ }
+
+ ph->xops->xfer_put(ph, t);
+
+ return ret;
+}
+
+static int scmi_telemetry_group_config_lookup(struct telemetry_info *ti,
+ struct scmi_telemetry_group *grp)
+{
+ return scmi_telemetry_config_lookup(ti, grp->info->grp_id, &grp->enabled,
+ &grp->active_update_interval);
+}
+
+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;
+
+ ret = scmi_telemetry_config_lookup(ti, SCMI_TLM_GRP_INVALID,
+ &ti->info.enabled,
+ &ti->info.active_update_interval);
+ 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\n",
+ SCMI_TLM_GET_UPDATE_INTERVAL_SECS(ti->info.active_update_interval),
+ SCMI_TLM_GET_UPDATE_INTERVAL_EXP(ti->info.active_update_interval));
+ /*
+ * 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 int
scmi_telemetry_de_groups_init(struct device *dev, struct telemetry_info *ti)
{
@@ -1008,6 +1253,9 @@ scmi_telemetry_de_groups_init(struct device *dev, struct telemetry_info *ti)
}
}
+ for (int i = 0; i < ti->info.base.num_groups; i++)
+ scmi_telemetry_group_config_lookup(ti, &rinfo->grps[i]);
+
/* Expose all groups once all fully initialized */
rinfo->num_groups = num_groups;
@@ -2075,9 +2323,17 @@ static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti,
tde->eplg = SHMTI_EPLG(shmti);
tde->offset = (void __iomem *)payld - (void __iomem *)shmti->base;
+ /* Just in case it was passively discovered boot-on */
+ if (tde->ts_type == TSTAMP_NONE) {
+ if (use_blk_ts)
+ tde->ts_type = TSTAMP_BLK;
+ else if (LINE_TS_VALID(payld))
+ tde->ts_type = TSTAMP_LINE;
+ }
+
dev_dbg(ti->ph->dev,
- "TDCF-updated DE_ID:0x%08X - shmti:%pK offset:%u\n",
- tde->de.info->id, tde->base, tde->offset);
+ "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);
}
/* Has any value/tstamp really changed ?*/
@@ -3981,6 +4237,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;
ret = ph->set_priv(ph, ti);
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 20/32] firmware: arm-scmi: Add Telemetry generic event support
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (18 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 19/32] firmware: arm_scmi: Add Telemetry boot-on support Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 21/32] firmware: arm_scmi: Add Telemetry generation counter event Cristian Marussi
` (11 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 allow SCMI driver users to subscribe/unsubscribe to specific
Telemetry abstract (non protocol) events.
This enables SCMI drivers to subscribe to available events by registering
an eventfd context that the core SCMI stack will use as a signalling
mechanisms.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v12 --> v13
- refacto resources cleanup
v11 --> v12
- dropped eventfd_ctx_put on external ref
v10 --> v11
- move events cleanup into resource_free
v8 --> v9
- add a TLM_EVT_DUMMY placeholder (waiting for more events later on..)
---
drivers/firmware/arm_scmi/telemetry.c | 96 +++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 16 +++++
2 files changed, 112 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 08250372a282..b1e3f986919e 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/eventfd.h>
#include <linux/hash.h>
#include <linux/io.h>
#include <linux/limits.h>
@@ -497,6 +498,11 @@ enum de_state {
ENA_MAX
};
+struct telemetry_event {
+ struct eventfd_ctx *ctx;
+ struct list_head item;
+};
+
struct telemetry_info {
bool streaming_mode;
unsigned int num_shmti;
@@ -525,6 +531,9 @@ struct telemetry_info {
/* Protect global and per-group configuration data */
struct mutex config_mtx;
struct scmi_telemetry_info info;
+ /* Mutex to protect access to @events */
+ struct mutex events_mtx;
+ struct list_head events[SCMI_TLM_EVT_MAX];
struct notifier_block telemetry_nb;
atomic_t rinfo_initializing;
struct completion rinfo_initdone;
@@ -552,6 +561,21 @@ struct mutex *lock_for_payld(struct mutex *locks, struct payload __iomem *payld)
return &locks[hash_long((__force unsigned long)payld, PAYLD_HASH_BITS)];
}
+static int scmi_telemetry_event_signal(struct telemetry_info *ti,
+ enum scmi_telemetry_event type)
+{
+ struct telemetry_event *evt;
+
+ if (type >= SCMI_TLM_EVT_MAX)
+ return -EINVAL;
+
+ guard(mutex)(&ti->events_mtx);
+ list_for_each_entry(evt, &ti->events[type], item)
+ eventfd_signal(evt->ctx);
+
+ return 0;
+}
+
static inline void
scmi_telemetry_de_state_update(struct telemetry_info *ti, enum de_state state,
bool *current_state, const bool next_state)
@@ -3691,6 +3715,54 @@ static int scmi_telemetry_reset(const struct scmi_protocol_handle *ph)
return ret;
}
+static int scmi_telemetry_event_subscribe(const struct scmi_protocol_handle *ph,
+ enum scmi_telemetry_event type,
+ struct eventfd_ctx *ctx)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct telemetry_event *evt;
+
+ if (type >= SCMI_TLM_EVT_MAX)
+ return -EINVAL;
+
+ evt = kzalloc_obj(*evt);
+ if (!evt)
+ return -ENOMEM;
+
+ evt->ctx = ctx;
+ guard(mutex)(&ti->events_mtx);
+ list_add(&evt->item, &ti->events[type]);
+
+ trace_scmi_tlm_access(0, "TLM_EVT_SUBS", 0, 0);
+
+ return 0;
+}
+
+static int scmi_telemetry_event_unsubscribe(const struct scmi_protocol_handle *ph,
+ enum scmi_telemetry_event type,
+ struct eventfd_ctx *ctx)
+{
+ struct telemetry_info *ti = ph->get_priv(ph);
+ struct telemetry_event *evt, *n;
+
+ if (type >= SCMI_TLM_EVT_MAX)
+ return -EINVAL;
+
+ guard(mutex)(&ti->events_mtx);
+ list_for_each_entry_safe(evt, n, &ti->events[type], item) {
+ if (evt->ctx == ctx) {
+ list_del(&evt->item);
+ kfree(evt);
+
+ trace_scmi_tlm_access(0, "TLM_EVT_UNSUBS", 0, 0);
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.info_get = scmi_telemetry_info_get,
.de_lookup = scmi_telemetry_de_lookup,
@@ -3702,6 +3774,8 @@ static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
.des_bulk_read = scmi_telemetry_des_bulk_read,
.des_sample_get = scmi_telemetry_des_sample_get,
.reset = scmi_telemetry_reset,
+ .event_subscribe = scmi_telemetry_event_subscribe,
+ .event_unsubscribe = scmi_telemetry_event_unsubscribe,
};
static bool
@@ -4020,10 +4094,28 @@ static void scmi_telemetry_groups_free(struct scmi_telemetry_res_info *rinfo)
}
}
+static void scmi_telemetry_event_cleanup(struct telemetry_info *ti)
+{
+ guard(mutex)(&ti->events_mtx);
+ /* Clear any residual events */
+ for (int type = 0; type < SCMI_TLM_EVT_MAX; type++) {
+ struct telemetry_event *evt, *n;
+
+ list_for_each_entry_safe(evt, n, &ti->events[type], item) {
+ dev_warn(ti->ph->dev,
+ "Found UN-SUBSCRIBED event type %d!\n", type);
+ list_del(&evt->item);
+ kfree(evt);
+ }
+ }
+}
+
static void scmi_telemetry_instance_cleanup(void *arg)
{
struct telemetry_info *ti = arg;
+ /* Cleanup any still registered event */
+ scmi_telemetry_event_cleanup(ti);
scmi_telemetry_groups_free(ti->rinfo);
scmi_telemetry_tdes_cleanup(ti);
scmi_telemetry_uuids_cleanup(ti);
@@ -4185,6 +4277,10 @@ static int scmi_telemetry_instance_init(struct telemetry_info *ti)
return ret;
}
+ for (int i = 0; i < SCMI_TLM_EVT_MAX; i++)
+ INIT_LIST_HEAD(&ti->events[i]);
+ mutex_init(&ti->events_mtx);
+
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 8328ba9b592b..8ca6803df60a 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -11,6 +11,7 @@
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/device.h>
+#include <linux/eventfd.h>
#include <linux/notifier.h>
#include <linux/types.h>
#include <linux/uuid.h>
@@ -994,6 +995,11 @@ struct scmi_telemetry_de_sample {
unsigned long long val;
};
+enum scmi_telemetry_event {
+ SCMI_TLM_EVT_DUMMY,
+ SCMI_TLM_EVT_MAX,
+};
+
/**
* struct scmi_telemetry_proto_ops - represents the various operations provided
* by SCMI Telemetry Protocol
@@ -1017,6 +1023,10 @@ struct scmi_telemetry_de_sample {
* This causes an immediate update platform-side of all the
* enabled DEs.
* @reset: reset configuration and telemetry data.
+ * @event_subscribe: subscribe to the specified event @type using the provided
+ * @eventfd_ctx.
+ * @event_unsubscribe: unsubscribe to the specified event @type the previously
+ * registered @eventfd_ctx.
*/
struct scmi_telemetry_proto_ops {
const struct scmi_telemetry_info __must_check *(*info_get)
@@ -1044,6 +1054,12 @@ struct scmi_telemetry_proto_ops {
int __must_check (*des_sample_get)(const struct scmi_protocol_handle *ph,
int grp_id, int *num_samples,
struct scmi_telemetry_de_sample *samples);
+ int (*event_subscribe)(const struct scmi_protocol_handle *ph,
+ enum scmi_telemetry_event type,
+ struct eventfd_ctx *ctx);
+ int (*event_unsubscribe)(const struct scmi_protocol_handle *ph,
+ enum scmi_telemetry_event type,
+ struct eventfd_ctx *ctx);
int (*reset)(const struct scmi_protocol_handle *ph);
};
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 21/32] firmware: arm_scmi: Add Telemetry generation counter event
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (19 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 20/32] firmware: arm-scmi: Add Telemetry generic event support Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 22/32] firmware: arm_scmi: Add common per-protocol debugfs support Cristian Marussi
` (10 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 per-instance generation counter to track configuration changes which
uses the common telemetry events framework.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v12 --> v13
- update generation counter on do_xfer succcess even if failing to process
response since the change hass been applied: this avoids state misalignment
v11 -->v12
- reviewed generation counter update logic
v6 --> v7
- use new Generic event mechanism based on EventFD instead of WaitQ
v5 --> v6
- add relese semantic to the atomic set of the generation counter
- use new eventfd_ctx common event logic
---
drivers/firmware/arm_scmi/telemetry.c | 51 ++++++++++++++++++++++++++-
include/linux/scmi_protocol.h | 6 ++--
2 files changed, 54 insertions(+), 3 deletions(-)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index b1e3f986919e..a28d13048b54 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -16,6 +16,7 @@
#include <linux/hash.h>
#include <linux/io.h>
#include <linux/limits.h>
+#include <linux/lockdep.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/mutex.h>
@@ -26,6 +27,7 @@
#include <linux/string.h>
#include <linux/uuid.h>
#include <linux/xarray.h>
+#include <linux/wait.h>
#include "protocols.h"
#include "notify.h"
@@ -35,6 +37,7 @@
/* Updated only after ALL the mandatory features for that version are merged */
#define SCMI_PROTOCOL_SUPPORTED_VERSION 0x10000
+#define SCMI_TLM_GENERATION_ONE (SCMI_TLM_GENERATION_INVALID + 1U)
#define SCMI_TLM_TDCF_MAX_RETRIES 5
#define SCMI_TLM_TDCF_RETRY_GAP 1234
@@ -733,6 +736,37 @@ scmi_telemetry_tde_cache_lookup(struct telemetry_de *tde,
return 0;
}
+static inline void __scmi_telemetry_generation_set(struct telemetry_info *ti,
+ unsigned int new)
+{
+ int ret;
+
+ atomic_set_release(&ti->info.generation, new);
+
+ ret = scmi_telemetry_event_signal(ti, SCMI_TLM_EVT_GENERATION);
+ if (ret)
+ dev_warn_once(ti->ph->dev,
+ "Could NOT signal telemetry event %d\n",
+ SCMI_TLM_EVT_GENERATION);
+}
+
+static inline void scmi_telemetry_generation_update(struct telemetry_info *ti)
+{
+ unsigned int next;
+
+ lockdep_assert_held(&ti->config_mtx);
+
+ /* Wrap around skipping invalid generation 0 */
+ next = (atomic_read(&ti->info.generation) + 1) ?: SCMI_TLM_GENERATION_ONE;
+
+ __scmi_telemetry_generation_set(ti, next);
+}
+
+static inline void scmi_telemetry_generation_reset(struct telemetry_info *ti)
+{
+ __scmi_telemetry_generation_set(ti, SCMI_TLM_GENERATION_ONE);
+}
+
struct scmi_tlm_de_priv {
struct telemetry_info *ti;
void *next;
@@ -2875,6 +2909,9 @@ scmi_telemetry_remote_state_update_needed(struct telemetry_info *ti,
is_group ? obj : NULL,
!is_group ? obj : NULL);
+ /* A local change can have an impact anyway */
+ scmi_telemetry_generation_update(ti);
+
return false;
}
@@ -2973,6 +3010,8 @@ static int scmi_telemetry_tde_state_set(const struct scmi_protocol_handle *ph,
dev_err(ph->dev,
"Could NOT process DE ID 0x%08X state response - ret:%d\n",
tde->de.info->id, ret);
+
+ scmi_telemetry_generation_update(ti);
}
ph->xops->xfer_put(ph, t);
@@ -3019,6 +3058,8 @@ static int scmi_telemetry_group_state_set(const struct scmi_protocol_handle *ph,
dev_err(ph->dev,
"Could NOT process GROUP %u state response - ret:%d\n",
grp->info->grp_id, ret);
+
+ scmi_telemetry_generation_update(ti);
}
ph->xops->xfer_put(ph, t);
@@ -3170,6 +3211,9 @@ scmi_telemetry_collection_configure(const struct scmi_protocol_handle *ph,
ph->xops->xfer_put(ph, t);
+ if (!ret)
+ scmi_telemetry_generation_update(ti);
+
return ret;
}
@@ -3707,8 +3751,11 @@ static int scmi_telemetry_reset(const struct scmi_protocol_handle *ph)
put_unaligned_le32(0, t->tx.buf);
ret = ph->xops->do_xfer(ph, t);
- if (!ret)
+ if (!ret) {
scmi_telemetry_local_resources_reset(ti);
+ /* Reset generation now that server has been reset */
+ scmi_telemetry_generation_reset(ti);
+ }
ph->xops->xfer_put(ph, t);
@@ -4281,6 +4328,8 @@ static int scmi_telemetry_instance_init(struct telemetry_info *ti)
INIT_LIST_HEAD(&ti->events[i]);
mutex_init(&ti->events_mtx);
+ /* Generation counter init */
+ atomic_set(&ti->info.generation, SCMI_TLM_GENERATION_ONE);
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 8ca6803df60a..0210247c0166 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -891,6 +891,7 @@ enum scmi_telemetry_collection {
SCMI_TLM_SINGLE_READ,
};
+#define SCMI_TLM_GENERATION_INVALID 0U
#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF
struct scmi_telemetry_intervals {
@@ -986,6 +987,7 @@ struct scmi_telemetry_info {
bool enabled;
bool notif_enabled;
enum scmi_telemetry_collection current_mode;
+ atomic_t generation;
};
struct scmi_telemetry_de_sample {
@@ -996,7 +998,7 @@ struct scmi_telemetry_de_sample {
};
enum scmi_telemetry_event {
- SCMI_TLM_EVT_DUMMY,
+ SCMI_TLM_EVT_GENERATION,
SCMI_TLM_EVT_MAX,
};
@@ -1022,11 +1024,11 @@ enum scmi_telemetry_event {
* the ones belonging to a specific group when provided.
* This causes an immediate update platform-side of all the
* enabled DEs.
- * @reset: reset configuration and telemetry data.
* @event_subscribe: subscribe to the specified event @type using the provided
* @eventfd_ctx.
* @event_unsubscribe: unsubscribe to the specified event @type the previously
* registered @eventfd_ctx.
+ * @reset: reset configuration and telemetry data.
*/
struct scmi_telemetry_proto_ops {
const struct scmi_telemetry_info __must_check *(*info_get)
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 22/32] firmware: arm_scmi: Add common per-protocol debugfs support
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (20 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 21/32] firmware: arm_scmi: Add Telemetry generation counter event Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 23/32] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Cristian Marussi
` (9 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 35 +++++++++++++++++++++++++--
drivers/firmware/arm_scmi/protocols.h | 6 +++++
include/linux/scmi_protocol.h | 1 -
5 files changed, 55 insertions(+), 3 deletions(-)
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 c31896672b81..ce30cc025dae 100644
--- a/drivers/firmware/arm_scmi/common.h
+++ b/drivers/firmware/arm_scmi/common.h
@@ -326,6 +326,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
@@ -333,6 +334,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 7b4cb257605d..601f2e569d64 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -18,6 +18,7 @@
#include <linux/bitmap.h>
#include <linux/cleanup.h>
+#include <linux/dcache.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/export.h>
@@ -102,6 +103,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 DT and implemented by the SCMI server fw.
@@ -116,6 +119,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)
@@ -2094,6 +2098,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,
@@ -2104,6 +2127,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,
};
/**
@@ -2289,12 +2313,12 @@ 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);
if (ret != proto->id)
- goto clean;
+ goto clean_dbg;
/*
* Warn but ignore events registration errors since we do not want
@@ -2315,6 +2339,8 @@ scmi_alloc_init_protocol_instance(struct scmi_info *info,
return pi;
+clean_dbg:
+ debugfs_remove_recursive(pi->dbg);
clean:
/* Take care to put the protocol module's owner before releasing all */
scmi_protocol_put(proto);
@@ -2435,6 +2461,8 @@ void scmi_protocol_release_unlocked(const struct scmi_handle *handle,
if (refcount_dec_and_test(&pi->users)) {
void *gid = pi->gid;
+ debugfs_remove_recursive(pi->dbg);
+
if (pi->proto->events)
scmi_deregister_protocol_events(handle, protocol_id);
@@ -3191,6 +3219,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 1f9925b8f495..04e196a9fbf4 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>
@@ -284,6 +285,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,
@@ -307,6 +311,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);
};
/**
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index 0210247c0166..fbed0229c3f3 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -1156,7 +1156,6 @@ struct scmi_handle {
void (*devm_protocol_put)(struct scmi_device *sdev, u8 proto);
bool (*is_transport_atomic)(const struct scmi_handle *handle,
unsigned int *atomic_threshold);
-
const struct scmi_notify_ops *notify_ops;
};
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 23/32] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (21 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 22/32] firmware: arm_scmi: Add common per-protocol debugfs support Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 24/32] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Cristian Marussi
` (8 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 98 +++++++++++++++++++++++++++
1 file changed, 98 insertions(+)
diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index a28d13048b54..6e76cfebdf1b 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -9,10 +9,12 @@
#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/eventfd.h>
+#include <linux/fs.h>
#include <linux/hash.h>
#include <linux/io.h>
#include <linux/limits.h>
@@ -419,6 +421,14 @@ struct telemetry_shmti {
struct scmi_telemetry_shmti_info info;
};
+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); \
@@ -1523,6 +1533,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, *shmti_top;
+
+ top = ph->hops->debugfs_proto_dentry_get(ph);
+ 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, 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)
{
@@ -4410,6 +4505,9 @@ static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
}
}
+ if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS))
+ scmi_telemetry_debugfs_initialize(ti);
+
return 0;
}
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 24/32] firmware: arm_scmi: Add Telemetry debugfs ABI documentation
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (22 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 23/32] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 25/32] firmware: arm_scmi: Expose per-instance identifier Cristian Marussi
` (7 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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..edbbe360321d 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: Nov 2026
+KernelVersion: 7.3
+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: Nov 2026
+KernelVersion: 7.3
+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 v14 25/32] firmware: arm_scmi: Expose per-instance identifier
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (23 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 24/32] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 26/32] firmware: arm_scmi: Add un-managed methods to get/put protocols operations Cristian Marussi
` (6 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 601f2e569d64..9531027a12c4 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -3374,6 +3374,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 fbed0229c3f3..3714be973bd0 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -1125,6 +1125,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,
@@ -1145,6 +1149,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 v14 26/32] firmware: arm_scmi: Add un-managed methods to get/put protocols operations
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (24 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 25/32] firmware: arm_scmi: Expose per-instance identifier Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 27/32] uapi: Add ARM SCMI Telemetry definitions Cristian Marussi
` (5 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 couple of simple common methods to be able to manually get and put
protocol operations to address situation in which devres managed methods
cannot be used.
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v10 --> v11
- fixed a few comments typos
---
drivers/firmware/arm_scmi/driver.c | 39 ++++++++++++++++++++++++++++++
include/linux/scmi_protocol.h | 7 ++++++
2 files changed, 46 insertions(+)
diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 9531027a12c4..5e33b38cc6bb 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -2593,6 +2593,31 @@ scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
return pi->proto->ops;
}
+/**
+ * scmi_protocol_operations_get - UnManaged get protocol operations
+ *
+ * @handle: A reference to the SCMI platform instance.
+ * @protocol_id: The protocol being acquired.
+ * @ph: A pointer reference used to pass back the associated protocol handle.
+ */
+static const void __must_check *
+scmi_protocol_operations_get(const struct scmi_handle *handle, u8 protocol_id,
+ struct scmi_protocol_handle **ph)
+{
+ struct scmi_protocol_instance *pi;
+
+ if (!ph)
+ return ERR_PTR(-EINVAL);
+
+ pi = scmi_get_protocol_instance(handle, protocol_id);
+ if (IS_ERR(pi))
+ return pi;
+
+ *ph = &pi->ph;
+
+ return pi->proto->ops;
+}
+
/**
* scmi_devm_protocol_acquire - Devres managed helper to get hold of a protocol
* @sdev: A reference to an scmi_device whose embedded struct device is to
@@ -2649,6 +2674,18 @@ static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
WARN_ON(ret);
}
+/**
+ * scmi_protocol_operations_put - UnManaged put protocol operations
+ *
+ * @handle: A reference to the SCMI platform instance.
+ * @protocol_id: The protocol being released.
+ */
+static void scmi_protocol_operations_put(const struct scmi_handle *handle,
+ u8 protocol_id)
+{
+ scmi_protocol_release(handle, protocol_id);
+}
+
/**
* scmi_is_transport_atomic - Method to check if underlying transport for an
* SCMI instance is configured as atomic.
@@ -3380,6 +3417,8 @@ static int scmi_probe(struct platform_device *pdev)
handle->devm_protocol_acquire = scmi_devm_protocol_acquire;
handle->devm_protocol_get = scmi_devm_protocol_get;
handle->devm_protocol_put = scmi_devm_protocol_put;
+ handle->protocol_get = scmi_protocol_operations_get;
+ handle->protocol_put = scmi_protocol_operations_put;
handle->is_transport_atomic = scmi_is_transport_atomic;
/* Setup all channels described in the DT at first */
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index 3714be973bd0..436c20d1528d 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -1137,6 +1137,9 @@ struct scmi_notify_ops {
* @devm_protocol_get: devres managed method to acquire a protocol and get specific
* operations and a dedicated protocol handler
* @devm_protocol_put: devres managed method to release a protocol
+ * @protocol_get: unmanaged method to acquire a protocol and get specific
+ * operations and a dedicated protocol handler
+ * @protocol_put: unmanaged method to release a protocol
* @is_transport_atomic: method to check if the underlying transport for this
* instance handle is configured to support atomic
* transactions for commands.
@@ -1159,6 +1162,10 @@ struct scmi_handle {
(*devm_protocol_get)(struct scmi_device *sdev, u8 proto,
struct scmi_protocol_handle **ph);
void (*devm_protocol_put)(struct scmi_device *sdev, u8 proto);
+ const void __must_check *
+ (*protocol_get)(const struct scmi_handle *handle, u8 proto,
+ struct scmi_protocol_handle **ph);
+ void (*protocol_put)(const struct scmi_handle *handle, u8 proto);
bool (*is_transport_atomic)(const struct scmi_handle *handle,
unsigned int *atomic_threshold);
const struct scmi_notify_ops *notify_ops;
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 27/32] uapi: Add ARM SCMI Telemetry definitions
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (25 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 26/32] firmware: arm_scmi: Add un-managed methods to get/put protocols operations Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 28/32] firmware: arm_scmi: Add System Telemetry driver Cristian Marussi
` (4 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 522 ++++++++++++++++++++++++++++++++++++++
2 files changed, 523 insertions(+)
create mode 100644 include/uapi/linux/scmi.h
diff --git a/MAINTAINERS b/MAINTAINERS
index f53047aa8d3c..b336521ae4ce 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -26629,6 +26629,7 @@ F: drivers/regulator/scmi-regulator.c
F: drivers/reset/reset-scmi.c
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..41fa2db9f1d6
--- /dev/null
+++ b/include/uapi/linux/scmi.h
@@ -0,0 +1,522 @@
+/* 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)
+ __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.
+ *
+ * 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
+ *
+ * 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. Anybody with knowledge of the @cookie can cancel the
+ * subscription.
+ * @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 v14 28/32] firmware: arm_scmi: Add System Telemetry driver
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (26 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 27/32] uapi: Add ARM SCMI Telemetry definitions Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 29/32] docs: ioctl-number: Add SCMI Ioctls Cristian Marussi
` (3 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
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 | 1751 +++++++++++++++++
3 files changed, 1762 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..a20f91757592
--- /dev/null
+++ b/drivers/firmware/arm_scmi/scmi_system_telemetry.c
@@ -0,0 +1,1751 @@
+// 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/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.
+ * @sessions_xa: An XArray holding sessions descriptors, indexed by per-process
+ * keys (@filp), that in turn contain the Telemetry events
+ * currently subscribed by this instance of the driver.
+ * @sessions_mtx: A mutex to protect sessions_xa
+ * @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;
+#define SCMI_TLM_EVT_XA_MIN 1
+#define scmi_evt_xa_limit XA_LIMIT(SCMI_TLM_EVT_XA_MIN, UINT_MAX)
+ struct xarray sessions_xa;
+ /* Protect sessions_xa */
+ struct mutex sessions_mtx;
+ /* 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)
+
+struct scmi_tlm_registered_event {
+ enum scmi_telemetry_event type;
+ struct eventfd_ctx *ctx;
+};
+
+struct scmi_tlm_events_session {
+ struct xarray events_xa;
+ /* Protect events_xa */
+ struct mutex events_mtx;
+};
+
+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_RESET | SCMI_TLM_ABI_FEAT_EVENT |
+ 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]);
+}
+
+static inline void scmi_tlm_to_uapi_grp_info(struct scmi_tlm_grp_info *out,
+ struct scmi_telemetry_grp_info *in)
+{
+ out->grp_id = in->grp_id;
+ out->num_des = in->num_des;
+ out->num_intervals = in->num_intervals;
+ out->pad = 0;
+}
+
+static inline void
+scmi_tlm_to_uapi_grps_list(unsigned int num_groups,
+ const struct scmi_telemetry_res_info *in,
+ struct scmi_tlm_grp_info *ginfo)
+{
+ struct scmi_tlm_grp_info *out_ginfo = ginfo;
+
+ for (int i = 0; i < num_groups; i++)
+ scmi_tlm_to_uapi_grp_info(&out_ginfo[i], &in->grps_store[i]);
+}
+
+static inline void
+scmi_tlm_to_uapi_composing_des(struct scmi_tlm_grp_desc *out,
+ const struct scmi_telemetry_res_info *in,
+ u32 *composing_des)
+{
+ struct scmi_telemetry_group *grp = &in->grps[out->grp_id];
+
+ for (int i = 0; i < out->num_des; i++)
+ composing_des[i] = in->des[grp->des[i]]->info->id;
+}
+
+/**
+ * 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 < 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)
+{
+ 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))) {
+ unsigned int active = ti->info->active_update_interval;
+
+ 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);
+ } else {
+ const struct scmi_telemetry_res_info *rinfo;
+ struct scmi_telemetry_group *grp;
+ unsigned int active;
+
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ if (cfg.grp_id >= rinfo->num_groups)
+ return -EINVAL;
+
+ grp = &rinfo->grps[cfg.grp_id];
+ active = grp->active_update_interval;
+
+ cfg.enable = grp->enabled;
+ cfg.t_enable = grp->tstamp_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 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, t_ena;
+
+ if (copy_from_user(&cfg, uptr, sizeof(cfg)))
+ return -EFAULT;
+
+ if (cfg.pad || cfg.reserved || cfg.flags & ~SCMI_TLM_CONFIG_FLAGS)
+ return -EINVAL;
+
+ ena = !!cfg.enable;
+ t_ena = !!cfg.t_enable;
+ if (!SCMI_TLM_CONFIG_IS_GROUP(cfg.flags)) {
+ cfg.grp_id = SCMI_TLM_GRP_INVALID;
+ } else {
+ int ret;
+
+ ret = tsp->ops->state_set(tsp->ph, true, cfg.grp_id, &ena, &t_ena,
+ NULL, NULL, NULL);
+ if (ret)
+ return ret;
+ }
+
+ active = SCMI_TLM_BUILD_UPDATE_INTERVAL(cfg.active.secs, cfg.active.exp);
+
+ 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)) {
+ tlm_ivs = ti->info->intervals;
+ } else {
+ const struct scmi_telemetry_res_info *rinfo;
+
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ if (ivs.grp_id >= rinfo->num_groups)
+ return -EINVAL;
+
+ tlm_ivs = rinfo->grps[ivs.grp_id].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;
+
+ /* Was a size query ? */
+ if (batch->num_items == 0) {
+ *out_items = NULL;
+ *out_batch_sz = 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) {
+ 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;
+
+ if (out_states)
+ *out_states = no_free_ptr(states_arr);
+ if (out_states_sz)
+ *out_states_sz = states_sz;
+ } else {
+ if (out_states)
+ *out_states = NULL;
+ if (out_states_sz)
+ *out_states_sz = 0;
+ }
+
+ *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) {
+ 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.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.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;
+
+ 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 = {};
+
+ 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.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_grp_info_get_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_grp_info ginfo;
+
+ if (copy_from_user(&ginfo, uptr, sizeof(ginfo)))
+ return -EFAULT;
+
+ if (ginfo.pad || ginfo.reserved)
+ return -EINVAL;
+
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ if (ginfo.grp_id >= rinfo->num_groups)
+ return -EINVAL;
+
+ scmi_tlm_to_uapi_grp_info(&ginfo, &rinfo->grps_store[ginfo.grp_id]);
+ if (copy_to_user(uptr, &ginfo, sizeof(ginfo)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static long
+scmi_tlm_grp_desc_get_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_grp_desc gdesc;
+ size_t composing_sz;
+
+ if (copy_from_user(&gdesc, uptr, sizeof(gdesc)))
+ return -EFAULT;
+
+ if (gdesc.reserved)
+ return -EINVAL;
+
+ rinfo = scmi_telemetry_res_info_get(ti->tsp);
+ if (gdesc.grp_id >= rinfo->num_groups)
+ return -EINVAL;
+
+ /* Reject only oversized output buffers */
+ if (gdesc.num_des > rinfo->grps_store[gdesc.grp_id].num_des)
+ return -ENOSPC;
+
+ composing_sz = array_size(gdesc.num_des, sizeof(u32));
+ if (composing_sz == SIZE_MAX)
+ return -EOVERFLOW;
+
+ /* A local scratch buffer... */
+ u32 *composing_des __free(kvfree) = kvzalloc(composing_sz, GFP_KERNEL);
+ if (!composing_des)
+ return -ENOMEM;
+
+ /* Passing the array as a param avoids dancing with uptr->intervals */
+ scmi_tlm_to_uapi_composing_des(&gdesc, rinfo, composing_des);
+ if (copy_to_user(u64_to_user_ptr(gdesc.composing_des), composing_des,
+ composing_sz))
+ return -EFAULT;
+
+ if (copy_to_user(uptr, &gdesc, sizeof(gdesc)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static long
+scmi_tlm_grps_list_get_ioctl(const struct scmi_tlm_instance *ti, unsigned long arg)
+{
+ struct scmi_tlm_grp_info *ginfo __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(*ginfo),
+ rinfo->num_groups, (void **)&ginfo,
+ &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_grps_list(batch.num_items, rinfo, ginfo);
+ else
+ batch.num_items = rinfo->num_groups;
+
+ return scmi_tlm_batch_finalize(uptr, &batch, ginfo, NULL, batch_sz, 0);
+}
+
+static long scmi_tlm_des_read_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg, bool single)
+{
+ 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;
+
+ /* 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;
+
+ if (!SCMI_TLM_READ_IS_GROUP(bulk.flags)) {
+ grp_id = SCMI_TLM_GRP_INVALID;
+ } else {
+ if (bulk.grp_id >= rinfo->num_groups)
+ return -EINVAL;
+
+ grp_id = bulk.grp_id;
+ }
+
+ /* Return max samples on zero-query */
+ if (!bulk.num_samples) {
+ if (!SCMI_TLM_READ_IS_GROUP(bulk.flags))
+ bulk.num_samples = rinfo->num_des;
+ else
+ bulk.num_samples = rinfo->grps[grp_id].info->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;
+
+ if (!single)
+ ret = tsp->ops->des_bulk_read(tsp->ph, grp_id,
+ &bulk.num_samples, samples);
+ else
+ ret = tsp->ops->des_sample_get(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;
+
+ 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;
+
+ if (vma->vm_flags & VM_WRITE)
+ return -EPERM;
+
+ /* Grab a ctx reference early on */
+ ctx = scmi_tlm_shmti_ctx_get(filp->private_data);
+ guard(srcu)(&ctx->ti->dead_srcu);
+ if (READ_ONCE(ctx->ti->dead)) {
+ scmi_tlm_shmti_ctx_put(ctx);
+ return -ENODEV;
+ }
+
+ req = vma->vm_end - vma->vm_start;
+ if (req < ctx->shmti->len) {
+ scmi_tlm_shmti_ctx_put(ctx);
+ return -EINVAL;
+ }
+
+ /* Align & reject oversized requests */
+ base = ctx->shmti->phys & PAGE_MASK;
+ needed = ctx->shmti->len + ctx->shmti->offset;
+ expect = DIV_ROUND_UP(needed, PAGE_SIZE);
+ npages = req >> PAGE_SHIFT;
+ if (npages > expect) {
+ 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) {
+ scmi_tlm_shmti_ctx_put(ctx);
+ return ret;
+ }
+
+ 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_reset_ioctl(const struct scmi_tlm_instance *ti,
+ unsigned long arg)
+{
+ return ti->tsp->ops->reset(ti->tsp->ph);
+}
+
+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 int scmi_tlm_event_subscribe(struct scmi_tlm_instance *ti,
+ struct scmi_tlm_event *evt, void *key)
+{
+ struct scmi_tlm_events_session *session;
+ struct scmi_tlm_registered_event *revt;
+ struct eventfd_ctx *ctx;
+ u32 cookie;
+ int ret;
+
+ scoped_guard(mutex, &ti->sessions_mtx) {
+ session = xa_load(&ti->sessions_xa, (unsigned long)key);
+ if (!session) {
+ /*
+ * Initialize a new session on first call.
+ * Once created successfully initialized sessions are
+ * freed only on final release.
+ */
+ session = kzalloc_obj(*session);
+ if (!session)
+ return -ENOMEM;
+
+ /* Initialize inner with proper flags to support xa_alloc */
+ xa_init_flags(&session->events_xa, XA_FLAGS_ALLOC);
+ mutex_init(&session->events_mtx);
+ ret = xa_insert(&ti->sessions_xa, (unsigned long)key,
+ session, GFP_KERNEL);
+ if (ret) {
+ xa_destroy(&session->events_xa);
+ kfree(session);
+ return ret;
+ }
+ }
+ }
+
+ ctx = eventfd_ctx_fdget(evt->efd);
+ if (IS_ERR(ctx))
+ return PTR_ERR(ctx);
+
+ revt = kzalloc_obj(*revt);
+ if (!revt) {
+ eventfd_ctx_put(ctx);
+ return -ENOMEM;
+ }
+
+ guard(mutex)(&session->events_mtx);
+ revt->ctx = ctx;
+ revt->type = evt->type;
+ ret = xa_alloc(&session->events_xa, &cookie, revt,
+ scmi_evt_xa_limit, GFP_KERNEL);
+ if (ret) {
+ eventfd_ctx_put(revt->ctx);
+ kfree(revt);
+ return ret;
+ }
+
+ ret = ti->tsp->ops->event_subscribe(ti->tsp->ph, revt->type, revt->ctx);
+ if (ret) {
+ xa_erase(&session->events_xa, cookie);
+ eventfd_ctx_put(revt->ctx);
+ kfree(revt);
+ return ret;
+ }
+
+ /* Return the allocated cookie */
+ evt->cookie = cookie;
+
+ /* Get hold of the instance */
+ get_device(&ti->dev);
+
+ return 0;
+}
+
+static int scmi_tlm_event_unsubscribe(struct scmi_tlm_instance *ti,
+ struct scmi_tlm_event *evt, void *key)
+{
+ struct scmi_tlm_events_session *session;
+ struct scmi_tlm_registered_event *revt;
+ int ret;
+
+ if (!evt->cookie)
+ return -EINVAL;
+
+ scoped_guard(mutex, &ti->sessions_mtx) {
+ session = xa_load(&ti->sessions_xa, (unsigned long)key);
+ if (!session)
+ return -ENODEV;
+ }
+
+ guard(mutex)(&session->events_mtx);
+ revt = xa_load(&session->events_xa, evt->cookie);
+ if (!revt)
+ return -ENODEV;
+
+ if (revt->type != evt->type)
+ return -EINVAL;
+
+ ret = ti->tsp->ops->event_unsubscribe(ti->tsp->ph, revt->type, revt->ctx);
+ if (ret)
+ return ret;
+
+ /* Clean up the registered event ONLY once successfully unsubscribed */
+ xa_erase(&session->events_xa, evt->cookie);
+ eventfd_ctx_put(revt->ctx);
+ kfree(revt);
+
+ put_device(&ti->dev);
+
+ return 0;
+}
+
+static long scmi_tlm_event_subscribe_ioctl(struct scmi_tlm_instance *ti,
+ unsigned long arg, void *session)
+{
+ void __user *uptr = (void __user *)arg;
+ struct scmi_tlm_event evt = {};
+ int ret;
+
+ if (copy_from_user(&evt, uptr, sizeof(evt)))
+ return -EFAULT;
+
+ if (evt.pad || evt.reserved)
+ return -EINVAL;
+
+ /* Reject unknown event types */
+ if (evt.type > SCMI_TLM_EVT_LAST)
+ return -ENODEV;
+
+ if (!evt.cookie) {
+ /* Register a per-session new event, returning new cookie */
+ ret = scmi_tlm_event_subscribe(ti, &evt, session);
+ if (ret)
+ return ret;
+
+ if (copy_to_user(uptr, &evt, sizeof(evt))) {
+ scmi_tlm_event_unsubscribe(ti, &evt, session);
+ return -EFAULT;
+ }
+ } else {
+ /* DE-Register if existent, put context */
+ ret = scmi_tlm_event_unsubscribe(ti, &evt, session);
+ if (ret)
+ return ret;
+ }
+
+ return 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 scmi_tlm_grps_list_get_ioctl(ti, arg);
+ case SCMI_TLM_GET_GRP_INFO:
+ return scmi_tlm_grp_info_get_ioctl(ti, arg);
+ case SCMI_TLM_GET_GRP_DESC:
+ return scmi_tlm_grp_desc_get_ioctl(ti, arg);
+ case SCMI_TLM_SINGLE_READ:
+ return scmi_tlm_des_read_ioctl(ti, arg, true);
+ case SCMI_TLM_BULK_READ:
+ return scmi_tlm_des_read_ioctl(ti, arg, false);
+ 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:
+ if (writable)
+ return scmi_tlm_reset_ioctl(ti, arg);
+ break;
+ case SCMI_TLM_GET_UUID_LIST:
+ return scmi_tlm_uuid_list_get_ioctl(ti, arg);
+ case SCMI_TLM_EVENT_SUBSCRIBE:
+ return scmi_tlm_event_subscribe_ioctl(ti, arg, filp);
+ 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 void
+scmi_tlm_events_session_unsubscribe(struct scmi_tlm_instance *ti,
+ struct scmi_tlm_events_session *session)
+{
+ unsigned long cookie = SCMI_TLM_EVT_XA_MIN;
+ struct device *dev = &ti->tsp->sdev->dev;
+ struct scmi_tlm_registered_event *revt;
+
+ //lockdep_assert_held(&ti->dead_mtx);
+
+ mutex_lock(&session->events_mtx);
+
+ /* Walk any still registered events and unsubscribe */
+ xa_for_each(&session->events_xa, cookie, revt) {
+ xa_erase(&session->events_xa, cookie);
+
+ /*
+ * Unsubscribe if still alive...this cleans up the pending
+ * events when a process has not cleaned up properly on its
+ * own on exit. (or died)
+ */
+ if (!READ_ONCE(ti->dead)) {
+ int ret;
+
+ ret = ti->tsp->ops->event_unsubscribe(ti->tsp->ph,
+ revt->type,
+ revt->ctx);
+ if (ret)
+ dev_warn(dev,
+ "Failed to unsubscribe event type:%u cookie:%lu\n",
+ revt->type, cookie);
+ }
+
+ dev_dbg(dev,
+ "Forcibly unsubscribed event type:%u cookie:%lu\n",
+ revt->type, cookie);
+
+ eventfd_ctx_put(revt->ctx);
+ kfree(revt);
+
+ /* Drop the per-subscribe reference */
+ put_device(&ti->dev);
+ }
+
+ xa_destroy(&session->events_xa);
+
+ mutex_unlock(&session->events_mtx);
+
+ kfree(session);
+}
+
+static int scmi_tlm_release(struct inode *ino, struct file *filp)
+{
+ struct scmi_tlm_instance *ti = filp->private_data;
+ struct scmi_tlm_events_session *session;
+
+ /* Any events at all for this session ?*/
+ mutex_lock(&ti->sessions_mtx);
+ session = xa_erase(&ti->sessions_xa, (unsigned long)filp);
+ mutex_unlock(&ti->sessions_mtx);
+ /*
+ * Any STILL subscribed event is cleaned-up here at the final release:
+ * this adddresses the case in which a userspace application had not
+ * properly unsubscribed OR died unexpectedly.
+ */
+ if (session) {
+ guard(srcu)(&ti->dead_srcu);
+ scmi_tlm_events_session_unsubscribe(ti, session);
+ }
+
+ 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;
+ /* Keep track of events via per-process sessions */
+ xa_init(&ti->sessions_xa);
+ mutex_init(&ti->sessions_mtx);
+
+ 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);
+ struct scmi_device *sdev = ti->tsp->sdev;
+
+ dev_dbg(&sdev->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_events_session *session;
+ struct scmi_tlm_instance *ti;
+ unsigned long index;
+
+ 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);
+ /*
+ * Drop any residual session and related events if unbind
+ * happened with some still open event fds.
+ */
+ mutex_lock(&ti->sessions_mtx);
+ xa_for_each(&ti->sessions_xa, index, session) {
+ xa_erase(&ti->sessions_xa, index);
+ scmi_tlm_events_session_unsubscribe(ti, session);
+ }
+ mutex_unlock(&ti->sessions_mtx);
+
+ 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 v14 29/32] docs: ioctl-number: Add SCMI Ioctls
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (27 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 28/32] firmware: arm_scmi: Add System Telemetry driver Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 30/32] [RFC] Documentation: Add SCMI System Telemetry documentation Cristian Marussi
` (2 subsequent siblings)
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 2fc53093752d..3a89b2065674 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -401,6 +401,7 @@ Code Seq# Include File Comments
0xE5 00-3F linux/fuse.h
0xEC 00-01 drivers/platform/chrome/cros_ec_dev.h ChromeOS EC driver
0xEE 00-09 uapi/linux/pfrut.h Platform Firmware Runtime Update and Telemetry
+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 v14 30/32] [RFC] Documentation: Add SCMI System Telemetry documentation
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (28 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 29/32] docs: ioctl-number: Add SCMI Ioctls Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 31/32] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 32/32] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases Cristian Marussi
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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
Document ARM SCMI Telemetry IOCTLs interface.
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>
---
RFC since it still needs more extensive examples
v12 --> v13
- added to MAINTAINERS files
v6 --> v7
- removed useless top '..stlm' reference
- added new IOCTLs, fixed a few grammatical errors
v5 --> v6
- fixed typos
---
Documentation/userspace-api/index.rst | 1 +
Documentation/userspace-api/stlm.rst | 148 ++++++++++++++++++++++++++
MAINTAINERS | 1 +
3 files changed, 150 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..e30b1d1d19bd
--- /dev/null
+++ b/Documentation/userspace-api/stlm.rst
@@ -0,0 +1,148 @@
+.. 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.
+
+Configuration
+=============
+
+ - SCMI_TLM_GET_CFG / SCMI_TLM_SET_CFG: Get or set the whole instance, or a
+ specific group, configuration.
+
+ - SCMI_TLM_GET_DE_CFG / SCMI_TLM_SET_DE_CFG: Get or set the configuration
+ of a specific DataEvent.
+
+ - SCMI_TLM_GET_ALL_CFG / SCMI_TLM_SET_ALL_CFG: Get or set the cumulative
+ configuration of ALL the DataEvents defined on the platform.
+
+ - SCMI_TLM_RESET: Reset the whole Telemetry configuration server side.
+
+ - SCMI_TLM_EVENT_SUBSCRIBE: Subscribe/unsubscribe to a Telemetry event.
+
+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/
diff --git a/MAINTAINERS b/MAINTAINERS
index b336521ae4ce..1b614c35731b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -26617,6 +26617,7 @@ L: arm-scmi@vger.kernel.org
L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
S: Maintained
F: Documentation/devicetree/bindings/firmware/arm,sc[mp]i.yaml
+F Documentation/userspace-api/stlm.rst
F: drivers/clk/clk-sc[mp]i.c
F: drivers/cpufreq/sc[mp]i-cpufreq.c
F: drivers/firmware/arm_scmi/
--
2.54.0
^ permalink raw reply related [flat|nested] 38+ messages in thread
* [PATCH v14 31/32] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (29 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 30/32] [RFC] Documentation: Add SCMI System Telemetry documentation Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 32/32] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases Cristian Marussi
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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>
---
v13 -->v14
- fix mmap/munmap calls
- add proper NULL checks around failed enumerations
- harden cmd line parsing and report errors
v12 --> v13
- misc fixes on alloc/free
- add single DE dump capability to get info/cfg/data
- add SHMTIs decoder (by LLM)
- fix mmap len/offset call to account for bigger pages and offset
- fixed SPDX header
- added to MAINTAINERS file
v11 --> v12
- renamed command helpers
- reviewed all internal/external error codes handling
- fixed dump_shmti read loop
- fixed shmti_check end address calculation
- fixed batch read boundary checks
- fixed group indexing
v10 --> v11
- add query for ALL new 0-sized commands: GET_INTRVS/BULK/SINGLE
v8 --> v9
- move to new _LIST IOCTLs support based on scmi_tlm_batch container
- add a "query" command to issue 0-items IOCTLs requests
v7 -> v8
- add monitor detached option
v6 --> v7
- added monitor command for events
- using and exposing DE's UUID/SID/OFFSET
- use BATCH READ with status
- use BATCH DE CONFIG GET/SET
- use new ABI/SCMI feats flags
v5 --> v6
- carry-on on error if feats NOT supported
- build BATCH_READ with enumerated values
- new UAPI naming
- reworked UUID print
- added info/shmti interactive cmds
- added cfg/single/bulk/batch interactive cmds
- added reset
- added tlm enable
- using new GET_ABI_INFO
- moved DE_READ per-de sampling away from get_state
v4 --> v5
- changes to adapt to new chardev/IOCTLs ABI
- change IOCTLs param to avoid flexible arrays
- added SHMTIs mmap dump
v3 --> v4
- added generation file support
---
MAINTAINERS | 1 +
tools/testing/scmi/Makefile | 25 +
tools/testing/scmi/stlm.c | 1635 ++++++++++++++++++++++++++++++
tools/testing/scmi/stlm_decode.c | 252 +++++
tools/testing/scmi/stlm_decode.h | 7 +
5 files changed, 1920 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 1b614c35731b..77a23536ceb8 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -26632,6 +26632,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..2d4f4e630a1b
--- /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..6498babf6eb8
--- /dev/null
+++ b/tools/testing/scmi/stlm.c
@@ -0,0 +1,1635 @@
+// 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 information 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 [<de_id>] - Display detailed DEs info, config and 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 inline void dump_des(struct tlm_state *st, uint32_t *de_id)
+{
+ 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");
+
+ for (int i = 0; i < st->num_des; i++) {
+ struct tlm_de *de = &st->des[i];
+
+ if (de_id && de->info->id != *de_id)
+ continue;
+
+ /* Collect fresh sample */
+ if (de->cfg.enable) {
+ int ret;
+
+ de->sample.id = de->info->id;
+ ret = ioctl(st->fd, SCMI_TLM_DE_READ, &de->sample);
+ if (ret)
+ perror(IOCTL_ERR_STR(SCMI_TLM_DE_READ));
+ }
+
+ fprintf(stdout, "\t[0x%08X][%s]\t\t%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 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;
+
+ if (args && args->cnt) {
+ id = strtoul(args->opts[0], NULL, 0);
+ if (id == ULONG_MAX || errno == EINVAL) {
+ fprintf(stderr, "\n+ BAD DE_ID: %s\n", args->opts[0]);
+ return 0;
+ }
+
+ de = &id;
+ }
+
+ dump_des(st, de);
+
+ return 0;
+}
+
+static inline int cmd_dump_groups(struct tlm_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)\n",
+ !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_DE_TRACKING));
+ 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 (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);
+ }
+
+ if (args && args->cnt && args->opts &&
+ !strncmp(args->opts[0], "full", strlen(args->opts[0]))) {
+ cmd_dump_des(st, NULL);
+ cmd_dump_groups(st);
+ }
+
+ 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 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_des) {
+ st->des = enumerate_des(st);
+ if (!st->des)
+ fprintf(stderr, "Failed to enumerate DES\n");
+ }
+
+ if (st->info.num_groups) {
+ st->grps = enumerate_groups(st);
+ if (!st->grps)
+ fprintf(stderr, "Failed to enumerate GROUPS\n");
+ }
+
+ 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:%u\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:%u\n",
+ i, shmtis[i].sid, shmtis[i].len, shmtis[i].offset);
+ continue;
+ }
+
+ fprintf(stdout, "Mapping SHMTI[%u] - SID:%u LEN:%u FD:%u OFFS:%u\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;
+
+ 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:%u",
+ 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:%u",
+ 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:%u",
+ 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;
+
+ 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, 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 == SIGINT)
+ fprintf(stderr, "Got SIGINT\n");
+ else
+ fprintf(stderr, "Got signal\n");
+ 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;
+
+ 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;
+
+ ret = query_items(st->fd, SCMI_TLM_GET_GRP_LIST,
+ sizeof(struct scmi_tlm_grp_info),
+ "SCMI_TLM_GET_GRP_LIST");
+ if (ret)
+ 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
* [PATCH v14 32/32] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
` (30 preceding siblings ...)
2026-10-16 18:55 ` [PATCH v14 31/32] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool Cristian Marussi
@ 2026-10-16 18:55 ` Cristian Marussi
31 siblings, 0 replies; 38+ messages in thread
From: Cristian Marussi @ 2026-10-16 18:55 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 | 1810 +++++++++++++++++
4 files changed, 1825 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..24a8170a4380
--- /dev/null
+++ b/tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c
@@ -0,0 +1,1810 @@
+// 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 > 1) {
+ int num_intervals = ksft_session.info.num_intervals;
+ struct scmi_tlm_update_interval *intervals;
+
+ intervals = (struct scmi_tlm_update_interval *)ksft_session.available.intervals;
+ for (int i = 0; i < num_intervals; i++) {
+ if (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);
+}
+
+TEST_F(stlm, tlm_de_read)
+{
+ struct scmi_tlm_de_sample sample = {};
+ __u32 de_id;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list availalbe");
+
+ de_id = ksft_session.de_list[0].id;
+ sample.id = de_id;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_DE_READ, &sample));
+ 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_de_sample sample = {};
+
+ sample.id = 0xffffffff;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_DE_READ, &sample));
+ EXPECT_EQ(ENOENT, errno);
+}
+
+TEST_F(stlm, tlm_single_read)
+{
+ struct scmi_tlm_de_sample *samples;
+ struct scmi_tlm_data_read read = {};
+ int num_samples = ksft_session.num_des;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ 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;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read));
+ 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 = {};
+
+ 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 = {};
+
+ 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)
+{
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, NULL));
+ EXPECT_EQ(EFAULT, errno);
+}
+
+TEST_F(stlm, tlm_bulk_read)
+{
+ struct scmi_tlm_de_sample *samples;
+ struct scmi_tlm_data_read read = {};
+ int num_samples = ksft_session.num_des;
+ int i;
+
+ if (!ksft_session.de_list || !ksft_session.num_des)
+ SKIP(return, "no DE list available");
+
+ 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;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_BULK_READ, &read));
+ 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_batch batch = {};
+ struct scmi_tlm_de_sample *samples;
+
+ 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(*samples),
+ false));
+
+ samples = (struct scmi_tlm_de_sample *)batch.items;
+ samples[0].id = ksft_session.de_list[0].id;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch));
+
+ 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_batch batch = {};
+ struct scmi_tlm_de_sample *samples;
+
+ ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*samples), false));
+
+ samples = (struct scmi_tlm_de_sample *)batch.items;
+ samples[0].id = 0xffffffff;
+
+ ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch));
+ EXPECT_EQ(ENOENT, errno);
+
+ scmi_tlm_batch_free(&batch);
+}
+
+TEST_F(stlm, tlm_batch_read_bad_id_with_states)
+{
+ struct scmi_tlm_batch batch = {};
+ struct scmi_tlm_de_sample *samples;
+ int *states;
+
+ 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;
+
+ ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch));
+ 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 = {};
+
+ 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.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 = {};
+
+ 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.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 = {};
+
+ 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 = {};
+
+ 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.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.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;
+
+ 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.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.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 = !old_all.t_enable;
+
+ 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(!!new_all.t_enable, !!check_all.t_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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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) == 0)
+ 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
* Re: [PATCH v14 01/32] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions
2026-10-16 18:55 ` [PATCH v14 01/32] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
@ 2026-10-19 20:34 ` Jonathan Cameron
0 siblings, 0 replies; 38+ messages in thread
From: Jonathan Cameron @ 2026-10-19 20:34 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 Fri, 16 Oct 2026 19:55:22 +0100
Cristian Marussi <cristian.marussi@arm.com> wrote:
> 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, so it needs to be
> distinguishable.
>
> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> ---
> v12 --> v13
> - use EBADMSG to map SCMI_PARTIAL_ERROR
That's a stretch given definition but I looked back at:
https://lore.kernel.org/all/arK7kLlrg491EsMf@pluto/
and appreciate the problem of running out of error codes..
Guess it is the best we can do,
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
If you do a v15, maybe capture some reasoning in the change
log or the patch description itself.
Jonathan
^ permalink raw reply [flat|nested] 38+ messages in thread
* Re: [PATCH v14 03/32] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper
2026-10-16 18:55 ` [PATCH v14 03/32] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Cristian Marussi
@ 2026-10-19 20:46 ` Jonathan Cameron
0 siblings, 0 replies; 38+ messages in thread
From: Jonathan Cameron @ 2026-10-19 20:46 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 Fri, 16 Oct 2026 19:55:24 +0100
Cristian Marussi <cristian.marussi@arm.com> wrote:
> Refactor the __scmi_event_handler_get_ops notification helper to simplify
> its logic and make use of guards for locking.
>
> 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.
Maybe mention why scmi_allocate_event_handler() is now wrapped in
an scmi_allocate_pending_event_handler()
I like the change but beyond logical division of activities it
isn't immediately obvious why that split exists.
One really minor thing inline that I don't care that much about.
Anyhow, both trivial so
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> ---
> drivers/firmware/arm_scmi/notify.c | 73 +++++++++++++++++++-----------
> 1 file changed, 46 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c
> index 7d1bb424a723..2bc2e03df639 100644
> --- a/drivers/firmware/arm_scmi/notify.c
> +++ b/drivers/firmware/arm_scmi/notify.c
> @@ -890,7 +890,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)
> *
> @@ -903,8 +902,7 @@ void scmi_deregister_protocol_events(const struct scmi_handle *handle,
> * 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;
>
> @@ -914,8 +912,20 @@ scmi_allocate_event_handler(struct scmi_notify_instance *ni, u32 evt_key)
> hndl->key = evt_key;
> BLOCKING_INIT_NOTIFIER_HEAD(&hndl->chain);
> refcount_set(&hndl->users, 1);
> - /* New handlers are created pending */
> - hash_add(ni->pending_events_handlers, &hndl->hash, hndl->key);
> +
> + 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)
> + hash_add(ni->pending_events_handlers, &hndl->hash, hndl->key);
Trivial but I'd keep to a strict errors out of line
if (!hndl)
return NULL
hash_add(ni->...
return hndl;
It burns a few lines be saves a few brain cells.
>
> return hndl;
> }
^ permalink raw reply [flat|nested] 38+ messages in thread
* Re: [PATCH v14 04/32] firmware: arm_scmi: Introduce protocol instance notifiers
2026-10-16 18:55 ` [PATCH v14 04/32] firmware: arm_scmi: Introduce protocol instance notifiers Cristian Marussi
@ 2026-10-19 21:02 ` Jonathan Cameron
0 siblings, 0 replies; 38+ messages in thread
From: Jonathan Cameron @ 2026-10-19 21:02 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 Fri, 16 Oct 2026 19:55:25 +0100
Cristian Marussi <cristian.marussi@arm.com> wrote:
> SCMI Protocol notifications are typically used by SCMI drivers to detect
> and react to particular conditions: this was the assumption and the classic
> usage scenario upon which the SCMI notification framework was built.
>
> Some protocols like Telemetry, though, could be interested in enabling and
> registering for such notifications: in such a scenario each initialized
> protocol instance could want to register a common protocol notifier.
>
> In order to reuse the existing SCMI Notifications machinery, allow
> protocols themselves to register for their own notifications and provide
> their own notifier callbacks.
>
> Each protocol, at the end of its initialization phase, can now provide one
> unique per-protocol-instance notifier block, which will be registered, by
> the core SCMI stack, on the related notification chain: such notifier will
> be automatically removed during the protocol de-initialization phase.
>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
Whilst I'm not that familiar with this code, it seems fine to me
(i.e. needs more eyes despite the tag)
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 38+ messages in thread
* Re: [PATCH v14 06/32] include: trace: Add Telemetry trace events
2026-10-16 18:55 ` [PATCH v14 06/32] include: trace: Add Telemetry trace events Cristian Marussi
@ 2026-10-19 21:03 ` Jonathan Cameron
0 siblings, 0 replies; 38+ messages in thread
From: Jonathan Cameron @ 2026-10-19 21:03 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 Fri, 16 Oct 2026 19:55:27 +0100
Cristian Marussi <cristian.marussi@arm.com> wrote:
> Add custom traces to report Telemetry accesses and collection events.
>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
Fairly standard looking simple tracepoints so FWIW
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 38+ messages in thread
* Re: [PATCH v14 07/32] firmware: arm_scmi: Add Telemetry basic attributes enumeration
2026-10-16 18:55 ` [PATCH v14 07/32] firmware: arm_scmi: Add Telemetry basic attributes enumeration Cristian Marussi
@ 2026-10-19 21:27 ` Jonathan Cameron
0 siblings, 0 replies; 38+ messages in thread
From: Jonathan Cameron @ 2026-10-19 21:27 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 Fri, 16 Oct 2026 19:55:28 +0100
Cristian Marussi <cristian.marussi@arm.com> wrote:
> 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>
A few minor comments but nothing I care strongly about.
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
> #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..32f6f8d1106d
> --- /dev/null
> +++ b/drivers/firmware/arm_scmi/telemetry.c
> @@ -0,0 +1,148 @@
> +
> +enum scmi_telemetry_protocol_cmd {
Perhaps add a comment on enum scmi_common_cmd containing additional values
that apply to telemetry.
> + TELEMETRY_LIST_SHMTI = 0x3,
> +
> +struct telemetry_info {
> + bool streaming_mode;
I'd bring fields of internal tracking structures in only when
they are used (even if that is just setting them).
> + 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];
> +
> + 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]));
That dance is a swap as data is stored le but UUID fields are be.
Maybe going via cpu endian is fine as would be silly to have le32_to_be32()
so it perhaps has documentation value. Fair enough - I argued myself around
with agreeing with how you did it here, but left the discussion for anyone
else who has similar thoughts.
> + 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;
> +}
> diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
> index 5ab73b1ab9aa..121c83e1c6d8 100644
> --- a/include/linux/scmi_protocol.h
> +++ b/include/linux/scmi_protocol.h
...
> +/**
> + * 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 {
info> + const struct scmi_telemetry_info __must_check *(*info_get)
> + (const struct scmi_protocol_handle *ph);
Why __must_check? Be a bit crazy if no one did anything with the
info they just requested, so that seems overkill and doesn't help with
the long line.
> +};
> +
^ permalink raw reply [flat|nested] 38+ messages in thread
end of thread, other threads:[~2026-10-19 21:27 UTC | newest]
Thread overview: 38+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-16 18:55 [PATCH v14 00/32] Introduce SCMI Telemetry support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 01/32] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
2026-10-19 20:34 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 02/32] firmware: arm_scmi: Allow registration of unknown-size events/reports Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 03/32] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Cristian Marussi
2026-10-19 20:46 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 04/32] firmware: arm_scmi: Introduce protocol instance notifiers Cristian Marussi
2026-10-19 21:02 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 05/32] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 06/32] include: trace: Add Telemetry trace events Cristian Marussi
2026-10-19 21:03 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 07/32] firmware: arm_scmi: Add Telemetry basic attributes enumeration Cristian Marussi
2026-10-19 21:27 ` Jonathan Cameron
2026-10-16 18:55 ` [PATCH v14 08/32] firmware: arm_scmi: Add Telemetry resources enumeration Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 09/32] firmware: arm_scmi: Add Telemetry groups initialization Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 10/32] firmware: arm_scmi: Add Telemetry update intervals enumeration Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 11/32] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 12/32] firmware: arm_scmi: Add Telemetry TDCF areas parsing support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 13/32] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 14/32] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 15/32] firmware: arm_scmi: Add Telemetry configuration operations Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 16/32] firmware: arm_scmi: Add Telemetry DataEvent read capabilities Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 17/32] firmware: arm_scmi: Add Telemetry reset logic Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 18/32] firmware: arm_scmi: Add Telemetry notification support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 19/32] firmware: arm_scmi: Add Telemetry boot-on support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 20/32] firmware: arm-scmi: Add Telemetry generic event support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 21/32] firmware: arm_scmi: Add Telemetry generation counter event Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 22/32] firmware: arm_scmi: Add common per-protocol debugfs support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 23/32] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 24/32] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 25/32] firmware: arm_scmi: Expose per-instance identifier Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 26/32] firmware: arm_scmi: Add un-managed methods to get/put protocols operations Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 27/32] uapi: Add ARM SCMI Telemetry definitions Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 28/32] firmware: arm_scmi: Add System Telemetry driver Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 29/32] docs: ioctl-number: Add SCMI Ioctls Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 30/32] [RFC] Documentation: Add SCMI System Telemetry documentation Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 31/32] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool Cristian Marussi
2026-10-16 18:55 ` [PATCH v14 32/32] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases Cristian Marussi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox