From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AC68643E9FC; Fri, 24 Jul 2026 14:45:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904319; cv=none; b=mflbMCMlnf5SVhekoKqauq0Gd7EwMp3irCqY947qI+kZlXDSVgdij0G8bTzZvH2+ndhlB/hf11epJm6m3/wNnf6iUpzkAXFlEi0s0OjKwZ0jECK5p70pg0lfI8p5oI/2dSurT0A/82nocxfLOWnWjr/EZ2uvfdx9yM921jXMf5k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904319; c=relaxed/simple; bh=MvVBkOZSe0geStki8LTVG9QNqXX9D+MNvefmfwSOKOI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JETnbb1ACDwubaRF9J6ZwWoSn68CtE/sZSZTpOD6iDrhoSyBz3MLDFzQGJib7tb+2+51cwB6o+W1XCTEi6DFn151gh1Kuge12dzEVjw5kT3oP/2TO84gLs2YH36IvTiw+R3NHLj8vDAOU63ta1uZDPNqs9r7DbF5VjhhtMzF0pc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=DGk/fUbk; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="DGk/fUbk" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 16AD01684; Fri, 24 Jul 2026 07:45:06 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8A8783F66F; Fri, 24 Jul 2026 07:45:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904310; bh=MvVBkOZSe0geStki8LTVG9QNqXX9D+MNvefmfwSOKOI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DGk/fUbkoNy3Zbn1os093yK4E9SybCnhSUohJQx793c6fhYdqiYi3KcYnY2UomI0b ZOOqj76KZbbHRF8r0j1cVamSb82oDu3NYJBClzzTjIwOlMX3f/qccrWjhiYoYpBN18 kn7NYtGnbC8u7cO/mXepPBW+j97y9T3Dx8KAzCGg= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 01/22] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Date: Fri, 24 Jul 2026 15:44:09 +0100 Message-ID: <20260724144430.1440170-2-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" SCMIv4.0 introduces a couple of new possible protocol error codes: add the needed definitions and mappings to Linux error values. Signed-off-by: Cristian Marussi --- 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 be0ed89b3524..767f08be416c 100644 --- a/drivers/firmware/arm_scmi/common.h +++ b/drivers/firmware/arm_scmi/common.h @@ -46,6 +46,8 @@ enum scmi_error_codes { SCMI_ERR_GENERIC =3D -8, /* Generic Error */ SCMI_ERR_HARDWARE =3D -9, /* Hardware Error */ SCMI_ERR_PROTOCOL =3D -10,/* Protocol Error */ + SCMI_ERR_IN_USE =3D -11, /* In Use Error */ + SCMI_ERR_PARTIAL =3D -12, /* Partial Error */ }; =20 static const int scmi_linux_errmap[] =3D { @@ -61,6 +63,8 @@ static const int scmi_linux_errmap[] =3D { -EIO, /* SCMI_ERR_GENERIC */ -EREMOTEIO, /* SCMI_ERR_HARDWARE */ -EPROTO, /* SCMI_ERR_PROTOCOL */ + -EBUSY, /* SCMI_ERR_IN_USE */ + -EINVAL, /* SCMI_ERR_PARTIAL */ }; =20 static inline int scmi_to_linux_errno(int errno) --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 33E99441600; Fri, 24 Jul 2026 14:45:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904327; cv=none; b=cpSXpy732rU1iQCig9bPXtkoDVarw9+bmu3wyI4qFMfnA5KbSxUMq8kQDM87IHUao0pLaSvh3SkvG53xpyzxjJ5Tjen60OF1EO+b+W44/5eVV5ru2E2b63toPRV1XYqtZoxZ3kNZ1+ij0qsA9I9jibRerXMamJDDzZGI+2+mDeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904327; c=relaxed/simple; bh=GxkdI0cQnjsDY8mKb+SV5LhLZ2F3qgscDqUrJETK/PQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HEVQ5WBUbs0KWTz1JC1Zg1PSIdlz6RYqwZQrzm0j2dEu+YJG2eVabsSaOMUrMn9cTKWutc9YIobCfSAGwzsu2nK7vyGnt0F2AnZ1g/KAMf/C6SAzre1KZmmIbzO5dH9kZPYO3wRToD4xPjfRXZI9+U0cyMzhAj8jD5bw7QVENHU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=Cc6KPieR; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="Cc6KPieR" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 66DE21477; Fri, 24 Jul 2026 07:45:14 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BD2233F66F; Fri, 24 Jul 2026 07:45:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904318; bh=GxkdI0cQnjsDY8mKb+SV5LhLZ2F3qgscDqUrJETK/PQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cc6KPieRCRXSG+RJ2670+eq5Gt2kmOH5hMGyA4l9eAqvkE4Cfy5AJEFa/eknx0TCz gydmvGzzZgblbCvvL3rqrC59F4mBXII7iCeFa+Wdi5KgfQjfxrHEnC49GSQ8c6EwwF id1VoHxPtJUQINqPm41r9En6HeyPdxo7i5XtWEMM= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 02/22] firmware: arm_scmi: Allow registration of unknown-size events/reports Date: Fri, 24 Jul 2026 15:44:10 +0100 Message-ID: <20260724144430.1440170-3-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Cristian Marussi --- v2 --> v3 - split out of previous patch on protocol notifier - use max() instead of max_t() --- drivers/firmware/arm_scmi/notify.c | 24 +++++++++++++++++++----- drivers/firmware/arm_scmi/notify.h | 8 ++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi= /notify.c index 0a192cf2deab..8995dd1b12aa 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 p= roto_id, u8 evt_id, if (!r_evt) return -EINVAL; =20 - 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; } @@ -754,7 +760,7 @@ int scmi_register_protocol_events(const struct scmi_han= dle *handle, u8 proto_id, const struct scmi_protocol_handle *ph, const struct scmi_protocol_events *ee) { - int i; + int i, max_msg_sz; unsigned int num_sources; size_t payld_sz =3D 0; struct scmi_registered_events_desc *pd; @@ -769,6 +775,8 @@ int scmi_register_protocol_events(const struct scmi_han= dle *handle, u8 proto_id, if (!ni) return -ENOMEM; =20 + max_msg_sz =3D ph->hops->get_max_msg_size(ph); + /* num_sources cannot be <=3D 0 */ if (ee->num_sources) { num_sources =3D ee->num_sources; @@ -781,8 +789,13 @@ int scmi_register_protocol_events(const struct scmi_ha= ndle *handle, u8 proto_id, } =20 evt =3D ee->evts; - for (i =3D 0; i < ee->num_events; i++) - payld_sz =3D max_t(size_t, payld_sz, evt[i].max_payld_sz); + for (i =3D 0; i < ee->num_events; i++) { + if (evt[i].max_payld_sz =3D=3D 0) { + payld_sz =3D max_msg_sz; + break; + } + payld_sz =3D max(payld_sz, evt[i].max_payld_sz); + } payld_sz +=3D sizeof(struct scmi_event_header); =20 pd =3D scmi_allocate_registered_events_desc(ni, proto_id, ee->queue_sz, @@ -811,7 +824,8 @@ int scmi_register_protocol_events(const struct scmi_han= dle *handle, u8 proto_id, mutex_init(&r_evt->sources_mtx); =20 r_evt->report =3D devm_kzalloc(ni->handle->dev, - evt->max_report_sz, GFP_KERNEL); + evt->max_report_sz ?: max_msg_sz, + GFP_KERNEL); if (!r_evt->report) return -ENOMEM; =20 diff --git a/drivers/firmware/arm_scmi/notify.h b/drivers/firmware/arm_scmi= /notify.h index 76758a736cf4..ecfa4b746487 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 mess= age - * @max_report_sz: Max possible size for the report of a notification mess= age + * @max_payld_sz: Max possible size for the payload of a notification mess= age. + * Set to zero to use the maximum payload size allowed by the + * transport. + * @max_report_sz: Max possible size for the report of a notification mess= age. + * Set to zero to use the maximum payload size allowed by the + * transport. * * Each SCMI protocol, during its initialization phase, can describe the e= vents * it wishes to support in a few struct scmi_event and pass them to the co= re --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 229D844210B; Fri, 24 Jul 2026 14:45:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904338; cv=none; b=a9/r9WiA4OYoTkxe5GrHXke1PwaXv6TK9YTN9cVz1ayHHrXH2UjslWQG4AsHDOcueIK7qotoCAJUQ6NTzo0OTSsIPo8r23z/+raBdsAqZT+jj517MreabpnOK8ji0meq+6/wqn6UkgvHH08/ykapVrhWc3YJaAT4dEQsbsUKIW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904338; c=relaxed/simple; bh=30J+uupH/OH53k25JuCADenLnhKSLAsXqdTxMiJdNEc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pRG81iZ2Bvyv7sqkwBhIP1q+udf+Ji+N4e+kD4B+WVu++XknWK4WE/PWwmESMMl4g+mMU7GQRbUgcU3Pfgyl0EGLgfOBB8UH74E8va85ePjjPDDuVlai9rpo/GbsAfzb2BeZiDyE8+EP5vPNCTOPlgMabXm7Hv95vfopwWwAzp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=IjXfvXfN; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="IjXfvXfN" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 34B141684; Fri, 24 Jul 2026 07:45:23 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F06F63F66F; Fri, 24 Jul 2026 07:45:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904327; bh=30J+uupH/OH53k25JuCADenLnhKSLAsXqdTxMiJdNEc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IjXfvXfNchRZBS8VPB0wVxVKO9nHK5m8ZD22rTYl3bo8yi0Aqb2iLJWIZHddNLO14 lGOrIm9M3/+N9QlVS1Q7EyXIIzFPczpoWzcAmLSWXO7StNAt6t+nXXHZTZRyVi1Op1 o0TcffRuWGY/937o0Pf0Am5OoWKkbkhpbmEIc4fQ= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 03/22] firmware: arm_scmi: Introduce protocol instance notifiers Date: Fri, 24 Jul 2026 15:44:11 +0100 Message-ID: <20260724144430.1440170-4-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow protocols themselves to register for their own notifications and provide their own notifier callbacks. Each protocol can now register one unique per-protocol instance notifier block whose callback will be registered on the proper notification chain as usual: such notifier will be automatically removed during the protocol de-initialiazation phase. Signed-off-by: Cristian Marussi --- 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 | 12 ++++ drivers/firmware/arm_scmi/driver.c | 86 +++++++++++++++++++++------ drivers/firmware/arm_scmi/notify.c | 14 +++++ drivers/firmware/arm_scmi/protocols.h | 10 ++++ 4 files changed, 103 insertions(+), 19 deletions(-) diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi= /common.h index 767f08be416c..f8fbb1a46482 100644 --- a/drivers/firmware/arm_scmi/common.h +++ b/drivers/firmware/arm_scmi/common.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -708,8 +709,19 @@ static struct platform_driver __drv =3D { \ .probe =3D __tag##_probe, \ } =20 +struct scmi_protocol_notifier { + u8 proto_id; + u8 evt_id; + const u32 *src_id; + struct notifier_block *nb; +}; + 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); 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 8e06e40d1a11..0f9e8dfc6137 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -90,6 +90,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 run= time. * @negotiated_version: When the platform supports a newer protocol versio= n, * the agent will try to negotiate with the platform the @@ -109,6 +111,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; @@ -1657,6 +1660,24 @@ static void *scmi_get_protocol_priv(const struct scm= i_protocol_handle *ph) return pi->priv; } =20 +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 =3D ph_to_pi(ph); + + pi->pno.proto_id =3D pi->proto->id; + pi->pno.evt_id =3D evt_id; + pi->pno.src_id =3D src_id; + /* + * Ensure the descriptor fields are visibile when the notifier block is + * made available + */ + smp_store_mb(pi->pno.nb, nb); + + return 0; +} + static const struct scmi_xfer_ops xfer_ops =3D { .xfer_get_init =3D xfer_get_init, .reset_rx_to_maxsz =3D reset_rx_to_maxsz, @@ -2257,6 +2278,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *i= nfo, pi->ph.hops =3D &helpers_ops; pi->ph.set_priv =3D scmi_set_protocol_priv; pi->ph.get_priv =3D scmi_get_protocol_priv; + pi->ph.instance_notifier_register =3D scmi_register_instance_notifier; refcount_set(&pi->users, 1); =20 /* @@ -2319,24 +2341,37 @@ static struct scmi_protocol_instance * __must_check scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_i= d) { struct scmi_protocol_instance *pi; + struct notifier_block *proto_notifier_nb =3D NULL; struct scmi_info *info =3D handle_to_scmi_info(handle); =20 - mutex_lock(&info->protocols_mtx); - pi =3D idr_find(&info->protocols, protocol_id); + scoped_guard(mutex, &info->protocols_mtx) { + pi =3D idr_find(&info->protocols, protocol_id); + if (pi) { + refcount_inc(&pi->users); + } else { + const struct scmi_protocol *proto; =20 - if (pi) { - refcount_inc(&pi->users); - } else { - const struct scmi_protocol *proto; + /* Fails if protocol not registered on bus */ + proto =3D scmi_protocol_get(protocol_id, &info->version); + if (!proto) + return ERR_PTR(-EPROBE_DEFER); =20 - /* Fails if protocol not registered on bus */ - proto =3D scmi_protocol_get(protocol_id, &info->version); - if (proto) pi =3D scmi_alloc_init_protocol_instance(info, proto); - else - pi =3D ERR_PTR(-EPROBE_DEFER); + if (IS_ERR(pi)) + return pi; + + proto_notifier_nb =3D READ_ONCE(pi->pno.nb); + } + } + + if (proto_notifier_nb) { + int ret; + + ret =3D scmi_protocol_notifier_register(pi->handle, &pi->pno); + if (ret) + dev_warn(handle->dev, + "Failed to register protocol notifier\n"); } - mutex_unlock(&info->protocols_mtx); =20 return pi; } @@ -2367,13 +2402,29 @@ int scmi_protocol_acquire(const struct scmi_handle = *handle, u8 protocol_id) void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_i= d) { struct scmi_info *info =3D handle_to_scmi_info(handle); + struct notifier_block *proto_notifier_nb =3D NULL; struct scmi_protocol_instance *pi; =20 - mutex_lock(&info->protocols_mtx); - pi =3D idr_find(&info->protocols, protocol_id); - if (WARN_ON(!pi)) - goto out; + scoped_guard(mutex, &info->protocols_mtx) { + pi =3D idr_find(&info->protocols, protocol_id); + if (WARN_ON(!pi)) + return; + + proto_notifier_nb =3D pi->pno.nb; + /* Ensure NULL is visible */ + smp_store_mb(pi->pno.nb, NULL); + } =20 + if (proto_notifier_nb) { + int ret; + + ret =3D scmi_protocol_notifier_unregister(pi->handle, &pi->pno); + if (ret) + dev_err(handle->dev, + "Failed to release protocol notifier\n"); + } + + guard(mutex)(&info->protocols_mtx); if (refcount_dec_and_test(&pi->users)) { void *gid =3D pi->gid; =20 @@ -2391,9 +2442,6 @@ 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); } =20 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 8995dd1b12aa..938d575e22fe 100644 --- a/drivers/firmware/arm_scmi/notify.c +++ b/drivers/firmware/arm_scmi/notify.c @@ -1420,6 +1420,13 @@ static int scmi_notifier_register(const struct scmi_= handle *handle, return ret; } =20 +int scmi_protocol_notifier_register(const struct scmi_handle *handle, + struct scmi_protocol_notifier *pno) +{ + return scmi_notifier_register(handle, pno->proto_id, pno->evt_id, + pno->src_id, pno->nb); +} + /** * scmi_notifier_unregister() - Unregister a notifier_block for an event * @handle: The handle identifying the platform instance against which the @@ -1477,6 +1484,13 @@ static int scmi_notifier_unregister(const struct scm= i_handle *handle, return 0; } =20 +int scmi_protocol_notifier_unregister(const struct scmi_handle *handle, + struct scmi_protocol_notifier *pno) +{ + return scmi_notifier_unregister(handle, pno->proto_id, pno->evt_id, + pno->src_id, pno->nb); +} + 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_s= cmi/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); }; =20 /** --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C132844161F; Fri, 24 Jul 2026 14:45:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904345; cv=none; b=Y8FlgrMM0xWPwXU1BfvHawmOaALGOwA0e60PMXyRObwryVM6vM/uOimPvrP8fs+q27Kit3EMBH3+t48YR384960FUo2myBmR4w3EGsHfn4V+5FGAXUwJsfq+lmP/ju/HVuJ5Q193o5u5iGMl/gSZ27P2vw3YdDPXkxnMPG8T7XQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904345; c=relaxed/simple; bh=9vZoBgzFE2THNFE8s+a0rg1bPJg4MV54NAwQFcIi1xY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XN1G5Bd8lAazzYnoPMFZ30kj6YMjVbyFxmE5oovTdw6z2DLa/fNJPBvRJQS/jXlDguPZjJi4cooM6A4iSZAUfLwdBQopQ/sz7S6M+uXGO0zsNWSSl+geYBKr4EHJKPeIz1Jfg2KkIxjw611Y4Z4NeVLxyNgGTer8XluY+GP1eWM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=pmCPx95V; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="pmCPx95V" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2DCD91477; Fri, 24 Jul 2026 07:45:32 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C39423F66F; Fri, 24 Jul 2026 07:45:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904336; bh=9vZoBgzFE2THNFE8s+a0rg1bPJg4MV54NAwQFcIi1xY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pmCPx95VuU37XI7tKKQBWdyUAT6Dg1RqHhLCLE9ZNgjl+bMv4g5Gz0O3F2EWAmIn1 P4czgW8hwEWxRwKFKwQ+7jLqEkJw2mjBjO/3TQk8Vfbs0xeC5lQk9STpM1GJGSvFx0 GO5FtEa8wXTEnH0E3bpt8bFZUJy1Zz7s9A0aPYHU= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi , "Rob Herring (Arm)" , Krzysztof Kozlowski , Conor Dooley , devicetree@vger.kernel.org Subject: [PATCH v6 04/22] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Date: Fri, 24 Jul 2026 15:44:12 +0100 Message-ID: <20260724144430.1440170-5-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add new SCMI v4.0 Telemetry protocol bindings definitions. Acked-by: Rob Herring (Arm) Signed-off-by: Cristian Marussi --- v3 --> v4 - changed protocol number to lowercase 1b - fixed misplaced block for protocol 0x1b Cc: Rob Herring Cc: Krzysztof Kozlowski Cc: Conor Dooley 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/Doc= umentation/devicetree/bindings/firmware/arm,scmi.yaml index d06cca9273c4..be733f8d60a0 100644 --- a/Documentation/devicetree/bindings/firmware/arm,scmi.yaml +++ b/Documentation/devicetree/bindings/firmware/arm,scmi.yaml @@ -324,6 +324,14 @@ properties: required: - reg =20 + protocol@1b: + $ref: '#/$defs/protocol-node' + unevaluatedProperties: false + + properties: + reg: + const: 0x1b + unevaluatedProperties: false =20 $defs: --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2E959441618; Fri, 24 Jul 2026 14:45:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904353; cv=none; b=F4wJSpa+rIZKLTTb5KjSWtXCs0rp8kYeWliM1Gb6r5Zwb5hU7SRIVKbtkoCPul0H08IK9Ws+bVJfCMGvFsWAHy+EOgjR9+OPIDbXuLNaiAhJM3WAg/nGV4HKgfqEIr40OOpjCYPYvVYrbmkmYCjBwqsnKwOXMlGhjJoDL5X32es= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904353; c=relaxed/simple; bh=CwJGWGgRXKnupukDLWD6QwCAt4HaM/Pbt3pAL9OqFfU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HCHb4hnrhQy8ziAXuXfDMSZlCPPSSEVNBFoY0r5w1XIIYaBqUFR7D0ptawqJv99w4eq2YqTFNM0dsECoABld9WEiT1rBGh7K+KPzJCPg2FVBu6z9eUoDFZKNH9XFpYlSyg0853RZpA4nMQ3TKlRoqXvx+Tsd6p8BnJ0YGEP/eL0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=fAnkGis3; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="fAnkGis3" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 37C4B1684; Fri, 24 Jul 2026 07:45:40 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C1F983F66F; Fri, 24 Jul 2026 07:45:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904344; bh=CwJGWGgRXKnupukDLWD6QwCAt4HaM/Pbt3pAL9OqFfU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fAnkGis3b4S98AV4Im+nANcd31jfdc2BA+tSj40mq1ZQhv8xEOrb+cGSubZiC32Lb P2K4g7niZEjpHm1EVqq/kHzGbL04HZ7UIeaZPYpxuQsuQWU38GTODN13cvaXowJzJL zQt5S0C2HsBUYZ5YZGUB7lMmEnKsDADH6AGSn57w= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 05/22] include: trace: Add Telemetry trace events Date: Fri, 24 Jul 2026 15:44:13 +0100 Message-ID: <20260724144430.1440170-6-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add custom traces to report Telemetry failed accesses and to report when DE values are updated internally after a notification is processed. Signed-off-by: Cristian Marussi --- include/trace/events/scmi.h | 48 ++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/include/trace/events/scmi.h b/include/trace/events/scmi.h index 703b7bb68e44..b70b26e467b8 100644 --- a/include/trace/events/scmi.h +++ b/include/trace/events/scmi.h @@ -7,7 +7,8 @@ =20 #include =20 -#define TRACE_SCMI_MAX_TAG_LEN 6 +#define TRACE_SCMI_MAX_TAG_LEN 6 +#define TRACE_SCMI_TLM_MAX_TAG_LEN 16 =20 TRACE_EVENT(scmi_fc_call, TP_PROTO(u8 protocol_id, u8 msg_id, u32 res_id, u32 val1, u32 val2), @@ -180,6 +181,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, unsigned char *tag, u64 startm, u64 endm), + TP_ARGS(de_id, tag, startm, endm), + + TP_STRUCT__entry( + __field(u64, de_id) + __array(char, tag, TRACE_SCMI_TLM_MAX_TAG_LEN) + __field(u64, startm) + __field(u64, endm) + ), + + TP_fast_assign( + __entry->de_id =3D de_id; + strscpy(__entry->tag, tag, TRACE_SCMI_TLM_MAX_TAG_LEN); + __entry->startm =3D startm; + __entry->endm =3D endm; + ), + + TP_printk("de_id=3D0x%llX [%s] - startm=3D%016llX endm=3D%016llX", + __entry->de_id, __entry->tag, __entry->startm, __entry->endm) +); + +TRACE_EVENT(scmi_tlm_collect, + TP_PROTO(u64 ts, u64 de_id, u64 value, unsigned char *tag), + TP_ARGS(ts, de_id, value, tag), + + TP_STRUCT__entry( + __field(u64, ts) + __field(u64, de_id) + __field(u64, value) + __array(char, tag, TRACE_SCMI_TLM_MAX_TAG_LEN) + ), + + TP_fast_assign( + __entry->ts =3D ts; + __entry->de_id =3D de_id; + __entry->value =3D value; + strscpy(__entry->tag, tag, TRACE_SCMI_TLM_MAX_TAG_LEN); + ), + + TP_printk("ts=3D%llu de_id=3D0x%04llX value=3D%016llu [%s]", + __entry->ts, __entry->de_id, __entry->value, __entry->tag) +); + #endif /* _TRACE_SCMI_H */ =20 /* This part must be outside protection */ --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7BD4043E9FB; Fri, 24 Jul 2026 14:45:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904365; cv=none; b=tQy45CcdWp6z4H3JLrIJrY4GjBAQbjNOa4eVJNhcpUvpmFr9nv3YRR3qEFZZzoFZan94sXxdq+rbTpGL61M4SVoyAR/YSCQwhMjP7AH4Fd4oVNUONdpx0hahJ5l1taR4c6dztWwpJkC8Ov3yuWnN3gJa2pfPe+FXPZG+utxcEMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904365; c=relaxed/simple; bh=Zoe+YeZt0ibudELJF1MZbvuDgwJ3+U1RR2DQJIWING0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=krNWlrRlWxc/NFt5/NEDNn8X7kmVNrAP+1PzraXns2msAy+ggs7SoQ6IiQZYWL87yMer576RCkK4iaOO58NTlaQnI4LeNypev2JrCNo842ZiUqB1r4h/FH5D2hLUdH6Vp9Sar8rOoiu8ZllJ49OkvGFrE8yVIl7/1bUKIoWyH0I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=Xs4Xfbst; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="Xs4Xfbst" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 358D61BB0; Fri, 24 Jul 2026 07:45:49 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E81E53F66F; Fri, 24 Jul 2026 07:45:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904353; bh=Zoe+YeZt0ibudELJF1MZbvuDgwJ3+U1RR2DQJIWING0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xs4XfbstBizQStLC25a8NdsHQcJcNDCmMGVBwhtRWSXI5wAfQwWFRt+A+5ozcQXW/ fukGko5JiPDaluucoymTs46Cx0E/VACGS2ckljozZpu0CJ+wOJvbe9YyS+aobt+054 jmFz8KXBXij0ZXZ46wl89qIfM9WJBsT73PtywYiI= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 06/22] firmware: arm_scmi: Add basic Telemetry support Date: Fri, 24 Jul 2026 15:44:14 +0100 Message-ID: <20260724144430.1440170-7-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add SCMIv4.0 Telemetry basic support to enable initialization and resources enumeration: add all the telemetry messages definitions and parsing logic but only a few simple state gathering protocol operations. Signed-off-by: Cristian Marussi --- v5 --> v6 - define uuid_line dwords as BE - fixed Sashiko reviews where applicable: https://sashiko.dev/#/message/20260703123601.381275-8-cristian.marussi%4= 0arm.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 | 1442 +++++++++++++++++++++++++ include/linux/scmi_protocol.h | 181 +++- 5 files changed, 1626 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) +=3D raw_mo= de.o scmi-transport-$(CONFIG_ARM_SCMI_HAVE_SHMEM) =3D shmem.o scmi-transport-$(CONFIG_ARM_SCMI_HAVE_MSG) +=3D msg.o scmi-protocols-y :=3D base.o clock.o perf.o power.o reset.o sensors.o syst= em.o voltage.o powercap.o -scmi-protocols-y +=3D pinctrl.o +scmi-protocols-y +=3D pinctrl.o telemetry.o scmi-module-objs :=3D $(scmi-driver-y) $(scmi-protocols-y) $(scmi-transpor= t-y) =20 obj-$(CONFIG_ARM_SCMI_PROTOCOL) +=3D transports/ diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi= /driver.c index 0f9e8dfc6137..4c200b6168a9 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -3587,6 +3587,7 @@ static int __init scmi_driver_init(void) scmi_system_register(); scmi_powercap_register(); scmi_pinctrl_register(); + scmi_telemetry_register(); =20 return platform_driver_register(&scmi_driver); } @@ -3605,6 +3606,7 @@ static void __exit scmi_driver_exit(void) scmi_system_unregister(); scmi_powercap_unregister(); scmi_pinctrl_unregister(); + scmi_telemetry_unregister(); =20 platform_driver_unregister(&scmi_driver); =20 diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_s= cmi/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); =20 #endif /* _SCMI_PROTOCOLS_H */ diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c new file mode 100644 index 000000000000..372051ad1e62 --- /dev/null +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -0,0 +1,1442 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * System Control and Management Interface (SCMI) Telemetry Protocol + * + * Copyright (C) 2026 ARM Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "protocols.h" +#include "notify.h" + +#include + +/* Updated only after ALL the mandatory features for that version are merg= ed */ +#define SCMI_PROTOCOL_SUPPORTED_VERSION 0x10000 + +#define SCMI_TLM_TDCF_MAX_RETRIES 5 + +enum scmi_telemetry_protocol_cmd { + TELEMETRY_LIST_SHMTI =3D 0x3, + TELEMETRY_DE_DESCRIPTION =3D 0x4, + TELEMETRY_LIST_UPDATE_INTERVALS =3D 0x5, + TELEMETRY_DE_CONFIGURE =3D 0x6, + TELEMETRY_DE_ENABLED_LIST =3D 0x7, + TELEMETRY_CONFIG_SET =3D 0x8, + TELEMETRY_READING_COMPLETE =3D TELEMETRY_CONFIG_SET, + TELEMETRY_CONFIG_GET =3D 0x9, + TELEMETRY_RESET =3D 0xA, +}; + +struct scmi_msg_resp_telemetry_protocol_attributes { + __le32 de_num; + __le32 groups_num; + __le32 de_implementation_rev_dword[SCMI_TLM_DE_IMPL_MAX_DWORDS]; + __le32 attributes; +#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 scmi_telemetry_update_notify_payld { + __le32 agent_id; + __le32 status; + __le32 num_dwords; + __le32 array[] __counted_by(num_dwords); +}; + +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[] __counted_by(num_shmti); +}; + +struct de_desc_fc { + __le32 addr_low; + __le32 addr_high; + __le32 size; +}; + +struct scmi_de_desc { + __le32 id; + __le32 grp_id; + __le32 data_sz; + __le32 attr_1; +#define IS_NAME_SUPPORTED(d) (le32_get_bits((d)->attr_1, BIT(31))) +#define IS_FC_SUPPORTED(d) (le32_get_bits((d)->attr_1, BIT(30))) +#define GET_DE_TYPE(d) (le32_get_bits((d)->attr_1, GENMASK(29, 22))) +#define IS_PERSISTENT(d) (le32_get_bits((d)->attr_1, BIT(21))) +#define GET_DE_UNIT_EXP(d) \ + ({ \ + __u32 __signed_exp =3D \ + le32_get_bits((d)->attr_1, GENMASK(20, 13)); \ + \ + sign_extend32(__signed_exp, 7); \ + }) +#define GET_DE_UNIT(d) (le32_get_bits((d)->attr_1, GENMASK(12, 5))) +#define TSTAMP_SUPPORT(d) (le32_get_bits((d)->attr_1, GENMASK(1, 0))) + __le32 attr_2; +#define GET_DE_INSTA_ID(d) (le32_get_bits((d)->attr_2, GENMASK(31, 24))) +#define GET_COMPO_INSTA_ID(d) (le32_get_bits((d)->attr_2, GENMASK(23, 8))) +#define GET_COMPO_TYPE(d) (le32_get_bits((d)->attr_2, GENMASK(7, 0))) + __le32 reserved; +}; + +#define SCMI_TDE_VAR_SZ(_t) \ + ({ \ + struct telemetry_de *__t =3D _t; \ + size_t _sz =3D 0; \ + \ + _sz +=3D __t->ts_type =3D=3D TSTAMP_LINE ? sizeof(__le32) : 0; \ + _sz +=3D __t->de.fc_support ? sizeof(struct de_desc_fc) : 0; \ + _sz +=3D __t->de.name_support ? SCMI_SHORT_NAME_MAX_SIZE : 0; \ + \ + _sz; \ + }) + +struct scmi_msg_resp_telemetry_de_description { + __le32 num_desc; + struct scmi_de_desc desc[] __counted_by(num_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)) =3D=3D 0) + __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 entry[]; +}; + +struct scmi_msg_telemetry_de_configure { + __le32 id; + __le32 flags; +#define DE_ENABLE_NO_TSTAMP BIT(0) +#define DE_ENABLE_WTH_TSTAMP BIT(1) +#define DE_DISABLE_ALL BIT(2) +#define GROUP_SELECTOR BIT(3) +#define EVENT_DE 0 +#define EVENT_GROUP 1 +#define DE_DISABLE_ONE 0x0 +}; + +struct scmi_msg_resp_telemetry_de_configure { + __le32 shmti_id; +#define IS_SHMTI_ID_VALID(x) ((x) !=3D 0xFFFFFFFF) + __le32 shmti_de_offset; + __le32 blk_ts_offset; +}; + +struct scmi_msg_telemetry_config_set { + __le32 grp_id; + __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; +}; + +struct scmi_msg_resp_telemetry_reading_complete { + __le32 num_dwords; + __le32 dwords[] __counted_by(num_dwords); +}; + +struct scmi_msg_telemetry_config_get { + __le32 grp_id; + __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))) + +#define TO_CPU_64(h, l) ((((u64)(h)) << 32) | (l)) + +/* + * 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 blk_tsline { + __le32 ts_low; + __le32 ts_high; +}; + +struct tsline { + __le32 data_low; + __le32 data_high; + __le32 ts_low; + __le32 ts_high; +}; + +struct uuid_line { + __be32 dwords[SCMI_TLM_DE_IMPL_MAX_DWORDS]; +}; + +enum tdcf_line_types { + TDCF_DATA_LINE, + TDCF_BLK_TS_LINE, + TDCF_UUID_LINE, +}; + +struct payload { + __le32 meta; +#define LINE_TYPE(x) (FIELD_GET(GENMASK(7, 4), _I(&((x)->meta)))) +#define IS_DATA_LINE(x) (LINE_TYPE(x) =3D=3D TDCF_DATA_LINE) +#define IS_BLK_TS_LINE(x) (LINE_TYPE(x) =3D=3D TDCF_BLK_TS_LINE) +#define IS_UUID_LINE(x) (LINE_TYPE(x) =3D=3D TDCF_UUID_LINE) +#define USE_BLK_TS(x) (_I(&((x)->meta)) & BIT(3)) +#define HAS_LINE_EXT(x) (_I(&((x)->meta)) & BIT(2)) +#define LINE_TS_VALID(x) (_I(&((x)->meta)) & BIT(1)) +#define DATA_INVALID(x) (_I(&((x)->meta)) & BIT(0)) +#define BLK_TS_INVALID(p) \ +({ \ + typeof(p) _p =3D (p); \ + bool invalid; \ + \ + invalid =3D LINE_TS_VALID(_p) || HAS_LINE_EXT(_p) || \ + USE_BLK_TS(_p) || DATA_INVALID(_p); \ + invalid; \ +}) + +#define UUID_INVALID(p) \ +({ \ + typeof(p) _p =3D (p); \ + bool invalid; \ + \ + invalid =3D LINE_TS_VALID(_p) || USE_BLK_TS(_p) || \ + DATA_INVALID(_p) || !HAS_LINE_EXT(_p); \ + invalid; \ +}) + __le32 id; + union { + struct line l; + struct tsline tsl; + struct blk_tsline blk_tsl; + struct uuid_line uuid_l; + }; +}; + +#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(x) \ + (HAS_LINE_EXT((x)) ? EXT_LINE_DATA_PAYLD_WORDS : LINE_DATA_PAYLD_WORDS) + +#define LINE_LENGTH_QWORDS(x) ((LINE_LENGTH_WORDS(x)) / 2) + +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 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))) +#define IS_BAD_START_SEQ(s) ((s) & 0x1) + +#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; + void __iomem *base; + u32 last_magic; + struct scmi_telemetry_shmti_info info; +}; + +#define SHMTI_EPLG(s) \ + ({ \ + struct telemetry_shmti *_s =3D (s); \ + struct eplg __iomem *_eplg; \ + \ + _eplg =3D _s->base + _s->info.len - TDCF_EPLG_SZ; \ + (_eplg); \ + }) + +struct telemetry_line { + refcount_t users; + u32 last_magic; + struct payload __iomem *payld; + /* Protect line accesses */ + struct mutex mtx; +}; + +struct telemetry_block_ts { + u64 last_ts; + u32 last_rate; + struct telemetry_line line; +}; + +#define to_blkts(l) container_of(l, struct telemetry_block_ts, line) + +struct telemetry_uuid { + u32 de_impl_version[SCMI_TLM_DE_IMPL_MAX_DWORDS]; + struct telemetry_line line; +}; + +#define to_uuid(l) container_of(l, struct telemetry_uuid, line) + +enum timestamps { + TSTAMP_NONE, + TSTAMP_LINE, + TSTAMP_BLK +}; + +struct telemetry_de { + enum timestamps ts_type; + u32 ts_rate; + bool enumerated; + bool cached_msg; + void __iomem *base; + struct eplg __iomem *eplg; + u32 offset; + /* NOTE THAT DE data_sz is registered in scmi_telemetry_de */ + u32 fc_size; + /* Protect last_val/ts/magic accesses */ + struct mutex mtx; + u64 last_val; + u64 last_ts; + u32 last_magic; + struct list_head item; + struct telemetry_block_ts *bts; + struct telemetry_uuid *uuid; + struct scmi_telemetry_de de; +}; + +#define to_tde(d) container_of(d, struct telemetry_de, de) + +#define DE_ENABLED_WITH_TSTAMP 2 + +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_shmti *shmti; + struct telemetry_de *tdes; + struct scmi_telemetry_group *grps; + 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 =3D 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) +{ + guard(mutex)(&ti->free_mtx); + + list_add_tail(&tde->item, &ti->free_des); +} + +static struct telemetry_de *scmi_telemetry_tde_lookup(struct telemetry_inf= o *ti, + unsigned int de_id) +{ + struct scmi_telemetry_de *de; + + de =3D 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 =3D scmi_telemetry_free_tde_get(ti); + if (!tde) { + dev_err(ti->ph->dev, "Cannot allocate DE for ID:0x%08X\n", de_id); + return ERR_PTR(-ENOSPC); + } + + return tde; +} + +static int scmi_telemetry_tde_register(struct telemetry_info *ti, + struct telemetry_de *tde) +{ + struct scmi_telemetry_res_info *rinfo =3D ti->rinfo; + int ret; + + if (rinfo->num_des >=3D ti->info.base.num_des) { + ret =3D -ENOSPC; + goto err; + } + + /* Store DE pointer by de_id ... */ + ret =3D 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++] =3D &tde->de; + + return 0; + +err: + dev_err(ti->ph->dev, "Cannot register DE for ID:0x%08X\n", + tde->de.info->id); + + return ret; +} + +struct scmi_tlm_de_priv { + struct telemetry_info *ti; + void *next; +}; + +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 =3D ti->ph; + struct scmi_xfer *t; + int ret; + + ret =3D ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0, + sizeof(*resp), &t); + if (ret) + return ret; + + resp =3D t->rx.buf; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + __le32 attr =3D resp->attributes; + + ti->info.base.num_des =3D le32_to_cpu(resp->de_num); + ti->info.base.num_groups =3D le32_to_cpu(resp->groups_num); + for (int i =3D 0; i < SCMI_TLM_DE_IMPL_MAX_DWORDS; i++) + ti->info.base.de_impl_version[i] =3D + le32_to_cpu(resp->de_implementation_rev_dword[i]); + ti->info.single_read_support =3D SUPPORTS_SINGLE_READ(attr); + ti->info.continuos_update_support =3D SUPPORTS_CONTINUOS_UPDATE(attr); + ti->info.per_group_config_support =3D SUPPORTS_PER_GROUP_CONFIG(attr); + ti->info.reset_support =3D SUPPORTS_RESET(attr); + ti->info.fc_support =3D SUPPORTS_FC(attr); + ti->num_shmti =3D le32_get_bits(attr, GENMASK(15, 0)); + ti->default_blk_ts_rate =3D le32_to_cpu(resp->default_blk_ts_rate); + } + + ph->xops->xfer_put(ph, t); + + 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 =3D response; + struct scmi_tlm_de_priv *p =3D priv; + + st->num_returned =3D le32_get_bits(r->num_desc, GENMASK(15, 0)); + st->num_remaining =3D 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 */ + p->next =3D (void *)r->desc; + + return 0; +} + +static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti, + struct telemetry_de *tde, + void **next, size_t rx_len) +{ + struct scmi_telemetry_res_info *rinfo =3D ti->rinfo; + const struct scmi_de_desc *desc =3D *next; + unsigned int grp_id; + size_t payld_sz =3D sizeof(*desc); + + if (rx_len < payld_sz) + return -ENOSPC; + + tde->de.info->id =3D le32_to_cpu(desc->id); + grp_id =3D le32_to_cpu(desc->grp_id); + if (grp_id !=3D SCMI_TLM_GRP_INVALID) { + /* Group descriptors are empty but allocated at this point */ + if (grp_id >=3D ti->info.base.num_groups) + return -EINVAL; + + /* Link to parent group */ + tde->de.grp =3D &rinfo->grps[grp_id]; + } + + tde->de.info->grp_id =3D grp_id; + tde->de.info->data_sz =3D le32_to_cpu(desc->data_sz); + tde->de.info->type =3D GET_DE_TYPE(desc); + tde->de.info->unit =3D GET_DE_UNIT(desc); + tde->de.info->unit_exp =3D GET_DE_UNIT_EXP(desc); + tde->de.info->instance_id =3D GET_DE_INSTA_ID(desc); + tde->de.info->compo_instance_id =3D GET_COMPO_INSTA_ID(desc); + tde->de.info->compo_type =3D GET_COMPO_TYPE(desc); + tde->de.info->persistent =3D IS_PERSISTENT(desc); + tde->ts_type =3D TSTAMP_SUPPORT(desc); + tde->de.tstamp_support =3D !!tde->ts_type; + /* Count timestamped DEs */ + ti->num_des_tstamp +=3D !!tde->de.tstamp_support; + tde->de.fc_support =3D IS_FC_SUPPORTED(desc); + tde->de.name_support =3D IS_NAME_SUPPORTED(desc); + /* Update DE_DESCRIPTOR size for the next iteration */ + *next +=3D sizeof(*desc); + + payld_sz +=3D SCMI_TDE_VAR_SZ(tde); + if (rx_len < payld_sz) + return -ENOSPC; + + if (tde->ts_type =3D=3D TSTAMP_LINE) { + __le32 *line_ts_rate =3D *next; + + tde->de.info->ts_rate =3D le32_to_cpu((*line_ts_rate)); + + /* Variably sized depending on TS support */ + *next +=3D sizeof(*line_ts_rate); + } else if (tde->ts_type =3D=3D TSTAMP_BLK) { + /* Setup default BLK TS value at first */ + tde->de.info->ts_rate =3D ti->default_blk_ts_rate; + } + + if (tde->de.fc_support) { + u32 size; + u64 phys_addr; + void __iomem *addr; + struct de_desc_fc *dfc; + + dfc =3D *next; + phys_addr =3D le32_to_cpu(dfc->addr_low); + phys_addr |=3D (u64)le32_to_cpu(dfc->addr_high) << 32; + + size =3D le32_to_cpu(dfc->size); + addr =3D devm_ioremap(ti->ph->dev, phys_addr, size); + if (!addr) + return -EADDRNOTAVAIL; + + tde->base =3D addr; + tde->offset =3D 0; + tde->fc_size =3D size; + + /* Variably sized depending on FC support */ + *next +=3D sizeof(*dfc); + } + + if (tde->de.name_support) { + const char *de_name =3D *next; + + strscpy(tde->de.info->name, de_name, SCMI_SHORT_NAME_MAX_SIZE); + /* Variably sized depending on name support */ + *next +=3D SCMI_SHORT_NAME_MAX_SIZE; + } + + return 0; +} + +static int iter_de_descr_process_response(const struct scmi_protocol_handl= e *ph, + const void *response, + struct scmi_iterator_state *st, + void *priv) +{ + struct scmi_tlm_de_priv *p =3D priv; + struct telemetry_info *ti =3D p->ti; + const struct scmi_de_desc *desc =3D p->next; + struct telemetry_de *tde; + bool discovered =3D false; + unsigned int de_id; + int ret; + + de_id =3D le32_to_cpu(desc->id); + /* Check if this DE has already been discovered by other means... */ + tde =3D scmi_telemetry_tde_lookup(ti, de_id); + if (!tde) { + /* Create a new one */ + tde =3D scmi_telemetry_tde_get(ti, de_id); + if (IS_ERR(tde)) + return PTR_ERR(tde); + + discovered =3D true; + } else 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 =3D scmi_telemetry_de_descriptor_parse(ti, tde, &p->next, st->rx_len); + if (ret) { + dev_err(ph->dev, "Malformed DE Descriptor - ret:%d\n", ret); + goto err; + } + + /* Now fully enumerated */ + tde->enumerated =3D true; + + if (discovered) { + /* Register if it was not already ... */ + ret =3D scmi_telemetry_tde_register(ti, tde); + if (ret) + goto err; + } + + /* Add to FastChannels list */ + 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; + +err: + /* DE not enumerated at this point were created in this call */ + if (discovered) + scmi_telemetry_free_tde_put(ti, tde); + + return ret; +} + +static int +scmi_telemetry_de_groups_init(struct device *dev, struct telemetry_info *t= i) +{ + struct scmi_telemetry_res_info *rinfo =3D ti->rinfo; + + /* Allocate all groups DEs IDs arrays at first ... */ + for (int i =3D 0; i < ti->info.base.num_groups; i++) { + struct scmi_telemetry_group *grp =3D &rinfo->grps[i]; + size_t des_str_sz; + + unsigned int *des __free(kfree) =3D kcalloc(grp->info->num_des, + sizeof(unsigned int), + GFP_KERNEL); + if (!des) + return -ENOMEM; + + /* + * Max size 32bit ID string in Hex: 0xCAFECAFE + * - 10 digits + ' '/'\n' =3D 11 bytes per number + * - terminating NUL character + */ + des_str_sz =3D grp->info->num_des * 11 + 1; + char *des_str __free(kfree) =3D kzalloc(des_str_sz, GFP_KERNEL); + if (!des_str) + return -ENOMEM; + + grp->des =3D no_free_ptr(des); + grp->des_str =3D no_free_ptr(des_str); + /* Reset group DE counter */ + grp->info->num_des =3D 0; + } + + /* Scan DEs and populate DE IDs arrays for all groups */ + for (int i =3D 0; i < rinfo->num_des; i++) { + struct scmi_telemetry_group *grp =3D 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++] =3D i; + } + + /* Build composing DES string */ + for (int i =3D 0; i < ti->info.base.num_groups; i++) { + struct scmi_telemetry_group *grp =3D &rinfo->grps[i]; + size_t bufsize =3D grp->info->num_des * 11 + 1; + char *buf =3D grp->des_str; + + for (int j =3D 0; j < grp->info->num_des; j++) { + char term =3D j !=3D (grp->info->num_des - 1) ? ' ' : '\0'; + int len; + + len =3D scnprintf(buf, bufsize, "0x%08X%c", + rinfo->des[grp->des[j]]->info->id, term); + + buf +=3D len; + bufsize -=3D len; + } + } + + rinfo->num_groups =3D ti->info.base.num_groups; + + return 0; +} + +static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti) +{ + const struct scmi_protocol_handle *ph =3D ti->ph; + + struct scmi_iterator_ops ops =3D { + .prepare_message =3D iter_tlm_prepare_message, + .update_state =3D iter_de_descr_update_state, + .process_response =3D iter_de_descr_process_response, + }; + struct scmi_tlm_de_priv tpriv =3D { + .ti =3D ti, + .next =3D NULL, + }; + void *iter; + int ret; + + if (!ti->info.base.num_des) + return 0; + + iter =3D 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 =3D ph->hops->iter_response_run(iter); + if (ret) + return ret; + + 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 =3D message; + const struct scmi_tlm_ivl_priv *p =3D priv; + + msg->index =3D cpu_to_le32(desc_index); + msg->group_identifier =3D cpu_to_le32(p->grp_id); + msg->flags =3D 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 =3D response; + + st->num_returned =3D le32_get_bits(r->flags, GENMASK(11, 0)); + st->num_remaining =3D 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 =3D priv; + struct scmi_telemetry_intervals *intrvs; + bool discrete; + int inum; + + discrete =3D INTERVALS_DISCRETE(r->flags); + /* Check consistency on first call */ + if (!discrete && (st->num_returned !=3D 3 || st->num_remaining !=3D 0)) + return -EINVAL; + + inum =3D st->num_returned + st->num_remaining; + intrvs =3D kzalloc(sizeof(*intrvs) + inum * sizeof(__u32), GFP_KERNEL); + if (!intrvs) + return -ENOMEM; + + intrvs->num_intervals =3D inum; + intrvs->discrete =3D discrete; + st->max_resources =3D intrvs->num_intervals; + + *p->intrvs =3D 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 =3D response; + struct scmi_tlm_ivl_priv *p =3D priv; + struct scmi_telemetry_intervals *intrvs =3D *p->intrvs; + unsigned int idx =3D st->loop_idx; + + intrvs->update_intervals[st->desc_index + idx] =3D + 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 =3D { + .prepare_message =3D iter_intervals_prepare_message, + .update_state =3D iter_intervals_update_state, + .process_response =3D iter_intervals_process_response, + }; + const struct scmi_protocol_handle *ph =3D ti->ph; + struct scmi_telemetry_intervals *intrvs =3D NULL; + struct scmi_tlm_ivl_priv ipriv =3D { + .dev =3D ph->dev, + .grp_id =3D grp_id, + .intrvs =3D &intrvs, + .flags =3D flags, + }; + void *iter; + int ret; + + iter =3D 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 =3D ph->hops->iter_response_run(iter); + if (ret) { + kfree(intrvs); + return ret; + } + + *intervals =3D intrvs; + + return 0; +} + +static int +scmi_telemetry_enumerate_groups_intervals(struct telemetry_info *ti) +{ + struct scmi_telemetry_res_info *rinfo =3D ti->rinfo; + + if (!ti->info.per_group_config_support) + return 0; + + for (int id =3D 0; id < rinfo->num_groups; id++) { + int ret; + + ret =3D scmi_tlm_enumerate_update_intervals(ti, + &rinfo->grps[id].intervals, + id, SPECIFIC_GROUP_DES); + if (ret) + return ret; + + rinfo->grps_store[id].num_intervals =3D + 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 =3D !ti->info.per_group_config_support ? + ALL_DES_ANY_GROUP : ALL_DES_NO_GROUP; + + ret =3D 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 =3D 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 int iter_shmti_update_state(struct scmi_iterator_state *st, + const void *response, void *priv) +{ + const struct scmi_msg_resp_telemetry_shmti_list *r =3D response; + + st->num_returned =3D le32_get_bits(r->num_shmti, GENMASK(15, 0)); + st->num_remaining =3D 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 =3D shmti->base; + u32 sign_start, sign_end; + + sign_start =3D TDCF_START_SIGNATURE(tdcf); + sign_end =3D TDCF_END_SIGNATURE(SHMTI_EPLG(shmti)); + + if (sign_start !=3D SIGNATURE_START || sign_end !=3D 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 =3D response; + struct telemetry_info *ti =3D priv; + struct telemetry_shmti *shmti; + const struct scmi_shmti_desc *desc; + void __iomem *addr; + u64 phys_addr; + u32 len; + + desc =3D &r->desc[st->loop_idx]; + shmti =3D &ti->shmti[st->desc_index + st->loop_idx]; + + shmti->info.sid =3D le32_to_cpu(desc->id); + shmti->flags =3D le32_to_cpu(desc->flags); + phys_addr =3D le32_to_cpu(desc->addr_low); + phys_addr |=3D (u64)le32_to_cpu(desc->addr_high) << 32; + + len =3D 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; + } + + addr =3D devm_ioremap(ph->dev, phys_addr, len); + if (!addr) + return -EADDRNOTAVAIL; + + shmti->base =3D addr; + shmti->info.len =3D len; + shmti->info.phys =3D phys_addr; + shmti->info.offset =3D 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 =3D { + .prepare_message =3D iter_tlm_prepare_message, + .update_state =3D iter_shmti_update_state, + .process_response =3D iter_shmti_process_response, + }; + void *iter; + + iter =3D 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 =3D ti->ph; + struct scmi_telemetry_shmti_info **shmtis; + int ret; + + if (!ti->num_shmti) + return 0; + + ti->shmti =3D devm_kcalloc(ph->dev, ti->num_shmti, sizeof(*ti->shmti), + GFP_KERNEL); + if (!ti->shmti) + return -ENOMEM; + + ret =3D scmi_telemetry_shmti_list(ph, ti); + if (ret) { + dev_err(ph->dev, "Cannot get SHMTI list descriptors"); + return ret; + } + + /* Expose initialized SHMTIs references */ + shmtis =3D devm_kcalloc(ph->dev, ti->num_shmti, + sizeof(*shmtis), GFP_KERNEL); + if (!shmtis) + return -ENOMEM; + + for (int i =3D 0; i < ti->num_shmti; i++) + shmtis[i] =3D &ti->shmti[i].info; + + ti->info.shmtis =3D shmtis; + ti->info.base.num_shmtis =3D ti->num_shmti; + + return 0; +} + +static const struct scmi_telemetry_info * +scmi_telemetry_info_get(const struct scmi_protocol_handle *ph) +{ + struct telemetry_info *ti =3D ph->get_priv(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 =3D 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 telemetry_info *ti =3D ph->get_priv(ph); + + return ti->res_get(ti); +} + +static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { + .info_get =3D scmi_telemetry_info_get, + .de_lookup =3D scmi_telemetry_de_lookup, + .res_get =3D 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 poss= ible + * 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) =3D + kcalloc(ti->info.base.num_des, sizeof(*des), GFP_KERNEL); + if (!des) + return -ENOMEM; + + /* The allocated DE descriptors */ + struct telemetry_de *tdes __free(kfree) =3D + kcalloc(ti->info.base.num_des, sizeof(*tdes), GFP_KERNEL); + if (!tdes) + return -ENOMEM; + + /* Allocate a set of contiguous DE info descriptors. */ + struct scmi_telemetry_de_info *dei_store __free(kfree) =3D + kcalloc(ti->info.base.num_des, sizeof(*dei_store), GFP_KERNEL); + if (!dei_store) + return -ENOMEM; + + /* Array to hold descriptors of discovered GROUPs */ + struct scmi_telemetry_group *grps __free(kfree) =3D + kcalloc(ti->info.base.num_groups, sizeof(*grps), GFP_KERNEL); + if (!grps) + return -ENOMEM; + + /* Allocate a set of contiguous Group info descriptors. */ + struct scmi_telemetry_grp_info *grps_store __free(kfree) =3D + kcalloc(ti->info.base.num_groups, sizeof(*grps_store), GFP_KERNEL); + if (!grps_store) + return -ENOMEM; + + struct scmi_telemetry_res_info *rinfo __free(kfree) =3D + kzalloc(sizeof(*rinfo), GFP_KERNEL); + if (!rinfo) + return -ENOMEM; + + mutex_init(&ti->free_mtx); + INIT_LIST_HEAD(&ti->free_des); + for (int i =3D 0; i < ti->info.base.num_des; i++) { + mutex_init(&tdes[i].mtx); + /* Bind contiguous DE info structures */ + tdes[i].de.info =3D &dei_store[i]; + list_add_tail(&tdes[i].item, &ti->free_des); + } + + for (int i =3D 0; i < ti->info.base.num_groups; i++) { + grps_store[i].grp_id =3D i; + /* Bind contiguous Group info struct */ + grps[i].info =3D &grps_store[i]; + } + + INIT_LIST_HEAD(&ti->fcs_des); + + ti->tdes =3D no_free_ptr(tdes); + + rinfo->des =3D no_free_ptr(des); + rinfo->dei_store =3D no_free_ptr(dei_store); + rinfo->grps =3D no_free_ptr(grps); + rinfo->grps_store =3D no_free_ptr(grps_store); + + ti->rinfo =3D no_free_ptr(rinfo); + + return 0; +} + +static void scmi_telemetry_groups_free(struct scmi_telemetry_res_info *rin= fo) +{ + for (int i =3D 0; i < rinfo->num_groups; i++) { + struct scmi_telemetry_group *grp =3D &rinfo->grps[i]; + + kfree(grp->des); + kfree(grp->des_str); + kfree(grp->intervals); + } +} + +static void scmi_telemetry_resources_free(void *arg) +{ + struct telemetry_info *ti =3D arg; + struct scmi_telemetry_res_info *rinfo =3D ti->rinfo; + + xa_destroy(&ti->xa_des); + kfree(ti->tdes); + kfree(rinfo->des); + kfree(rinfo->dei_store); + scmi_telemetry_groups_free(rinfo); + kfree(rinfo->grps); + kfree(rinfo->grps_store); + + kfree(rinfo); + + ti->rinfo =3D NULL; +} + +static struct scmi_telemetry_res_info * +__scmi_telemetry_resources_get(struct telemetry_info *ti) +{ + return ti->rinfo; +} + +/** + * 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 resou= rces + * 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 w= ith + * the end result that only a partially populated, but consistent, resourc= es + * descriptor will be returned; in such a case the incomplete descriptor w= ill + * be marked as NOT fully_enumerated: this design enables the kernel to de= al + * with badly implemented out-of-spec firmware support while keep on provi= ding + * a minimal sane, albeit possibly incomplete, set of telemetry respources. + * + * Return: A reference to a fully or partially populated resources descrip= tor + */ +static struct scmi_telemetry_res_info * +scmi_telemetry_resources_enumerate(struct telemetry_info *ti) +{ + struct scmi_telemetry_res_info *rinfo =3D ti->rinfo; + struct device *dev =3D ti->ph->dev; + int ret; + + /* + * Ensure this init function can be called only once and + * handles properly concurrent calls. + */ + if (atomic_cmpxchg(&ti->rinfo_initializing, 0, 1)) { + if (!completion_done(&ti->rinfo_initdone)) + wait_for_completion(&ti->rinfo_initdone); + goto out; + } + + ret =3D scmi_telemetry_de_descriptors_get(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot fully enumerate DEs resources. Degraded syst= em.\n"); + goto done; + } + + ret =3D scmi_telemetry_enumerate_groups_intervals(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot fully enumerate group intervals. Degraded sy= stem.\n"); + goto done; + } + + /* If we got here, the enumeration was fully successful */ + rinfo->fully_enumerated =3D true; +done: + /* Disable initialization permanently */ + smp_store_mb(ti->res_get, __scmi_telemetry_resources_get); + complete_all(&ti->rinfo_initdone); + +out: + return 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 need= ed + * and then setups the @scmi_telemetry_resources_enumerate helper as the + * default method for the first call to ti->res_get(): this mechanism enab= les + * the possibility of optionally implementing deferred enumeration policies + * which optionally delay the discovery phase and related SCMI message exc= hanges + * 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 =3D scmi_telemetry_resources_alloc(ti); + if (ret) + return ret; + + xa_init(&ti->xa_des); + ret =3D devm_add_action_or_reset(ti->ph->dev, + scmi_telemetry_resources_free, ti); + if (ret) + return ret; + + /* Setup resources lazy initialization */ + atomic_set(&ti->rinfo_initializing, 0); + init_completion(&ti->rinfo_initdone); + /* Ensure the new res_get() operation is visible after this point */ + smp_store_mb(ti->res_get, scmi_telemetry_resources_enumerate); + + return 0; +} + +static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle = *ph) +{ + struct device *dev =3D 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 =3D devm_kzalloc(dev, sizeof(*ti), GFP_KERNEL); + if (!ti) + return -ENOMEM; + + ti->ph =3D ph; + + ret =3D scmi_telemetry_protocol_attributes_get(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot retrieve protocol attributes. Abort.\n"); + return ret; + } + + ret =3D scmi_telemetry_instance_init(ti); + if (ret) { + dev_err(dev, "Cannot initialize instance. Abort.\n"); + return ret; + } + + ret =3D scmi_telemetry_enumerate_common_intervals(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot enumerate update intervals. Abort.\n"); + return ret; + } + + ret =3D scmi_telemetry_enumerate_shmti(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot enumerate SHMTIs. Abort.\n"); + return ret; + } + + ti->info.base.version =3D ph->version; + + return ph->set_priv(ph, ti); +} + +static const struct scmi_protocol scmi_telemetry =3D { + .id =3D SCMI_PROTOCOL_TELEMETRY, + .owner =3D THIS_MODULE, + .instance_init =3D &scmi_telemetry_protocol_init, + .ops =3D &tlm_proto_ops, + .supported_version =3D 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..a03921e9a698 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. */ =20 #ifndef _LINUX_SCMI_PROTOCOL_H #define _LINUX_SCMI_PROTOCOL_H =20 #include +#include #include #include #include @@ -824,6 +825,183 @@ struct scmi_pinctrl_proto_ops { int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin); }; =20 +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 =3D 0xef, + SCMI_TLM_DE_TYPE_OEM_START =3D 0xf0, + SCMI_TLM_DE_TYPE_OEM_END =3D 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 =3D 0x1d, + SCMI_TLM_COMPO_TYPE_RESERVED_END =3D 0xdf, + SCMI_TLM_COMPO_TYPE_OEM_START =3D 0xe0, + SCMI_TLM_COMPO_TYPE_OEM_END =3D 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)) + +#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))) + +enum scmi_telemetry_collection { + SCMI_TLM_ONDEMAND, + SCMI_TLM_NOTIFICATION, + SCMI_TLM_SINGLE_READ, +}; + +#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF +#define SCMI_TLM_DE_IMPL_MAX_DWORDS 4 + +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; + 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; + unsigned int active_update_interval; + struct scmi_telemetry_intervals *intervals; + enum scmi_telemetry_collection current_mode; +}; + +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; + struct scmi_telemetry_group *grp; + 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; + unsigned int num_groups; + struct scmi_telemetry_group *grps; + struct scmi_telemetry_grp_info *grps_store; +}; + +struct scmi_telemetry_base_info { + unsigned int version; + unsigned int de_impl_version[SCMI_TLM_DE_IMPL_MAX_DWORDS]; + unsigned int num_des; + unsigned int num_groups; + unsigned int num_intervals; + 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 continuos_update_support; + bool per_group_config_support; + bool reset_support; + bool fc_support; + struct scmi_telemetry_base_info base; + unsigned int active_update_interval; + struct scmi_telemetry_intervals *intervals; + struct scmi_telemetry_shmti_info **shmtis; + bool enabled; + bool notif_enabled; + enum scmi_telemetry_collection current_mode; +}; + +/** + * struct scmi_telemetry_proto_ops - represents the various operations pro= vided + * 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); +}; + /** * struct scmi_notify_ops - represents notifications' operations provided= by * SCMI core @@ -930,6 +1108,7 @@ enum scmi_std_protocol { SCMI_PROTOCOL_VOLTAGE =3D 0x17, SCMI_PROTOCOL_POWERCAP =3D 0x18, SCMI_PROTOCOL_PINCTRL =3D 0x19, + SCMI_PROTOCOL_TELEMETRY =3D 0x1b, }; =20 enum scmi_system_events { --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 220B24218A4; Fri, 24 Jul 2026 14:46:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904374; cv=none; b=NgD3xPhM2S3Y4OhHtxUOcE//v824apB9kBEldn9AHmrqG3I+JgvtpkKkVLfOpWbwbXCXq5KYD1srkT+pAdUnfJivzUnRzSWiv06vtsMApjIiWq6UOyEkAUOemLJZDBw4jm4cLb71XOoXr9mtZT1t1RmOdU7cJE55xR9af6GSn+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904374; c=relaxed/simple; bh=bv6JBrAaz2AAsvzgc3GcpsOptkxnDrp0nhbQ+rnTL8M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u6rbnoV+e5l9OyV9D4cftDKtJCZIyWDsMcgDtf5PKidqOACNFozVPLtLe564stVUAKuaSv+EU9gaERNvwGDiWcgcv/EpLvSpu8b9CM+5vC+OjTUR5XoOC3Xp5WLUheRGYYUd9bOqgUeP3dI1ADKj+rWuN/FwcoP9m/dj0EbD33E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=mouUNdpc; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="mouUNdpc" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 226621477; Fri, 24 Jul 2026 07:45:58 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BECB63F66F; Fri, 24 Jul 2026 07:45:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904362; bh=bv6JBrAaz2AAsvzgc3GcpsOptkxnDrp0nhbQ+rnTL8M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mouUNdpcwC7DrMdrEDywGXDxBtT5sEk4S0mBdAxSpek8FDGOFkLbu1wiPxc3v9uCR /bvnOAVwfQiAuN0QSL2ofbevHy/LezYL32G3MdNR4l61rtfZsY4JqBBk7V/6WQ3/Bs 9s1jna448LM7CyecFg3mB/h1dFHI/B0885hQ7/1c= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 07/22] firmware: arm_scmi: Add support to parse SHMTIs areas Date: Fri, 24 Jul 2026 15:44:15 +0100 Message-ID: <20260724144430.1440170-8-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add logic to scan the SHMTI areas, parsing the TDCF descriptors while collecting DataEvent, BlockTimestamp and UUID lines. Signed-off-by: Cristian Marussi --- 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=3D8 - 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 XAr= ray - 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 | 650 ++++++++++++++++++++++++++ 1 file changed, 650 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 372051ad1e62..b9b6df48ab6b 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -265,6 +265,23 @@ struct uuid_line { __be32 dwords[SCMI_TLM_DE_IMPL_MAX_DWORDS]; }; =20 +#define LINE_DATA_GET(f) \ +({ \ + typeof(f) _f =3D (f); \ + \ + (TO_CPU_64(_I(&_f->data_high), _I(&_f->data_low))); \ +}) + +#define LINE_TSTAMP_GET(f) \ +({ \ + typeof(f) _f =3D (f); \ + \ + (TO_CPU_64(_I(&_f->ts_high), _I(&_f->ts_low))); \ +}) + +#define BLK_TS_STAMP(f) LINE_TSTAMP_GET(f) +#define BLK_TS_RATE(p) PAYLD_ID(p) + enum tdcf_line_types { TDCF_DATA_LINE, TDCF_BLK_TS_LINE, @@ -320,6 +337,7 @@ struct payload { (HAS_LINE_EXT((x)) ? EXT_LINE_DATA_PAYLD_WORDS : LINE_DATA_PAYLD_WORDS) =20 #define LINE_LENGTH_QWORDS(x) ((LINE_LENGTH_WORDS(x)) / 2) +#define LINE_LENGTH_BYTES(x) ((LINE_LENGTH_WORDS(x)) * sizeof(u32)) =20 struct prlg { u32 sign_start; @@ -371,10 +389,22 @@ struct telemetry_shmti { (_eplg); \ }) =20 +#define SHMTI_OVERFLOWS(_n, _s) \ + ({ \ + struct telemetry_shmti *__s =3D (_s); \ + void *__n =3D (_n); \ + bool oflow; \ + \ + __n +=3D LINE_LENGTH_BYTES((struct payload __iomem *)__n);\ + oflow =3D __n >=3D __s->base + __s->info.len; \ + oflow; \ + }) + struct telemetry_line { refcount_t users; u32 last_magic; struct payload __iomem *payld; + struct xarray *xa_lines; /* Protect line accesses */ struct mutex mtx; }; @@ -423,18 +453,31 @@ struct telemetry_de { =20 #define to_tde(d) container_of(d, struct telemetry_de, de) =20 +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 =20 +enum de_state { + ENA_STATE, + ENA_TSTAMP, + ENA_MAX +}; + struct telemetry_info { bool streaming_mode; unsigned int num_shmti; unsigned int num_des_tstamp; + atomic_t des_enabled[ENA_MAX]; 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; + struct xarray xa_lines; /* Mutex to protect access to @free_des */ struct mutex free_mtx; struct list_head free_des; @@ -449,6 +492,21 @@ struct telemetry_info { static struct scmi_telemetry_res_info * __scmi_telemetry_resources_get(struct telemetry_info *ti); =20 +static inline void +scmi_telemetry_de_state_update(struct telemetry_info *ti, enum de_state st= ate, + bool *current_state, const bool next_state) +{ + if (!current_state || *current_state !=3D next_state) + atomic_add(next_state ? 1 : -1, &ti->des_enabled[state]); + + if (current_state) + *current_state =3D next_state; + + dev_dbg(ti->ph->dev, "Telemetry des_enabled[%s]:%u\n", + state =3D=3D ENA_STATE ? "STATE" : "TSTAMP", + atomic_read(&ti->des_enabled[state])); +} + static struct telemetry_de * scmi_telemetry_free_tde_get(struct telemetry_info *ti) { @@ -528,6 +586,27 @@ static int scmi_telemetry_tde_register(struct telemetr= y_info *ti, return ret; } =20 +static bool +scmi_telemetry_tde_cache_unchanged(struct telemetry_de *tde, u32 magic) +{ + guard(mutex)(&tde->mtx); + + return tde->last_magic =3D=3D magic; +} + +static void +scmi_telemetry_tde_cache_update(struct telemetry_de *tde, u64 val, + u64 *tstamp, u32 *magic) +{ + guard(mutex)(&tde->mtx); + + tde->last_magic =3D magic ? *magic : TDCF_BAD_END_SEQ; + tde->last_val =3D val; + tde->last_ts =3D tstamp && scmi_tde_has_tstamp(tde) ? *tstamp : 0; + if (tstamp) + *tstamp =3D tde->last_ts; +} + struct scmi_tlm_de_priv { struct telemetry_info *ti; void *next; @@ -1171,6 +1250,566 @@ scmi_telemetry_resources_get(const struct scmi_prot= ocol_handle *ph) return ti->res_get(ti); } =20 +static u64 +scmi_telemetry_blkts_read(u32 magic, struct telemetry_block_ts *bts) +{ + if (WARN_ON(!bts || !refcount_read(&bts->line.users))) + return 0; + + guard(mutex)(&bts->line.mtx); + + if (bts->line.last_magic =3D=3D magic) + return bts->last_ts; + + /* Note that the bts->last_rate can change ONLY on creation */ + bts->last_ts =3D BLK_TS_STAMP(&bts->line.payld->blk_tsl); + bts->line.last_magic =3D magic; + + return bts->last_ts; +} + +static void scmi_telemetry_blkts_update(struct telemetry_info *ti, u32 mag= ic, + struct telemetry_block_ts *bts) +{ + guard(mutex)(&bts->line.mtx); + + if (bts->line.last_magic !=3D magic) { + bts->last_ts =3D BLK_TS_STAMP(&bts->line.payld->blk_tsl); + bts->last_rate =3D BLK_TS_RATE(bts->line.payld); + /* BLK_TS clock rate value can change ONLY here on creation */ + if (!bts->last_rate) + bts->last_rate =3D ti->default_blk_ts_rate; + bts->line.last_magic =3D magic; + } +} + +static void scmi_telemetry_line_put(struct telemetry_line *line, void *blo= b) +{ + if (refcount_dec_and_test(&line->users)) { + scoped_guard(mutex, &line->mtx) + xa_erase(line->xa_lines, (unsigned long)line->payld); + kfree(blob); + } +} + +static void scmi_telemetry_blkts_unlink(struct telemetry_de *tde) +{ + scmi_telemetry_line_put(&tde->bts->line, tde->bts); + tde->bts =3D NULL; +} + +static void scmi_telemetry_uuid_unlink(struct telemetry_de *tde) +{ + scmi_telemetry_line_put(&tde->uuid->line, tde->uuid); + tde->uuid =3D NULL; +} + +static void scmi_telemetry_de_unlink(struct scmi_telemetry_de *de) +{ + struct telemetry_de *tde =3D to_tde(de); + + /* Unlink all related lines triggering their deallocation */ + if (tde->bts) + scmi_telemetry_blkts_unlink(tde); + if (tde->uuid) + scmi_telemetry_uuid_unlink(tde); +} + +static struct telemetry_line * +scmi_telemetry_line_get(struct xarray *xa_lines, struct payload __iomem *p= ayld) +{ + struct telemetry_line *line; + + line =3D xa_load(xa_lines, (unsigned long)payld); + if (!line) + return NULL; + + refcount_inc(&line->users); + + return line; +} + +static int +scmi_telemetry_line_init(struct telemetry_line *line, struct xarray *xa_li= nes, + struct payload __iomem *payld) +{ + refcount_set(&line->users, 1); + line->payld =3D payld; + line->xa_lines =3D xa_lines; + mutex_init(&line->mtx); + + return xa_insert(xa_lines, (unsigned long)payld, line, GFP_KERNEL); +} + +static struct telemetry_block_ts * +scmi_telemetry_blkts_create(struct device *dev, struct xarray *xa_lines, + struct payload __iomem *payld) +{ + struct telemetry_block_ts *bts; + int ret; + + bts =3D kzalloc_obj(*bts); + if (!bts) + return NULL; + + ret =3D scmi_telemetry_line_init(&bts->line, xa_lines, payld); + 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 device *dev, struct xarray *xa_l= ines, + struct payload __iomem *payld) +{ + struct telemetry_line *line; + + line =3D scmi_telemetry_line_get(xa_lines, payld); + if (line) + return to_blkts(line); + + return scmi_telemetry_blkts_create(dev, xa_lines, payld); +} + +static struct telemetry_uuid * +scmi_telemetry_uuid_create(struct device *dev, struct xarray *xa_lines, + struct payload __iomem *payld) +{ + struct telemetry_uuid *uuid; + struct uuid_line __iomem *uuid_l =3D &payld->uuid_l; + int ret; + + uuid =3D kzalloc_obj(*uuid); + if (!uuid) + return NULL; + + for (int i =3D 0; i < SCMI_TLM_DE_IMPL_MAX_DWORDS; i++) + /* + * Fetch BigEndian in-memory UUID fields as per SCMIv4.0 + * specification 3.12.2.2 + */ + uuid->de_impl_version[i] =3D ioread32be(&uuid_l->dwords[i]); + + ret =3D scmi_telemetry_line_init(&uuid->line, xa_lines, payld); + if (ret) { + kfree(uuid); + 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 device *dev, struct xarray *xa_li= nes, + struct payload __iomem *payld) +{ + struct telemetry_line *line; + + line =3D scmi_telemetry_line_get(xa_lines, payld); + if (line) + return to_uuid(line); + + return scmi_telemetry_uuid_create(dev, xa_lines, payld); +} + +static void scmi_telemetry_tdcf_uuid_parse(struct telemetry_info *ti, + struct payload __iomem *payld, + struct telemetry_shmti *shmti, + void **active_uuid) +{ + struct telemetry_uuid *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 =3D scmi_telemetry_uuid_get_or_create(ti->ph->dev, &ti->xa_lines, + payld); + if (WARN_ON(!uuid)) + return; + + *active_uuid =3D uuid; +} + +static struct payload __iomem * +scmi_telemetry_nearest_line_by_type(struct telemetry_shmti *shmti, + void __iomem *last, enum tdcf_line_types ltype) +{ + struct tdcf __iomem *tdcf =3D shmti->base; + void __iomem *next, *found =3D NULL; + + /* Scan from start of TDCF payloads up to last_payld */ + next =3D tdcf->payld; + while (next < last) { + if (LINE_TYPE((struct payload __iomem *)next) =3D=3D ltype) + found =3D next; + + next +=3D LINE_LENGTH_BYTES((struct payload __iomem *)next); + } + + return found; +} + +static struct telemetry_block_ts * +scmi_telemetry_blkts_bind(struct device *dev, struct telemetry_shmti *shmt= i, + struct payload __iomem *payld, struct xarray *xa_lines, + 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 =3D scmi_telemetry_nearest_line_by_type(shmti, payld, + TDCF_BLK_TS_LINE); + if (!bts_payld) + return NULL; + } + + return scmi_telemetry_blkts_get_or_create(dev, xa_lines, bts_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 fo= und + * + * 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 current= ly + * 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, + void **active_bts) +{ + struct telemetry_block_ts *bts; + + /* Check for spec compliance */ + if (BLK_TS_INVALID(payld)) { + trace_scmi_tlm_access(0, "BLK_TS_INVALID", 0, 0); + return; + } + + /* A BLK_TS descriptor MUST be returned: it is found or it is created */ + bts =3D scmi_telemetry_blkts_get_or_create(ti->ph->dev, + &ti->xa_lines, payld); + if (WARN_ON(!bts)) + return; + + /* Update the descriptor with the lastest TS */ + scmi_telemetry_blkts_update(ti, shmti->last_magic, bts); + *active_bts =3D bts; +} + +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 =3D scmi_telemetry_tde_get(ti, de_id); + if (IS_ERR(tde)) + return NULL; + + tde->de.info->id =3D de_id; + tde->de.enabled =3D true; + tde->de.tstamp_enabled =3D 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 void +scmi_telemetry_line_data_parse(struct telemetry_de *tde, u64 *val, u64 *ts= tamp, + struct payload __iomem *payld, u32 magic) +{ + /* Data is always valid since we are NOT handling BLK TS lines here */ + *val =3D LINE_DATA_GET(&payld->l); + if (tstamp) { + if (USE_BLK_TS(payld)) { + /* Read out the actual BLK_TS */ + *tstamp =3D scmi_telemetry_blkts_read(magic, tde->bts); + } else if (LINE_TS_VALID(payld)) { + /* + * Note that LINE_TS_VALID implies HAS_LINE_EXT and that + * the per DE line_ts_rate is advertised in the DE + * descriptor. + */ + *tstamp =3D LINE_TSTAMP_GET(&payld->tsl); + } else { + *tstamp =3D 0; + } + } + + trace_scmi_tlm_collect(tstamp ? *tstamp : 0, tde->de.info->id, + *val, "SHMTI_DE_READ"); + + scmi_telemetry_tde_cache_update(tde, *val, tstamp, &magic); +} + +static inline void scmi_telemetry_bts_link(struct telemetry_de *tde, + struct telemetry_block_ts *bts) +{ + guard(mutex)(&bts->line.mtx); + refcount_inc(&bts->line.users); + tde->bts =3D bts; + /* Update TS clock rate if provided by the BLK_TS */ + if (tde->bts->last_rate) + tde->de.info->ts_rate =3D tde->bts->last_rate; +} + +static inline void scmi_telemetry_uuid_link(struct telemetry_de *tde, + struct telemetry_uuid *uuid) +{ + refcount_inc(&uuid->line.users); + tde->uuid =3D uuid; +} + +/** + * 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 DataLi= ne + * 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_LOOK= UP + * 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, + void *active_bts, void *active_uuid) +{ + bool use_blk_ts =3D USE_BLK_TS(payld); + struct telemetry_de *tde; + u64 val, tstamp =3D 0; + u32 de_id; + + de_id =3D 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 =3D scmi_telemetry_tde_lookup(ti, de_id); + if (!tde) { + if (mode !=3D 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 =3D scmi_telemetry_tde_allocate(ti, de_id, payld); + if (!tde) + return; + } + + /* Update DE location refs if requested: normally done only on enable */ + if (mode >=3D SCAN_UPDATE) { + tde->base =3D shmti->base; + tde->eplg =3D SHMTI_EPLG(shmti); + tde->offset =3D (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; + + /* Link the related BTS when needed, it's unlinked on disable */ + if (use_blk_ts && !tde->bts) + scmi_telemetry_bts_link(tde, active_bts); + + /* Link the active UUID when existent, it's unlinked on disable */ + if (active_uuid) + scmi_telemetry_uuid_link(tde, active_uuid); + + /* Parse data words */ + scmi_telemetry_line_data_parse(tde, &val, &tstamp, payld, + shmti->last_magic); +} + +static int scmi_telemetry_tdcf_line_parse(struct telemetry_info *ti, + struct payload __iomem *payld, + struct telemetry_shmti *shmti, + enum scan_mode mode, + void **active_bts, void **active_uuid) +{ + int used_qwords; + + used_qwords =3D 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; + 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_UNKNOWN", 0, 0); + break; + } + + return used_qwords; +} + +/** + * 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 =3D &ti->shmti[shmti_id]; + struct tdcf __iomem *tdcf =3D shmti->base; + int retries =3D SCMI_TLM_TDCF_MAX_RETRIES; + u32 startm =3D 0, endm =3D TDCF_BAD_END_SEQ; + + if (!tdcf) + return -ENODEV; + + do { + void *active_bts =3D NULL, *active_uuid =3D NULL; + unsigned int qwords; + void __iomem *next; + + /* A bit of exponential backoff between retries */ + fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * 1000); + + /* + * 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 =3D TDCF_START_SEQ_GET(tdcf); + 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 =3D startm; + + qwords =3D QWORDS(tdcf); + next =3D tdcf->payld; + while (qwords) { + int used_qwords; + + if (SHMTI_OVERFLOWS(next, shmti)) { + trace_scmi_tlm_access(PAYLD_ID(next), + "SHMTI_OVERFLOW", startm, 0); + return -EINVAL; + } + + used_qwords =3D scmi_telemetry_tdcf_line_parse(ti, next, + shmti, mode, + &active_bts, + &active_uuid); + if (qwords < used_qwords) { + trace_scmi_tlm_access(PAYLD_ID(next), + "BAD_QWORDS", startm, 0); + return -EINVAL; + } + + next +=3D used_qwords * 8; + qwords -=3D used_qwords; + } + + endm =3D TDCF_END_SEQ_GET(SHMTI_EPLG(shmti)); + if (startm !=3D endm) + trace_scmi_tlm_access(0, "MSEQ_MISMATCH", startm, endm); + } while (startm !=3D endm && --retries); + + if (startm !=3D 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 =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, @@ -1271,7 +1910,15 @@ static void scmi_telemetry_resources_free(void *arg) struct telemetry_info *ti =3D arg; struct scmi_telemetry_res_info *rinfo =3D ti->rinfo; =20 + /* + * Unlinking all the BLK_TS/UUID lines related to a DE triggers also + * the deallocation of such lines when the embedded refcount hits zero. + */ + for (int i =3D 0; i < rinfo->num_des; i++) + scmi_telemetry_de_unlink(rinfo->des[i]); + xa_destroy(&ti->xa_des); + xa_destroy(&ti->xa_lines); kfree(ti->tdes); kfree(rinfo->des); kfree(rinfo->dei_store); @@ -1373,11 +2020,14 @@ static int scmi_telemetry_instance_init(struct tele= metry_info *ti) return ret; =20 xa_init(&ti->xa_des); + xa_init(&ti->xa_lines); ret =3D devm_add_action_or_reset(ti->ph->dev, scmi_telemetry_resources_free, ti); if (ret) return ret; =20 + atomic_set(&ti->des_enabled[ENA_STATE], 0); + atomic_set(&ti->des_enabled[ENA_TSTAMP], 0); /* Setup resources lazy initialization */ atomic_set(&ti->rinfo_initializing, 0); init_completion(&ti->rinfo_initdone); --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E78C943B3FF; Fri, 24 Jul 2026 14:46:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904379; cv=none; b=shZcM4xBULol4xdki/IwFqVGxR3SSxcqY+lSXnNpUKosOL6hKrjeryZpqU/RKcH+rr8Xtr4HCJIfOR3jRGhxMHiVpTuyAxO6VRkbhOPwhDfmy2yFWHqOQIAgs0tu+j8V0ZOVgH4gsBx9sCvZumbEET/iQT2e47+gn4+obncfnrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904379; c=relaxed/simple; bh=isgKOwgvIXg95jQKBvyD6at5aAIOIoZ46ko5FqtTmJo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e/fr/lAalIelSQF/1ooYgSvbnK7UxKZy2q4rsxno3fST/KNBqW8D6LwUGdsqos8/tjEr7fDYbkYRtjU8Vy/fPWiLbvE/jRkXfPbYDSX6Mwsm67DFgY3lqJ5gTm/hamL8Dz18ZbQ1xvlaB5FwUEjEvQBKuH5LFONgGHmkmBpwCNo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=VUw3AkVa; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="VUw3AkVa" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4CD2F1684; Fri, 24 Jul 2026 07:46:06 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D11B93F66F; Fri, 24 Jul 2026 07:46:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904370; bh=isgKOwgvIXg95jQKBvyD6at5aAIOIoZ46ko5FqtTmJo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VUw3AkVaac9FAGRcx3AAaRxuNzqVGlnM+yAXxqisoZDyy8MKmeAFyhAbGFIXnyM8R gYiuj3fqgEdrbUUhBvhCbQe6cCwVdy2ZWjW5FVxS6xui1LBKnxq9jsMhGAC7+Mc2z+ kGckiFDFbVsIOb+LMV/wYLMXaYRsPFjWyZXEAT2M= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 08/22] firmware: arm_scmi: Add Telemetry configuration operations Date: Fri, 24 Jul 2026 15:44:16 +0100 Message-ID: <20260724144430.1440170-9-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for basic Telemetry configuration operations to selectively enable or disable DataEvents monitoring. Signed-off-by: Cristian Marussi --- 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=3D9 - 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 | 400 ++++++++++++++++++++++++++ include/linux/scmi_protocol.h | 17 ++ 2 files changed, 417 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index b9b6df48ab6b..a40d031e591d 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -492,6 +492,9 @@ struct telemetry_info { static struct scmi_telemetry_res_info * __scmi_telemetry_resources_get(struct telemetry_info *ti); =20 +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 st= ate, bool *current_state, const bool next_state) @@ -1810,10 +1813,407 @@ static int scmi_telemetry_shmti_scan(struct teleme= try_info *ti, return 0; } =20 +static int 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 =3D ti->res_get(ti); + for (int i =3D 0; i < grp->info->num_des; i++) { + struct scmi_telemetry_de *de =3D rinfo->des[grp->des[i]]; + + 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); + } + + return 0; +} + +static void scmi_telemetry_group_unlink(struct telemetry_info *ti, + struct scmi_telemetry_group *grp) +{ + struct scmi_telemetry_res_info *rinfo; + + rinfo =3D ti->res_get(ti); + /* Unlink the related BLK_TS/UUID lines of DEs composing the group */ + for (int i =3D 0; i < grp->info->num_des; i++) + scmi_telemetry_de_unlink(rinfo->des[grp->des[i]]); +} + +static int +scmi_telemetry_state_set_resp_process(struct telemetry_info *ti, void *obj, + void *r, bool is_group) +{ + struct scmi_msg_resp_telemetry_de_configure *resp =3D r; + u32 sid =3D le32_to_cpu(resp->shmti_id); + + /* Update DE SHMTI and offset, if applicable */ + if (IS_SHMTI_ID_VALID(sid)) { + if (sid >=3D ti->num_shmti) + return -EPROTO; + + /* + * Update SHMTI/offset while skipping non-SHMTI-DEs like + * FCs and notif-only. + */ + if (!is_group) { + struct telemetry_de *tde; + struct payload __iomem *payld; + struct scmi_telemetry_de *de =3D obj; + u32 de_offs; + + de_offs =3D le32_to_cpu(resp->shmti_de_offset); + payld =3D ti->shmti[sid].base; + /* Check boundary first... */ + if (de_offs + LINE_LENGTH_BYTES(payld) >=3D + ti->shmti[sid].info.len) + return -EPROTO; + + tde =3D to_tde(de); + /*...then assign */ + tde->base =3D ti->shmti[sid].base; + tde->offset =3D de_offs; + /* A handy reference to the Epilogue updated */ + tde->eplg =3D SHMTI_EPLG(&ti->shmti[sid]); + + payld =3D tde->base + tde->offset; + if (USE_BLK_TS(payld) && !tde->bts) { + struct payload __iomem *bts_payld; + u32 bts_offs; + + bts_offs =3D le32_to_cpu(resp->blk_ts_offset); + bts_payld =3D (bts_offs) ? tde->base + bts_offs : NULL; + + /* Check SHMTI overflow on returned offset */ + if (bts_payld && + SHMTI_OVERFLOWS(bts_payld, &ti->shmti[sid])) + return -ENOSPC; + + tde->bts =3D scmi_telemetry_blkts_bind(ti->ph->dev, + &ti->shmti[sid], + payld, + &ti->xa_lines, + bts_payld); + if (WARN_ON(!tde->bts)) + return -EPROTO; + } + } else { + int ret; + + /* + * 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 =3D 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 */ + if (!is_group) + scmi_telemetry_de_unlink(obj); + else + scmi_telemetry_group_unlink(ti, obj); + } + + return 0; +} + +static int __scmi_telemetry_state_set(const struct scmi_protocol_handle *p= h, + bool is_group, const bool *enable, + bool *enabled_state, const bool *tstamp, + bool *tstamp_enabled_state, void *obj) +{ + struct scmi_msg_resp_telemetry_de_configure *resp; + struct scmi_msg_telemetry_de_configure *msg; + struct telemetry_info *ti =3D ph->get_priv(ph); + struct scmi_telemetry_de *de =3D !is_group ? obj : NULL; + struct scmi_telemetry_group *grp =3D is_group ? obj : NULL; + unsigned int obj_id =3D !is_group ? de->info->id : grp->info->grp_id; + struct scmi_xfer *t; + int ret; + + if (!enabled_state || !tstamp_enabled_state) + return -EINVAL; + + /* Is anything to do at all on this DE ? */ + if (!is_group && (!enable || *enable =3D=3D *enabled_state) && + (!tstamp || *tstamp =3D=3D *tstamp_enabled_state)) + return 0; + + /* + * DE is currently disabled AND no enable state change was requested, + * while timestamp is being changed: update only local state...no need + * to send a message. + */ + if (!is_group && !enable && !*enabled_state) { + if (de->tstamp_support) + scmi_telemetry_de_state_update(ti, ENA_TSTAMP, + tstamp_enabled_state, + *tstamp); + + return 0; + } + + ret =3D ph->xops->xfer_get_init(ph, TELEMETRY_DE_CONFIGURE, + sizeof(*msg), sizeof(*resp), &t); + if (ret) + return ret; + + msg =3D t->tx.buf; + /* Note that BOTH DE and GROUPS have a first ID field.. */ + msg->id =3D cpu_to_le32(obj_id); + /* Default to disable mode for one DE */ + msg->flags =3D DE_DISABLE_ONE; + msg->flags |=3D cpu_to_le32(FIELD_PREP(GENMASK(3, 3), + is_group ? EVENT_GROUP : EVENT_DE)); + + if ((!enable && *enabled_state) || (enable && *enable)) { + /* Already enabled but tstamp_enabled state changed */ + if (tstamp) { + /* Here, tstamp cannot be NULL too */ + msg->flags |=3D cpu_to_le32(*tstamp ? + DE_ENABLE_WTH_TSTAMP : DE_ENABLE_NO_TSTAMP); + } else { + msg->flags |=3D cpu_to_le32(*tstamp_enabled_state ? + DE_ENABLE_WTH_TSTAMP : DE_ENABLE_NO_TSTAMP); + } + } + + resp =3D t->rx.buf; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + ret =3D scmi_telemetry_state_set_resp_process(ti, obj, resp, is_group); + if (!ret) { + /* Update cached state on success */ + if (enable) { + if (!is_group) + scmi_telemetry_de_state_update(ti, ENA_STATE, + enabled_state, + *enable); + else + *enabled_state =3D *enable; + } + if (tstamp) { + if (!is_group) { + if (de->tstamp_support) + scmi_telemetry_de_state_update(ti, ENA_TSTAMP, + tstamp_enabled_state, + *tstamp); + } else { + *tstamp_enabled_state =3D *tstamp; + } + } + + if (is_group) + scmi_telemetry_group_state_update(ti, grp, enable, + tstamp); + } + } + + 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) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + struct scmi_telemetry_de *de; + + if (!enabled || !tstamp_enabled) + return -EINVAL; + + if (!id) { + /* Returning the all_des_* state */ + *enabled =3D + (atomic_read(&ti->des_enabled[ENA_STATE]) =3D=3D ti->info.base.num_des); + *tstamp_enabled =3D + (atomic_read(&ti->des_enabled[ENA_TSTAMP]) =3D=3D ti->num_des_tstamp); + + return 0; + } + + de =3D xa_load(&ti->xa_des, *id); + if (!de) + return -ENODEV; + + *enabled =3D de->enabled; + *tstamp_enabled =3D de->tstamp_enabled; + + 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) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + bool *enabled_state, *tstamp_enabled_state; + struct scmi_telemetry_res_info *rinfo; + void *obj; + + rinfo =3D ti->res_get(ti); + if (!is_group) { + struct scmi_telemetry_de *de; + + de =3D xa_load(&ti->xa_des, id); + if (!de) + return -ENODEV; + + enabled_state =3D &de->enabled; + tstamp_enabled_state =3D &de->tstamp_enabled; + obj =3D de; + } else { + struct scmi_telemetry_group *grp; + + if (id >=3D ti->info.base.num_groups) + return -EINVAL; + + grp =3D &rinfo->grps[id]; + + enabled_state =3D &grp->enabled; + tstamp_enabled_state =3D &grp->tstamp_enabled; + obj =3D grp; + } + + return __scmi_telemetry_state_set(ph, is_group, enable, enabled_state, + tstamp, tstamp_enabled_state, obj); +} + +static int scmi_telemetry_all_disable(const struct scmi_protocol_handle *p= h, + bool is_group) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + struct scmi_msg_telemetry_de_configure *msg; + struct scmi_telemetry_res_info *rinfo; + struct scmi_xfer *t; + int ret; + + rinfo =3D ti->res_get(ti); + ret =3D ph->xops->xfer_get_init(ph, TELEMETRY_DE_CONFIGURE, + sizeof(*msg), 0, &t); + if (ret) + return ret; + + msg =3D t->tx.buf; + msg->flags =3D cpu_to_le32(DE_DISABLE_ALL); + if (is_group) + msg->flags |=3D cpu_to_le32(GROUP_SELECTOR); + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + for (int i =3D 0; i < rinfo->num_des; i++) + scmi_telemetry_de_state_update(ti, ENA_STATE, + &rinfo->des[i]->enabled, + false); + if (is_group) { + for (int i =3D 0; i < ti->info.base.num_groups; i++) { + rinfo->grps[i].enabled =3D false; + scmi_telemetry_group_unlink(ti, &rinfo->grps[i]); + } + } + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + +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) +{ + bool tlm_enable, is_group =3D res_id !=3D SCMI_TLM_GRP_INVALID; + enum scmi_telemetry_collection *current_mode, next_mode; + struct telemetry_info *ti =3D ph->get_priv(ph); + struct scmi_msg_telemetry_config_set *msg; + unsigned int *active_update_interval; + struct scmi_xfer *t; + __le32 interval; + u32 control; + int ret; + + if (mode && *mode =3D=3D SCMI_TLM_NOTIFICATION && + !ti->info.continuos_update_support) + return -EINVAL; + + if (is_group && res_id >=3D ti->info.base.num_groups) + return -EINVAL; + + if (!is_group) { + active_update_interval =3D &ti->info.active_update_interval; + current_mode =3D &ti->info.current_mode; + } else { + struct scmi_telemetry_res_info *rinfo; + + rinfo =3D ti->res_get(ti); + active_update_interval =3D + &rinfo->grps[res_id].active_update_interval; + current_mode =3D &rinfo->grps[res_id].current_mode; + } + + if (!enable && !update_interval_ms && (!mode || *mode =3D=3D *current_mod= e)) + return 0; + + ret =3D ph->xops->xfer_get_init(ph, TELEMETRY_CONFIG_SET, + sizeof(*msg), 0, &t); + if (ret) + return ret; + + if (!update_interval_ms) + interval =3D cpu_to_le32(*active_update_interval); + else + interval =3D cpu_to_le32(*update_interval_ms); + + tlm_enable =3D enable ? *enable : ti->info.enabled; + next_mode =3D mode ? *mode : *current_mode; + + msg =3D t->tx.buf; + msg->grp_id =3D cpu_to_le32(res_id); + control =3D tlm_enable ? TELEMETRY_ENABLE : 0; + control |=3D !is_group ? TELEMETRY_SET_SELECTOR_ALL : TELEMETRY_SET_SELEC= TOR_GROUP; + control |=3D TELEMETRY_MODE_SET(next_mode); + msg->control =3D cpu_to_le32(control); + msg->sampling_rate =3D interval; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + ti->info.enabled =3D tlm_enable; + *current_mode =3D next_mode; + ti->info.notif_enabled =3D *current_mode =3D=3D SCMI_TLM_NOTIFICATION; + if (update_interval_ms) + *active_update_interval =3D le32_to_cpu(interval); + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, .res_get =3D scmi_telemetry_resources_get, + .state_get =3D scmi_telemetry_state_get, + .state_set =3D scmi_telemetry_state_set, + .all_disable =3D scmi_telemetry_all_disable, + .collection_configure =3D scmi_telemetry_collection_configure, }; =20 /** diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index a03921e9a698..38d44ba8c097 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -992,6 +992,13 @@ 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 tim= estamps. + * @all_disable: disable ALL DEs or GROUPs. + * @collection_configure: choose a sampling rate and enable SHMTI/FC sampl= ing + * 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) @@ -1000,6 +1007,16 @@ 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); + int (*state_set)(const struct scmi_protocol_handle *ph, + bool is_group, u32 id, const bool *enable, + const bool *tstamp); + int (*all_disable)(const struct scmi_protocol_handle *ph, bool group); + 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); }; =20 /** --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 226BD44210B; Fri, 24 Jul 2026 14:46:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904389; cv=none; b=ahnQ23ApnG0Fhw2eF9noBep3aHhtOorAPdoHhF2t6C4ND14/dbU5FhRFKGGN//UkKwbKWoneo4JOKCLg0Y22zJR8kkkw4+hhq7hUTr1BbgBAQrap2eNah3aLv4TywQ9Npf/LZk2rsqMJdouWp/8wV1wKgRHwZ4Kbx90OSOoJuzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904389; c=relaxed/simple; bh=3rXIrYmxkT67inr0OR/misyHxGbNsAgTqaVS+0rfyUg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PPV/fNF/vwybfvk2SDq0RIvieZFHes/B8jfqlrDEiEDwMQ6DMPvILjT78M2uJB450qeJT57v4Kzq15rjdZo7RbfiMARjNpkcoifGgPpA7/JYpmmLKzQZb3oYeJL38BxKrYimHKa7X0x6A/1nNo/eAWJWsonKDpqwOc16nuz4kUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=RMpVRlU+; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="RMpVRlU+" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 745391477; Fri, 24 Jul 2026 07:46:14 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0801D3F66F; Fri, 24 Jul 2026 07:46:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904378; bh=3rXIrYmxkT67inr0OR/misyHxGbNsAgTqaVS+0rfyUg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RMpVRlU+2u2R4MB79PK2nJJ2zi+5mHQtTyFssbcxjlJgV60wkLmdm+B4gzQYXhZE+ H3RhvdIlPmtw28/3hy79jRZwtZhNUXCZ/y1E8X0taELfqHxKEcspMrBy1e8a+ksJYz sB8wk+hRViRls1V1OPo53SYZ0ycrQ/Bu6o0/ajmU= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 09/22] firmware: arm_scmi: Add Telemetry DataEvent read capabilities Date: Fri, 24 Jul 2026 15:44:17 +0100 Message-ID: <20260724144430.1440170-10-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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%40= arm.com?part=3D10 - 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 | 548 +++++++++++++++++++++++--- include/linux/scmi_protocol.h | 24 ++ 2 files changed, 519 insertions(+), 53 deletions(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index a40d031e591d..d1704c4e028e 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -265,6 +265,13 @@ struct uuid_line { __be32 dwords[SCMI_TLM_DE_IMPL_MAX_DWORDS]; }; =20 +#define LINE_DATA_GET_RAW(f) \ +({ \ + typeof(f) _f =3D (f); \ + \ + (TO_CPU_64(_f->data_high, _f->data_low)); \ +}) + #define LINE_DATA_GET(f) \ ({ \ typeof(f) _f =3D (f); \ @@ -272,11 +279,18 @@ struct uuid_line { (TO_CPU_64(_I(&_f->data_high), _I(&_f->data_low))); \ }) =20 +#define LINE_TSTAMP_GET_RAW(f) \ +({ \ + typeof(f) _f =3D (f); \ + \ + (TO_CPU_64(_f->ts_high, _f->ts_low)); \ +}) + #define LINE_TSTAMP_GET(f) \ ({ \ typeof(f) _f =3D (f); \ \ - (TO_CPU_64(_I(&_f->ts_high), _I(&_f->ts_low))); \ + (TO_CPU_64(_I(&_f->ts_high), _I(&_f->ts_low))); \ }) =20 #define BLK_TS_STAMP(f) LINE_TSTAMP_GET(f) @@ -290,32 +304,38 @@ enum tdcf_line_types { =20 struct payload { __le32 meta; -#define LINE_TYPE(x) (FIELD_GET(GENMASK(7, 4), _I(&((x)->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) =3D=3D TDCF_DATA_LINE) #define IS_BLK_TS_LINE(x) (LINE_TYPE(x) =3D=3D TDCF_BLK_TS_LINE) #define IS_UUID_LINE(x) (LINE_TYPE(x) =3D=3D TDCF_UUID_LINE) -#define USE_BLK_TS(x) (_I(&((x)->meta)) & BIT(3)) -#define HAS_LINE_EXT(x) (_I(&((x)->meta)) & BIT(2)) -#define LINE_TS_VALID(x) (_I(&((x)->meta)) & BIT(1)) -#define DATA_INVALID(x) (_I(&((x)->meta)) & BIT(0)) -#define BLK_TS_INVALID(p) \ -({ \ - typeof(p) _p =3D (p); \ - bool invalid; \ - \ - invalid =3D LINE_TS_VALID(_p) || HAS_LINE_EXT(_p) || \ - USE_BLK_TS(_p) || DATA_INVALID(_p); \ - invalid; \ +#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(p) \ +({ \ + u32 _m =3D PAYLD_META(p); \ + bool invalid; \ + \ + invalid =3D LINE_TS_VALID_RAW(_m) || HAS_LINE_EXT_RAW(_m) || \ + USE_BLK_TS_RAW(_m) || DATA_INVALID_RAW(_m); \ + invalid; \ }) =20 -#define UUID_INVALID(p) \ -({ \ - typeof(p) _p =3D (p); \ - bool invalid; \ - \ - invalid =3D LINE_TS_VALID(_p) || USE_BLK_TS(_p) || \ - DATA_INVALID(_p) || !HAS_LINE_EXT(_p); \ - invalid; \ +#define UUID_INVALID(p) \ +({ \ + u32 _m =3D PAYLD_META(p); \ + bool invalid; \ + \ + invalid =3D LINE_TS_VALID_RAW(_m) || USE_BLK_TS_RAW(_m) || \ + DATA_INVALID_RAW(_m) || !HAS_LINE_EXT_RAW(_m); \ + invalid; \ }) __le32 id; union { @@ -333,8 +353,10 @@ struct payload { #define EXT_LINE_DATA_PAYLD_WORDS \ ((sizeof(u32) + sizeof(u32) + sizeof(struct tsline)) / sizeof(u32)) =20 -#define LINE_LENGTH_WORDS(x) \ - (HAS_LINE_EXT((x)) ? EXT_LINE_DATA_PAYLD_WORDS : LINE_DATA_PAYLD_WORDS) +#define LINE_LENGTH_WORDS_RAW(m) \ + (HAS_LINE_EXT_RAW((m)) ? EXT_LINE_DATA_PAYLD_WORDS : LINE_DATA_PAYLD_WORD= S) + +#define LINE_LENGTH_WORDS(x) LINE_LENGTH_WORDS_RAW(PAYLD_META(x)) =20 #define LINE_LENGTH_QWORDS(x) ((LINE_LENGTH_WORDS(x)) / 2) #define LINE_LENGTH_BYTES(x) ((LINE_LENGTH_WORDS(x)) * sizeof(u32)) @@ -598,16 +620,34 @@ scmi_telemetry_tde_cache_unchanged(struct telemetry_d= e *tde, u32 magic) } =20 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) { guard(mutex)(&tde->mtx); =20 tde->last_magic =3D magic ? *magic : TDCF_BAD_END_SEQ; - tde->last_val =3D val; - tde->last_ts =3D tstamp && scmi_tde_has_tstamp(tde) ? *tstamp : 0; - if (tstamp) - *tstamp =3D tde->last_ts; + tde->last_val =3D sample->val; + tde->last_ts =3D scmi_tde_has_tstamp(tde) ? sample->tstamp : 0; + /* Update returned value too */ + sample->tstamp =3D tde->last_ts; +} + +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 !=3D *magic) + return -EAGAIN; + + sample->id =3D tde->de.info->id; + sample->val =3D tde->last_val; + sample->tstamp =3D tde->last_ts; + + return 0; } =20 struct scmi_tlm_de_priv { @@ -1543,31 +1583,31 @@ scmi_telemetry_tde_allocate(struct telemetry_info *= ti, u32 de_id, } =20 static inline void -scmi_telemetry_line_data_parse(struct telemetry_de *tde, u64 *val, u64 *ts= tamp, +scmi_telemetry_line_data_parse(struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample, struct payload __iomem *payld, u32 magic) { /* Data is always valid since we are NOT handling BLK TS lines here */ - *val =3D LINE_DATA_GET(&payld->l); - if (tstamp) { - if (USE_BLK_TS(payld)) { - /* Read out the actual BLK_TS */ - *tstamp =3D scmi_telemetry_blkts_read(magic, tde->bts); - } else if (LINE_TS_VALID(payld)) { - /* - * Note that LINE_TS_VALID implies HAS_LINE_EXT and that - * the per DE line_ts_rate is advertised in the DE - * descriptor. - */ - *tstamp =3D LINE_TSTAMP_GET(&payld->tsl); - } else { - *tstamp =3D 0; - } + sample->val =3D LINE_DATA_GET(&payld->l); + if (USE_BLK_TS(payld)) { + /* Read out the actual BLK_TS */ + sample->tstamp =3D scmi_telemetry_blkts_read(magic, tde->bts); + } else if (LINE_TS_VALID(payld)) { + /* + * Note that LINE_TS_VALID implies HAS_LINE_EXT and that + * the per DE line_ts_rate is advertised in the DE + * descriptor. + */ + sample->tstamp =3D LINE_TSTAMP_GET(&payld->tsl); + } else { + sample->tstamp =3D 0; } =20 - 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"); =20 - scmi_telemetry_tde_cache_update(tde, *val, tstamp, &magic); + scmi_telemetry_tde_cache_update(tde, sample, &magic); } =20 static inline void scmi_telemetry_bts_link(struct telemetry_de *tde, @@ -1640,9 +1680,9 @@ static void scmi_telemetry_tdcf_data_parse(struct tel= emetry_info *ti, enum scan_mode mode, void *active_bts, void *active_uuid) { + struct scmi_telemetry_de_sample sample =3D {}; bool use_blk_ts =3D USE_BLK_TS(payld); struct telemetry_de *tde; - u64 val, tstamp =3D 0; u32 de_id; =20 de_id =3D PAYLD_ID(payld); @@ -1689,9 +1729,8 @@ static void scmi_telemetry_tdcf_data_parse(struct tel= emetry_info *ti, if (active_uuid) scmi_telemetry_uuid_link(tde, active_uuid); =20 - /* Parse data words */ - scmi_telemetry_line_data_parse(tde, &val, &tstamp, payld, - shmti->last_magic); + /* Parse data words ... only for the sake of updating TDE cache */ + scmi_telemetry_line_data_parse(tde, &sample, payld, shmti->last_magic); } =20 static int scmi_telemetry_tdcf_line_parse(struct telemetry_info *ti, @@ -2206,6 +2245,406 @@ scmi_telemetry_collection_configure(const struct sc= mi_protocol_handle *ph, return ret; } =20 +static inline void +scmi_telemetry_de_data_fc_read(struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample) +{ + struct fc_tsline __iomem *fc =3D tde->base + tde->offset; + + sample->val =3D LINE_DATA_GET(fc); + sample->tstamp =3D LINE_TSTAMP_GET(fc); + + /* 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 =3D 0; id < ti->num_shmti; id++) { + int ret; + + ret =3D 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 =3D {}; + + 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] =3D 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] =3D 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] =3D 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] =3D 0101b iff it was a LINE_TSTAMP + * -> BIT[3:0] =3D 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] =3D 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] =3D 0100b + * + the hole from the timestamp remain there unused until + * - you enable again the TS so the hole is used again + * -> BIT[3:0] =3D 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; + + payld =3D 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) !=3D 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. + */ + if (WARN_ON((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) +{ + int retries =3D SCMI_TLM_TDCF_MAX_RETRIES; + struct tdcf __iomem *tdcf =3D tde->base; + u32 startm =3D 0, endm =3D TDCF_BAD_END_SEQ; + + if (!tdcf) + return -ENODEV; + + do { + struct payload __iomem *payld; + int err; + + /* A bit of exponential backoff between retries */ + fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * 1000); + + startm =3D 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 =3D scmi_telemetry_tde_cache_lookup(tde, sample, &startm); + if (!err) + return 0; + + /* Retry also when payld get gave a transient error (-EINVAL) */ + payld =3D scmi_telemetry_tdcf_de_payld_get(tde); + if (!IS_ERR(payld)) + scmi_telemetry_line_data_parse(tde, sample, payld, startm); + else if (PTR_ERR(payld) !=3D -EINVAL) + return PTR_ERR(payld); + + endm =3D TDCF_END_SEQ_GET(tde->eplg); + if (startm !=3D endm) + trace_scmi_tlm_access(tde->de.info->id, "MSEQ_MISMATCH", + startm, endm); + } while (startm !=3D endm && --retries); + + if (startm !=3D endm) { + trace_scmi_tlm_access(tde->de.info->id, "TDCF_DE_FAIL", + startm, endm); + return -EPROTO; + } + + return 0; +} + +static int scmi_telemetry_de_access(struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample) +{ + 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 =3D to_tde(de); + + 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 =3D ph->get_priv(ph); + struct scmi_telemetry_de *de; + + if (!ti->info.enabled || !sample) + return -ENODATA; + + de =3D 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 =3D *num_samples; + *num_samples =3D 0; + + rinfo =3D ti->res_get(ti); + for (int i =3D 0; i < rinfo->num_des; i++) { + struct scmi_telemetry_de *de; + + de =3D rinfo->des[i]; + /* Skip disabled DEs and not belonging to the selected Group */ + if (!de->enabled || + (grp_id !=3D SCMI_TLM_GRP_INVALID && + (!de->grp || de->grp->info->grp_id !=3D grp_id))) + continue; + + /* Must be checked here to skip disabled and non selected */ + if (*num_samples =3D=3D max_samples) + return -ENOSPC; + + /* Note that this cannot fail when passing a NULL magic */ + scmi_telemetry_tde_cache_lookup(to_tde(de), + &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 =3D 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, u32 *dwords, + size_t payld_bytes) +{ + struct scmi_telemetry_res_info *rinfo; + u32 next =3D 0; + + rinfo =3D 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 =3D (struct payload *)&dwords[next]; + struct scmi_telemetry_de *de; + struct telemetry_de *tde; + unsigned int wlen; + u32 de_id; + + wlen =3D LINE_LENGTH_WORDS_RAW(payld->meta); + /* Check Boundary */ + if (next + wlen > payld_bytes / sizeof(u32)) + return; + + next +=3D wlen; + if (DATA_INVALID_RAW(payld->meta)) { + dev_err(ti->ph->dev, "MSG - Received INVALID DATA line\n"); + continue; + } + + de_id =3D le32_to_cpu(payld->id); + de =3D 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 =3D to_tde(de); + guard(mutex)(&tde->mtx); + tde->cached_msg =3D true; + tde->last_val =3D LINE_DATA_GET_RAW(&payld->tsl); + /* TODO BLK_TS in notification payloads */ + tde->last_ts =3D HAS_LINE_EXT_RAW(payld->meta) && + LINE_TS_VALID_RAW(payld->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 =3D 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 =3D grp_id !=3D SCMI_TLM_GRP_INVALID; + if (is_group && grp_id >=3D ti->info.base.num_groups) + return -EINVAL; + + ret =3D ph->xops->xfer_get_init(ph, TELEMETRY_CONFIG_SET, + sizeof(*msg), 0, &t); + if (ret) + return ret; + + msg =3D t->tx.buf; + msg->grp_id =3D cpu_to_le32(grp_id); + control =3D TELEMETRY_ENABLE; + control |=3D !is_group ? TELEMETRY_SET_SELECTOR_ALL : + TELEMETRY_SET_SELECTOR_GROUP; + control |=3D TELEMETRY_MODE_SINGLE; + msg->control =3D cpu_to_le32(control); + msg->sampling_rate =3D 0; + + ret =3D ph->xops->do_xfer_with_response(ph, t); + if (!ret) { + struct scmi_msg_resp_telemetry_reading_complete *r =3D t->rx.buf; + + /* Update cached DEs values from payload */ + if (r->num_dwords) + scmi_telemetry_msg_payld_process(ti, le32_to_cpu(r->num_dwords), + (u32 *)r->dwords, + t->rx.len - sizeof(*r)); + + /* Scan and update SMHTIs and FCs */ + scmi_telemetry_scan_update(ti); + + /* Collect all last cached values */ + ret =3D scmi_telemetry_samples_collect(ti, grp_id, num_samples, + samples); + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, @@ -2214,6 +2653,9 @@ static const struct scmi_telemetry_proto_ops tlm_prot= o_ops =3D { .state_set =3D scmi_telemetry_state_set, .all_disable =3D scmi_telemetry_all_disable, .collection_configure =3D scmi_telemetry_collection_configure, + .de_data_read =3D scmi_telemetry_de_data_read, + .des_bulk_read =3D scmi_telemetry_des_bulk_read, + .des_sample_get =3D scmi_telemetry_des_sample_get, }; =20 /** diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 38d44ba8c097..0e727ad270b4 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -985,6 +985,13 @@ struct scmi_telemetry_info { enum scmi_telemetry_collection current_mode; }; =20 +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 pro= vided * by SCMI Telemetry Protocol @@ -999,6 +1006,15 @@ struct scmi_telemetry_info { * @collection_configure: choose a sampling rate and enable SHMTI/FC sampl= ing * 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 times= tamp: + * 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 ju= st + * 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) @@ -1017,6 +1033,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); }; =20 /** --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 29D4044161E; Fri, 24 Jul 2026 14:46:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904394; cv=none; b=IXXCd/tJ2+cp8coLufv7JgjAz4FOgGDcZoPUsJ7RGiju7I3dVU32i94IW+dVSLTT7iSshDTOCaL/h5Pbm5BKOAqdxqRkxxjxJ7GFPZNUEGPRRmbV2xBYqVBb0sfiT/eFxq5SJhRrfKinEV4NyHgg0y4kySViw8UatgajxWUa/xk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904394; c=relaxed/simple; bh=zPcpi6/+9D6CcYLbEIrxtpYVAuTCKkgDvs02yLr9SlA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oWpGbl9S3sLXf7LRN00EpKbAxIAMiCYlDP8XQyEiPCNdz0QU2208VvQf4rHc6ucTcaEmGQAFW3V/SaW/DcIzd91/MVWKhcJUXtVSrmEzuP0USPHKKPJPGvmi1GQgqiDng8vx+wTprKfW105II22XtvtgnK96qu98P0UzyWABXVw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=L79aav7n; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="L79aav7n" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 87EF81684; Fri, 24 Jul 2026 07:46:22 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0B28E3F66F; Fri, 24 Jul 2026 07:46:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904386; bh=zPcpi6/+9D6CcYLbEIrxtpYVAuTCKkgDvs02yLr9SlA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L79aav7nk1xwcXFf1apBXrINYD2kiiL4ud8jWxu8WN4nhpmPscq6fmebxBlbfoG7J uBZxrWhxo0TofsMsleZJvilI+VIEjmdZqfrvnQX8G7F09dYaDb1G0Ks0S9eYfY0Ybv ehlsg1E8GQ+FN4nQFJzazUujdkdJLDm0qXlEHTg0= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 10/22] firmware: arm_scmi: Add support for Telemetry reset Date: Fri, 24 Jul 2026 15:44:18 +0100 Message-ID: <20260724144430.1440170-11-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for Telemetry operations needed to request platform to reset telemetry current configuration and data events values. Signed-off-by: Cristian Marussi --- 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 | 51 +++++++++++++++++++++++++++ include/linux/scmi_protocol.h | 2 ++ 2 files changed, 53 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index d1704c4e028e..c26fb67603e7 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -2645,6 +2645,56 @@ static int scmi_telemetry_des_sample_get(const struc= t scmi_protocol_handle *ph, return ret; } =20 +static void scmi_telemetry_local_resources_reset(struct telemetry_info *ti) +{ + struct scmi_telemetry_res_info *rinfo; + + ti->info.enabled =3D false; + ti->info.notif_enabled =3D false; + ti->info.current_mode =3D SCMI_TLM_ONDEMAND; + + /* Get rinfo as it is...without triggering an enumeration */ + rinfo =3D __scmi_telemetry_resources_get(ti); + /* Clear all local state...*/ + for (int i =3D 0; i < rinfo->num_des; i++) { + rinfo->des[i]->enabled =3D false; + rinfo->des[i]->tstamp_enabled =3D false; + + scmi_telemetry_de_unlink(rinfo->des[i]); + } + for (int i =3D 0; i < rinfo->num_groups; i++) { + rinfo->grps[i].enabled =3D false; + rinfo->grps[i].tstamp_enabled =3D false; + rinfo->grps[i].current_mode =3D SCMI_TLM_ONDEMAND; + rinfo->grps[i].active_update_interval =3D 0; + } + + atomic_set(&ti->des_enabled[ENA_STATE], 0); + atomic_set(&ti->des_enabled[ENA_TSTAMP], 0); +} + +static int scmi_telemetry_reset(const struct scmi_protocol_handle *ph) +{ + struct scmi_xfer *t; + int ret; + + ret =3D ph->xops->xfer_get_init(ph, TELEMETRY_RESET, sizeof(u32), 0, &t); + if (ret) + return ret; + + put_unaligned_le32(0, t->tx.buf); + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + struct telemetry_info *ti =3D ph->get_priv(ph); + + scmi_telemetry_local_resources_reset(ti); + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, @@ -2656,6 +2706,7 @@ static const struct scmi_telemetry_proto_ops tlm_prot= o_ops =3D { .de_data_read =3D scmi_telemetry_de_data_read, .des_bulk_read =3D scmi_telemetry_des_bulk_read, .des_sample_get =3D scmi_telemetry_des_sample_get, + .reset =3D scmi_telemetry_reset, }; =20 /** diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 0e727ad270b4..6f7172d99d23 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -1015,6 +1015,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) @@ -1041,6 +1042,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); }; =20 /** --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2775D34E777; Fri, 24 Jul 2026 14:46:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904404; cv=none; b=GUnFw+QPpDQo6ELuH6C7YG9tblJ/LZQ5qSdzGubchmjIsFvFxW8uEM2gPGT0Yq4+laV8eAfSzn9BVRQxGb8F7N8dkM+rlINhTXFz/muiwOD80ZJIQqYK7OOxS8ZMtl3Upwg6aPpq2rp2GAE8P3eYvg7QcpZoSlsaJzdfj2RKD+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904404; c=relaxed/simple; bh=adTrlxNbjenUubKHvkEW9cvmtTaHGM96m4SU4DOuP6A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tUc/yKX5FH1vyCkHR4fC0FUjVZMC4gzj7bdDkHjfQhPh8g+Thi4t+VmzMZxT/gOgYWnt9keeoQZj7P6ZstWvT8TIWQeeDdRc0VR6KtiajvLGjsKb+bjpGWYZuFRt2xs+0PrfrpKkdoNtAO6l26gUnsD96IW57SYQv1B83qTXgX8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=vOu72bLQ; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="vOu72bLQ" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 06FB01477; Fri, 24 Jul 2026 07:46:32 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1D3A63F66F; Fri, 24 Jul 2026 07:46:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904396; bh=adTrlxNbjenUubKHvkEW9cvmtTaHGM96m4SU4DOuP6A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vOu72bLQ3GCbQ4yy5d58K4Tde8WKQanqf6st/7P81MH3ait2NVBB+R1BrCThM9Vfo qaUyznUUpdcXeVtO0Sd+vH1jZ25iJwxfUsNJORdpZZrBdYWlN1zDRzzE1enZeHxRZ/ /a77/1HtsN2AHZ4c8B0lUmvpeuTT2yv0klmxD3g8= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 11/22] firmware: arm_scmi: Add Telemetry notification support Date: Fri, 24 Jul 2026 15:44:19 +0100 Message-ID: <20260724144430.1440170-12-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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 | 160 +++++++++++++++++++++++--- include/linux/scmi_protocol.h | 9 ++ 2 files changed, 155 insertions(+), 14 deletions(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index c26fb67603e7..94bc10ccb0cf 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -505,12 +505,16 @@ struct telemetry_info { struct list_head free_des; struct list_head fcs_des; struct scmi_telemetry_info info; + struct notifier_block telemetry_nb; 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); }; =20 +#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); =20 @@ -2551,6 +2555,7 @@ scmi_telemetry_msg_payld_process(struct telemetry_inf= o *ti, =20 while (next < num_dwords) { struct payload *payld =3D (struct payload *)&dwords[next]; + struct scmi_telemetry_de_sample sample =3D {}; struct scmi_telemetry_de *de; struct telemetry_de *tde; unsigned int wlen; @@ -2563,30 +2568,35 @@ scmi_telemetry_msg_payld_process(struct telemetry_i= nfo *ti, =20 next +=3D wlen; if (DATA_INVALID_RAW(payld->meta)) { - dev_err(ti->ph->dev, "MSG - Received INVALID DATA line\n"); + trace_scmi_tlm_access(payld->id, "MSG_INVALID", 0, 0); continue; } =20 de_id =3D le32_to_cpu(payld->id); de =3D 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; } =20 tde =3D to_tde(de); - guard(mutex)(&tde->mtx); - tde->cached_msg =3D true; - tde->last_val =3D LINE_DATA_GET_RAW(&payld->tsl); - /* TODO BLK_TS in notification payloads */ - tde->last_ts =3D HAS_LINE_EXT_RAW(payld->meta) && - LINE_TS_VALID_RAW(payld->meta) ? - LINE_TSTAMP_GET_RAW(&payld->tsl) : 0; + scoped_guard(mutex, &tde->mtx) { + tde->cached_msg =3D true; + tde->last_val =3D LINE_DATA_GET_RAW(&payld->tsl); + /* TODO BLK_TS in notification payloads */ + tde->last_ts =3D HAS_LINE_EXT_RAW(payld->meta) && + LINE_TS_VALID_RAW(payld->meta) ? + LINE_TSTAMP_GET_RAW(&payld->tsl) : 0; + sample.val =3D tde->last_val; + sample.tstamp =3D tde->last_ts; + } + + /* Trace originally read tstamp */ + trace_scmi_tlm_collect(sample.tstamp, de->info->id, sample.val, + "MESSAGE"); =20 - trace_scmi_tlm_collect(tde->last_ts, tde->de.info->id, - tde->last_val, "MESSAGE"); + scmi_telemetry_tde_cache_update(tde, &sample, NULL); } } =20 @@ -2709,6 +2719,107 @@ static const struct scmi_telemetry_proto_ops tlm_pr= oto_ops =3D { .reset =3D scmi_telemetry_reset, }; =20 +static bool +scmi_telemetry_notify_supported(const struct scmi_protocol_handle *ph, + u8 evt_id, u32 src_id) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + + return ti->info.continuos_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 =3D payld; + struct scmi_telemetry_update_report *r =3D report; + + /* At least sized as an empty notification */ + if (payld_sz < sizeof(*p)) + return NULL; + + r->timestamp =3D timestamp; + r->agent_id =3D le32_to_cpu(p->agent_id); + r->status =3D le32_to_cpu(p->status); + r->num_dwords =3D le32_to_cpu(p->num_dwords); + + if (r->num_dwords * sizeof(r->dwords[0]) > 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 =3D 0; + + return r; +} + +static const struct scmi_event tlm_events[] =3D { + { + .id =3D SCMI_EVENT_TELEMETRY_UPDATE, + .max_payld_sz =3D 0, + .max_report_sz =3D 0, + }, +}; + +static const struct scmi_event_ops tlm_event_ops =3D { + .is_notify_supported =3D scmi_telemetry_notify_supported, + .set_notify_enabled =3D scmi_telemetry_set_notify_enabled, + .fill_custom_report =3D scmi_telemetry_fill_custom_report, +}; + +static const struct scmi_protocol_events tlm_protocol_events =3D { + .queue_sz =3D SCMI_PROTO_QUEUE_SZ, + .ops =3D &tlm_event_ops, + .evts =3D tlm_events, + .num_events =3D ARRAY_SIZE(tlm_events), + .num_sources =3D 1, +}; + +static int scmi_telemetry_notifier(struct notifier_block *nb, + unsigned long event, void *data) +{ + struct scmi_telemetry_update_report *er =3D data; + struct telemetry_info *ti =3D telemetry_nb_to_info(nb); + + if (er->status) { + trace_scmi_tlm_access(0, "BAD_NOTIF_MSG", 0, 0); + return NOTIFY_DONE; + } + + 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 =3D sizeof(er->dwords[0]) * er->num_dwords; + + /* The report payload size was bound checked on rx */ + scmi_telemetry_msg_payld_process(ti, er->num_dwords, er->dwords, + payld_bytes); + } + + /* ...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 @@ -2971,7 +3082,27 @@ static int scmi_telemetry_protocol_init(const struct= scmi_protocol_handle *ph) =20 ti->info.base.version =3D ph->version; =20 - return ph->set_priv(ph, ti); + ret =3D 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.continuos_update_support) { + ti->telemetry_nb.notifier_call =3D &scmi_telemetry_notifier; + ret =3D 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; } =20 static const struct scmi_protocol scmi_telemetry =3D { @@ -2979,6 +3110,7 @@ static const struct scmi_protocol scmi_telemetry =3D { .owner =3D THIS_MODULE, .instance_init =3D &scmi_telemetry_protocol_init, .ops =3D &tlm_proto_ops, + .events =3D &tlm_protocol_events, .supported_version =3D SCMI_PROTOCOL_SUPPORTED_VERSION, }; =20 diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 6f7172d99d23..10d098f73d05 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -1250,6 +1250,7 @@ enum scmi_notification_events { SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER =3D 0x0, SCMI_EVENT_POWERCAP_CAP_CHANGED =3D 0x0, SCMI_EVENT_POWERCAP_MEASUREMENTS_CHANGED =3D 0x1, + SCMI_EVENT_TELEMETRY_UPDATE =3D 0x0, }; =20 struct scmi_power_state_changed_report { @@ -1337,4 +1338,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 */ --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BBF5844160D; Fri, 24 Jul 2026 14:46:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904412; cv=none; b=oqV6yHZolJRwgm6AXkjZMD5BhxgrMkrvu2WwC+GGbGirDuqaB15rc1xMiWMaB7I6R5mcKB5CyhFDanVhaA0GipS4NhMiglzHhLJP/bwAfP6D5Mp1tryk85IPjEP7UmdLFZG+Olv+X+L2OwRtgR9sPtrWRMHeWBj1kqyCpyne/wU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904412; c=relaxed/simple; bh=RnxJ55T99mr9WXsejnZDMLVqjN3WiIFhnm5e7yk3JvA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tcicGVZY8mAkFNt8APijqY5+WsSnXdlHPEgoTsiirzP7g604EJ2tovWRMxi5rP3rOUzDADe08Gnd5JOXEW99ecbp3ngLqoUAvV5X3wVB4KNj52KWJ4PzH9PvTnthKuAQJ8ghHxc8wusQYNR7OtDP1KzxsPkJsxTA9arDaF/OZOY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=d+kuU9FS; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="d+kuU9FS" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A162D1477; Fri, 24 Jul 2026 07:46:40 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A2E953F66F; Fri, 24 Jul 2026 07:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904404; bh=RnxJ55T99mr9WXsejnZDMLVqjN3WiIFhnm5e7yk3JvA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d+kuU9FS+r7fKCBes2jLRpU+I42u31bWk1OV3BKYT5F+W2zXdG3RUckdbzMyqVSXW T1RG2goMltm4cEjJEbX0y01p5pxiEPc7cZXYfu0SjFTZ/MEmKLs1d+E5NxrgO+BteZ u11ql9H8vguHqu17AAFYepcjzrTDlDlh1w9VccpQ= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 12/22] firmware: arm_scmi: Add support for boot-on Telemetry Date: Fri, 24 Jul 2026 15:44:20 +0100 Message-ID: <20260724144430.1440170-13-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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 | 206 ++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 94bc10ccb0cf..c2945d001ae2 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -876,6 +876,198 @@ static int iter_de_descr_process_response(const struc= t scmi_protocol_handle *ph, return ret; } =20 +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 =3D 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 =3D ph->xops->xfer_get_init(ph, TELEMETRY_CONFIG_GET, + sizeof(*msg), sizeof(*resp), &t); + if (ret) + return ret; + + msg =3D t->tx.buf; + msg->grp_id =3D cpu_to_le32(grp_id); + flags =3D grp_id =3D=3D SCMI_TLM_GRP_INVALID ? + TELEMETRY_GET_SELECTOR_ORPHANS : TELEMETRY_GET_SELECTOR_GROUP; + msg->flags =3D cpu_to_le32(flags); + + resp =3D t->rx.buf; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + *enabled =3D le32_get_bits(resp->control, TELEMETRY_ENABLE); + *active_update_interval =3D + 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 =3D message; + + msg->index =3D cpu_to_le32(desc_index); + msg->flags =3D 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 =3D response; + + st->num_returned =3D le32_get_bits(r->flags, GENMASK(15, 0)); + st->num_remaining =3D le32_get_bits(r->flags, GENMASK(31, 16)); + + if (st->rx_len < (sizeof(*r) + sizeof(r->entry[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 =3D 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 =3D response; + const struct scmi_enabled_de_desc *desc; + struct telemetry_info *ti =3D priv; + struct telemetry_de *tde; + u32 de_id; + int ret; + + desc =3D &r->entry[st->loop_idx]; + de_id =3D 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 =3D scmi_telemetry_tde_get(ti, de_id); + if (IS_ERR(tde)) + return PTR_ERR(tde); + + tde->de.info->id =3D de_id; + tde->de.enabled =3D true; + tde->de.tstamp_enabled =3D le32_to_cpu(desc->mode) =3D=3D DE_ENABLED_WITH= _TSTAMP; + + ret =3D 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) +{ + struct scmi_telemetry_res_info *rinfo =3D ti->rinfo; + const struct scmi_protocol_handle *ph =3D ti->ph; + struct scmi_iterator_ops ops =3D { + .prepare_message =3D iter_enabled_list_prepare_message, + .update_state =3D iter_enabled_list_update_state, + .process_response =3D iter_enabled_list_process_response, + }; + void *iter; + int ret; + + iter =3D 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 =3D ph->hops->iter_response_run(iter); + if (ret) + return ret; + + 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 =3D ti->ph->dev; + int ret; + + ret =3D 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 =3D 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 =3D 0; id < ti->num_shmti; id++) { + ret =3D scmi_telemetry_shmti_scan(ti, id, SCAN_DISCOVERY); + 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 *t= i) { @@ -940,6 +1132,9 @@ scmi_telemetry_de_groups_init(struct device *dev, stru= ct telemetry_info *ti) } } =20 + for (int i =3D 0; i < ti->info.base.num_groups; i++) + scmi_telemetry_group_config_lookup(ti, &rinfo->grps[i]); + rinfo->num_groups =3D ti->info.base.num_groups; =20 return 0; @@ -2698,6 +2893,11 @@ static int scmi_telemetry_reset(const struct scmi_pr= otocol_handle *ph) struct telemetry_info *ti =3D ph->get_priv(ph); =20 scmi_telemetry_local_resources_reset(ti); + /* Fetch again the states from platform. */ + ret =3D scmi_telemetry_initial_state_lookup(ti); + if (ret) + dev_warn(ph->dev, + FW_BUG "Cannot retrieve initial state after reset.\n"); } =20 ph->xops->xfer_put(ph, t); @@ -3080,6 +3280,12 @@ static int scmi_telemetry_protocol_init(const struct= scmi_protocol_handle *ph) return ret; } =20 + ret =3D 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 =3D ph->version; =20 ret =3D ph->set_priv(ph, ti); --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8465531E846; Fri, 24 Jul 2026 14:46:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904416; cv=none; b=Aqyiy9N91WASvT50rxr1QLhuIduxVsLc6RBdCn73SKn7boJw2nsdc4CYu+eu1ejOBpqCWP7L4VMeOrwV2VNObQZcgwGE3S9aDFp5P/7ymQEca4Y4n5cvSyo5YbYRG3LjgOK89fFs+jjfEOyuPBElpPUogD010023p3x5hZtdnd4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904416; c=relaxed/simple; bh=NuESCRU5An7rfbwVrEPz6SRUS//AGdj1RuLSFxy//nI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FdG/09sdjEEnFoPHygth6ZIGT1VMjJVsaqmCYlfWgm62Zz5fGfNdJe1oNXLFM3uuo78XhXqakUooqfJDG/9Q4kloQXY9u2H781m+Ue2T17+YG2pxHiE/6j4pNviqlsfvvtML+/kQ0gy65K9R74fok/JgcRQ0XhCqE/woybFNftI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=I+TTAE4t; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="I+TTAE4t" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E781F1477; Fri, 24 Jul 2026 07:46:48 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3659B3F66F; Fri, 24 Jul 2026 07:46:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904412; bh=NuESCRU5An7rfbwVrEPz6SRUS//AGdj1RuLSFxy//nI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I+TTAE4tTU0nCelhPUdDOmVHXNji8E/KjUVCo3ezqMTgt4qx+5CO5FFj1fKV63yeD d6MGAYZ1GwQ7yUrZWLVscJerNZl20b5kAX/xtp1ajli3z8lZSgNr9ZuUWQ/iAxhyNQ Ft42y97ZqSHmLkr0HA199a0oWVNtKPu9WGPL6Yw0= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 13/22] firmware: arm_scmi: Add Telemetry generation counter Date: Fri, 24 Jul 2026 15:44:21 +0100 Message-ID: <20260724144430.1440170-14-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a per-instance generation counter to track configuration changes and expose a telemetry operations to get a related waitqueue for monitoring. Signed-off-by: Cristian Marussi --- v5 --> v6 - add relese semantic to the atomic set of the generation counter --- drivers/firmware/arm_scmi/telemetry.c | 53 +++++++++++++++++++++++++++ include/linux/scmi_protocol.h | 5 +++ 2 files changed, 58 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index c2945d001ae2..5f1c1abfbdc8 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -21,6 +21,7 @@ #include #include #include +#include =20 #include "protocols.h" #include "notify.h" @@ -30,6 +31,7 @@ /* Updated only after ALL the mandatory features for that version are merg= ed */ #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x10000 =20 +#define SCMI_TLM_GENERATION_ONE (SCMI_TLM_GENERATION_INVALID + 1U) #define SCMI_TLM_TDCF_MAX_RETRIES 5 =20 enum scmi_telemetry_protocol_cmd { @@ -505,6 +507,7 @@ struct telemetry_info { struct list_head free_des; struct list_head fcs_des; struct scmi_telemetry_info info; + struct wait_queue_head gen_wq; struct notifier_block telemetry_nb; atomic_t rinfo_initializing; struct completion rinfo_initdone; @@ -654,6 +657,29 @@ scmi_telemetry_tde_cache_lookup(struct telemetry_de *t= de, return 0; } =20 +static inline void __scmi_telemetry_generation_set(struct telemetry_info *= ti, + unsigned int new) +{ + atomic_set_release(&ti->info.generation, new); + + wake_up_all(&ti->gen_wq); +} + +static inline void scmi_telemetry_generation_update(struct telemetry_info = *ti) +{ + unsigned int next; + + /* Wrap around skipping invalid generation 0 */ + next =3D (atomic_read(&ti->info.generation) + 1) ?: SCMI_TLM_GENERATION_O= NE; + + __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; @@ -2204,6 +2230,8 @@ static int __scmi_telemetry_state_set(const struct sc= mi_protocol_handle *ph, tstamp_enabled_state, *tstamp); =20 + /* A local change can have an impact anyway */ + scmi_telemetry_generation_update(ti); return 0; } =20 @@ -2265,6 +2293,9 @@ static int __scmi_telemetry_state_set(const struct sc= mi_protocol_handle *ph, =20 ph->xops->xfer_put(ph, t); =20 + if (!ret) + scmi_telemetry_generation_update(ti); + return ret; } =20 @@ -2369,6 +2400,9 @@ static int scmi_telemetry_all_disable(const struct sc= mi_protocol_handle *ph, =20 ph->xops->xfer_put(ph, t); =20 + if (!ret) + scmi_telemetry_generation_update(ti); + return ret; } =20 @@ -2441,6 +2475,9 @@ scmi_telemetry_collection_configure(const struct scmi= _protocol_handle *ph, =20 ph->xops->xfer_put(ph, t); =20 + if (!ret) + scmi_telemetry_generation_update(ti); + return ret; } =20 @@ -2893,6 +2930,10 @@ static int scmi_telemetry_reset(const struct scmi_pr= otocol_handle *ph) struct telemetry_info *ti =3D ph->get_priv(ph); =20 scmi_telemetry_local_resources_reset(ti); + + /* Reset generation now that server has been reset */ + scmi_telemetry_generation_reset(ti); + /* Fetch again the states from platform. */ ret =3D scmi_telemetry_initial_state_lookup(ti); if (ret) @@ -2905,6 +2946,14 @@ static int scmi_telemetry_reset(const struct scmi_pr= otocol_handle *ph) return ret; } =20 +static struct wait_queue_head * +scmi_telemetry_event_wq_get(const struct scmi_protocol_handle *ph) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + + return &ti->gen_wq; +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, @@ -2917,6 +2966,7 @@ static const struct scmi_telemetry_proto_ops tlm_prot= o_ops =3D { .des_bulk_read =3D scmi_telemetry_des_bulk_read, .des_sample_get =3D scmi_telemetry_des_sample_get, .reset =3D scmi_telemetry_reset, + .event_wq_get =3D scmi_telemetry_event_wq_get, }; =20 static bool @@ -3230,6 +3280,9 @@ static int scmi_telemetry_instance_init(struct teleme= try_info *ti) if (ret) return ret; =20 + /* Generation counter init */ + atomic_set(&ti->info.generation, SCMI_TLM_GENERATION_ONE); + init_waitqueue_head(&ti->gen_wq); atomic_set(&ti->des_enabled[ENA_STATE], 0); atomic_set(&ti->des_enabled[ENA_TSTAMP], 0); /* Setup resources lazy initialization */ diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 10d098f73d05..5b1d6eede287 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -13,6 +13,7 @@ #include #include #include +#include =20 #define SCMI_MAX_STR_SIZE 64 #define SCMI_SHORT_NAME_MAX_SIZE 16 @@ -889,6 +890,7 @@ enum scmi_telemetry_collection { SCMI_TLM_SINGLE_READ, }; =20 +#define SCMI_TLM_GENERATION_INVALID 0U #define SCMI_TLM_GRP_INVALID 0xFFFFFFFF #define SCMI_TLM_DE_IMPL_MAX_DWORDS 4 =20 @@ -983,6 +985,7 @@ struct scmi_telemetry_info { bool enabled; bool notif_enabled; enum scmi_telemetry_collection current_mode; + atomic_t generation; }; =20 struct scmi_telemetry_de_sample { @@ -1016,6 +1019,7 @@ struct scmi_telemetry_de_sample { * This causes an immediate update platform-side of all the * enabled DEs. * @reset: reset configuration and telemetry data. + * @event_wq_get: get a reference to the event waitqueue for this instance. */ struct scmi_telemetry_proto_ops { const struct scmi_telemetry_info __must_check *(*info_get) @@ -1043,6 +1047,7 @@ struct scmi_telemetry_proto_ops { int grp_id, int *num_samples, struct scmi_telemetry_de_sample *samples); int (*reset)(const struct scmi_protocol_handle *ph); + struct wait_queue_head *(*event_wq_get)(const struct scmi_protocol_handle= *ph); }; =20 /** --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B7C032AABD; Fri, 24 Jul 2026 14:47:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904424; cv=none; b=iFghtnKlfLm72P70TXIhYpgBgGdf8xWoDUbgLsyzIXJd3yv/S0tK7APR0hVnv/bPckWLZCOefo4NDmeeO49B0gAE5zln/rliFz+tJNKz5Zs5U5pTw3oS8R+paZsxSbzR7FipnBbBe8ZMTKYXl/S3V3QZHdZ9XEQ74tHiQSEysGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904424; c=relaxed/simple; bh=MtMui1/C+LzbgsLio7B/p/5PShR97yA+5VPZG+67X4Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lpqbEl1oUaxOpyyMAcCP1hRAc/n8g58b4N+7T8eSmAYc4rVkARN/WflJnYkSfV1M+Ntu/mRNkzwCLrWtZrwljKX/9IzebMqum0Tg56w2HeFr3NFrirWC98K3hWg3C91aZ+HnSDPkfghSU6K9zgp8mlav7skEBkXToH30pIJg8vY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=snCrgNaq; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="snCrgNaq" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4AFB81477; Fri, 24 Jul 2026 07:46:57 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 855D13F66F; Fri, 24 Jul 2026 07:46:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904421; bh=MtMui1/C+LzbgsLio7B/p/5PShR97yA+5VPZG+67X4Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=snCrgNaqilDZubCcKLG/HT1mAM4R483bagK/PO/mK7EiFPcmY3rKwCqZFQZqqt/e6 cM8lHHGUEe9kRQQCm94mMOqDsDUhCDn5TiQonjiF29p1nL7DasEnHC9Y+rHGbjqlZm 1CmbNf2HImHLAKX8IbbvwcTgUBEQvbL4Lo5o/pTw= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 14/22] firmware: arm_scmi: Add common per-protocol debugfs support Date: Fri, 24 Jul 2026 15:44:22 +0100 Message-ID: <20260724144430.1440170-15-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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//protocols// 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 --- 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 | 30 +++++++++++++++++++++++++++ drivers/firmware/arm_scmi/protocols.h | 6 ++++++ include/linux/scmi_protocol.h | 1 - 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/arm_scmi/Kconfig b/drivers/firmware/arm_scmi/= Kconfig index 783c24a20e29..f506c4d1d96a 100644 --- a/drivers/firmware/arm_scmi/Kconfig +++ b/drivers/firmware/arm_scmi/Kconfig @@ -69,6 +69,20 @@ config ARM_SCMI_DEBUG_COUNTERS such useful debug counters. This can be helpful for debugging and SCMI monitoring. =20 +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 f8fbb1a46482..d34fa7fd5253 100644 --- a/drivers/firmware/arm_scmi/common.h +++ b/drivers/firmware/arm_scmi/common.h @@ -322,6 +322,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 @@ -329,6 +330,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 4c200b6168a9..d209b3ca0174 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -18,6 +18,7 @@ =20 #include #include +#include #include #include #include @@ -101,6 +102,8 @@ struct scmi_xfers_info { * has completed. * @ph: An embedded protocol handle that will be passed down to protocol * initialization code to identify this instance. + * @dbg: An optional reference to this protocol top debugfs directory; it = will + * be automatically recursively removed on protocol de-initialization. * * Each protocol is initialized independently once for each SCMI platform = in * which is defined by DT and implemented by the SCMI server fw. @@ -115,6 +118,7 @@ struct scmi_protocol_instance { unsigned int version; unsigned int negotiated_version; struct scmi_protocol_handle ph; + struct dentry *dbg; }; =20 #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph) @@ -2095,6 +2099,25 @@ static void scmi_common_fastchannel_db_ring(struct s= cmi_fc_db_info *db) SCMI_PROTO_FC_RING_DB(64); } =20 +static struct dentry * +scmi_debugfs_proto_dentry_get(const struct scmi_protocol_handle *ph) +{ + struct scmi_protocol_instance *pi =3D ph_to_pi(ph); + struct scmi_info *info =3D 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 =3D debugfs_create_dir(proto_dir, info->dbg->protos); + } + + return pi->dbg; +} + static const struct scmi_proto_helpers_ops helpers_ops =3D { .extended_name_get =3D scmi_common_extended_name_get, .get_max_msg_size =3D scmi_common_get_max_msg_size, @@ -2105,6 +2128,7 @@ static const struct scmi_proto_helpers_ops helpers_op= s =3D { .protocol_msg_check =3D scmi_protocol_msg_check, .fastchannel_init =3D scmi_common_fastchannel_init, .fastchannel_db_ring =3D scmi_common_fastchannel_db_ring, + .debugfs_proto_dentry_get =3D scmi_debugfs_proto_dentry_get, }; =20 /** @@ -2317,6 +2341,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *i= nfo, return pi; =20 clean: + debugfs_remove_recursive(pi->dbg); /* Take care to put the protocol module's owner before releasing all */ scmi_protocol_put(proto); devres_release_group(handle->dev, gid); @@ -2428,6 +2453,8 @@ void scmi_protocol_release(const struct scmi_handle *= handle, u8 protocol_id) if (refcount_dec_and_test(&pi->users)) { void *gid =3D pi->gid; =20 + debugfs_remove_recursive(pi->dbg); + if (pi->proto->events) scmi_deregister_protocol_events(handle, protocol_id); =20 @@ -3150,6 +3177,9 @@ static struct scmi_debug_info *scmi_debugfs_common_se= tup(struct scmi_info *info) if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS)) scmi_debugfs_counters_setup(dbg, trans); =20 + if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS)) + dbg->protos =3D debugfs_create_dir("protocols", top_dentry); + dbg->top_dentry =3D top_dentry; =20 if (devm_add_action_or_reset(info->dev, diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_s= cmi/protocols.h index 1f9925b8f495..04e196a9fbf4 100644 --- a/drivers/firmware/arm_scmi/protocols.h +++ b/drivers/firmware/arm_scmi/protocols.h @@ -11,6 +11,7 @@ =20 #include #include +#include #include #include #include @@ -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 debugf= s 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); }; =20 /** diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 5b1d6eede287..8ff5ee9d6ac0 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -1141,7 +1141,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; }; =20 --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E693033ADBF; Fri, 24 Jul 2026 14:47:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904437; cv=none; b=tPrxQNzDKVhDRyD/QoEefXsagBMZe+kFyoNdKJWgy9CAsy5gDj5xbttwfhTdjFzEDUQIw2HXcBNVmotJD44d4CRynTWDEI9Eg745pBnE1Nc0u6IqP95oY3qWIk3v51NKgYB/NMI3wHsyc9O8gqxdSN/VPxPx9TV9UxIRMp51wuY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904437; c=relaxed/simple; bh=Ky8qI+Lz56pQnTiLLsUdf4HgCV77qJ3Bd6zVi+qJQiA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gOrU/c+m7WmeGtBrqq47V34AzVZCL/IPqIMWu9GylNx774wnHNatBki5uVwxbtl1HvF4GIrpKu+ZLxCTFE60lXfwC5HrYEyxGwPOb4tVdyuE7Gax1jxffo49vCkNN6UxPDjgnc1hoabE1y9HXuTEzC8FGl4ZuW46jppsYNaJCcY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=tr55NMeo; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="tr55NMeo" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 364491477; Fri, 24 Jul 2026 07:47:08 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D393E3F66F; Fri, 24 Jul 2026 07:47:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904432; bh=Ky8qI+Lz56pQnTiLLsUdf4HgCV77qJ3Bd6zVi+qJQiA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tr55NMeoaxtyYcz/rkIeCJFDUTSdE1L7uBFZhpf8F+1ALSsAbJZ+XCbrudGyvQMq0 ZRBbX6GyDA5PDSBmUU+mqntdK19Pd0cJnDU9f75vdA0lhp09EZsctwEta1UegzzlBx 1rK50p8oJyi3rw4gjSm6Ky1v83EMIEyh6k2+XdSk= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 15/22] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Date: Fri, 24 Jul 2026 15:44:23 +0100 Message-ID: <20260724144430.1440170-16-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- v5 --> v6 - added mutex to protect binary dump buffer during reads (Sashiko) - using debugfs_create_file_size() (Sashiko) --- drivers/firmware/arm_scmi/telemetry.c | 94 +++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 5f1c1abfbdc8..cfa3bfb1ad3d 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -404,6 +406,14 @@ struct telemetry_shmti { struct scmi_telemetry_shmti_info info; }; =20 +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 =3D (s); \ @@ -1366,6 +1376,87 @@ scmi_telemetry_enumerate_common_intervals(struct tel= emetry_info *ti) ti->info.intervals); } =20 +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 =3D inode->i_private; + sblob =3D kzalloc_obj(*sblob); + if (!sblob) + return -ENOMEM; + + sblob->shmti =3D shmti; + sblob->buf =3D kzalloc(shmti->info.len, GFP_KERNEL); + if (!sblob->buf) { + kfree(sblob); + return -ENOMEM; + } + + mutex_init(&sblob->mtx); + + filp->private_data =3D sblob; + + return 0; +} + +static ssize_t scmi_telemetry_dbg_shmti_read(struct file *filp, char __use= r *buf, + size_t count, loff_t *ppos) +{ + struct scmi_telemetry_dbg_shmti *sblob =3D filp->private_data; + + /* Dump on first read and again after each rewind to start pos */ + guard(mutex)(&sblob->mtx); + if (!sblob->buf_sz || *ppos =3D=3D 0) { + memcpy_fromio(sblob->buf, sblob->shmti->base, + sblob->shmti->info.len); + sblob->buf_sz =3D sblob->shmti->info.len; + /* update inode timestamps */ + simple_inode_init_ts(file_inode(filp)); + } + + return simple_read_from_buffer(buf, count, ppos, sblob->buf, sblob->buf_s= z); +} + +static int scmi_telemetry_dbg_shmti_release(struct inode *inode, struct fi= le *filp) +{ + struct scmi_telemetry_dbg_shmti *sblob =3D filp->private_data; + + kfree(sblob->buf); + kfree(sblob); + + return 0; +} + +static const struct file_operations scmi_telemetry_dbg_shmti_fops =3D { + .open =3D scmi_telemetry_dbg_shmti_open, + .release =3D scmi_telemetry_dbg_shmti_release, + .read =3D scmi_telemetry_dbg_shmti_read, + .llseek =3D generic_file_llseek, + .owner =3D THIS_MODULE, +}; + +static void scmi_telemetry_debugfs_initialize(struct telemetry_info *ti) +{ + const struct scmi_protocol_handle *ph =3D ti->ph; + struct dentry *top, *shmti_top; + + top =3D ph->hops->debugfs_proto_dentry_get(ph); + shmti_top =3D debugfs_create_dir("shmtis", top); + + for (unsigned int i =3D 0; i < ti->num_shmti; i++) { + char id[16]; + + snprintf(id, 16, "%u", i); + 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) { @@ -3361,6 +3452,9 @@ static int scmi_telemetry_protocol_init(const struct = scmi_protocol_handle *ph) } } =20 + if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS)) + scmi_telemetry_debugfs_initialize(ti); + return 0; } =20 --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 49697326D44; Fri, 24 Jul 2026 14:47:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904444; cv=none; b=ohFcPTIChVzRuidZEPN4I+EE8OpsFL9gawrLhXxZQcd9Xa7jYKFs3jl6EoW6bavOUPuCDI+JvAMHUeUve0TRCuCtlU2KNIMDiOFlRZH04Ds3ZpHe+3fiO/L8jCf2L105WJpsBZxkqSrhBTwCytT4ggg7TdADZznaS4uda/uerMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904444; c=relaxed/simple; bh=w7L8DbnoCEuyvO8JUAes+aM9b0PjMj4j60HnlNcmEQg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I6yDRzh9NaSjeCs1uixY45D+OJu87w+FqIj9lDZkB+UMee6iXhxXXo+Fr7iEo4DhxtLSqWBy9N33FB9Sq1g5cyc8Qfc7lRZMGNO9uAhWC7KlqRb9BJJLyVS85VqzxrR/+2iYPmjYufSqGkbhnfbeIT6ZV1qKNdUDiKXSadLvs7A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=gWnRulbH; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="gWnRulbH" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CADBB1477; Fri, 24 Jul 2026 07:47:16 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E37BE3F66F; Fri, 24 Jul 2026 07:47:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904440; bh=w7L8DbnoCEuyvO8JUAes+aM9b0PjMj4j60HnlNcmEQg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gWnRulbHl9nhWqWcZll0mO5saaOd0Mv2yqaLrKMItZNqsXjQkH2nFn5TsfVVRiAbb GVF3dJRukqZnq7Mb43pQvzyzPPSZl9L4D0EYmjmKjKNJnpcPFIKq4zrG7XICuIUMwp 9XC5bTLyDctVNokjLCfVfYedUvSF8NF+IJEsxD/s= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 16/22] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Date: Fri, 24 Jul 2026 15:44:24 +0100 Message-ID: <20260724144430.1440170-17-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add description of the debugfs SCMI Telemetry protocol ABI. Signed-off-by: Cristian Marussi --- 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/tes= ting/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-fligh= t SCMI messages for the currently configured SCMI transport for instance on the RX channels. Users: Debugging, any userspace test suite + +What: /sys/kernel/debug/scmi//protocols/0x/ +Date: Nov 2026 +KernelVersion: 7.3 +Contact: cristian.marussi@arm.com +Description: A subdirectory grouping debug entries related to protocol + for instance . Each protocol owns and defines the subtree + of entries rooted under this directory. +Users: Debugging, any userspace test suite + +What: /sys/kernel/debug/scmi//protocols/0x1B/shmtis/ +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 . +Users: Debugging, any userspace test suite --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 012C02C86D; Fri, 24 Jul 2026 14:47:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904454; cv=none; b=Jlp7izcfrdpEirGG1TYI3XswonRQ6CMh5HFlnVi7fpbvdOlZJBvlJZ8llUm6ktMJIA+3AbdZiJWIlpd9lz9FT1DnQTH5s4lkhfBAWBVLLAcUk4x0kQKmYyuBGY5xaOZ+Cd1DWIREw7jOd03uEPYm1bsKTr7vDmPhSNfwD2DAeGs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904454; c=relaxed/simple; bh=fmFxsVYq9Mhx1B5o4Q8GyxwWumwfNQYC2qH4bzCKsMs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j87mmK6qd8rd21vmmgy0sL98quKcHHkUpNPEdo+aGRvetUVTSJJet/DIxmMIC5lTwRu9twDtPLftJg94O5G2CBuNmf3vWC7+Fs4nC7JAi4uXTi3zYK/lqRxCEhlnUAx+PDrbAcXDozIOYWZTtJGuBGf8XEeI2lMhTsat/INhAtQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=HQ2m5H4s; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="HQ2m5H4s" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8B81F1477; Fri, 24 Jul 2026 07:47:25 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 671363F66F; Fri, 24 Jul 2026 07:47:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904449; bh=fmFxsVYq9Mhx1B5o4Q8GyxwWumwfNQYC2qH4bzCKsMs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HQ2m5H4sJ1SSAUXn1FH1ZKie6+CMSdkvmdEdnGRHY/rhzrdSquMaRhe8cxUzNEelG i7Vs5EOehMrvzO8n2PkT4VldwruUk9l4dJpH4yDLxk05f8ZGoiwnudsPjdO25wRbmx e/8zf0jyqvtek2EF1YHyKn6zfGKMBGv7MsTUi9TM= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 17/22] firmware: arm_scmi: Expose per-instance identifier Date: Fri, 24 Jul 2026 15:44:25 +0100 Message-ID: <20260724144430.1440170-18-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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 d209b3ca0174..87489bc075c2 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -3337,6 +3337,7 @@ static int scmi_probe(struct platform_device *pdev) idr_init(&info->rx_idr); =20 handle =3D &info->handle; + handle->id =3D info->id; handle->dev =3D info->dev; handle->version =3D &info->version; handle->devm_protocol_acquire =3D scmi_devm_protocol_acquire; diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 8ff5ee9d6ac0..8dc65de42448 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -1110,6 +1110,10 @@ struct scmi_notify_ops { /** * struct scmi_handle - Handle returned to ARM SCMI clients for usage. * + * @id: A unique positive 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, @@ -1130,6 +1134,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; =20 --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9329E43B3EA; Fri, 24 Jul 2026 14:47:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904463; cv=none; b=ZY3gREi52mBKgglZPrHbGsldcOYnZaDztMqP76ax8+84lbuQyAzL0T4pHQ5YTJaEbD8o/qngepCDvIITu+hTzdhCmUGzbpm8vE26JWBlpOdb6KNlpCVh0+UlVFcry+hOqdltRNhA+8F253Kh26kNXUoWSWtUuoCQoRgtJM/5SZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904463; c=relaxed/simple; bh=ymv/kIKrrnqWzhOuFsecKHzmvk5BcHPvyumkcygECFQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hnOJBtm6qOXG0pnjCVpZnXh7L9UJPLi4WL3S78ZAUTs56YqkhJkA6mmi1ItdJfbxQvwETrfAPzq1TBzY1BLDFTMRS4b/S5ZF1mdJEZXuWuX6QM1bMbHyqRRc+n3R/Tn9tYb0lL524AL39TqA+scjsjNf3+ZabRESAUnrU9ZdKp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=sbYaF2hJ; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="sbYaF2hJ" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 531491477; Fri, 24 Jul 2026 07:47:35 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 231A43F66F; Fri, 24 Jul 2026 07:47:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904459; bh=ymv/kIKrrnqWzhOuFsecKHzmvk5BcHPvyumkcygECFQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sbYaF2hJZ//Lk94h2jBweu4+XZhN8Q//sgm67dFkz38d1mDVabrFLEyajOvZh7fRA QF3du53GObkM7b1j8Kwvd5nqFn/ZLCC8ZiQnasJnK5Umz41GA8PXg7gFSDBRAi1YuE 2d0YmuJgpNKamTX0uJEbYvIPZSoNy+UeZ7egyhC0= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 18/22] uapi: Add ARM SCMI Telemetry definitions Date: Fri, 24 Jul 2026 15:44:26 +0100 Message-ID: <20260724144430.1440170-19-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a number of structures and ioctls definitions used by the ARM SCMI Telemetry protocol. Signed-off-by: Cristian Marussi --- 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 | 373 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 374 insertions(+) create mode 100644 include/uapi/linux/scmi.h diff --git a/MAINTAINERS b/MAINTAINERS index a674e36529f7..6587cba09aae 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -26305,6 +26305,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 =20 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..3684d390be06 --- /dev/null +++ b/include/uapi/linux/scmi.h @@ -0,0 +1,373 @@ +/* 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 +#include + +#define SCMI_TLM_ABI_VERSION_V1 1 +#define SCMI_TLM_CURRENT_ABI_VERSION SCMI_TLM_ABI_VERSION_V1 + +#define SCMI_TLM_DE_IMPL_UUID_MAX 16 + +/** + * struct scmi_tlm_abi_info - Basic information on the ABI/instance + * + * @size: sizeof this structure + * @abi_version: ABI Version + * @abi_features: ABI capabilities bitmap + * @de_impl_version: SCMI Telemetry DE implementation revision + * @num_des: Number of defined DEs + * @num_groups: Number of defined DEs groups + * @num_intervals: Number of available update intervals (instance-level) + * @num_shmtis: Number of discovered SHMTI areas + * @features: Instance specific SCMI feature-support bitmap + * @reserved: Some room for future expansion without the need to resize + * + * 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) + __u8 de_impl_version[SCMI_TLM_DE_IMPL_UUID_MAX]; + __u32 num_des; + __u32 num_groups; + __u32 num_intervals; + __u32 num_shmtis; + __u32 features; +#define SCMI_TLM_BASE_SUPPORT_RESET (1 << 0) +#define SCMI_TLM_BASE_SUPPORT_SINGLE_SAMPLE (1 << 1) +#define SCMI_TLM_BASE_SUPPORT_GROUP_CONFIG (1 << 2) +#define SCMI_TLM_BASE_SUPPORT_UPDATE_NOTIFICATION (1 << 3) + __u64 reserved; +}; + +/** + * struct scmi_tlm_update_interval - Update interval descriptor + * + * @secs: integer representing seconds + * @exp: signed integer representing the base 10 exponent used as a multip= lier + * with @secs to represent the interval + * + * The resulting update interval is calculated as: * 10 ^ + */ +struct scmi_tlm_update_interval { + __u32 secs; + __s32 exp; +}; + +/** + * struct scmi_tlm_config - Whole instance or group configuration + * + * @enable: Enable/Disable Telemetry for the whole instance or the group + * @t_enable: Enable/Disable timestamping for all the DEs belonging to a g= roup + * @flags: Bitmask to represent special characteristics + * @pad: Padding fields to enforce alignment + * @grp_id: Identifier of the target group upon which this configuration w= ill + * be applied: ignored if not marked as a group in @flags + * @active: Get/Set currently active update interval for the whole instanc= e or + * a group + * + * 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; +}; + +/** + * 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 + * @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 + * @pad: Padding fields to enforce alignment + * @num_intervals: Number of entries of @intervals + * @pad2: Padding fields to enforce alignment + * @intervals: A pointer to an array of struct scmi_tlm_update_interval + * + * 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; +#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 + * + * @id: Identifier of the DE to act upon (ignored by SCMI_TLM_GET/SET_ALL_= CFG) + * @enable: A boolean to enable/disable the DE + * @t_enable: A boolean to enable/disable the timestamp for this DE + * (if supported) + * @pad: Padding fields to enforce alignment + * + * Used by: + * RW - SCMI_TLM_GET_DE_CFG + * RW - SCMI_TLM_SET_DE_CFG + * RW - SCMI_TLM_GET_ALL_CFG + * RW - SCMI_TLM_SET_ALL_CFG + */ +struct scmi_tlm_de_config { + __u32 id; + __u32 enable; + __u32 t_enable; + __u32 pad; +}; + +/** + * struct scmi_tlm_de_info - DataEvent Descriptor + * + * @id: DE identifier + * @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. + * @data_sz: DE data size in bytes + * @type: DE type + * @unit: DE unit of measurements + * @unit_exp: Power-of-10 multiplier for DE unit + * @ts_rate: Clock rate in kHz used to generate the DE timestamp + * @instance_id: DE instance ID + * @compo_instance_id: DE component instance ID + * @compo_type: Type of component which is associated to this DE + * @persistent: Data value for this DE survives reboot (non-cold ones) + * @flags: Bitmask to represent special characteristics + * @pad: Padding fields to enforce alignment + * @pad2: Padding fields to enforce alignment + * @name: Name of this DE + * + * 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 + */ +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]; +}; + +/** + * struct scmi_tlm_des_list - List of all defined DEs + * + * @num_des: Number of entries in @des + * @pad: Padding fields to enforce alignment + * @des: A reference to an array containing struct scmi_tlm_de_info + * descriptors for all the existent DEs + * + * Used by: + * RW - SCMI_TLM_GET_DE_LIST + */ +struct scmi_tlm_des_list { + __u32 num_des; + __u32 pad; + __u64 des; +}; + +/** + * struct scmi_tlm_de_sample - A DataEvent reading + * + * @id: DE identifier + * @pad: Padding fields to enforce alignment. + * @tstamp: DE reading timestamp (0 if timestamp NOT supported) + * @val: Reading of the DE data value + * + * Used by: + * RW - SCMI_TLM_GET_DE_VALUE + */ +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 req= uest + * in @flags and not supported by SCMI_TLM_BATCH_READ + * @flags: Bitmask to represent special characteristics + * @pad: Padding fields to enforce alignment + * @pad2: Padding fields to enforce alignment + * @num_samples: Number of entries returned in @samples + * @samples: A reference to an array of struct scmi_tlm_de_sample containi= ng + * an entry for each DE + * + * Used by: + * RW - SCMI_TLM_SINGLE_SAMPLE + * RW - SCMI_TLM_BULK_READ + * RW - SCMI_TLM_BATCH_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_grp_info - Group info descriptor + * + * @grp_id: Group ID number + * @num_des: Number of DEs part of this group + * @num_intervals: Number of update intervals supported. Zero if group doe= s not + * support per-group update interval configuration. + * @pad: Padding fields to enforce alignment + * + * Used by: + * RW - SCMI_TLM_GET_GRP_INFO + */ +struct scmi_tlm_grp_info { + __u32 grp_id; + __u32 num_des; + __u32 num_intervals; + __u32 pad; +}; + +/** + * struct scmi_tlm_grps_list - Group info descriptor list + * + * @num_grps: Number of entries returned in @grps + * @pad: Padding fields to enforce alignment + * @grps: A reference to an array of struct scmi_tlm_grp_info containing an + * entry for each defined group + * + * Used by: + * RW - SCMI_TLM_GET_GRP_LIST + */ +struct scmi_tlm_grps_list { + __u32 num_grps; + __u32 pad; + __u64 grps; +}; + +/** + * struct scmi_tlm_grp_desc - Group descriptor + * + * @grp_id: Group ID number + * @num_des: Number of DEs composing this group + * @composing_des: A reference to an array of __u32 elements containing the + * DataEvent IDs composing this group. + * + * Used by: + * RW - SCMI_TLM_GET_GRP_DESC + */ +struct scmi_tlm_grp_desc { + __u32 grp_id; + __u32 num_des; + __u64 composing_des; +}; + +/** + * struct scmi_tlm_shmti_info - SHMTI descriptor + * + * @sid: SHMTI ID + * @fd: Associated opened file descriptor to use for mmap + * @len: Size of the SHMTI to be used with mmap on this SHMTI + * @offset: Offset in the mmap where the specified SHMTI start + * + * Used by: + * RW - SCMI_TLM_GET_SHMTI_LIST + */ +struct scmi_tlm_shmti_info { + __u32 sid; + __u32 fd; + __u32 len; + __u32 offset; +}; + +/** + * struct scmi_tlm_shmtis_list - SHMTIs List + * + * @num_shmtis: Number of entries returned in @shmtis + * @pad: Padding fields to enforce alignment + * @shmtis: A reference to an array of struct scmi_tlm_shmti_info containi= ng + * an entry for each defined SHMTI + * + * Used by: + * RW - SCMI_TLM_GET_SHMTI_LIST + */ +struct scmi_tlm_shmtis_list { + __u32 num_shmtis; + __u32 pad; + __u64 shmtis; +}; + +#define SCMI_TLM_IOCTL_MAGIC 0xF1 + +#define SCMI_TLM_GET_ABI_INFO _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x00, struct sc= mi_tlm_abi_info) +#define SCMI_TLM_GET_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x01, struct scmi_tlm= _config) +#define SCMI_TLM_SET_CFG _IOWR(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_de_config) +#define SCMI_TLM_SET_DE_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x05, struct scmi_= tlm_de_config) +#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_des_list) +#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 _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0A, struct scmi= _tlm_de_config) +#define SCMI_TLM_GET_GRP_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0B, struct scm= i_tlm_grps_list) +#define SCMI_TLM_GET_GRP_INFO _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0C, struct scm= i_tlm_grp_info) +#define SCMI_TLM_GET_GRP_DESC _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0D, struct scm= i_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_t= lm_data_read) +#define SCMI_TLM_BATCH_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x10, struct scmi_= tlm_data_read) +#define SCMI_TLM_GET_SHMTI_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x11, struct s= cmi_tlm_shmtis_list) +#define SCMI_TLM_RESET _IO(SCMI_TLM_IOCTL_MAGIC, 0x12) + +#endif /* _UAPI_LINUX_SCMI_H */ --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1A7B22931DE; Fri, 24 Jul 2026 14:47:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904473; cv=none; b=jmWevUTEWSfwLFuL7x8+DMeihr2fanqjOgw2aA8xJpwrAv5nDlOgwW36YjAqpt671WyvFNJXjSL+LABy2OPBUrffyKVqDITMwxNEfkH9lx2Tn36li2018z17U556NdbUrd1avWt3+GpwtUpB0i+L0zfZ2cTJFr3Eba5I4mPUmso= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904473; c=relaxed/simple; bh=/Wt3AnI7QfG5VAm7yokIpq8sIaZ9CtqC83TTess9E4s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ImeECF+nK5MIXrqbuK81QJDtH5fucyDq4+yMsMLXUZtQUG31a+yoo0CZtRYkViMeEF5AKiGaamIA0SIlLG6zFZ8j+Rfw9678cjZZFokiKuaJiY6Bmq4yi9d1VTw5TgivpApYGIreAX7bDxAh1TeS0eMekeS/io7DnxUfxa5rtBM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=RfxcU4fy; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="RfxcU4fy" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E35E91477; Fri, 24 Jul 2026 07:47:45 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E5E7B3F66F; Fri, 24 Jul 2026 07:47:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904469; bh=/Wt3AnI7QfG5VAm7yokIpq8sIaZ9CtqC83TTess9E4s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RfxcU4fyyOuQ43e1+xi/5lH4n7bacSHmN/uCBThkp0qgUMHBf4dHYbP8KYE7Yjg8Z 4kie14+Q2pat6e+0gouqfeWKs51Ckl/3fMZAwAMDEbZhRPBYTtR1uA7ezgIk04cXwp vrbewuOd22CtIEPj5LYF0614d8G2x9aWycYmdozs= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 19/22] firmware: arm_scmi: Add System Telemetry driver Date: Fri, 24 Jul 2026 15:44:27 +0100 Message-ID: <20260724144430.1440170-20-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- v5 -> v6 - drop hunan 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 | 1135 +++++++++++++++++ 3 files changed, 1146 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. =20 +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) +=3D scmi-core.o obj-$(CONFIG_ARM_SCMI_PROTOCOL) +=3D scmi-module.o =20 obj-$(CONFIG_ARM_SCMI_POWER_CONTROL) +=3D scmi_power_control.o +obj-$(CONFIG_ARM_SCMI_SYSTEM_TELEMETRY) +=3D scmi_system_telemetry.o diff --git a/drivers/firmware/arm_scmi/scmi_system_telemetry.c b/drivers/fi= rmware/arm_scmi/scmi_system_telemetry.c new file mode 100644 index 000000000000..06042e37912d --- /dev/null +++ b/drivers/firmware/arm_scmi/scmi_system_telemetry.c @@ -0,0 +1,1135 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * SCMI - System Telemetry Driver + * + * Copyright (C) 2026 ARM Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#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) !=3D \ + 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 + * @dev: A reference to the related device + * @ph: A reference to the protocol handle to be used with the ops + * @rinfo: A reference to the resource info descriptor + * @ops: A reference to the protocol ops + */ +struct scmi_tlm_setup { + struct device *dev; + struct scmi_protocol_handle *ph; + const struct scmi_telemetry_res_info *rinfo; + const struct scmi_telemetry_proto_ops *ops; +}; + +/** + * struct scmi_tlm_instance - Telemetry instance descriptor + * @id: Progressive number identifying this probed instance. + * @devt: The dev_t descriptor attached to this instance + * @cdev: + * @node: A node to link this in the list of all instances. + * @tsp: A reference to the SCMI instance data. + * @info: A reference to this instance SCMI Telemetry info data. + */ +struct scmi_tlm_instance { + unsigned int id; + dev_t devt; + struct cdev cdev; + struct list_head node; + struct scmi_tlm_setup *tsp; + const struct scmi_telemetry_info *info; +}; + +#define to_instance(c) container_of(c, struct scmi_tlm_instance, cdev) + +struct scmi_tlm_shmti_ctx { + struct scmi_telemetry_shmti_info *shmti; + struct kref kref; +}; + +#define to_shmti_ctx(c) container_of(c, struct scmi_tlm_shmti_ctx, kref) + +static inline void scmi_tlm_to_uapi_abi_info(struct scmi_tlm_abi_info *out, + const struct scmi_telemetry_info *in) +{ + u8 *impl_version =3D (u8 *)in->base.de_impl_version; + + out->size =3D sizeof(*out); + out->abi_version =3D SCMI_TLM_CURRENT_ABI_VERSION; + out->abi_features =3D SCMI_TLM_ABI_FEAT_RESET; + /* Each word composing the UUID was stored in big-endian */ + for (int i =3D 0; i < SCMI_TLM_DE_IMPL_UUID_MAX; i++) + out->de_impl_version[i] =3D impl_version[i]; + + out->num_des =3D in->base.num_des; + out->num_groups =3D in->base.num_groups; + out->num_intervals =3D in->base.num_intervals; + out->num_shmtis =3D in->base.num_shmtis; + out->features =3D in->reset_support ? SCMI_TLM_BASE_SUPPORT_RESET : 0; + out->features |=3D in->single_read_support ? + SCMI_TLM_BASE_SUPPORT_SINGLE_SAMPLE : 0; + out->features |=3D in->per_group_config_support ? + SCMI_TLM_BASE_SUPPORT_GROUP_CONFIG : 0; + out->features |=3D in->continuos_update_support ? + SCMI_TLM_BASE_SUPPORT_UPDATE_NOTIFICATION : 0; + out->reserved =3D 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 =3D in->update_intervals; + + out->num_intervals =3D 0; + for (int i =3D 0; i < in->num_intervals; i++) { + out_interv[i].secs =3D + SCMI_TLM_GET_UPDATE_INTERVAL_SECS(in_interv[i]); + out_interv[i].exp =3D + SCMI_TLM_GET_UPDATE_INTERVAL_EXP(in_interv[i]); + + /* Count returned items */ + out->num_intervals++; + } +} + +static inline void scmi_tlm_to_uapi_de_info(struct scmi_tlm_de_info *out, + struct scmi_telemetry_de_info *in) +{ + out->id =3D in->id; + out->grp_id =3D in->grp_id; + out->data_sz =3D in->data_sz; + out->type =3D in->type; + out->unit =3D in->unit; + out->unit_exp =3D in->unit_exp; + out->ts_rate =3D in->ts_rate; + out->instance_id =3D in->instance_id; + out->compo_instance_id =3D in->compo_instance_id; + out->compo_type =3D in->compo_type; + out->persistent =3D !!in->persistent; + out->flags =3D in->grp_id !=3D SCMI_TLM_GRP_INVALID ? + SCMI_TLM_DEINFO_GROUP : 0; + out->pad[0] =3D 0; + out->pad[1] =3D 0; + out->pad2 =3D 0; + memcpy(out->name, in->name, 16); +} + +static inline void +scmi_tlm_to_uapi_des_list(struct scmi_tlm_des_list *out, + const struct scmi_telemetry_res_info *in, + struct scmi_tlm_de_info *des) +{ + out->num_des =3D 0; + for (int i =3D 0; i < in->num_des; i++) { + scmi_tlm_to_uapi_de_info(&des[i], &in->dei_store[i]); + + /* Count returned items */ + out->num_des++; + } +} + +static inline void scmi_tlm_to_uapi_grp_info(struct scmi_tlm_grp_info *out, + struct scmi_telemetry_grp_info *in) +{ + out->grp_id =3D in->grp_id; + out->num_des =3D in->num_des; + out->num_intervals =3D in->num_intervals; + out->pad =3D 0; +} + +static inline void +scmi_tlm_to_uapi_grps_list(struct scmi_tlm_grps_list *out, + const struct scmi_telemetry_res_info *in, + struct scmi_tlm_grp_info *ginfo) +{ + struct scmi_tlm_grp_info *out_ginfo =3D ginfo; + + out->num_grps =3D 0; + for (int i =3D 0; i < in->num_groups; i++) { + scmi_tlm_to_uapi_grp_info(&out_ginfo[i], &in->grps_store[i]); + + /* Count returned items */ + out->num_grps++; + } +} + +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_grp_info *ginfo =3D &in->grps_store[out->grp_id]; + struct scmi_telemetry_group *grp =3D &in->grps[out->grp_id]; + + out->num_des =3D 0; + for (int i =3D 0; i < ginfo->num_des; i++) { + composing_des[i] =3D in->des[grp->des[i]]->info->id; + + /* Count returned items */ + out->num_des++; + } +} + +/** + * 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 reso= urces + * 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 =3D 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 l= ong arg) +{ + struct scmi_tlm_abi_info base =3D {}; + void __user *uptr =3D (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; + + if (copy_struct_from_user(&base, sizeof(base), uptr, usize)) + return -EFAULT; + + scmi_tlm_to_uapi_abi_info(&base, ti->info); + err =3D 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 lon= g arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_config cfg; + + if (copy_from_user(&cfg, uptr, sizeof(cfg))) + return -EFAULT; + + if (cfg.pad || cfg.flags & ~SCMI_TLM_CONFIG_FLAGS) + return -EINVAL; + + if (!(SCMI_TLM_CONFIG_IS_GROUP(cfg.flags))) { + unsigned int active =3D ti->info->active_update_interval; + + cfg.enable =3D !!ti->info->enabled; + cfg.active.secs =3D SCMI_TLM_GET_UPDATE_INTERVAL_SECS(active); + cfg.active.exp =3D SCMI_TLM_GET_UPDATE_INTERVAL_EXP(active); + } else { + const struct scmi_telemetry_res_info *rinfo; + struct scmi_telemetry_group *grp; + unsigned int active; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + if (cfg.grp_id >=3D rinfo->num_groups) + return -EINVAL; + + grp =3D &rinfo->grps[cfg.grp_id]; + active =3D grp->active_update_interval; + + cfg.enable =3D grp->enabled; + cfg.t_enable =3D grp->tstamp_enabled; + cfg.active.secs =3D SCMI_TLM_GET_UPDATE_INTERVAL_SECS(active); + cfg.active.exp =3D 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 lon= g arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D 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.flags & ~SCMI_TLM_CONFIG_FLAGS) + return -EINVAL; + + ena =3D !!cfg.enable; + t_ena =3D !!cfg.t_enable; + if (!SCMI_TLM_CONFIG_IS_GROUP(cfg.flags)) { + cfg.grp_id =3D SCMI_TLM_GRP_INVALID; + } else { + int ret; + + ret =3D tsp->ops->state_set(tsp->ph, true, cfg.grp_id, &ena, &t_ena); + if (ret) + return ret; + } + + active =3D 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 =3D (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.flags & ~SCMI_TLM_INTERV_FLAGS) + return -EINVAL; + + if (!SCMI_TLM_INTERV_IS_GROUP(ivs.flags)) { + tlm_ivs =3D ti->info->intervals; + } else { + const struct scmi_telemetry_res_info *rinfo; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + if (ivs.grp_id >=3D rinfo->num_groups) + return -EINVAL; + + tlm_ivs =3D rinfo->grps[ivs.grp_id].intervals; + } + + /* Reject only undersized buffers */ + if (ivs.num_intervals < tlm_ivs->num_intervals) + return -ENOSPC; + + /* A local scratch buffer... */ + ivs_intrv_sz =3D ivs.num_intervals * sizeof(struct scmi_tlm_update_interv= al); + struct scmi_tlm_update_interval *ivs_intrv __free(kfree) =3D + kzalloc(ivs_intrv_sz, GFP_KERNEL); + if (!ivs_intrv) + return -ENOMEM; + + if (copy_from_user(ivs_intrv, u64_to_user_ptr(ivs.intervals), + ivs_intrv_sz)) + return -EFAULT; + + /* 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 long +scmi_tlm_de_config_set_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg, bool all) +{ + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_de_config tcfg; + bool ena, t_ena; + int ret; + + if (copy_from_user(&tcfg, uptr, sizeof(tcfg))) + return -EFAULT; + + if (tcfg.pad) + return -EINVAL; + + ena =3D !!tcfg.enable; + t_ena =3D !!tcfg.t_enable; + if (!all) + return tsp->ops->state_set(tsp->ph, false, tcfg.id, &ena, &t_ena); + + rinfo =3D scmi_telemetry_res_info_get(tsp); + for (int i =3D 0; i < rinfo->num_des; i++) { + ret =3D tsp->ops->state_set(tsp->ph, false, + rinfo->des[i]->info->id, &ena, &t_ena); + if (ret) + return ret; + } + + return 0; +} + +static long +scmi_tlm_de_config_get_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg, bool all) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_de_config tcfg; + bool ena, t_ena; + int ret; + + if (copy_from_user(&tcfg, uptr, sizeof(tcfg))) + return -EFAULT; + + if (tcfg.pad) + return -EINVAL; + + ena =3D !!tcfg.enable; + t_ena =3D !!tcfg.t_enable; + ret =3D tsp->ops->state_get(tsp->ph, all ? NULL : &tcfg.id, &ena, &t_ena); + if (ret) + return ret; + + tcfg.enable =3D ena; + tcfg.t_enable =3D t_ena; + if (copy_to_user(uptr, &tcfg, sizeof(tcfg))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_de_info_get_ioctl(const struct scmi_tlm_instance *ti, unsigned lo= ng arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D 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) + return -EINVAL; + + de =3D 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 l= ong arg) +{ + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_des_list dsl; + size_t dsl_des_sz; + + if (copy_from_user(&dsl, uptr, sizeof(dsl))) + return -EFAULT; + + if (dsl.pad) + return -EINVAL; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + /* Reject only undersized buffers */ + if (dsl.num_des < rinfo->num_des) + return -ENOSPC; + + /* A local scratch buffer... */ + dsl_des_sz =3D dsl.num_des * sizeof(struct scmi_tlm_de_info); + struct scmi_tlm_de_info *dsl_des __free(kfree) =3D + kzalloc(dsl_des_sz, GFP_KERNEL); + if (!dsl_des) + return -ENOMEM; + + if (copy_from_user(dsl_des, u64_to_user_ptr(dsl.des), dsl_des_sz)) + return -EFAULT; + + /* Passing the array as a param avoids dancing with uptr->intervals */ + scmi_tlm_to_uapi_des_list(&dsl, rinfo, dsl_des); + if (copy_to_user(u64_to_user_ptr(dsl.des), (void *)dsl_des, dsl_des_sz)) + return -EFAULT; + + if (copy_to_user(uptr, &dsl, sizeof(dsl))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_de_value_get_ioctl(const struct scmi_tlm_instance *ti, unsigned l= ong arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D 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 =3D 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 l= ong arg) +{ + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_grp_info ginfo; + + if (copy_from_user(&ginfo, uptr, sizeof(ginfo))) + return -EFAULT; + + if (ginfo.pad) + return -EINVAL; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + if (ginfo.grp_id >=3D 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 l= ong arg) +{ + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_grp_desc gdesc; + size_t composing_sz; + + if (copy_from_user(&gdesc, uptr, sizeof(gdesc))) + return -EFAULT; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + if (gdesc.grp_id >=3D rinfo->num_groups) + return -EINVAL; + + /* Reject only undersized buffers */ + if (gdesc.num_des < rinfo->grps_store[gdesc.grp_id].num_des) + return -ENOSPC; + + /* A local scratch buffer... */ + composing_sz =3D gdesc.num_des * sizeof(u32); + u32 *composing_des __free(kfree) =3D kzalloc(composing_sz, GFP_KERNEL); + if (!composing_des) + return -ENOMEM; + + if (copy_from_user(composing_des, u64_to_user_ptr(gdesc.composing_des), + composing_sz)) + return -EFAULT; + + /* 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) +{ + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_grps_list gsl; + size_t ginfo_sz; + + if (copy_from_user(&gsl, uptr, sizeof(gsl))) + return -EFAULT; + + if (gsl.pad) + return -EINVAL; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + /* Reject only undersized buffers */ + if (gsl.num_grps < rinfo->num_groups) + return -ENOSPC; + + /* A local scratch buffer... */ + ginfo_sz =3D gsl.num_grps * sizeof(struct scmi_tlm_grp_info); + struct scmi_tlm_grp_info *ginfo __free(kfree) =3D + kzalloc(ginfo_sz, GFP_KERNEL); + if (!ginfo) + return -ENOMEM; + + if (copy_from_user(ginfo, u64_to_user_ptr(gsl.grps), ginfo_sz)) + return -EFAULT; + + scmi_tlm_to_uapi_grps_list(&gsl, rinfo, ginfo); + if (copy_to_user(u64_to_user_ptr(gsl.grps), ginfo, ginfo_sz)) + return -EFAULT; + + if (copy_to_user(uptr, &gsl, sizeof(gsl))) + return -EFAULT; + + return 0; +} + +static long scmi_tlm_des_read_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg, bool single) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_data_read bulk; + unsigned int grp_id; + int ret; + + if (copy_from_user(&bulk, uptr, sizeof(bulk))) + return -EFAULT; + + if (bulk.pad[0] || bulk.pad[1] || bulk.pad[2] || bulk.pad2 || + bulk.flags & ~SCMI_TLM_READ_FLAGS) + return -EINVAL; + + if (!SCMI_TLM_READ_IS_GROUP(bulk.flags)) { + grp_id =3D SCMI_TLM_GRP_INVALID; + } else { + const struct scmi_telemetry_res_info *rinfo; + + rinfo =3D scmi_telemetry_res_info_get(tsp); + if (bulk.grp_id >=3D rinfo->num_groups) + return -EINVAL; + + grp_id =3D bulk.grp_id; + } + + struct scmi_telemetry_de_sample *samples __free(kfree) =3D + kcalloc(bulk.num_samples, sizeof(*samples), GFP_KERNEL); + if (!samples) + return -ENOMEM; + + if (!single) + ret =3D tsp->ops->des_bulk_read(tsp->ph, grp_id, + &bulk.num_samples, samples); + else + ret =3D 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) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_data_read batch; + + if (copy_from_user(&batch, uptr, sizeof(batch))) + return -EFAULT; + + if (batch.pad[0] || batch.pad[1] || batch.pad[2] || batch.pad2 || + batch.flags & ~SCMI_TLM_READ_FLAGS) + return -EINVAL; + + struct scmi_telemetry_de_sample *samples __free(kfree) =3D + kcalloc(batch.num_samples, sizeof(*samples), GFP_KERNEL); + if (!samples) + return -ENOMEM; + + /* Read all the requested samples */ + if (copy_from_user(samples, u64_to_user_ptr(batch.samples), + batch.num_samples * sizeof(*samples))) + return -EFAULT; + + for (int i =3D 0; i < batch.num_samples; i++) { + int ret; + + ret =3D tsp->ops->de_data_read(tsp->ph, &samples[i]); + if (ret) + return ret; + } + + if (copy_to_user(u64_to_user_ptr(batch.samples), samples, + batch.num_samples * sizeof(*samples))) + return -EFAULT; + + if (copy_to_user(uptr, &batch, sizeof(batch))) + return -EFAULT; + + return 0; +} + +static struct scmi_tlm_shmti_ctx * +scmi_tlm_shmti_ctx_alloc(struct scmi_telemetry_shmti_info *shmti) +{ + struct scmi_tlm_shmti_ctx *ctx; + + ctx =3D kzalloc_obj(*ctx); + if (!ctx) + return ERR_PTR(-ENOMEM); + + ctx->shmti =3D shmti; + kref_init(&ctx->kref); + + return ctx; +} + +static void scmi_tlm_shmti_ctx_release(struct kref *kref) +{ + struct scmi_tlm_shmti_ctx *ctx =3D to_shmti_ctx(kref); + + kfree(ctx); +} + +static struct scmi_tlm_shmti_ctx * +scmi_tlm_shmti_ctx_get(struct scmi_tlm_shmti_ctx *ctx) +{ + kref_get(&ctx->kref); + + return ctx; +} + +static void scmi_tlm_shmti_ctx_put(struct scmi_tlm_shmti_ctx *ctx) +{ + kref_put(&ctx->kref, 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 =3D { + .open =3D scmi_tlm_shmti_vma_open, + .close =3D scmi_tlm_shmti_vma_close, +}; + +static int scmi_tlm_shmti_mmap(struct file *filp, struct vm_area_struct *v= ma) +{ + struct scmi_tlm_shmti_ctx *ctx =3D filp->private_data; + unsigned long npages, expect, needed, req =3D vma->vm_end - vma->vm_start; + phys_addr_t base; + size_t map_sz; + int ret; + + if (vma->vm_flags & VM_WRITE) + return -EPERM; + + if (req < ctx->shmti->len) + return -EINVAL; + + /* Align & reject oversized requests */ + base =3D ctx->shmti->phys & PAGE_MASK; + needed =3D ctx->shmti->len + ctx->shmti->offset; + expect =3D DIV_ROUND_UP(needed, PAGE_SIZE); + npages =3D req >> PAGE_SHIFT; + if (npages > expect) + return -EINVAL; + + map_sz =3D PAGE_ALIGN(needed); + vma->vm_private_data =3D scmi_tlm_shmti_ctx_get(ctx); + vma->vm_ops =3D &scmi_tlm_shmti_vm_ops; + /* Set appropriate caching attributes */ + vma->vm_page_prot =3D pgprot_noncached(vma->vm_page_prot); + + /* Map physical address range into user space */ + ret =3D 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 =3D { + .owner =3D THIS_MODULE, + .mmap =3D scmi_tlm_shmti_mmap, + .release =3D scmi_tlm_shmti_release, +}; + +static void +scmi_tlm_allocate_anon_fds(const struct scmi_tlm_instance *ti, + struct scmi_tlm_shmtis_list *out, + struct scmi_tlm_shmti_info *shinfo) +{ + const struct scmi_telemetry_info *info =3D ti->info; + struct device *dev =3D ti->tsp->dev; + int idx =3D 0; + + out->num_shmtis =3D 0; + for (int i =3D 0; i < info->base.num_shmtis; i++) { + struct scmi_telemetry_shmti_info *shmti =3D info->shmtis[i]; + struct scmi_tlm_shmti_ctx *ctx; + int fd; + + ctx =3D scmi_tlm_shmti_ctx_alloc(shmti); + if (IS_ERR(ctx)) { + dev_err(dev, "Failed CTX_ALLOC on SID: %u\n", shmti->sid); + continue; + } + + fd =3D anon_inode_getfd(SCMI_TLM_DRIVER, &scmi_tlm_shmti_fops, ctx, + O_RDONLY | O_CLOEXEC); + if (fd < 0) { + scmi_tlm_shmti_ctx_put(ctx); + dev_err(dev, "Failed ANON INODE on SID: %u\n", shmti->sid); + continue; + } + + shinfo[idx].sid =3D shmti->sid; + shinfo[idx].fd =3D fd; + shinfo[idx].len =3D shmti->len; + shinfo[idx].offset =3D shmti->offset; + + /* Count returned items */ + out->num_shmtis++; + idx++; + } +} + +static long +scmi_tlm_shmtis_list_get_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_shmtis_list ssl; + size_t shinfo_sz; + + if (copy_from_user(&ssl, uptr, sizeof(ssl))) + return -EFAULT; + + if (ssl.pad) + return -EINVAL; + + /* Reject only undersized buffers */ + if (ssl.num_shmtis < ti->info->base.num_shmtis) + return -ENOSPC; + + /* A local scratch buffer... */ + shinfo_sz =3D ssl.num_shmtis * sizeof(struct scmi_tlm_shmti_info); + struct scmi_tlm_shmti_info *shinfo __free(kfree) =3D + kzalloc(shinfo_sz, GFP_KERNEL); + if (!shinfo) + return -ENOMEM; + + if (copy_from_user(shinfo, u64_to_user_ptr(ssl.shmtis), shinfo_sz)) + return -EFAULT; + + scmi_tlm_allocate_anon_fds(ti, &ssl, shinfo); + if (copy_to_user(u64_to_user_ptr(ssl.shmtis), shinfo, shinfo_sz)) + return -EFAULT; + + if (copy_to_user(uptr, &ssl, sizeof(ssl))) + return -EFAULT; + + 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_unlocked_ioctl(struct file *filp, unsigned int cmd, + unsigned long arg) +{ + struct inode *ino =3D file_inode(filp); + struct scmi_tlm_instance *ti =3D to_instance(ino->i_cdev); + + 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: + return scmi_tlm_config_set_ioctl(ti, arg); + 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, false); + case SCMI_TLM_SET_DE_CFG: + return scmi_tlm_de_config_set_ioctl(ti, arg, false); + 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_de_config_get_ioctl(ti, arg, true); + case SCMI_TLM_SET_ALL_CFG: + return scmi_tlm_de_config_set_ioctl(ti, arg, true); + 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: + return scmi_tlm_reset_ioctl(ti, arg); + default: + return -ENOTTY; + } +} + +static const struct file_operations stlm_fops =3D { + .owner =3D THIS_MODULE, + .open =3D nonseekable_open, + .unlocked_ioctl =3D scmi_tlm_unlocked_ioctl, + .compat_ioctl =3D compat_ptr_ioctl, +}; + +static struct scmi_tlm_instance *scmi_tlm_init(struct scmi_tlm_setup *tsp, + unsigned int instance_id) +{ + struct device *dev =3D tsp->dev; + struct scmi_tlm_instance *ti; + + ti =3D devm_kzalloc(dev, sizeof(*ti), GFP_KERNEL); + if (!ti) + return ERR_PTR(-ENOMEM); + + ti->info =3D tsp->ops->info_get(tsp->ph); + if (!ti->info) + return dev_err_ptr_probe(dev, + -EINVAL, "invalid Telemetry info !\n"); + + ti->id =3D instance_id; + ti->tsp =3D tsp; + + return ti; +} + +static char *scmi_tlm_devnode(const struct device *dev, umode_t *mode) +{ + return kasprintf(GFP_KERNEL, "scmi/%s", dev_name(dev)); +} + +static struct class stlm_class =3D { + .name =3D "stlm", + .devnode =3D scmi_tlm_devnode, +}; + +static int scmi_telemetry_probe(struct scmi_device *sdev) +{ + const struct scmi_handle *handle =3D sdev->handle; + struct device *tdev, *dev =3D &sdev->dev; + struct scmi_protocol_handle *ph; + struct scmi_tlm_instance *ti; + struct scmi_tlm_setup *tsp; + unsigned int instance; + const void *ops; + int ret; + + if (!handle) + return -ENODEV; + + ops =3D handle->devm_protocol_get(sdev, sdev->protocol_id, &ph); + if (IS_ERR(ops)) + return dev_err_probe(dev, PTR_ERR(ops), + "Cannot access protocol:0x%X\n", + sdev->protocol_id); + + tsp =3D devm_kzalloc(dev, sizeof(*tsp), GFP_KERNEL); + if (!tsp) + return -ENOMEM; + + tsp->dev =3D dev; + tsp->ops =3D ops; + tsp->ph =3D ph; + + instance =3D handle->id; + if (instance >=3D SCMI_TLM_NUM_DEVS) + return -ENOSPC; + + ti =3D scmi_tlm_init(tsp, instance); + if (IS_ERR(ti)) + return PTR_ERR(ti); + + cdev_init(&ti->cdev, &stlm_fops); + ti->cdev.owner =3D THIS_MODULE; + ti->devt =3D MKDEV(MAJOR(stlm_devt), MINOR(stlm_devt) + instance); + ret =3D cdev_add(&ti->cdev, ti->devt, 1); + if (ret) + return ret; + + tdev =3D device_create(&stlm_class, NULL, ti->devt, ti, "tlm_%d", ti->id); + if (IS_ERR(tdev)) { + cdev_del(&ti->cdev); + return PTR_ERR(tdev); + } + + dev_set_drvdata(&sdev->dev, ti); + + return 0; +} + +static void scmi_telemetry_remove(struct scmi_device *sdev) +{ + struct scmi_tlm_instance *ti; + + ti =3D dev_get_drvdata(&sdev->dev); + if (!ti) + return; + + device_destroy(&stlm_class, ti->devt); + cdev_del(&ti->cdev); +} + +static const struct scmi_device_id scmi_id_table[] =3D { + { SCMI_PROTOCOL_TELEMETRY, "telemetry" }, + { } +}; +MODULE_DEVICE_TABLE(scmi, scmi_id_table); + +static struct scmi_driver scmi_telemetry_driver =3D { + .name =3D SCMI_TLM_DRIVER, + .probe =3D scmi_telemetry_probe, + .remove =3D scmi_telemetry_remove, + .id_table =3D scmi_id_table, +}; + +static int __init scmi_telemetry_init(void) +{ + int ret; + + ret =3D alloc_chrdev_region(&stlm_devt, 0, SCMI_TLM_NUM_DEVS, + SCMI_TLM_DRIVER); + if (ret) + return ret; + + ret =3D class_register(&stlm_class); + if (ret) + return ret; + + return scmi_register(&scmi_telemetry_driver); +} +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 "); +MODULE_DESCRIPTION("ARM SCMI Telemetry Driver"); +MODULE_LICENSE("GPL"); --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E1DDD4432FE; Fri, 24 Jul 2026 14:47:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904481; cv=none; b=J8DMj2xSm+x/Fm3tRLZdNG5c/oIGQzeGP7oZ3PsNwAKv/+FuYGDYl40vI5oQczcQxgDKho9jPkjGSwv7nmM2GGF/rNDPHJbOb/mjLm0nlCErrL9wrDC8O7i506Z/UM3AWCTsetGrmOAqR26nc6SXDRoNmC1TnFVtteO3A+vi7MY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904481; c=relaxed/simple; bh=0ASHp4GelyBBW6AUV8K6qt1iaX/L1O6I0Qak7wuQ8e4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=okGercw3AWWbB1DjPqrolcIaGpGpL7U581u+pLwYiD1nA1hJXdwGsVOOOQC1jouQB0Igu/vEahwddsX0iL9A0XcjKYiZFQbsrgsr5UbgNPcqKrM57sSfjy0gt2IItzsOA3TLCzURNh6bOD2mIfkuu1ISaIm5Ukwwje93Eme3K3k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=c54Olf8g; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="c54Olf8g" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A55521477; Fri, 24 Jul 2026 07:47:54 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9F01B3F66F; Fri, 24 Jul 2026 07:47:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904478; bh=0ASHp4GelyBBW6AUV8K6qt1iaX/L1O6I0Qak7wuQ8e4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c54Olf8gMDsqFSj4P2fvdMgb8KMZ553NJzzzwUlamckQ1HsBvRjbPs1bLofPrOdqn V7+Ui8JsXVjhMCQ2laVI7AaGk798xO/LHXwxXEAEq+o+MQ6Co244hW+eXY4Jn0WJtC e+krfL+9p0M/qQ+437LbdEjknhbCa6w7NQP3ckUE= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi , Jonathan Corbet , Shuah Khan Subject: [PATCH v6 20/22] docs: ioctl-number: Add SCMI Ioctls Date: Fri, 24 Jul 2026 15:44:28 +0100 Message-ID: <20260724144430.1440170-21-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add SCMI Subsystem IOCTLs numbers Cc: Jonathan Corbet Cc: Shuah Khan Cc: linux-doc@vger.kernel.org Signed-off-by: Cristian Marussi --- Documentation/userspace-api/ioctl/ioctl-number.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documenta= tion/userspace-api/ioctl/ioctl-number.rst index 3f0ef1e27eb0..de136c5967f4 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -399,6 +399,7 @@ Code Seq# Include File = Comments 0xE5 00-3F linux/fuse.h 0xEC 00-01 drivers/platform/chrome/cros_ec_dev.h Chr= omeOS EC driver 0xEE 00-09 uapi/linux/pfrut.h Pla= tform Firmware Runtime Update and Telemetry +0xF1 00-FF uapi/linux/scmi.h ARM SCMI Subsystem 0xF3 00-3F drivers/usb/misc/sisusbvga/sisusb.h sis= fb (in development) 0xF6 all LTT= ng Linux Trace Toolkit Next Generation --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C529131E821; Fri, 24 Jul 2026 14:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904491; cv=none; b=UtyCESAh4ORBW5ls9e6YNnxhyGrqrY5r0CQKR69SXq+U88PhSr1Ad2ANhOuCPdsO9LccU92792oN3NTFRIcq5IPZ2wmPqWrKImc9wiTDC+SuZ3SR/htReqPmlqsZ2HJhTkvbJCE3DYCN3sFAZk616nGEXuvCw/jrGFDAjbNb2O8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904491; c=relaxed/simple; bh=gJrS8aRyosgxk4J/MRWCLvh+ScGKTFe6jdAdNUFzGck=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PEI3LIt0SMpozHdpOQkH4hl9pQs1uR2DFokIxTKHyzyqMQNAO+xeQBxyVBhde+AzyAv4y/IP6zGb2yW3rGKzd3g5YfaGz74xkFzlN3GqHxxegzi0GjaBlabelGU3ZcxAQTe6y4sd1b6N5g7W1ryyZoY5UdMjh4CIKaCYCGAOitM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=RkbILKE7; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="RkbILKE7" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6647E1477; Fri, 24 Jul 2026 07:48:04 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A94423F66F; Fri, 24 Jul 2026 07:47:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904488; bh=gJrS8aRyosgxk4J/MRWCLvh+ScGKTFe6jdAdNUFzGck=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RkbILKE70zyIcI07ojA0bioCQJ7PAW7Lqb89p9entbHvlhDbldKc/EqMrNtpbOP3o I0+a0QXA9H7vS2Pwu9UHTSQCS8hcpReHEu0D18uIv2xpi3e1J5sfGLQyUQI0k1amKi NJC3eEkj4f13PfG54LOx2MTLp++2/m8kNEiueP2Y= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi , Jonathan Corbet , Shuah Khan Subject: [PATCH v6 21/22] [RFC] Documentation: Add SCMI System Telemetry documentation Date: Fri, 24 Jul 2026 15:44:29 +0100 Message-ID: <20260724144430.1440170-22-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Document ARM SCMI Telemetry IOCTLs interface. Cc: Jonathan Corbet Cc: Shuah Khan Cc: linux-doc@vger.kernel.org Signed-off-by: Cristian Marussi --- RFC since it still needs more extensive examples v5 --> v6 - fixed typos --- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/stlm.rst | 143 ++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 Documentation/userspace-api/stlm.rst diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspac= e-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 =20 Everything else =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D diff --git a/Documentation/userspace-api/stlm.rst b/Documentation/userspace= -api/stlm.rst new file mode 100644 index 000000000000..e34226b11373 --- /dev/null +++ b/Documentation/userspace-api/stlm.rst @@ -0,0 +1,143 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. stlm: + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +STLM - ARM SCMI Telemetry Userspace API +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. contents:: + +Overview +=3D=3D=3D=3D=3D=3D=3D=3D + +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_, grouped under /dev/scmi/, one for each discovered SCMI +instance. + +A 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 +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + + - SCMI_TLM_GET_INFO: Gather basic Telemetry features like number of + resources and versioning information. + + - 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 Gro= ups. + + - 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. + + +Configuration +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + + - 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. + +Data Collection +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + + - 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 enabl= ed + 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 areas 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 +=3D=3D=3D=3D=3D=3D=3D + +.. code-block:: c + + int main(int argc, char **argv) + { + struct scmi_tlm_de_config des_cfg =3D {}; + struct scmi_tlm_config cfg =3D {}; + struct scmi_tlm_de_sample samples[3] =3D {}; + struct scmi_tlm_data_read data =3D {}; + int fd, ret; + + fd =3D open("/dev/scmi/tlm_0", O_RDWR); + if (fd < 0) + return fd; + + /* Enable ALL Data Events with timestamps*/ + des_cfg.enable =3D 1; + des_cfg.t_enable =3D 1; + ret =3D 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 =3D 1; + cfg.active.secs =3D 400; + cfg.active.exp =3D -3; + + ret =3D ioctl(fd, SCMI_TLM_SET_CFG, &cfg); + if (ret) + return ret; + + /* Read a selection of DEs */ + samples[0].id =3D 0xA001; + samples[1].id =3D 0x0002; + samples[2].id =3D 0x1010; + data.num_samples =3D 3; + data.samples =3D (unsigned long)samples; + + ret =3D ioctl(fd, SCMI_TLM_BATCH_READ, &data); + if (ret) + return ret; + + for (int i =3D 0; i < 3; i++) + fprintf(stdout, "%llu: 0x%08X -> %llu\n", + samples[i].tstamp, samples[i].id, samples[i].val); + + return 0; + } + +References +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +[1]: https://developer.arm.com/documentation/den0056/latest/ --=20 2.54.0 From nobody Fri Jul 24 21:30:16 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B43B3101B0; Fri, 24 Jul 2026 14:48:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904504; cv=none; b=lNIAL5iTekcJS2mpLm/8Bhc9wklKCNkEmz+8RQmEHD7uHV2F00lz0nGyYVHfy4Zs+mvC92Q+73benOt49nzeczh5Vq38KxmqwK8CHv1HUpzz5SYSOm6yyv7ZEGiKQSaF9jep4g7C+YZaQ53+0TyRj3KHCVxoYriGcMXvyIMSpDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784904504; c=relaxed/simple; bh=Tp+kWMZHZJCv5h6T0wJTh1SN3mNOWErLuYERXCzTm9c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PJE6E2e413e318eJaQG+JT4xovcagCqssgUwZ8wrDIJp114uzU5gL9OZxROkLkVwa7/Ep3aas41/KGDzE8w0pUO3z++qolVsxHwW3n/gZQcx326qkeHP+esBSKz8DjyMBhdeUZY9CDrcBSO57RrFT8CxOfFJfTh0LHJIO0mYLb8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=OZd0UFLZ; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="OZd0UFLZ" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4690A1684; Fri, 24 Jul 2026 07:48:16 -0700 (PDT) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B135B3F66F; Fri, 24 Jul 2026 07:48:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1784904500; bh=Tp+kWMZHZJCv5h6T0wJTh1SN3mNOWErLuYERXCzTm9c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OZd0UFLZnE29kb7LQsInxM5wXMgbDaKCG+l4PRCGcKq6b6RqZXiydhGjbdDczdU5t lCem1AqiCLjGWBPP7nbZkVYAwgGwr6VQJTpIc5EFzC9XkYV6PFv9009HEd+WSv26X9 NcYyC3afgjawzXmcPLYhOq2Owk/Vi6tAXevi6Wlg= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jic23@kernel.org, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, brauner@kernel.org, souvik.chakravarty@arm.com, david@kernel.org, subrahmanya.lingappa@oss.qualcomm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v6 22/22] [RFC] tools/scmi: Add SCMI Telemetry testing tool Date: Fri, 24 Jul 2026 15:44:30 +0100 Message-ID: <20260724144430.1440170-23-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260724144430.1440170-1-cristian.marussi@arm.com> References: <20260724144430.1440170-1-cristian.marussi@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- More interactive support needed. 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 --- tools/testing/scmi/Makefile | 25 + tools/testing/scmi/stlm.c | 1191 +++++++++++++++++++++++++++++++++++ 2 files changed, 1216 insertions(+) create mode 100644 tools/testing/scmi/Makefile create mode 100644 tools/testing/scmi/stlm.c diff --git a/tools/testing/scmi/Makefile b/tools/testing/scmi/Makefile new file mode 100644 index 000000000000..a6a101f8398b --- /dev/null +++ b/tools/testing/scmi/Makefile @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +CC?=3D$(CROSS_COMPILE)gcc +OBJS =3D stlm.o + +CFLAGS=3D-Wall -static -std=3Dgnu11 -I ../../../include/uapi/ +ifneq ($(DEBUG), ) + CFLAGS+=3D-O0 -g -ggdb +else + CFLAGS+=3D-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..8fcdba5783c7 --- /dev/null +++ b/tools/testing/scmi/stlm.c @@ -0,0 +1,1191 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#define SLEEP_MS 3000 + +#define IOCTL_ERR_STR(_ioctl) "IOCTL:" #_ioctl + +#define USAGE_STRING \ + "\t[OPTIONS]\n" \ + "\t -i \t\tUse this device instance path. Default: /dev/sc= mi/tlm_0\n" \ + "\t[COMMANDS]\n" \ + "\t info [full] - Display summarized (or full) configuration and informat= ion data\n" \ + "\t shmti [check][dump ] - SHMTI operations\n" \ + "\t\t check - Sanity check of all SHMTis\n" \ + "\t\t dump [] - Binary dump of or all SHMTI areas\n" \ + "\t des - Display detailed DEs info and config 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 - Set global update interval\n" \ + "\t\t all_des [[on|off]:[on|off]] - Enable or disable ALL DEs with or wou= t TS\n" \ + "\t\t de [ [[on|off]:[on|off]]] - Get/Set configuration of one or = ALL de\n" \ + "\t\t group [] [[on|off]:[on|off]:[]] - Get/Set GRO= UP configuration\n" \ + "\t single - Perform a single sample read\n" \ + "\t bulk - Perform a bulk read\n" \ + "\t batch [de_id [de_id]... ] - Perform a batch read of the DE ID= s 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_shmtis_list *ssl; + 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 inline void dump_des(struct tlm_state *st) +{ + fprintf(stdout, "\n+ Found #%d DEs:\n", st->num_des); + for (int i =3D 0; i < st->num_des; i++) { + 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 =3D 0; i < st->num_des; i++) { + struct tlm_de *de =3D &st->des[i]; + + /* Collect fresh sample */ + if (de->cfg.enable) { + int ret; + + de->sample.id =3D de->info->id; + ret =3D 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 dump_groups(struct tlm_state *st) +{ + fprintf(stdout, "\n+ Found %d GRPs: ", st->num_groups); + for (int i =3D 0; i < st->num_groups; i++) { + struct scmi_tlm_update_interval *grp_intrvs; + struct tlm_group *grp =3D &st->grps[i]; + uint32_t *composing_des; + + fprintf(stdout, "\n\tGRP_ID:%d DES#:%d INTRVS#:%d\n", + grp->info->grp_id, grp->info->num_des, + grp->info->num_intervals); + + fprintf(stdout, "\tGRP Intervals:\n"); + grp_intrvs =3D (struct scmi_tlm_update_interval *)grp->ivs->intervals; + for (int j =3D 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 =3D (uint32_t *)grp->desc->composing_des; + fprintf(stdout, "\tCOMPOSING_DES:"); + for (int j =3D 0; j < grp->desc->num_des; j++) + fprintf(stdout, "0x%08X ", composing_des[j]); + fprintf(stdout, "\n"); + } +} + +static inline void dump_state(struct tlm_state *st, struct parsed_args *ar= gs) +{ + 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)\n", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_RESET)); + fprintf(stdout, "+ SCMI Features: 0x%04X\n", st->info.features); + fprintf(stdout, "\t (RESET:%u", + !!(st->info.features & SCMI_TLM_BASE_SUPPORT_RESET)); + fprintf(stdout, " SINGLE_SAMPLE:%u", + !!(st->info.features & SCMI_TLM_BASE_SUPPORT_SINGLE_SAMPLE)); + fprintf(stdout, " GROUP_CONFIG:%u", + !!(st->info.features & SCMI_TLM_BASE_SUPPORT_GROUP_CONFIG)); + fprintf(stdout, " UPDATE_NOTIFICATIONS:%u)\n", + !!(st->info.features & SCMI_TLM_BASE_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\n", st->info.num_intervals); + fprintf(stdout, "+ SHMTIS#: %d\n", st->info.num_shmtis); + + fprintf(stdout, "+ Primary UUID: "); + for (int i =3D 0; i < SCMI_TLM_DE_IMPL_UUID_MAX; i++) { + fprintf(stdout, "%02X", st->info.de_impl_version[i]); + switch (i) { + case 3: + case 5: + case 7: + case 9: + fprintf(stdout, "%c", '-'); + break; + case 15: + fprintf(stdout, "%c", '\n'); + break; + } + } + + 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 =3D (struct scmi_tlm_update_interval *)st->ivs->intervals; + for (int i =3D 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->ssl->num_shmtis); + shinfo =3D (struct scmi_tlm_shmti_info *)st->ssl->shmtis; + for (int i =3D 0; i < st->ssl->num_shmtis; i++) + fprintf(stdout, "\t[%d]:: FD:%u LEN:%u\n", + shinfo[i].sid, shinfo[i].fd, shinfo[i].len); + + if (st->info.num_des !=3D 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]))) { + dump_des(st); + dump_groups(st); + } +} + +static int discover_base_info(int fd, struct scmi_tlm_abi_info *info) +{ + int ret; + + info->size =3D sizeof(*info); + ret =3D ioctl(fd, SCMI_TLM_GET_ABI_INFO, info); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_ABI_INFO)); + return ret; + } + + return ret; +} + +static struct scmi_tlm_des_list *scmi_get_des_list(int fd, int num_des) +{ + struct scmi_tlm_des_list *dsl; + struct scmi_tlm_de_info *dei; + size_t sz, d_sz; + int ret; + + sz =3D sizeof(*dsl); + dsl =3D malloc(sz); + if (!dsl) + return NULL; + + bzero(dsl, sz); + + d_sz =3D sizeof(*dei) * num_des; + dei =3D malloc(d_sz); + if (!dei) { + free(dsl); + return NULL; + } + + bzero(dei, d_sz); + + dsl->num_des =3D num_des; + dsl->des =3D (uint64_t)dei; + ret =3D ioctl(fd, SCMI_TLM_GET_DE_LIST, dsl); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_LIST)); + return NULL; + } + + return dsl; +} + +static struct tlm_de *enumerate_des(struct tlm_state *st) +{ + struct scmi_tlm_des_list *dsl; + struct tlm_de *des; + + dsl =3D scmi_get_des_list(st->fd, st->info.num_des); + if (!dsl) + return NULL; + + st->num_des =3D dsl->num_des; + des =3D malloc(sizeof(*des) * st->num_des); + if (!des) + return NULL; + + bzero(des, sizeof(*des) * st->num_des); + for (int i =3D 0; i < st->num_des; i++) { + struct tlm_de *de =3D &des[i]; + int ret; + + de->info =3D &(((struct scmi_tlm_de_info *)dsl->des)[i]); + de->cfg.id =3D de->info->id; + ret =3D ioctl(st->fd, SCMI_TLM_GET_DE_CFG, &de->cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_CFG)); + continue; + } + } + + return des; +} + +static int get_current_config(int fd, struct scmi_tlm_config *cfg) +{ + int ret; + + ret =3D ioctl(fd, SCMI_TLM_GET_CFG, cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_CFG)); + return ret; + } + + return ret; +} + +static struct scmi_tlm_grps_list *scmi_get_grps_list(int fd, int num_group= s) +{ + struct scmi_tlm_grps_list *gsl; + struct scmi_tlm_grp_info *grps; + size_t sz, grps_sz; + int ret; + + sz =3D sizeof(*gsl); + gsl =3D malloc(sz); + if (!gsl) + return NULL; + + bzero(gsl, sz); + grps_sz =3D sizeof(*grps) * num_groups; + grps =3D malloc(grps_sz); + if (!grps) { + free(gsl); + return NULL; + } + + gsl->num_grps =3D num_groups; + gsl->grps =3D (uint64_t)grps; + ret =3D ioctl(fd, SCMI_TLM_GET_GRP_LIST, gsl); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_GRP_LIST)); + free(grps); + free(gsl); + return NULL; + } + + return gsl; +} + +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 =3D sizeof(*ivs); + ivs =3D malloc(sz); + if (!ivs) + return NULL; + + bzero(ivs, sz); + + i_sz =3D sizeof(*intervs) * num_intervals; + intervs =3D malloc(i_sz); + if (!intervs) { + free(ivs); + return NULL; + } + + bzero(intervs, i_sz); + + ivs->num_intervals =3D num_intervals; + ivs->intervals =3D (uint64_t)intervs; + if (grp_id) { + ivs->grp_id =3D *grp_id; + ivs->flags =3D SCMI_TLM_INTERV_GROUP; + } + + ret =3D ioctl(fd, SCMI_TLM_GET_INTRVS, ivs); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_INTRVS)); + free(ivs); + free(intervs); + return NULL; + } + + return ivs; +} + +static struct scmi_tlm_shmtis_list *enumerate_shmtis(struct tlm_state *st) +{ + struct scmi_tlm_shmti_info *shmtis; + struct scmi_tlm_shmtis_list *ssl; + size_t sz, shmtis_sz; + int ret; + + sz =3D sizeof(*ssl); + ssl =3D malloc(sz); + if (!ssl) + return NULL; + + bzero(ssl, sz); + shmtis_sz =3D sizeof(*shmtis) * st->info.num_shmtis; + shmtis =3D malloc(shmtis_sz); + if (!shmtis) { + free(ssl); + return NULL; + } + + ssl->num_shmtis =3D st->info.num_shmtis; + ssl->shmtis =3D (uint64_t)shmtis; + ret =3D ioctl(st->fd, SCMI_TLM_GET_SHMTI_LIST, ssl); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_SHMTI_LIST)); + free(shmtis); + free(ssl); + return NULL; + } + + return ssl; +} + +static struct tlm_group *enumerate_groups(struct tlm_state *st) +{ + struct scmi_tlm_grps_list *gsl; + struct tlm_group *grps; + + gsl =3D scmi_get_grps_list(st->fd, st->info.num_groups); + if (!gsl) + return NULL; + + st->num_groups =3D gsl->num_grps; + grps =3D malloc(sizeof(*grps) * st->num_groups); + if (!grps) + return NULL; + + bzero(grps, sizeof(*grps) * st->num_groups); + for (int i =3D 0; i < st->num_groups; i++) { + struct tlm_group *grp =3D &grps[i]; + size_t size, composing_sz; + uint32_t *composing_des; + int ret; + + grp->info =3D &(((struct scmi_tlm_grp_info *)gsl->grps)[i]); + size =3D sizeof(*grp->desc); + grp->desc =3D malloc(size); + if (!grp->desc) + return NULL; + + bzero(grp->desc, size); + composing_sz =3D sizeof(uint32_t) * grp->info->num_des; + composing_des =3D malloc(composing_sz); + if (!composing_des) //XXX BAD Cleanup + return NULL; + + grp->desc->grp_id =3D grp->info->grp_id; + grp->desc->num_des =3D grp->info->num_des; + grp->desc->composing_des =3D (uint64_t)composing_des; + ret =3D ioctl(st->fd, SCMI_TLM_GET_GRP_DESC, grp->desc); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_GRP_DESC)); + continue; + } + + grp->ivs =3D enumerate_intervals(st->fd, grp->info->num_intervals, + &grp->info->grp_id); + } + + return grps; +} + +static int gather_tlm_state(struct tlm_state *st) +{ + int ret; + + ret =3D discover_base_info(st->fd, &st->info); + if (ret) + return ret; + + st->ivs =3D enumerate_intervals(st->fd, st->info.num_intervals, NULL); + if (!st->ivs) + return -1; + + ret =3D get_current_config(st->fd, &st->cfg); + if (ret) + return ret; + + if (st->info.num_des) + st->des =3D enumerate_des(st); + + if (st->info.num_groups) + st->grps =3D enumerate_groups(st); + + if (st->info.num_shmtis) + st->ssl =3D enumerate_shmtis(st); + + return 0; +} + +static struct tlm_state *open_session(const char *path) +{ + struct tlm_state *st; + + st =3D malloc(sizeof(*st)); + if (!st) + return NULL; + + st->fd =3D open(path, O_RDWR); + if (st->fd < 0) { + perror("open"); + free(st); + return NULL; + } + + st->path =3D path; + + return st; +} + +#define MAX_GENERATIONS 5 + +#if 0 +static void get_tlm_generation(struct tlm_state *st) +{ + int fd, i =3D 0; + struct pollfd pfds[1]; + + fd =3D openat(st->dfd, "generation", O_RDONLY); + if (fd < 0) { + perror("openat"); + return; + } + + pfds[0].fd =3D fd; + pfds[0].events =3D POLLIN; + + do { + int ret; + + pfds[0].revents =3D 0; + ret =3D poll(pfds, 1, -1); + if (ret < 0) { + perror("poll generation"); + break; + } + + if (!pfds[0].revents) + continue; + + if (pfds[0].revents & POLLIN) { + int n; + char buf[32] =3D {}; + + n =3D read(fd, buf, 32); + if (n < 0) { + perror("read generation"); + break; + } + + fprintf(stdout, "Generation[%u]: %s\n", i, buf); + } + } while (i++ < MAX_GENERATIONS); + + close(fd); +} +#endif + +static int dump_shmti(struct scmi_tlm_shmti_info *shmti) +{ + void *tdcf; + ssize_t bytes =3D 0; + + tdcf =3D mmap(NULL, shmti->len, PROT_READ, MAP_SHARED, shmti->fd, 0); + if (tdcf =3D=3D MAP_FAILED) { + fprintf(stderr, "%s\n", strerror(errno)); + return -1; + } + + tdcf +=3D shmti->offset; + do { + bytes +=3D write(1, tdcf + bytes, shmti->len - bytes); + if (bytes < 0) + return -1; + } while (bytes < shmti->len); + + return bytes; +} + +static int shmti_dump(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_shmti_info *shmti; + unsigned long first, last; + + if (args->cnt > 1) { + first =3D last =3D strtoul(args->opts[1], NULL, 0); + if (first =3D=3D ULONG_MAX || errno =3D=3D -EINVAL || + first >=3D st->ssl->num_shmtis) + return -1; + } else { + first =3D 0; + last =3D st->ssl->num_shmtis - 1; + } + + for (int sid =3D first; sid <=3D last; sid++) { + int ret; + + shmti =3D &(((struct scmi_tlm_shmti_info *)(st->ssl->shmtis))[sid]); + fprintf(stderr, "Binary DUMP of SHMTI[%u] LEN:%u\n", + shmti->sid, shmti->len); + + ret =3D dump_shmti(shmti); + 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; + + shmtis =3D (struct scmi_tlm_shmti_info *)st->ssl->shmtis; + + fprintf(stdout, "\n- SHMTI mmap SANITY CHECK -\n-------------------\n"); + + for (int i =3D 0; i < st->ssl->num_shmtis; i++) { + unsigned long *start, *end; + void *tdcf; + char *buf; + + 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); + + tdcf =3D mmap(NULL, shmtis[i].len, PROT_READ, MAP_SHARED, + shmtis[i].fd, 0); + if (tdcf =3D=3D MAP_FAILED) { + fprintf(stderr, "%s\n", strerror(errno)); + return -1; + } + + start =3D tdcf + shmtis[i].offset; + end =3D tdcf + shmtis[i].offset + shmtis[i].len; + + fprintf(stdout, "-----------------------------------------------------\n= "); + fprintf(stdout, "Mapped SHMTI - SID:%u %u@%p\n", + shmtis[i].sid, shmtis[i].offset, tdcf); + + buf =3D (char *)start; + for (int j =3D 0; j < 4; j++) + fprintf(stdout, "%c ", buf[j]); + + fprintf(stdout, " =3D=3D=3D> "); + + buf =3D ((char *)end) - 4; + for (int j =3D 0 ; j < 4; j++) + fprintf(stdout, "%c ", buf[j]); + fprintf(stdout, "\n-----------------------------------------------------= \n"); + } + + return 0; +} + +static int shmti_ops(struct tlm_state *st, struct parsed_args *args) +{ + if (!args->cnt || !args->opts) + return -1; + + if (!strncmp(args->opts[0], "dump", strlen(args->opts[0]))) + return shmti_dump(st, args); + if (!strncmp(args->opts[0], "check", strlen(args->opts[0]))) + return shmti_check(st, args); + + return -1; +} + +static int parse_options(int argc, char **argv, struct parsed_args *args) +{ + int opt =3D 0; + + /* Set defaults */ + args->instance =3D "/dev/scmi/tlm_0"; + while ((opt =3D getopt(argc, argv, "i:h")) !=3D -1) { + switch (opt) { + case 'i': + args->instance =3D optarg; + break; + case 'h': + default: + return -EINVAL; + } + } + + if (optind <=3D argc) { + if (optind < argc - 1) { + args->cnt =3D argc - 1 - optind; + args->opts =3D malloc(sizeof(*args->opts) * args->cnt); + if (!args->opts) + return -1; + } + + args->cmd =3D argv[optind]; + for (int i =3D 0, j =3D optind + 1; j < argc; i++, j++) + args->opts[i] =3D argv[j]; + } + + return 0; +} + +static inline void usage(char *program_name) +{ + printf("Usage: %s [OPTIONS] [CMD [CMD_OPTS]...]\n", program_name); + printf("%s", USAGE_STRING); +} + +static inline struct scmi_tlm_data_read *bulk_buffer_alloc(struct tlm_stat= e *st) +{ + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read *bulk; + size_t bulk_sz, samples_sz; + + bulk_sz =3D sizeof(*bulk); + bulk =3D malloc(bulk_sz); + if (!bulk) + return NULL; + + bzero(bulk, bulk_sz); + samples_sz =3D st->info.num_des * sizeof(*samples); + samples =3D malloc(samples_sz); + if (!samples) { + free(bulk); + return NULL; + } + + bzero(samples, samples_sz); + bulk->samples =3D (uint64_t)samples; + bulk->num_samples =3D st->info.num_des; + + return bulk; +} + +static int single_read(struct tlm_state *st) +{ + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read *bulk; + int ret; + + bulk =3D bulk_buffer_alloc(st); + if (!bulk) + return -1; + + ret =3D ioctl(st->fd, SCMI_TLM_SINGLE_READ, bulk); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_SINGLE_READ)); + return -1; + } + + fprintf(stdout, "\n- Single ASYNC read -\n-------------------\n"); + samples =3D (struct scmi_tlm_de_sample *)bulk->samples; + for (int i =3D 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 bulk_read(struct tlm_state *st) +{ + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read *bulk; + int ret; + + bulk =3D bulk_buffer_alloc(st); + if (!bulk) + return -1; + + ret =3D ioctl(st->fd, SCMI_TLM_BULK_READ, bulk); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_BULK_READ)); + return -1; + } + + fprintf(stdout, "\n- BULK read -\n-------------------\n"); + samples =3D (struct scmi_tlm_de_sample *)bulk->samples; + for (int i =3D 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 tlm_reset(struct tlm_state *st) +{ + int ret; + + ret =3D ioctl(st->fd, SCMI_TLM_RESET); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_RESET)); + return -1; + } + + fprintf(stdout, "\n--- TLM RESET ISSUED\n"); + + return 0; +} + +static int batch_read(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read *bulk; + int ret; + + if (!args->opts || !args->cnt) + return -1; + + bulk =3D bulk_buffer_alloc(st); + if (!bulk) + return -1; + + samples =3D (struct scmi_tlm_de_sample *)bulk->samples; + bulk->num_samples =3D args->cnt; + for (int i =3D 0; i < bulk->num_samples; i++) { + unsigned long val; + + val =3D strtoul(args->opts[i], NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D -EINVAL) + continue; + + samples[i].id =3D (unsigned int)val; + } + + ret =3D ioctl(st->fd, SCMI_TLM_BATCH_READ, bulk); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_BATCH_READ)); + return -1; + } + + fprintf(stdout, "\n- BATCH read -\n-------------------\n"); + for (int i =3D 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 void +parse_item_cfg(char *str, bool *ena, bool *t_ena, unsigned int *update) +{ + char *tok; + + if (!ena) + return; + + tok =3D strtok(str, ":"); + if (tok) + *ena =3D strncmp(tok, "on", strlen(tok)) =3D=3D 0; + + if (!t_ena) + return; + + tok =3D strtok(NULL, ":"); + if (tok) + *t_ena =3D strncmp(tok, "on", strlen(tok)) =3D=3D 0; + + if (!update) + return; + + tok =3D strtok(NULL, ":"); + if (tok) { + unsigned long val; + + val =3D strtoul(tok, NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D -EINVAL) + return; + + *update =3D (unsigned int)val; + } +} + +static int configure_all_des(struct tlm_state *st, struct parsed_args *arg= s) +{ + struct scmi_tlm_de_config de_cfg =3D {}; + bool ena, t_ena; + int ret; + + if (!args->opts) + return -1; + + ret =3D ioctl(st->fd, SCMI_TLM_GET_ALL_CFG, &de_cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_ALL_CFG)); + return ret; + } + + 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 =3D !!ena; + de_cfg.t_enable =3D !!t_ena; + + fprintf(stdout, "--- [SET] ALL DEs ON:%d T_ON:%d\n", + de_cfg.enable, de_cfg.t_enable); + + ret =3D ioctl(st->fd, SCMI_TLM_SET_ALL_CFG, &de_cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_ALL_CFG)); + return ret; + } + + return 0; +} + +static int de_config_get(struct tlm_state *st, struct scmi_tlm_de_config *= de_cfg) +{ + int ret; + + /* Get current config */ + ret =3D ioctl(st->fd, SCMI_TLM_GET_DE_CFG, de_cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_CFG)); + return ret; + } + + return 0; +} + +static int configure_de(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_de_config de_cfg =3D {}; + bool ena, t_ena; + unsigned long val; + int ret; + + if (!args->opts) + return -1; + + if (args->cnt < 2) { + /* Dump all DE configs */ + for (int i =3D 0; i < st->num_des; i++) { + de_cfg.id =3D st->des[i].info->id; + ret =3D de_config_get(st, &de_cfg); + if (!ret) + fprintf(stdout, + "--- [GET] -> DE:0x%08X ON:%d T_ON:%d\n", + de_cfg.id, de_cfg.enable, de_cfg.t_enable); + } + + return 0; + } + + /*Dump one DE config */ + val =3D strtoul(args->opts[1], NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D -EINVAL) + return -1; + + de_cfg.id =3D (unsigned int)val; + ret =3D de_config_get(st, &de_cfg); + if (ret) + return ret; + + fprintf(stdout, "--- [GET] -> DE:0x%08X ON:%d T_ON:%d\n", + de_cfg.id, de_cfg.enable, de_cfg.t_enable); + + if (args->cnt < 3) + return 0; + + /* Configure one DE: */ + parse_item_cfg(args->opts[2], &ena, &t_ena, NULL); + de_cfg.enable =3D !!ena; + de_cfg.t_enable =3D !!t_ena; + + fprintf(stdout, "--- [SET] -> DE:0x%08X ON:%d T_ON:%d\n", + de_cfg.id, de_cfg.enable, de_cfg.t_enable); + + ret =3D ioctl(st->fd, SCMI_TLM_SET_DE_CFG, &de_cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_DE_CFG)); + return ret; + } + + return 0; +} + +static int grp_config_get(struct tlm_state *st, struct scmi_tlm_config *cf= g) +{ + int ret; + + /* Get current config */ + ret =3D ioctl(st->fd, SCMI_TLM_GET_CFG, cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_CFG)); + return ret; + } + + return 0; +} + +static int configure_group(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_config cfg =3D {}; + 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 =3D 0; i < st->num_groups; i++) { + cfg.grp_id =3D st->grps[i].info->grp_id; + cfg.active.exp =3D -3; + cfg.flags =3D SCMI_TLM_CONFIG_GROUP; + ret =3D 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 =3D strtoul(args->opts[1], NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D -EINVAL) + return -1; + + cfg.grp_id =3D (unsigned int)val; + cfg.flags =3D SCMI_TLM_CONFIG_GROUP; + cfg.active.exp =3D -3; + ret =3D 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 =3D !!ena; + cfg.t_enable =3D !!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 =3D ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG)); + return ret; + } + + return 0; +} + +static int configure_telemetry(struct tlm_state *st, struct parsed_args *a= rgs) +{ + struct scmi_tlm_config cfg =3D st->cfg; + bool val; + int ret; + + if (args->cnt < 2 || !args->opts) + return -1; + + val =3D strncmp(args->opts[1], "on", strlen(args->opts[1])) =3D=3D 0; + + cfg.enable =3D !!val; + cfg.flags =3D 0; + + fprintf(stdout, "\n---%sabling Telemetry\n", val ? "En" : "Dis"); + ret =3D ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG)); + return ret; + } + + return 0; +} + +static int configure_update(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_config cfg =3D st->cfg; + unsigned long val; + int ret; + + if (args->cnt < 2 || !args->opts) + return -1; + + val =3D strtoul(args->opts[1], NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D -EINVAL) + return -1; + + cfg.flags =3D 0; + if (!val) { + cfg.enable =3D val; + fprintf(stdout, "\n---Disabling Telemetry\n"); + } else { + cfg.enable =3D !!val; + cfg.active.secs =3D val; + cfg.active.exp =3D -3; + fprintf(stdout, "\n--- Setting update interval to %lu\n", val); + } + + ret =3D ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG)); + return ret; + } + + return 0; +} + +static int configure(struct tlm_state *st, struct parsed_args *args) +{ + if (!args->cnt || !args->opts) + return -1; + + 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 -1; +} + +static inline void full_scan_and_dump(struct tlm_state *st) +{ + dump_state(st, NULL); + dump_des(st); + dump_groups(st); +} + +int main(int argc, char **argv) +{ + struct parsed_args args =3D {}; + struct tlm_state *st; + int ret; + + //get_tlm_generation(&st); + + ret =3D parse_options(argc, argv, &args); + if (ret) { + usage(argv[0]); + return ret; + } + + st =3D open_session(args.instance); + if (!st) + return -1; + + ret =3D gather_tlm_state(st); + if (ret) { + free(st); + return ret; + } + + if (!args.cmd) { + full_scan_and_dump(st); + } else if (!strncmp(args.cmd, "info", strlen(args.cmd))) { + dump_state(st, &args); + } else if (!strncmp(args.cmd, "shmti", strlen(args.cmd))) { + shmti_ops(st, &args); + } else if (!strncmp(args.cmd, "des", strlen(args.cmd))) { + dump_des(st); + } else if (!strncmp(args.cmd, "groups", strlen(args.cmd))) { + dump_groups(st); + } else if (!strncmp(args.cmd, "cfg", strlen(args.cmd))) { + configure(st, &args); + } else if (!strncmp(args.cmd, "single", strlen(args.cmd))) { + single_read(st); + } else if (!strncmp(args.cmd, "bulk", strlen(args.cmd))) { + bulk_read(st); + } else if (!strncmp(args.cmd, "batch", strlen(args.cmd))) { + batch_read(st, &args); + } else if (!strncmp(args.cmd, "reset", strlen(args.cmd))) { + tlm_reset(st); + } else { + fprintf(stderr, "Unknown command '%s'\n", args.cmd); + usage(argv[0]); + return -1; + } + + return 0; +} --=20 2.54.0