From nobody Thu Oct 9 02:16:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 40F78221F12; Fri, 20 Jun 2025 19:28: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=1750447738; cv=none; b=S0u9YqkA1zEqjMGM6sqr0Kq11AeucJGl/V2tHQ2lcobuLB/4/Vx4tp15sy46aqclY0Mi+nT//fPdGQAZZNb89G9CAZPykMU0GKfZefHH3eRnCvtgNfw2stviIForbyTUy86T/8cB8zSZwlk9bfdBoXM43v0Gz3b8Qv4EaUcp1ag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750447738; c=relaxed/simple; bh=9ZMgjZCPC+lBHp0QqotHidVX4Qfl3YhujfAoMy/663c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A/ggOnnXD2gY7y9rrllQqsuZzapaqVx66l4LE6wrpdWp8Ys6ClyVlFjTfUkVXlxsaX1ODgBZ2O/RZDb1iep/nVNowBGZLtyEstNJs+0+5C7VjWU0ZnAMF71GjnpyF8Gq+cT4xTaebbUzqlbh+eu27E/yD/KIOmhCAjL+o3W7Du8= 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; 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 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 DDA9C16F2; Fri, 20 Jun 2025 12:28:35 -0700 (PDT) Received: from pluto.fritz.box (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 255623F673; Fri, 20 Jun 2025 12:28:53 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org Cc: sudeep.holla@arm.com, 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, quic_sibis@quicinc.com, dan.carpenter@linaro.org, d-gole@ti.com, souvik.chakravarty@arm.com, Cristian Marussi Subject: [RFC PATCH 1/7] firmware: arm_scmi: Define a common SCMI_MAX_PROTOCOLS value Date: Fri, 20 Jun 2025 20:28:07 +0100 Message-ID: <20250620192813.2463367-2-cristian.marussi@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250620192813.2463367-1-cristian.marussi@arm.com> References: <20250620192813.2463367-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 common definition of SCMI_MAX_PROTOCOLS and use it all over the SCMI stack. Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/notify.c | 4 +--- include/linux/scmi_protocol.h | 3 +++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi= /notify.c index e160ecb22948..27a53a6729dd 100644 --- a/drivers/firmware/arm_scmi/notify.c +++ b/drivers/firmware/arm_scmi/notify.c @@ -94,8 +94,6 @@ #include "common.h" #include "notify.h" =20 -#define SCMI_MAX_PROTO 256 - #define PROTO_ID_MASK GENMASK(31, 24) #define EVT_ID_MASK GENMASK(23, 16) #define SRC_ID_MASK GENMASK(15, 0) @@ -1652,7 +1650,7 @@ int scmi_notification_init(struct scmi_handle *handle) ni->gid =3D gid; ni->handle =3D handle; =20 - ni->registered_protocols =3D devm_kcalloc(handle->dev, SCMI_MAX_PROTO, + ni->registered_protocols =3D devm_kcalloc(handle->dev, SCMI_MAX_PROTOCOLS, sizeof(char *), GFP_KERNEL); if (!ni->registered_protocols) goto err; diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 688466a0e816..6f8d36e1f8fc 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -926,8 +926,11 @@ enum scmi_std_protocol { SCMI_PROTOCOL_VOLTAGE =3D 0x17, SCMI_PROTOCOL_POWERCAP =3D 0x18, SCMI_PROTOCOL_PINCTRL =3D 0x19, + SCMI_PROTOCOL_LAST =3D 0xff, }; =20 +#define SCMI_MAX_PROTOCOLS (SCMI_PROTOCOL_LAST + 1) + enum scmi_system_events { SCMI_SYSTEM_SHUTDOWN, SCMI_SYSTEM_COLDRESET, --=20 2.47.0 From nobody Thu Oct 9 02:16:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9ED1E2080E8; Fri, 20 Jun 2025 19:28:58 +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=1750447740; cv=none; b=GnMzRa9kNJlGpoAvJFIJQE/4DwKEd3p5qm/ZfMSdTorW+qZX7W2qrcKNjcBl3XsMEWLpkLvox4z6mp9TLexR+pT2ut03wHjvnCWifBW9uV0FOBXaWtbljOT7nKoIGcBRZqW4wpwut9oRPBM2Z8OseBUZq5Z71V8pQnZCigGebuU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750447740; c=relaxed/simple; bh=0icq1GTsalXqIWlS88Mp17MAFTw4w4WuKuqL7RadlYg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kRjLiqve7FaiM3Q+Bk3FM/bj+LsljjCYPi5rKMU4nRYAd3jyGl/SZ3Nn8Z56fIXgRifHqAuRwO4K90jAjGA3FYBb9ZfIXetFFsFxiGOXnunJ79WRx/D+y5v7dTY25t5/AUTtCZfqZhel1ZNmTftI/wL9eGDE3oVfxy5nHtx74Hw= 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; 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 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 6208C176A; Fri, 20 Jun 2025 12:28:38 -0700 (PDT) Received: from pluto.fritz.box (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9F2713F673; Fri, 20 Jun 2025 12:28:55 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org Cc: sudeep.holla@arm.com, 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, quic_sibis@quicinc.com, dan.carpenter@linaro.org, d-gole@ti.com, souvik.chakravarty@arm.com, Cristian Marussi Subject: [RFC PATCH 2/7] firmware: arm_scmi: Allow protocols to register for notifications Date: Fri, 20 Jun 2025 20:28:08 +0100 Message-ID: <20250620192813.2463367-3-cristian.marussi@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250620192813.2463367-1-cristian.marussi@arm.com> References: <20250620192813.2463367-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 providing their own notifier callbacks. While at that, allow for a protocol to register events with compilation-time unknown report/event sizes: such events will use the maximum transport size. Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/common.h | 4 ++++ drivers/firmware/arm_scmi/driver.c | 12 ++++++++++++ drivers/firmware/arm_scmi/notify.c | 27 ++++++++++++++++++++------- drivers/firmware/arm_scmi/notify.h | 8 ++++++-- drivers/firmware/arm_scmi/protocols.h | 8 ++++++++ 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi= /common.h index dab758c5fdea..daf500503166 100644 --- a/drivers/firmware/arm_scmi/common.h +++ b/drivers/firmware/arm_scmi/common.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -498,4 +499,7 @@ static struct platform_driver __drv =3D { \ 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_notifier_register(const struct scmi_handle *handle, u8 proto_id, + u8 evt_id, const u32 *src_id, + struct notifier_block *nb); #endif /* _SCMI_COMMON_H */ diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi= /driver.c index 395fe9289035..1b9404175098 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -1661,6 +1661,17 @@ 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) +{ + const struct scmi_protocol_instance *pi =3D ph_to_pi(ph); + + return scmi_notifier_register(pi->handle, pi->proto->id, + evt_id, src_id, nb); +} + static const struct scmi_xfer_ops xfer_ops =3D { .version_get =3D version_get, .xfer_get_init =3D xfer_get_init, @@ -2161,6 +2172,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.notifier_register =3D scmi_register_instance_notifier; refcount_set(&pi->users, 1); /* proto->init is assured NON NULL by scmi_protocol_register */ ret =3D pi->proto->instance_init(&pi->ph); diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi= /notify.c index 27a53a6729dd..2ae705dab2fd 100644 --- a/drivers/firmware/arm_scmi/notify.c +++ b/drivers/firmware/arm_scmi/notify.c @@ -589,7 +589,12 @@ 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; } @@ -748,7 +753,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; @@ -763,6 +768,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; @@ -775,8 +782,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++) + 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_t(size_t, 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, @@ -805,7 +817,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 @@ -1352,9 +1365,9 @@ static int scmi_event_handler_enable_events(struct sc= mi_event_handler *hndl) * * Return: 0 on Success */ -static int scmi_notifier_register(const struct scmi_handle *handle, - u8 proto_id, u8 evt_id, const u32 *src_id, - struct notifier_block *nb) +int scmi_notifier_register(const struct scmi_handle *handle, + u8 proto_id, u8 evt_id, const u32 *src_id, + struct notifier_block *nb) { int ret =3D 0; u32 evt_key; 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 diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_s= cmi/protocols.h index d62c4469d1fd..2e40a7bb5b01 100644 --- a/drivers/firmware/arm_scmi/protocols.h +++ b/drivers/firmware/arm_scmi/protocols.h @@ -161,8 +161,13 @@ struct scmi_proto_helpers_ops; * @dev: A reference to the associated SCMI instance device (handle->dev). * @xops: A reference to a struct holding refs to the core xfer operations= that * can be used by the protocol implementation to generate SCMI messages. + * @hops: A reference to a struct holding refs to the common helper operat= ions + * that can be used by the protocol implementation. * @set_priv: A method to set protocol private data for this instance. * @get_priv: A method to get protocol private data previously set. + * @notifier_register: A method to register interest for notifications from + * within a protocol implementation unit: notifiers can + * be registered only for the same protocol. * * This structure represents a protocol initialized against specific SCMI * instance and it will be used as follows: @@ -182,6 +187,9 @@ struct scmi_protocol_handle { int (*set_priv)(const struct scmi_protocol_handle *ph, void *priv, u32 version); void *(*get_priv)(const struct scmi_protocol_handle *ph); + int (*notifier_register)(const struct scmi_protocol_handle *ph, + u8 evt_id, const u32 *src_id, + struct notifier_block *nb); }; =20 /** --=20 2.47.0 From nobody Thu Oct 9 02:16:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 693812EBBA9; Fri, 20 Jun 2025 19:29:01 +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=1750447745; cv=none; b=I26XxbiKiTBm5VmerjwGv8e94ILXMgqlOnpSPFumDGRGZsGu4cYNvsuA7fzPLnYOyBaGSBcT3qKKYtVMH1SQ+k24xapZfONAGH/rMP/CtfwfgppWT1Chky0NQDmJMPlbKzNiUj5oz9XPgg29NUwuNKCVOnfOBVQ6DvXt3O+ahDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750447745; c=relaxed/simple; bh=1mBMa0j54XAs8/pcKATd4UKU8viPI5T6UeJ1s7MotVA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YqYe2ctLmKdbD+iRYSTkiLfiMWnW4/fU0ZMOi55NEMEda0/z1Ce36ZxdhUt0sgzUtldGiPNtjSebqM/ywBXnzMUewe+hWO2RX996ybjhEGyrFxQDVx/5+MWUd3dl1BjKgNPcQ8vwujJq7LXsJeiudFoMIaxVz+8EQVL8ajn3v7k= 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; 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 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 2EAEC16A3; Fri, 20 Jun 2025 12:28:41 -0700 (PDT) Received: from pluto.fritz.box (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 29BDE3F673; Fri, 20 Jun 2025 12:28:58 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org Cc: sudeep.holla@arm.com, 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, quic_sibis@quicinc.com, dan.carpenter@linaro.org, d-gole@ti.com, souvik.chakravarty@arm.com, Cristian Marussi Subject: [RFC PATCH 3/7] firmware: arm_scmi: Add Telemetry protocol support Date: Fri, 20 Jun 2025 20:28:09 +0100 Message-ID: <20250620192813.2463367-4-cristian.marussi@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250620192813.2463367-1-cristian.marussi@arm.com> References: <20250620192813.2463367-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 basic support for SCMI V4.0-alpha_0 Telemetry protocol including SHMTI, FastChannels, Notifications and Single Sample Reads collection methods. Still lacking: - Block timestamp lines - Handling of already-on-at-boot Telemetry setup - Complete groups support (offset on enable and ungrouped DEs) - mmap access operations for RAW access Signed-off-by: Cristian Marussi --- 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 | 1719 +++++++++++++++++++++++++ include/linux/scmi_protocol.h | 198 ++- 5 files changed, 1920 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 1b9404175098..b235fc7f1a65 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -3445,6 +3445,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); } @@ -3463,6 +3464,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 2e40a7bb5b01..edd83a02e272 100644 --- a/drivers/firmware/arm_scmi/protocols.h +++ b/drivers/firmware/arm_scmi/protocols.h @@ -387,5 +387,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..3cbad06251a9 --- /dev/null +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -0,0 +1,1719 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * System Control and Management Interface (SCMI) Telemetry Protocol + * + * Copyright (C) 2025 ARM Ltd. + * + */ + +#include +#include +#include +#include +#include +#include + +#include "protocols.h" +#include "notify.h" + +/* 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, //TODO IMPLEMENT + TELEMETRY_CONFIG_SET =3D 0x8, + TELEMETRY_READING_COMPLETE =3D TELEMETRY_CONFIG_SET, + TELEMETRY_CONFIG_GET =3D 0x9, //TODO IMPLEMENT ! + TELEMETRY_RESET =3D 0xA, +}; + +struct scmi_msg_resp_telemetry_protocol_attributes { + __le32 de_num; + __le32 groups_num; + __le32 de_implementation_rev_dword[SCMI_TLM_MAX_DWORD]; + __le32 attributes; +#define SUPPORTS_SINGLE_READ(x) ((x) & BIT(31)) +#define SUPPORTS_CONTINUOS_UPDATE(x) ((x) & BIT(30)) +#define SUPPORTS_PER_GROUP_CONFIG(x) ((x) & BIT(18)) +#define SUPPORTS_RESET(x) ((x) & BIT(17)) +#define SUPPORTS_FC(x) ((x) & BIT(16)) +}; + +struct scmi_telemetry_update_notify_payld { + __le32 agent_id; + __le32 status; + __le32 num_dwords; + __le32 array[]; +}; + +struct scmi_shmti_desc { + __le32 id; + __le32 addr_low; + __le32 addr_high; + __le32 length; +}; + +struct scmi_msg_resp_telemetry_shmti_list { + __le32 num_shmti; + struct scmi_shmti_desc desc[]; +}; + +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) ((d)->attr_1 & BIT(31)) +#define IS_FC_SUPPORTED(d) ((d)->attr_1 & BIT(30)) +#define GET_DE_TYPE(d) (le32_get_bits((d)->attr_1, GENMASK(29, 22))) +#define IS_PERSISTENT(d) ((d)->attr_1 & BIT(21)) +#define GET_DE_UNIT_EXP(d) \ + ({ \ + int __signed_exp =3D \ + le32_get_bits((d)->attr_1, GENMASK(20, 13)); \ + \ + if (__signed_exp & BIT(7)) \ + __signed_exp |=3D GENMASK(31, 8); \ + __signed_exp; \ + }) +#define GET_DE_UNIT(d) (le32_get_bits((d)->attr_1, GENMASK(12, 5))) + +#define GET_DE_TSTAMP_EXP(d) \ + ({ \ + int __signed_exp =3D \ + FIELD_GET(GENMASK(4, 1), (d)->attr_1); \ + \ + if (__signed_exp & BIT(3)) \ + __signed_exp |=3D GENMASK(31, 4); \ + __signed_exp; \ + }) +#define IS_TSTAMP_SUPPORTED(d) ((d)->attr_1 & BIT(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; +}; + +struct scmi_msg_resp_telemetry_de_description { + __le32 num_desc; + struct scmi_de_desc desc[]; +}; + +struct scmi_msg_telemetry_update_intervals { + __le32 index; + __le32 group_identifier; +#define ALL_DES_NO_GROUP 0x0 +#define SPECIFIC_GROUP_DES 0x1 +#define ALL_DES_ANY_GROUP 0x2 + __le32 flags; +}; + +struct scmi_msg_resp_telemetry_update_intervals { + __le32 flags; +#define INTERVALS_DISCRETE(x) (!((x) & BIT(12))) + __le32 intervals[]; +}; + +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 tdcf_de_offset; +}; + +struct scmi_msg_telemetry_config_set { + __le32 grp_id; + __le32 control; +#define TELEMETRY_ENABLE (BIT(0)) + +#define TELEMETRY_MODE(x) (FIELD_PREP(GENMASK(4, 1), (x))) +#define TELEMETRY_MODE_ONDEMAND TELEMETRY_MODE(0) +#define TELEMETRY_MODE_NOTIFS TELEMETRY_MODE(1) +#define TELEMETRY_MODE_SINGLE TELEMETRY_MODE(2) + +#define TELEMETRY_SELECTOR(x) (FIELD_PREP(GENMASK(8, 5), (x))) +#define TELEMETRY_SELECTOR_ORPHANS TELEMETRY_SELECTOR(0) +#define TELEMETRY_SELECTOR_GROUP TELEMETRY_SELECTOR(1) +#define TELEMETRY_SELECTOR_ALL TELEMETRY_SELECTOR(2) + __le32 sampling_rate; +}; + +struct scmi_msg_resp_telemetry_reading_complete { + __le32 num_dwords; + __le32 dwords[]; +}; + +/* TDCF */ + +#define TO_CPU_64(h, l) (((u64)le32_to_cpu((h)) << 32) | le32_to_cpu((l))) + +struct fc_line { + u32 data_low; + u32 data_high; +}; + +struct fc_tsline { + u32 data_low; + u32 data_high; + u32 ts_low; + u32 ts_high; +}; + +struct line { + u32 data_low; + u32 data_high; +}; + +#define BLK_TSTAMP(l) ((((u64)(l)->data_high) << 32) | (l)->data_low) + +struct blk_tsline { + u32 ts_low; + u32 ts_high; +}; + +struct tsline { + u32 data_low; + u32 data_high; + u32 ts_low; + u32 ts_high; +}; + +#define LINE_DATA_GET(f) (TO_CPU_64((f)->data_high, (f)->data_low)) +#define LINE_TSTAMP_GET(f) (TO_CPU_64((f)->ts_high, (f)->ts_low)) + +struct payload { + u32 meta; +#define IS_BLK_TS(x) ((x)->meta & BIT(4)) +#define USE_BLK_TS(x) ((x)->meta & BIT(3)) +#define USE_LINE_TS(x) ((x)->meta & BIT(2)) +#define TS_VALID(x) ((x)->meta & BIT(1)) +#define DATA_INVALID(x) ((x)->meta & BIT(0)) + u32 id; + union { + struct line l; + struct tsline tsl; + struct blk_tsline blk_tsl; + }; +}; + +#define LINE_DATA_PAYLD_WORDS \ + ((sizeof(u32) + sizeof(u32) + sizeof(struct line)) / sizeof(u32)) +#define TS_LINE_DATA_PAYLD_WORDS \ + ((sizeof(u32) + sizeof(u32) + sizeof(struct tsline)) / sizeof(u32)) + +struct prlg { + u32 seq_low; + u32 seq_high; + u32 num_qwords; + u32 _meta_header_high; +}; + +struct eplg { + u32 seq_low; + u32 seq_high; +}; + +#define TDCF_EPLG_SZ (sizeof(struct eplg)) + +struct tdcf { + struct prlg prlg; + unsigned char payld[]; +}; + +#define TDCF_START_SEQ_GET(x) \ + ({ \ + u64 _val; \ + struct prlg *_p =3D &((x)->prlg); \ + \ + _val =3D TO_CPU_64(_p->seq_high, _p->seq_low); \ + (_val); \ + }) + +#define IS_BAD_START_SEQ(s) ((s) & 0x1) + +#define TDCF_END_SEQ_GET(e) \ + ({ \ + u64 _val; \ + struct eplg *_e =3D (e); \ + \ + _val =3D TO_CPU_64(_e->seq_high, _e->seq_low); \ + (_val); \ + }) + +struct telemetry_shmti { + int id; + void __iomem *base; + u32 len; +}; + +#define SHMTI_EPLG(s) \ + ({ \ + struct telemetry_shmti *_s =3D (s); \ + void *_eplg; \ + \ + _eplg =3D _s->base + _s->len; \ + (_eplg); \ + }) + +struct telemetry_info { + bool streaming_mode; + int num_shmti; + struct device *dev; + struct telemetry_shmti *shmti; + struct xarray xa_des; + struct scmi_telemetry_info info; + struct notifier_block telemetry_nb; +}; + +#define telemetry_nb_to_info(x) \ + container_of(x, struct telemetry_info, telemetry_nb) + +struct telemetry_de { + bool cached; + char name[SCMI_SHORT_NAME_MAX_SIZE]; + void __iomem *base; + void __iomem *eplg; + u32 offset; + /* NOTE THAT DE data_sz is registered in scmi_telemetry_de */ + u32 fc_size; + struct scmi_telemetry_de de; + u64 last_val; + u64 last_ts; + /* Protect last_val/ts accesses */ + struct mutex mtx; +}; + +#define to_tde(d) container_of(d, struct telemetry_de, de) + +struct scmi_tlm_de_priv { + struct telemetry_info *ti; + void *next; +}; + +static int +scmi_telemetry_protocol_attributes_get(const struct scmi_protocol_handle *= ph, + struct telemetry_info *ti) +{ + int ret; + struct scmi_xfer *t; + struct scmi_msg_resp_telemetry_protocol_attributes *resp; + + 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.num_de =3D le32_to_cpu(resp->de_num); + ti->info.num_groups =3D le32_to_cpu(resp->groups_num); + for (int i =3D 0; i < SCMI_TLM_MAX_DWORD; i++) + ti->info.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)); + /* Allocate DEs descriptors */ + ti->info.des =3D devm_kcalloc(ph->dev, ti->info.num_de, + sizeof(*ti->info.des), GFP_KERNEL); + if (!ti->info.des) + ret =3D -ENOMEM; + + /* Allocate DE GROUPS descriptors */ + ti->info.des_groups =3D devm_kcalloc(ph->dev, ti->info.num_groups, + sizeof(*ti->info.des_groups), + GFP_KERNEL); + if (!ti->info.des_groups) + ret =3D -ENOMEM; + + for (int i =3D 0; i < ti->info.num_groups; i++) + ti->info.des_groups[i].id =3D i; + } + + 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)); + + /* Initialized to first descriptor */ + p->next =3D (void *)r->desc; + + 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 telemetry_de *tde; + struct scmi_tlm_de_priv *p =3D priv; + const struct scmi_de_desc *desc =3D p->next; + unsigned int grp_id; + int ret; + + tde =3D to_tde(p->ti->info.des[st->desc_index + st->loop_idx]); + + tde->de.id =3D le32_to_cpu(desc->id); + grp_id =3D le32_to_cpu(desc->grp_id); + if (grp_id !=3D SCMI_TLM_GRP_INVALID) { + if (grp_id >=3D p->ti->info.num_groups) + return -EINVAL; + + /* Link to parent group */ + tde->de.grp =3D &p->ti->info.des_groups[grp_id]; + } + tde->de.data_sz =3D le32_to_cpu(desc->data_sz); + tde->de.type =3D GET_DE_TYPE(desc); + tde->de.unit =3D GET_DE_UNIT(desc); + tde->de.unit_exp =3D GET_DE_UNIT_EXP(desc); + tde->de.tstamp_exp =3D GET_DE_TSTAMP_EXP(desc); + tde->de.instance_id =3D GET_DE_INSTA_ID(desc); + tde->de.compo_instance_id =3D GET_COMPO_INSTA_ID(desc); + tde->de.compo_type =3D GET_COMPO_TYPE(desc); + tde->de.persistent =3D IS_PERSISTENT(desc); + tde->de.tstamp_support =3D IS_TSTAMP_SUPPORTED(desc); + tde->de.fc_support =3D IS_FC_SUPPORTED(desc); + tde->de.name_support =3D IS_NAME_SUPPORTED(desc); + p->next +=3D sizeof(*desc); + if (tde->de.fc_support) { + u32 size; + u64 phys_addr; + void __iomem *addr; + struct de_desc_fc *dfc; + + dfc =3D p->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(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 */ + p->next +=3D sizeof(*dfc); + } + + if (tde->de.name_support) { + const char *de_name =3D p->next; + + strscpy(tde->name, de_name, SCMI_SHORT_NAME_MAX_SIZE); + tde->de.name =3D tde->name; + + /* Variably sized depending on name support */ + p->next +=3D SCMI_SHORT_NAME_MAX_SIZE; + } + + /* Store DE pointer by de_id */ + ret =3D xa_insert(&p->ti->xa_des, tde->de.id, &tde->de, GFP_KERNEL); + if (ret) + return ret; + + /* Account for this DE in group num_de counter */ + if (tde->de.grp) + tde->de.grp->num_de++; + + return 0; +} + +static int +scmi_telemetry_de_groups_init(struct device *dev, struct telemetry_info *t= i) +{ + /* Allocate all groups DEs IDs arrays at first ... */ + for (int i =3D 0; i < ti->info.num_groups; i++) { + struct scmi_telemetry_group *grp =3D &ti->info.des_groups[i]; + + grp->des =3D devm_kcalloc(dev, grp->num_de, sizeof(unsigned int), + GFP_KERNEL); + if (!grp->des) + return -ENOMEM; + + /* + * Max size 32bit ID string in Hex: 0xCAFECAFE + * - 10 digits + ' '/'\n' =3D 11 bytes per number + * - terminating NUL character + */ + grp->des_str_sz =3D grp->num_de * 11 + 1; + grp->des_str =3D devm_kzalloc(dev, grp->des_str_sz, GFP_KERNEL); + if (!grp->des_str) + return -ENOMEM; + + /* Reset group DE counter */ + grp->num_de =3D 0; + } + + /* Scan DEs and populate group DE IDs arrays */ + for (int i =3D 0; i < ti->info.num_de; i++) { + struct scmi_telemetry_group *grp =3D ti->info.des[i]->grp; + + if (!grp) + continue; + + /* Note that, at this point, num_de is guaranteed to be + * sane (in-bounds) by construction. + */ + grp->des[grp->num_de++] =3D i; + } + + /* Build compsing DES string */ + for (int i =3D 0; i < ti->info.num_groups; i++) { + struct scmi_telemetry_group *grp =3D &ti->info.des_groups[i]; + char *buf =3D grp->des_str; + size_t bufsize =3D grp->des_str_sz; + + for (int j =3D 0; j < grp->num_de; j++) { + char term =3D j !=3D (grp->num_de - 1) ? ' ' : '\0'; + int len; + + len =3D snprintf(buf, bufsize, "0x%04X%c", + ti->info.des[grp->des[j]]->id, term); + + buf +=3D len; + bufsize -=3D len; + } + } + + return 0; +} + +static int +scmi_telemetry_de_descriptors_get(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_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; + + xa_init(&ti->xa_des); + iter =3D ph->hops->iter_response_init(ph, &ops, ti->info.num_de, + 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); +} + +static int scmi_telemetry_enumerate_de(const struct scmi_protocol_handle *= ph, + struct telemetry_info *ti) +{ + int ret; + + if (!ti->info.num_de) + return 0; + + for (int i =3D 0; i < ti->info.num_de; i++) { + struct telemetry_de *tde; + + tde =3D devm_kzalloc(ph->dev, sizeof(*tde), GFP_KERNEL); + if (!tde) + return -ENOMEM; + + mutex_init(&tde->mtx); + ti->info.des[i] =3D &tde->de; + } + + ret =3D scmi_telemetry_de_descriptors_get(ph, ti); + if (ret) { + dev_err(ph->dev, "Cannot get DE descriptors"); + return ret; + } + + return 0; +} + +struct scmi_tlm_ivl_priv { + struct device *dev; + struct scmi_telemetry_update_interval *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 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)); + + /* + * 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; + + p->intrvs->discrete =3D INTERVALS_DISCRETE(r->flags); + /* Check consistency on first call */ + if (!p->intrvs->discrete && + (st->num_returned !=3D 3 || st->num_remaining !=3D 0)) + return -EINVAL; + + p->intrvs->num =3D st->num_returned + st->num_remaining; + p->intrvs->update_intervals =3D + devm_kcalloc(p->dev, p->intrvs->num, + sizeof(*p->intrvs->update_intervals), + GFP_KERNEL); + if (!p->intrvs->update_intervals) { + p->intrvs->num =3D 0; + return -ENOMEM; + } + + st->max_resources =3D p->intrvs->num; + } + + 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; + unsigned int idx =3D st->loop_idx; + + p->intrvs->update_intervals[st->desc_index + idx] =3D r->intervals[idx]; + + return 0; +} + +static int +scmi_tlm_enumerate_update_intervals(const struct scmi_protocol_handle *ph, + struct telemetry_info *ti, 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, + }; + struct scmi_tlm_ivl_priv ipriv =3D { + .dev =3D ph->dev, + .grp_id =3D grp_id, + .intrvs =3D (grp_id =3D=3D SCMI_TLM_GRP_INVALID) ? + &ti->info.intervals : + &ti->info.des_groups[grp_id].intervals, + .flags =3D flags, + }; + void *iter; + + 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); + + return ph->hops->iter_response_run(iter); +} + +static int +scmi_telemetry_enumerate_update_intervals(const struct scmi_protocol_handl= e *ph, + struct telemetry_info *ti) +{ + int ret; + unsigned int flags; + + flags =3D !ti->info.per_group_config_support ? + ALL_DES_ANY_GROUP : ALL_DES_NO_GROUP; + + ret =3D scmi_tlm_enumerate_update_intervals(ph, ti, SCMI_TLM_GRP_INVALID, + flags); + if (ret) + return ret; + + if (ti->info.num_groups && ti->info.per_group_config_support) { + flags =3D SPECIFIC_GROUP_DES; + for (int grp_id =3D 0; grp_id < ti->info.num_groups; grp_id++) { + ret =3D scmi_tlm_enumerate_update_intervals(ph, ti, grp_id, + flags); + if (ret) + break; + } + } + + return ret; +} + +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)); + + 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->id =3D le32_to_cpu(desc->id); + 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); + addr =3D devm_ioremap(ph->dev, phys_addr, len); + if (!addr) + return -EADDRNOTAVAIL; + + shmti->base =3D addr; + shmti->len =3D len; + + return 0; +} + +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->info.num_de, + 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(const struct scmi_protocol_handl= e *ph, + struct telemetry_info *ti) +{ + 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; + } + + 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 int scmi_telemetry_tdcf_parse_one(struct telemetry_info *ti, + struct payload __iomem *payld, + struct telemetry_shmti *shmti) +{ + struct scmi_telemetry_de *de; + struct telemetry_de *tde; + u64 val, tstamp; + int used_qwords; + + de =3D xa_load(&ti->xa_des, le32_to_cpu(payld->id)); + if (!de || DATA_INVALID(payld)) + return -EINVAL; + + used_qwords =3D 4; + + tde =3D to_tde(de); + if (shmti) { + tde->base =3D shmti->base; + tde->eplg =3D shmti->base + shmti->len - TDCF_EPLG_SZ; + tde->offset =3D (void *)payld - (void *)shmti->base; + } + + //TODO BLK_TS + if (USE_LINE_TS(payld)) { + used_qwords +=3D 2; + if (TS_VALID(payld)) + tstamp =3D LINE_TSTAMP_GET(&payld->tsl); + } + val =3D LINE_DATA_GET(&payld->tsl); + + guard(mutex)(&tde->mtx); + tde->last_val =3D val; + if (de->tstamp_enabled) + tde->last_ts =3D tstamp; + else + tde->last_ts =3D 0; + + return used_qwords; +} + +static int scmi_telemetry_shmti_scan(struct telemetry_info *ti, + unsigned int shmti_id, u64 ts, + bool update) +{ + struct telemetry_shmti *shmti =3D &ti->shmti[shmti_id]; + struct tdcf __iomem *tdcf =3D shmti->base; + int retries =3D SCMI_TLM_TDCF_MAX_RETRIES; + u64 startm =3D 0, endm =3D 0xffffffffffffffff; + void *eplg =3D SHMTI_EPLG(shmti); + + if (!tdcf) + return -ENODEV; + + do { + unsigned int qwords; + void __iomem *next; + + /* A bit of exponential backoff between retries */ + fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * 1000); + + startm =3D TDCF_START_SEQ_GET(tdcf); + if (IS_BAD_START_SEQ(startm)) + continue; + + qwords =3D tdcf->prlg.num_qwords; + next =3D tdcf->payld; + while (qwords) { + int used_qwords; + + used_qwords =3D scmi_telemetry_tdcf_parse_one(ti, next, + update ? shmti : NULL); + if (qwords < used_qwords) + return -EINVAL; + + next +=3D used_qwords * 4; + qwords -=3D used_qwords; + } + + endm =3D TDCF_END_SEQ_GET(eplg); + } while (startm !=3D endm && --retries); + + if (startm !=3D endm) + return -EPROTO; + + return 0; +} + +static int scmi_telemetry_group_state_update(struct telemetry_info *ti, + struct scmi_telemetry_group *grp, + bool *enable, bool *tstamp) +{ + struct scmi_telemetry_de *de; + + for (int i =3D 0; i < grp->num_de; i++) { + de =3D ti->info.des[grp->des[i]]; + + if (enable) + de->enabled =3D *enable; + if (tstamp) + de->tstamp_enabled =3D *tstamp; + } + + return 0; +} + +static int __scmi_telemetry_state_set(const struct scmi_protocol_handle *p= h, + bool is_group, bool *enable, + bool *enabled_state, bool *tstamp, + bool *tstamp_enabled_state, void *priv) +{ + 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_group *grp =3D priv; + 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) { + *tstamp_enabled_state =3D *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(grp->id); + /* Default to disable mode for one DE */ + msg->flags =3D DE_DISABLE_ONE; + msg->flags |=3D 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 *tstamp ? DE_ENABLE_WTH_TSTAMP : + DE_ENABLE_NO_TSTAMP; + } else { + msg->flags |=3D *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) { + u32 id =3D le32_to_cpu(resp->shmti_id); + + /* Update DE SHMTI and offset, if applicable */ + if (enable && IS_SHMTI_ID_VALID(id)) { + if (id >=3D ti->num_shmti) { + ret =3D -EPROTO; + goto out; + } + + /* + * Update SHMTI/offset while skipping non-SHMTI-DEs like + * FCs and notif-only. + */ + if (!is_group) { + struct scmi_telemetry_de *de =3D priv; + struct telemetry_de *tde; + u32 offs; + + offs =3D le32_to_cpu(resp->tdcf_de_offset); + if (offs >=3D ti->shmti[id].len - de->data_sz) { + ret =3D -EPROTO; + goto out; + } + + tde =3D to_tde(de); + tde->base =3D ti->shmti[id].base; + tde->offset =3D offs; + /* A handy reference to the Epilogue updated */ + tde->eplg =3D tde->base + ti->shmti[id].len - + TDCF_EPLG_SZ; + } else { + /* + * A full SHMTI scan is needed when enabling a + * group in order to retrieve offsets. + */ + scmi_telemetry_shmti_scan(ti, id, 0, true); + } + } + + /* Update cached state on success */ + if (enable) + *enabled_state =3D *enable; + if (tstamp) + *tstamp_enabled_state =3D *tstamp; + + if (is_group) + scmi_telemetry_group_state_update(ti, grp, enable, + tstamp); + } + +out: + ph->xops->xfer_put(ph, t); + + return ret; +} + +static int scmi_telemetry_state_get(const struct scmi_protocol_handle *ph, + 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; + + 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, bool *enable, + bool *tstamp) +{ + void *obj; + bool *enabled_state, *tstamp_enabled_state; + struct telemetry_info *ti =3D ph->get_priv(ph); + + 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.num_groups) + return -EINVAL; + + grp =3D &ti->info.des_groups[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 scmi_msg_telemetry_de_configure *msg; + struct scmi_xfer *t; + int ret; + + 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 DE_DISABLE_ALL; + if (is_group) + msg->flags |=3D GROUP_SELECTOR; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + struct telemetry_info *ti =3D ph->get_priv(ph); + + for (int i =3D 0; i < ti->info.num_de; i++) + ti->info.des[i]->enabled =3D false; + + if (is_group) { + for (int i =3D 0; i < ti->info.num_groups; i++) + ti->info.des_groups[i].enabled =3D false; + } + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + +static int +scmi_telemetry_collection_configure(const struct scmi_protocol_handle *ph, + unsigned int res_id, bool grp_ignore, + bool *enable, + unsigned int *update_interval_ms, + enum scmi_telemetry_collection *mode) +{ + struct scmi_telemetry_update_interval *intervals; + struct telemetry_info *ti =3D ph->get_priv(ph); + enum scmi_telemetry_collection *current_mode, next_mode; + struct scmi_msg_telemetry_config_set *msg; + struct scmi_xfer *t; + bool tlm_enable; + u32 interval; + int ret; + + if (mode && *mode =3D=3D SCMI_TLM_NOTIFICATION && + !ti->info.continuos_update_support) + return -EINVAL; + + if (res_id !=3D SCMI_TLM_GRP_INVALID && res_id >=3D ti->info.num_groups) + return -EINVAL; + + if (res_id =3D=3D SCMI_TLM_GRP_INVALID || grp_ignore) { + intervals =3D &ti->info.intervals; + current_mode =3D &ti->info.current_mode; + } else { + intervals =3D &ti->info.des_groups[res_id].intervals; + current_mode =3D &ti->info.des_groups[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(intervals->active_update_interval); + } else { + //TODO Fix .. not only ms...review API + interval =3D FIELD_PREP(GENMASK(20, 5), + cpu_to_le32(*update_interval_ms)); + interval |=3D FIELD_PREP(GENMASK(4, 0), (-3 & 0x1f)); + } + + tlm_enable =3D enable ? *enable : ti->info.enabled; + next_mode =3D mode ? *mode : *current_mode; + + msg =3D t->tx.buf; + msg->grp_id =3D res_id; + msg->control =3D tlm_enable ? TELEMETRY_ENABLE : 0; + msg->control |=3D grp_ignore ? TELEMETRY_SELECTOR_ALL : + TELEMETRY_SELECTOR_GROUP; + msg->control |=3D TELEMETRY_MODE(next_mode); + 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) + intervals->active_update_interval =3D interval; + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + +static int scmi_telemetry_de_data_fc_read(struct telemetry_de *tde, + u64 *tstamp, u64 *val) +{ + struct fc_tsline __iomem *fc =3D tde->base + tde->offset; + + *val =3D LINE_DATA_GET(fc); + if (tstamp) { + if (tde->de.tstamp_support) + *tstamp =3D LINE_TSTAMP_GET(fc); + else + *tstamp =3D 0; + } + + return 0; +} + +/* + * TDCF and TS Line Management Notes + * --------------------------------- + * (from a chat with ATG) + * + * TCDF Payload Metadata notable bits: + * - Bit[2]: USE Tstamp + * - Bit[1]: Tstamp VALID + * + * CASE_1: + * ------- + * + A DE is enabled with timestamp disabled, so the TS_Line is NOT present + * -> BIT[2:1] =3D 00b + * + that DE is then 're-enabled' with TS_line: 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[2:1]: 11b + * + 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) + * + * CASE_2: + * ------- + * + A DE is enabled with timestamp enabled, so the TS_Line is there + * -> BIT[2:1]:11b + * + 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 physiclly there but NOT valid + * -> BIT[2:1]=3D 10b + * + the hole from the timestamp remain there unused until + * - you enable again the TS so the hole is used again + * -> BIT[2:1]=3D11b + * OR + * - you disable fully the DE and then re-enable it with the TS + * -> potentially CASE_1 the DE is relocated on enable + */ +static int scmi_telemetry_de_tdcf_parse(struct telemetry_de *tde, + u64 *tstamp, u64 *val) +{ + struct tdcf __iomem *tdcf =3D tde->base; + u64 startm, endm; + int retries =3D SCMI_TLM_TDCF_MAX_RETRIES; + + if (!tdcf) + return -ENODEV; + + do { + struct payload __iomem *payld; + + /* 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)) + continue; + + payld =3D tde->base + tde->offset; + if (le32_to_cpu(payld->id) !=3D tde->de.id || DATA_INVALID(payld)) + return -EINVAL; + + //TODO BLK_TS + if (tstamp && USE_LINE_TS(payld) && TS_VALID(payld)) + *tstamp =3D LINE_TSTAMP_GET(&payld->tsl); + + *val =3D LINE_DATA_GET(&payld->tsl); + + endm =3D TDCF_END_SEQ_GET(tde->eplg); + } while (startm !=3D endm && --retries); + + if (startm !=3D endm) + return -EPROTO; + + return 0; +} + +static int scmi_telemetry_de_lookup(struct telemetry_de *tde, + u64 *tstamp, u64 *val) +{ + if (!tde->de.fc_support) + return scmi_telemetry_de_tdcf_parse(tde, tstamp, val); + + return scmi_telemetry_de_data_fc_read(tde, tstamp, val); +} + +static int scmi_telemetry_de_collect(struct telemetry_info *ti, + struct scmi_telemetry_de *de, + u64 *tstamp, u64 *val) +{ + struct telemetry_de *tde =3D to_tde(de); + + if (!de->enabled) + return -EINVAL; + + /* + * DE readings returns cached values when: + * - DE data value was retrieved via notification + */ + scoped_guard(mutex, &tde->mtx) { + if (tde->cached) { + *val =3D tde->last_val; + if (tstamp) + *tstamp =3D tde->last_ts; + return 0; + } + } + + return scmi_telemetry_de_lookup(tde, tstamp, val); +} + +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 -EINVAL; + + de =3D xa_load(&ti->xa_des, sample->id); + if (!de) + return -ENODEV; + + return scmi_telemetry_de_collect(ti, de, &sample->tstamp, &sample->val); +} + +static int +scmi_telemetry_samples_collect(struct telemetry_info *ti, int grp_id, + int *num_samples, + struct scmi_telemetry_de_sample *samples) +{ + int max_samples; + + max_samples =3D *num_samples; + *num_samples =3D 0; + + for (int i =3D 0; i < ti->info.num_de; i++) { + struct scmi_telemetry_de *de; + u64 val, tstamp; + int ret; + + de =3D ti->info.des[i]; + if (grp_id !=3D SCMI_TLM_GRP_INVALID && + (!de->grp || de->grp->id !=3D grp_id)) + continue; + + ret =3D scmi_telemetry_de_collect(ti, de, &tstamp, &val); + if (ret) + continue; + + if (*num_samples =3D=3D max_samples) + return -ENOSPC; + + samples[*num_samples].tstamp =3D tstamp; + samples[*num_samples].val =3D val; + samples[*num_samples].id =3D de->id; + + (*num_samples)++; + } + + 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; + + 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, unsigned int *dwords, + ktime_t timestamp) +{ + u32 next =3D 0; + + while (next < num_dwords) { + struct payload *payld =3D (struct payload *)&dwords[next]; + struct scmi_telemetry_de *de; + struct telemetry_de *tde; + u32 de_id; + + next +=3D USE_LINE_TS(payld) ? + TS_LINE_DATA_PAYLD_WORDS : LINE_DATA_PAYLD_WORDS; + + if (DATA_INVALID(payld)) { + dev_err(ti->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->dev, + "MSG - Received INVALID DE - ID:%u enabled:%d\n", + de_id, de ? (de->enabled ? 'Y' : 'N') : 'X'); + continue; + } + + tde =3D to_tde(de); + guard(mutex)(&tde->mtx); + tde->cached =3D true; + tde->last_val =3D LINE_DATA_GET(&payld->tsl); + //TODO BLK_TS ? + if (USE_LINE_TS(payld) && TS_VALID(payld)) + tde->last_ts =3D LINE_TSTAMP_GET(&payld->tsl); + else + tde->last_ts =3D 0; + } +} + +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 grp_ignore; + int ret; + + if (!ti->info.enabled || !num_samples || !samples) + return -EINVAL; + + grp_ignore =3D grp_id =3D=3D SCMI_TLM_GRP_INVALID ? true : false; + if (!grp_ignore && grp_id >=3D ti->info.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 grp_id; + msg->control =3D TELEMETRY_ENABLE; + msg->control |=3D grp_ignore ? TELEMETRY_SELECTOR_ALL : + TELEMETRY_SELECTOR_GROUP; + msg->control |=3D TELEMETRY_MODE_SINGLE; + 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, r->num_dwords, + r->dwords, 0); + ret =3D scmi_telemetry_samples_collect(ti, grp_id, num_samples, + samples); + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + +static int scmi_telemetry_config_get(const struct scmi_protocol_handle *ph, + bool *enabled, int *mode, + u32 *update_interval) +{ + ///TODO + return 0; +} + +static int scmi_telemetry_reset(const struct scmi_protocol_handle *ph) +{ + int ret; + struct scmi_xfer *t; + + 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); + + //XXX Better would be to read back from platform + // CONFIG_GET + DE_ENABLED_LIST + ti->info.enabled =3D false; + ti->info.notif_enabled =3D false; + ti->info.current_mode =3D SCMI_TLM_ONDEMAND; + ti->info.intervals.active_update_interval =3D 0; + + for (int i =3D 0; i < ti->info.num_de; i++) { + ti->info.des[i]->enabled =3D false; + ti->info.des[i]->tstamp_enabled =3D false; + } + + for (int i =3D 0; i < ti->info.num_groups; i++) { + ti->info.des_groups[i].enabled =3D false; + ti->info.des_groups[i].tstamp_enabled =3D false; + ti->info.des_groups[i].current_mode =3D SCMI_TLM_ONDEMAND; + ti->info.des_groups[i].intervals.active_update_interval =3D 0; + } + } + 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, + .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, + .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, + .config_get =3D scmi_telemetry_config_get, + .reset =3D scmi_telemetry_reset, +}; + +static bool +scmi_telemetry_notify_supported(const struct scmi_protocol_handle *ph, + u8 evt_id, u32 src_id) +{ + struct telemetry_info *ti =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); + /* + * 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(r->dwords, p->array, r->num_dwords * sizeof(u32)); + + *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 void scmi_telemetry_scan_update(struct telemetry_info *ti, u64 ts) +{ + /* Scan all SHMTIs ... */ + for (int id =3D 0; id < ti->num_shmti; id++) + scmi_telemetry_shmti_scan(ti, id, ts, false); + + /* ... then scan all FCs ... XXX Use a list */ + for (int i =3D 0; i < ti->info.num_de; i++) { + struct scmi_telemetry_de *de; + struct telemetry_de *tde; + u64 val, tstamp; + int ret; + + de =3D ti->info.des[i]; + if (!de->enabled) + continue; + + tde =3D to_tde(de); + if (!tde->de.fc_support) + continue; + + //TODO Repoet errors + ret =3D scmi_telemetry_de_data_fc_read(tde, &tstamp, &val); + if (ret) + return; + + guard(mutex)(&tde->mtx); + tde->last_val =3D val; + if (de->tstamp_enabled) + tde->last_ts =3D tstamp; + else + tde->last_ts =3D 0; + } +} + +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) { + dev_err(ti->dev, "Bad Telemetry update notification - ret: %dn", + er->status); + return NOTIFY_DONE; + } + + /* Lookup the embedded DEs in the notification payload ... */ + if (er->num_dwords) + scmi_telemetry_msg_payld_process(ti, er->num_dwords, + er->dwords, er->timestamp); + + /* ...scan the SHMTI/FCs for any other DE updates. */ + if (ti->streaming_mode) + scmi_telemetry_scan_update(ti, er->timestamp); + + return NOTIFY_OK; +} + +static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle = *ph) +{ + struct telemetry_info *ti; + u32 version; + int ret; + + ret =3D ph->xops->version_get(ph, &version); + if (ret) + return ret; + + dev_dbg(ph->dev, "Telemetry Version %d.%d\n", + PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version)); + + ti =3D devm_kzalloc(ph->dev, sizeof(*ti), GFP_KERNEL); + if (!ti) + return -ENOMEM; + + ti->dev =3D ph->dev; + + ret =3D scmi_telemetry_protocol_attributes_get(ph, ti); + if (ret) + return ret; + + ret =3D scmi_telemetry_enumerate_de(ph, ti); + if (ret) + return ret; + + ret =3D scmi_telemetry_enumerate_update_intervals(ph, ti); + if (ret) + return ret; + + ret =3D scmi_telemetry_enumerate_shmti(ph, ti); + if (ret) + return ret; + + ti->info.version =3D version; + + ret =3D ph->set_priv(ph, ti, version); + 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->notifier_register(ph, SCMI_EVENT_TELEMETRY_UPDATE, + NULL, &ti->telemetry_nb); + if (ret) + dev_warn(ph->dev, + "Could NOT register Telemetry notifications\n"); + } + + return ret; +} + +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, + .events =3D &tlm_protocol_events, + .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 6f8d36e1f8fc..f02f8f353d30 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -2,7 +2,7 @@ /* * SCMI Message Protocol driver header * - * Copyright (C) 2018-2021 ARM Ltd. + * Copyright (C) 2018-2025 ARM Ltd. */ =20 #ifndef _LINUX_SCMI_PROTOCOL_H @@ -820,6 +820,192 @@ 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, +}; + +struct scmi_telemetry_update_interval { + bool discrete; +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW 0 +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH 1 +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP 2 + int num; + unsigned int *update_intervals; + unsigned int active_update_interval; +#define SCMI_GET_UPDATE_INTERVAL_SECS(x) \ + (le32_get_bits((x), GENMASK(20, 5))) +#define SCM_IGET_UPDATE_INTERVAL_EXP(x) \ + ({ \ + int __signed_exp =3D FIELD_GET(GENMASK(4, 0), (x)); \ + \ + if (__signed_exp & BIT(4)) \ + __signed_exp |=3D GENMASK(31, 5); \ + __signed_exp; \ + }) +}; + +enum scmi_telemetry_collection { + SCMI_TLM_ONDEMAND, + SCMI_TLM_NOTIFICATION, + SCMI_TLM_SINGLE_READ, +}; + +struct scmi_telemetry_group { +#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF + int id; + bool enabled; + bool tstamp_enabled; + unsigned int num_de; + unsigned int *des; + size_t des_str_sz; + char *des_str; + struct scmi_telemetry_update_interval intervals; + enum scmi_telemetry_collection current_mode; +}; + +//XXX Check what to hide +struct scmi_telemetry_de { + int id; + unsigned int data_sz; + enum scmi_telemetry_de_type type; + int unit; + int unit_exp; + int tstamp_exp; + int instance_id; + int compo_instance_id; + enum scmi_telemetry_compo_type compo_type; + bool persistent; + bool tstamp_support; + bool fc_support; + bool name_support; + char *name; + struct scmi_telemetry_group *grp; + bool enabled; + bool tstamp_enabled; +}; + +//XXX Check what to hide +struct scmi_telemetry_info { + unsigned int version; +#define SCMI_TLM_MAX_DWORD 4 + unsigned int de_impl_version[SCMI_TLM_MAX_DWORD]; + bool single_read_support; + bool continuos_update_support; + bool per_group_config_support; + bool reset_support; + bool fc_support; + int num_de; + struct scmi_telemetry_de **des; + struct scmi_telemetry_update_interval intervals; + int num_groups; + struct scmi_telemetry_group *des_groups; + bool enabled; + bool notif_enabled; + enum scmi_telemetry_collection current_mode; +}; + +struct scmi_telemetry_de_sample { + u32 id; + u64 tstamp; + u64 val; +}; + +/** + * struct scmi_telemetry_proto_ops - represents the various operations pro= vided + * by SCMI Telemetry Protocol + * + * @info_get: get the general Telemetry information. + * @state_get: retrieve the specific DE or GROUP state. + * @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. + * @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. + * @config_get: retrieve current telemetry configuration. + * @reset: reset configuration and telemetry data. + */ +struct scmi_telemetry_proto_ops { + const struct scmi_telemetry_info __must_check *(*info_get) + (const struct scmi_protocol_handle *ph); + int (*state_get)(const struct scmi_protocol_handle *ph, + u32 id, bool *enabled, bool *tstamp_enabled); + int (*state_set)(const struct scmi_protocol_handle *ph, + bool is_group, u32 id, bool *enable, 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, bool grp_ignore, + bool *enable, + unsigned int *update_interval_ms, + enum scmi_telemetry_collection *mode); + int (*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); + int (*config_get)(const struct scmi_protocol_handle *ph, bool *enabled, + int *mode, u32 *update_interval); + int (*reset)(const struct scmi_protocol_handle *ph); +}; + /** * struct scmi_notify_ops - represents notifications' operations provided= by * SCMI core @@ -926,6 +1112,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, SCMI_PROTOCOL_LAST =3D 0xff, }; =20 @@ -1027,6 +1214,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 { @@ -1114,4 +1302,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.47.0 From nobody Thu Oct 9 02:16:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B97A32222BF; Fri, 20 Jun 2025 19:29: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=1750447747; cv=none; b=nPGPIIVPu29wzdUgBMwzNWsn94Mvr5dR9D0XSzusc7TVAOrTglMKtFSZrc8T2JK2afId6KC/mCzV1FvqK6+0Vjm5HAUkPtejvVxoUCErQm9lx2MR/XMOj39K+pdrbFB4EPFsUXHKxwWs2niAzgI0rQ7/59yVoe3JJ+fTxKPnO3M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750447747; c=relaxed/simple; bh=iTMfpekzIVBGCyX8l8yyJGOKcthl0keNPwE36XLa33w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=paDS1CS4HOntQk9kKggeVwfFtyjtZZIljWc7hlyXtqK3mq31XKcdE7p7qu8UqOrhJYfI3/YzWBkQ6rRjARQlCF2zFvWKiLUy52ejrn0umNpukkw2t0pxYtituY3l300Qu2izXmd0MiCYtdxVANFXbRoz4UR/NXRqU0HcV5dvv2E= 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; 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 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 A526616F2; Fri, 20 Jun 2025 12:28:43 -0700 (PDT) Received: from pluto.fritz.box (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E3BE83F673; Fri, 20 Jun 2025 12:29:00 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org Cc: sudeep.holla@arm.com, 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, quic_sibis@quicinc.com, dan.carpenter@linaro.org, d-gole@ti.com, souvik.chakravarty@arm.com, Cristian Marussi Subject: [RFC PATCH 4/7] firmware: arm_scmi: Add System Telemetry driver Date: Fri, 20 Jun 2025 20:28:10 +0100 Message-ID: <20250620192813.2463367-5-cristian.marussi@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250620192813.2463367-1-cristian.marussi@arm.com> References: <20250620192813.2463367-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 System Telemetry driver which exposes a sysfs custom API to configure and consume Telemetry data from userspace. Still lacking: - complete groups handling - ungrouped DEs handling Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/Kconfig | 10 + drivers/firmware/arm_scmi/Makefile | 1 + .../firmware/arm_scmi/scmi_system_telemetry.c | 967 ++++++++++++++++++ 3 files changed, 978 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 e3fb36825978..9e51b3cd0c93 100644 --- a/drivers/firmware/arm_scmi/Kconfig +++ b/drivers/firmware/arm_scmi/Kconfig @@ -99,4 +99,14 @@ config ARM_SCMI_POWER_CONTROL called scmi_power_control. Note this may 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..a2f59001747d --- /dev/null +++ b/drivers/firmware/arm_scmi/scmi_system_telemetry.c @@ -0,0 +1,967 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * SCMI - System Telemetry Driver + * + * Copyright (C) 2025 ARM Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_BULK_LINE_CHAR_LENGTH 64 + +struct scmi_tlm_setup; + +static atomic_t scmi_tlm_instance_count =3D ATOMIC_INIT(0); + +struct scmi_tlm_grp_dev { + struct device dev; + struct scmi_tlm_setup *tsp; + const struct scmi_telemetry_group *grp; +}; + +#define to_tlm_grp_dev(d) \ + (container_of((d), struct scmi_tlm_grp_dev, dev)) + +struct scmi_tlm_de_dev { + struct device dev; + struct scmi_tlm_setup *tsp; + const struct scmi_telemetry_de *de; +}; + +#define to_tlm_de_dev(d) \ + (container_of((d), struct scmi_tlm_de_dev, dev)) + +struct scmi_tlm_instance { + struct device dev; + struct device des_dev; + struct device groups_dev; + struct scmi_tlm_de_dev **des; + struct scmi_tlm_setup *tsp; + const struct scmi_telemetry_info *info; +}; + +#define dev_to_tlm_instance(d) \ + (container_of((d), struct scmi_tlm_instance, dev)) + +#define des_dev_to_tlm_instance(e) \ + (container_of((e), struct scmi_tlm_instance, des_dev)) + +#define groups_dev_to_tlm_instance(e) \ + (container_of((e), struct scmi_tlm_instance, groups_dev)) + +/** + * struct scmi_tlm_setup - Telemetry setup descriptor + * @sdev: A reference to the related SCMI device + * @ops: A reference to the protocol ops + * @ph: A reference to the protocol handle to be used with the ops + * @priv: A reference to optional driver-specific data + */ +struct scmi_tlm_setup { + struct scmi_device *sdev; + const struct scmi_telemetry_proto_ops *ops; + struct scmi_protocol_handle *ph; + const void *priv; +}; + +static void scmi_telemetry_release(struct device *dev) +{ +} + +static ssize_t __all_enable_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len, + bool is_enable_entry) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + struct scmi_tlm_setup *tsp =3D ti->tsp; + bool enable; + int ret; + + if (kstrtobool(buf, &enable)) + return -EINVAL; + + if (is_enable_entry && !enable) { + ret =3D tsp->ops->all_disable(tsp->ph, false); + if (ret) + return ret; + } else { + for (int i =3D 0; i < ti->info->num_de; i++) { + const struct scmi_telemetry_de *de =3D ti->info->des[i]; + + ret =3D tsp->ops->state_set(tsp->ph, false, de->id, + is_enable_entry ? &enable : NULL, + !is_enable_entry ? &enable : NULL); + if (ret) + return ret; + } + } + + return len; +} + +static ssize_t all_des_enable_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + return __all_enable_store(dev, attr, buf, len, true); +} + +static ssize_t all_des_tstamp_enable_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + return __all_enable_store(dev, attr, buf, len, false); +} + +static inline ssize_t __current_update_show(char *buf, + unsigned int active_update_interval) +{ + return sysfs_emit(buf, "%u\n", + SCMI_GET_UPDATE_INTERVAL_SECS(active_update_interval)); +} + +static inline ssize_t __current_update_store(struct scmi_tlm_setup *tsp, + const char *buf, size_t len, + unsigned int grp_id) +{ + bool grp_ignore =3D grp_id =3D=3D SCMI_TLM_GRP_INVALID ? true : false; + unsigned int update_interval_ms =3D 0; + int ret; + + ret =3D kstrtouint(buf, 0, &update_interval_ms); + if (ret) + return ret; + + ret =3D tsp->ops->collection_configure(tsp->ph, grp_id, grp_ignore, NULL, + &update_interval_ms, NULL); + if (ret) + return ret; + + return len; +} + +static ssize_t current_update_interval_ms_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + + return __current_update_show(buf, + ti->info->intervals.active_update_interval); +} + +static ssize_t current_update_interval_ms_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + struct scmi_tlm_setup *tsp =3D ti->tsp; + + return __current_update_store(tsp, buf, len, SCMI_TLM_GRP_INVALID); +} + +static ssize_t tlm_enable_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + + return sysfs_emit(buf, "%c\n", ti->info->enabled ? 'Y' : 'N'); +} + +static ssize_t tlm_enable_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + struct scmi_tlm_setup *tsp =3D ti->tsp; + enum scmi_telemetry_collection mode =3D SCMI_TLM_ONDEMAND; + bool enabled; + int ret; + + ret =3D kstrtobool(buf, &enabled); + if (ret) + return ret; + + ret =3D tsp->ops->collection_configure(tsp->ph, SCMI_TLM_GRP_INVALID, tru= e, + &enabled, NULL, &mode); + if (ret) + return ret; + + return len; +} + +static int scmi_tlm_buffer_fill(struct device *dev, char *buf, size_t size, + int *len, int num, + struct scmi_telemetry_de_sample *samples) +{ + int idx, bytes =3D 0; + + /* Loop till there space for the next line */ + for (idx =3D 0; idx < num && size - bytes >=3D MAX_BULK_LINE_CHAR_LENGTH;= idx++) { + bytes +=3D snprintf(buf + bytes, size - bytes, + "0x%04X %llu %016llX\n", samples[idx].id, + samples[idx].tstamp, samples[idx].val); + } + + if (idx < num) { + dev_err(dev, "Bulk buffer truncated !\n"); + return -ENOSPC; + } + + if (len) + *len =3D bytes; + + return 0; +} + +static inline ssize_t __des_bulk_read_show(struct scmi_tlm_instance *ti, + unsigned int grp_id, char *buf, + int size) +{ + struct scmi_telemetry_de_sample *samples; + struct scmi_tlm_setup *tsp =3D ti->tsp; + int ret, num; + + num =3D ti->info->num_de; + samples =3D kcalloc(num, sizeof(*samples), GFP_KERNEL); + if (!samples) + return -ENOMEM; + + ret =3D tsp->ops->des_bulk_read(tsp->ph, grp_id, &num, samples); + if (ret) { + kfree(samples); + return ret; + } + + ret =3D scmi_tlm_buffer_fill(&ti->dev, buf, size, NULL, num, samples); + kfree(samples); + if (ret) + return ret; + + return sysfs_emit(buf, "%s", buf); +} + +static ssize_t des_bulk_read_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + + return __des_bulk_read_show(ti, SCMI_TLM_GRP_INVALID, buf, PAGE_SIZE); +} + +static inline ssize_t __des_single_sample_read_show(struct scmi_tlm_instan= ce *ti, + unsigned int grp_id, + char *buf, int len) +{ + struct scmi_telemetry_de_sample *samples; + struct scmi_tlm_setup *tsp =3D ti->tsp; + int ret, num, bytes =3D 0; + + num =3D ti->info->num_de; + samples =3D kcalloc(num, sizeof(*samples), GFP_KERNEL); + if (!samples) + return -ENOMEM; + + ret =3D tsp->ops->des_sample_get(tsp->ph, grp_id, &num, samples); + if (ret) { + kfree(samples); + return ret; + } + + for (int i =3D 0; i < num; i++) { + bytes +=3D snprintf(buf + bytes, len - bytes, + "0x%04X %llu %016llX\n", samples[i].id, + samples[i].tstamp, samples[i].val); + + if (bytes >=3D len) { + dev_err(&ti->dev, "=3D=3D>> BULK BUFFER OVERFLOW !\n"); + kfree(samples); + return -ENOSPC; + } + } + + kfree(samples); + + return sysfs_emit(buf, "%s", buf); +} + +static ssize_t des_single_sample_read_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + + return __des_single_sample_read_show(ti, SCMI_TLM_GRP_INVALID, + buf, PAGE_SIZE); +} + +static ssize_t de_implementation_version_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + + return sysfs_emit(buf, "%pUL\n", ti->info->de_impl_version); +} + +static inline ssize_t __intervals_discrete_show(char *buf, const bool disc= rete) +{ + return sysfs_emit(buf, "%c\n", discrete ? 'Y' : 'N'); +} + +static ssize_t intervals_discrete_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + + return __intervals_discrete_show(buf, ti->info->intervals.discrete); +} + +//TODO Review available interval show +#define BUF_SZ 1024 +static inline ssize_t +__available_update_show(char *buf, + const struct scmi_telemetry_update_interval *intervals) +{ + int len =3D 0, num_intervals =3D intervals->num; + char available[BUF_SZ]; + + for (int i =3D 0; i < num_intervals; i++) { + len +=3D scnprintf(available + len, BUF_SZ - len, "%u ", + intervals->update_intervals[i]); + } + + available[len - 1] =3D '\0'; + + return sysfs_emit(buf, "%s\n", available); +} + +static ssize_t available_update_intervals_ms_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + + return __available_update_show(buf, &ti->info->intervals); +} + +static ssize_t version_show(struct device *dev, struct device_attribute *a= ttr, + char *buf) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + + return sysfs_emit(buf, "0x%08x\n", ti->info->version); +} + +static DEVICE_ATTR_WO(all_des_enable); +static DEVICE_ATTR_WO(all_des_tstamp_enable); +static DEVICE_ATTR_RW(current_update_interval_ms); +static DEVICE_ATTR_RW(tlm_enable); +static DEVICE_ATTR_RO(des_bulk_read); +static DEVICE_ATTR_RO(des_single_sample_read); +static DEVICE_ATTR_RO(de_implementation_version); +static DEVICE_ATTR_RO(intervals_discrete); +static DEVICE_ATTR_RO(available_update_intervals_ms); +static DEVICE_ATTR_RO(version); + +static ssize_t reset_store(struct device *dev, struct device_attribute *at= tr, + const char *buf, size_t len) +{ + struct scmi_tlm_instance *ti =3D dev_to_tlm_instance(dev); + int ret; + + ret =3D ti->tsp->ops->reset(ti->tsp->ph); + if (ret) + return ret; + + return len; +} + +static struct device_attribute dev_attr_reset =3D { + .attr =3D { .name =3D "reset", .mode =3D 0200 }, + .store =3D reset_store, +}; + +static struct attribute *scmi_telemetry_attrs[] =3D { + &dev_attr_all_des_enable.attr, + &dev_attr_all_des_tstamp_enable.attr, + &dev_attr_current_update_interval_ms.attr, + &dev_attr_tlm_enable.attr, + &dev_attr_des_bulk_read.attr, + &dev_attr_des_single_sample_read.attr, + &dev_attr_de_implementation_version.attr, + &dev_attr_intervals_discrete.attr, + &dev_attr_available_update_intervals_ms.attr, + &dev_attr_version.attr, + NULL, +}; +ATTRIBUTE_GROUPS(scmi_telemetry); + +static struct class scmi_telemetry_class =3D { + .name =3D "scmi_telemetry", + .dev_release =3D scmi_telemetry_release, +}; + +static ssize_t value_show(struct device *dev, struct device_attribute *att= r, + char *buf) +{ + struct scmi_tlm_de_dev *tde =3D to_tlm_de_dev(dev); + struct scmi_tlm_setup *tsp =3D tde->tsp; + struct scmi_telemetry_de_sample sample; + int ret; + + sample.id =3D tde->de->id; + ret =3D tsp->ops->de_data_read(tsp->ph, &sample); + if (ret) + return ret; + + return sysfs_emit(buf, "%llu: %016llX\n", sample.tstamp, sample.val); +} +static DEVICE_ATTR_RO(value); + +#define DEFINE_DE_ATTR_INT_RO(_attr, _fmt) \ +static ssize_t _attr##_show(struct device *dev, \ + struct device_attribute *attr, \ + char *buf) \ +{ \ + struct scmi_tlm_de_dev *tde =3D to_tlm_de_dev(dev); \ + \ + return sysfs_emit(buf, _fmt "\n", tde->de->_attr); \ +} \ +static DEVICE_ATTR_RO(_attr) + +#define DEFINE_DE_ATTR_BOOL_RO(_attr) \ +static ssize_t _attr##_show(struct device *dev, \ + struct device_attribute *attr, \ + char *buf) \ +{ \ + struct scmi_tlm_de_dev *tde =3D to_tlm_de_dev(dev); \ + \ + return sysfs_emit(buf, "%c\n", tde->de->_attr ? 'Y' : 'N'); \ +} \ +static DEVICE_ATTR_RO(_attr) + +DEFINE_DE_ATTR_INT_RO(type, "%u"); +DEFINE_DE_ATTR_INT_RO(unit, "%u"); +DEFINE_DE_ATTR_INT_RO(unit_exp, "%d"); +DEFINE_DE_ATTR_INT_RO(instance_id, "%u"); +DEFINE_DE_ATTR_INT_RO(compo_type, "%u"); +DEFINE_DE_ATTR_INT_RO(compo_instance_id, "%u"); +DEFINE_DE_ATTR_BOOL_RO(persistent); +DEFINE_DE_ATTR_INT_RO(name, "%s"); +DEFINE_DE_ATTR_INT_RO(tstamp_exp, "%d"); + +#define DEFINE_DE_ATTR_STATE_RW(_name, _is_enable) \ +static ssize_t _name##_store(struct device *dev, \ + struct device_attribute *attr, \ + const char *buf, size_t len) \ +{ \ + struct scmi_tlm_de_dev *tde =3D to_tlm_de_dev(dev); \ + struct scmi_tlm_setup *tsp =3D tde->tsp; \ + typeof(_is_enable) _is_ena =3D _is_enable; \ + bool enabled; \ + int ret; \ + \ + ret =3D kstrtobool(buf, &enabled); \ + if (ret) \ + return ret; \ + \ + ret =3D tsp->ops->state_set(tsp->ph, false, tde->de->id, \ + _is_ena ? &enabled : NULL, \ + !_is_ena ? &enabled : NULL); \ + if (ret) \ + return ret; \ + \ + return len; \ +} \ + \ +static ssize_t _name##_show(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct scmi_tlm_de_dev *tde =3D to_tlm_de_dev(dev); \ + \ + return sysfs_emit(buf, "%c\n", tde->de->_name ## d ? 'Y' : 'N');\ +} \ +static DEVICE_ATTR_RW(_name) + +DEFINE_DE_ATTR_STATE_RW(enable, true); +DEFINE_DE_ATTR_STATE_RW(tstamp_enable, false); + +static struct attribute *scmi_des_attrs[] =3D { + &dev_attr_value.attr, + &dev_attr_type.attr, + &dev_attr_unit.attr, + &dev_attr_unit_exp.attr, + &dev_attr_instance_id.attr, + &dev_attr_compo_type.attr, + &dev_attr_compo_instance_id.attr, + &dev_attr_persistent.attr, + &dev_attr_enable.attr, + NULL, +}; +ATTRIBUTE_GROUPS(scmi_des); + +static void scmi_tlm_dev_release(struct device *dev) +{ +} + +static int +scmi_telemetry_dev_register(struct device *dev, struct device *parent, + const char *name) +{ + int ret; + + dev->parent =3D parent; + dev->release =3D scmi_tlm_dev_release; + dev_set_name(dev, "%s", name); + device_set_pm_not_required(dev); + dev_set_uevent_suppress(dev, true); + ret =3D device_register(dev); + if (ret) + put_device(dev); + + return ret; +} + +static int scmi_des_iter(struct device *dev, void *data) +{ + device_unregister(dev); + + return 0; +} + +static void scmi_telemetry_dev_unregister(struct device *parent) +{ + device_for_each_child(parent, NULL, scmi_des_iter); + device_unregister(parent); +} + +static ssize_t grp_obj_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + struct scmi_tlm_setup *tsp =3D gde->tsp; + bool enabled, is_ena_entry; + int ret; + + ret =3D kstrtobool(buf, &enabled); + if (ret) + return ret; + + is_ena_entry =3D !strncmp(attr->attr.name, "enable", 6); + ret =3D tsp->ops->state_set(tsp->ph, true, gde->grp->id, + is_ena_entry ? &enabled : NULL, + !is_ena_entry ? &enabled : NULL); + if (ret) + return ret; + + return len; +} + +static ssize_t grp_obj_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + bool enabled, is_ena_entry; + + is_ena_entry =3D !strncmp(attr->attr.name, "enable", 6); + enabled =3D is_ena_entry ? gde->grp->enabled : gde->grp->tstamp_enabled; + + return sysfs_emit(buf, "%c\n", enabled ? 'Y' : 'N'); +} + +static struct device_attribute dev_attr_grp_enable =3D { + .attr =3D { .name =3D "enable", .mode =3D 0600 }, + .show =3D grp_obj_show, + .store =3D grp_obj_store, +}; + +static struct device_attribute dev_attr_grp_tstamp_enable =3D { + .attr =3D { .name =3D "tstamp_enable", .mode =3D 0600 }, + .show =3D grp_obj_show, + .store =3D grp_obj_store, +}; + +static ssize_t composing_des_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + + return sysfs_emit(buf, "%s\n", gde->grp->des_str); +} +static DEVICE_ATTR_RO(composing_des); + +static ssize_t grp_current_update_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + + return __current_update_show(buf, + gde->grp->intervals.active_update_interval); +} + +static ssize_t grp_current_update_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + struct scmi_tlm_setup *tsp =3D gde->tsp; + + return __current_update_store(tsp, buf, len, gde->grp->id); +} + +static struct device_attribute dev_attr_grp_current_update =3D { + .attr =3D { .name =3D "current_update_interval_ms", .mode =3D 0600 }, + .show =3D grp_current_update_show, + .store =3D grp_current_update_store, +}; + +static ssize_t grp_intervals_discrete_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + + return __intervals_discrete_show(buf, gde->grp->intervals.discrete); +} + +static struct device_attribute dev_attr_grp_intervals_discrete =3D { + .attr =3D { .name =3D "intervals_discrete", .mode =3D 0400 }, + .show =3D grp_intervals_discrete_show, +}; + +static ssize_t grp_available_intervals_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + + return __available_update_show(buf, &gde->grp->intervals); +} + +static struct device_attribute dev_attr_grp_available_intervals =3D { + .attr =3D { .name =3D "available_update_intervals_ms", .mode =3D 0400 }, + .show =3D grp_available_intervals_show, +}; + +static ssize_t grp_des_bulk_read_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + struct scmi_tlm_instance *ti =3D + groups_dev_to_tlm_instance(gde->dev.parent); + + return __des_bulk_read_show(ti, gde->grp->id, buf, PAGE_SIZE); +} + +static ssize_t grp_des_single_sample_read_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct scmi_tlm_grp_dev *gde =3D to_tlm_grp_dev(dev); + struct scmi_tlm_instance *ti =3D + groups_dev_to_tlm_instance(gde->dev.parent); + + return __des_single_sample_read_show(ti, gde->grp->id, buf, PAGE_SIZE); +} + +static struct device_attribute dev_attr_grp_des_bulk_read =3D { + .attr =3D { .name =3D "des_bulk_read", .mode =3D 0400 }, + .show =3D grp_des_bulk_read_show, +}; + +static struct device_attribute dev_attr_grp_des_single_sample_read =3D { + .attr =3D { .name =3D "des_single_sample_read", .mode =3D 0400 }, + .show =3D grp_des_single_sample_read_show, +}; + +static struct attribute *scmi_grp_attrs[] =3D { + &dev_attr_grp_enable.attr, + &dev_attr_grp_tstamp_enable.attr, + &dev_attr_grp_des_bulk_read.attr, + &dev_attr_grp_des_single_sample_read.attr, + &dev_attr_composing_des.attr, + NULL, +}; +ATTRIBUTE_GROUPS(scmi_grp); + +static int scmi_telemetry_groups_initialize(struct device *dev, + struct scmi_tlm_instance *ti) +{ + int ret; + + if (ti->info->num_groups =3D=3D 0) + return 0; + + ret =3D scmi_telemetry_dev_register(&ti->groups_dev, &ti->dev, "groups"); + if (ret) + return ret; + + for (int i =3D 0; i < ti->info->num_groups; i++) { + const struct scmi_telemetry_group *grp =3D &ti->info->des_groups[i]; + struct scmi_tlm_grp_dev *gdev; + char name[16]; + + gdev =3D devm_kzalloc(dev, sizeof(*gdev), GFP_KERNEL); + if (!gdev) { + ret =3D -ENOMEM; + goto err; + } + + gdev->tsp =3D ti->tsp; + gdev->grp =3D grp; + gdev->dev.groups =3D scmi_grp_groups; + + snprintf(name, 8, "%d", grp->id); + ret =3D scmi_telemetry_dev_register(&gdev->dev, + &ti->groups_dev, name); + if (ret) + goto err; + + if (ti->info->per_group_config_support) { + sysfs_add_file_to_group(&gdev->dev.kobj, + &dev_attr_grp_current_update.attr, + NULL); + sysfs_add_file_to_group(&gdev->dev.kobj, + &dev_attr_grp_intervals_discrete.attr, + NULL); + sysfs_add_file_to_group(&gdev->dev.kobj, + &dev_attr_grp_available_intervals.attr, + NULL); + } + } + + dev_info(dev, "Found %d Telemetry GROUPS resources.\n", + ti->info->num_groups); + + return 0; + +err: + scmi_telemetry_dev_unregister(&ti->groups_dev); + + return ret; +} + +static int scmi_telemetry_des_initialize(struct device *dev, + struct scmi_tlm_instance *ti) +{ + int ret; + + ret =3D scmi_telemetry_dev_register(&ti->des_dev, &ti->dev, "des"); + if (ret) + return ret; + + for (int i =3D 0; i < ti->info->num_de; i++) { + const struct scmi_telemetry_de *de =3D ti->info->des[i]; + struct scmi_tlm_de_dev *tdev; + char name[16]; + + tdev =3D devm_kzalloc(dev, sizeof(*tdev), GFP_KERNEL); + if (!tdev) { + ret =3D -ENOMEM; + goto err; + } + + tdev->tsp =3D ti->tsp; + tdev->de =3D de; + tdev->dev.groups =3D scmi_des_groups; + + /*XXX What about of ID/name digits-length used ? */ + snprintf(name, 8, "0x%04X", de->id); + ret =3D scmi_telemetry_dev_register(&tdev->dev, + &ti->des_dev, name); + if (ret) + goto err; + + if (de->name) + sysfs_add_file_to_group(&tdev->dev.kobj, + &dev_attr_name.attr, NULL); + if (de->tstamp_support) { + sysfs_add_file_to_group(&tdev->dev.kobj, + &dev_attr_tstamp_exp.attr, + NULL); + sysfs_add_file_to_group(&tdev->dev.kobj, + &dev_attr_tstamp_enable.attr, + NULL); + } + } + + dev_info(dev, "Found %d Telemetry DE resources.\n", + ti->info->num_de); + + return 0; + +err: + scmi_telemetry_dev_unregister(&ti->des_dev); + + return ret; +} + +static int +scmi_tlm_root_instance_initialize(struct device *dev, + struct scmi_tlm_instance *ti, int instance_id) +{ + char name[16]; + int ret; + + ti->dev.class =3D &scmi_telemetry_class; + ti->dev.groups =3D scmi_telemetry_groups; + + snprintf(name, 16, "scmi_tlm_%d", instance_id); + ret =3D scmi_telemetry_dev_register(&ti->dev, NULL, name); + if (ret) + return ret; + + if (ti->info->reset_support) + ret =3D sysfs_add_file_to_group(&ti->dev.kobj, + &dev_attr_reset.attr, NULL); + + return ret; +} + +static struct scmi_tlm_instance *scmi_tlm_init(struct scmi_tlm_setup *tsp, + int instance_id) +{ + const struct scmi_telemetry_proto_ops *tlm_ops =3D tsp->ops; + struct device *dev =3D &tsp->sdev->dev; + struct scmi_tlm_instance *ti; + int ret; + + ti =3D devm_kzalloc(dev, sizeof(*ti), GFP_KERNEL); + if (!ti) + return ERR_PTR(-ENOMEM); + + ti->info =3D tlm_ops->info_get(tsp->ph); + if (!ti->info) { + dev_err(dev, "invalid Telemetry info !\n"); + return ERR_PTR(-EINVAL); + } + + ti->tsp =3D tsp; + + ret =3D scmi_tlm_root_instance_initialize(dev, ti, instance_id); + if (ret) + return ERR_PTR(ret); + + ret =3D scmi_telemetry_des_initialize(dev, ti); + if (ret) { + device_unregister(&ti->dev); + return ERR_PTR(ret); + } + + ret =3D scmi_telemetry_groups_initialize(dev, ti); + if (ret) { + scmi_telemetry_dev_unregister(&ti->des_dev); + device_unregister(&ti->dev); + return ERR_PTR(ret); + } + + return ti; +} + +static int scmi_telemetry_probe(struct scmi_device *sdev) +{ + const struct scmi_handle *handle =3D sdev->handle; + struct scmi_protocol_handle *ph; + struct device *dev =3D &sdev->dev; + struct scmi_tlm_instance *ti; + struct scmi_tlm_setup *tsp; + const void *ops; + + 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(&sdev->dev, sizeof(*tsp), GFP_KERNEL); + if (!tsp) + return -ENOMEM; + + tsp->sdev =3D sdev; + tsp->ops =3D ops; + tsp->ph =3D ph; + + //TODO Better to get info->id from SCMI/core + ti =3D scmi_tlm_init(tsp, atomic_fetch_inc(&scmi_tlm_instance_count)); + if (IS_ERR(ti)) + return PTR_ERR(ti); + + dev_set_drvdata(&sdev->dev, ti); + + return 0; +} + +static void scmi_telemetry_remove(struct scmi_device *sdev) +{ + struct device *dev =3D &sdev->dev; + struct scmi_tlm_instance *ti; + bool enabled =3D false; + int ret; + + ti =3D dev_get_drvdata(&sdev->dev); + + ret =3D ti->tsp->ops->collection_configure(ti->tsp->ph, + SCMI_TLM_GRP_INVALID, true, + &enabled, NULL, NULL); + if (ret) + dev_warn(dev, "Failed to stop Telemetry collection\n"); + + scmi_telemetry_dev_unregister(&ti->groups_dev); + scmi_telemetry_dev_unregister(&ti->des_dev); + device_unregister(&ti->dev); +} + +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-telemetry-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 class_register(&scmi_telemetry_class); + if (ret) + return ret; + + ret =3D scmi_register(&scmi_telemetry_driver); + if (ret) + class_unregister(&scmi_telemetry_class); + + return ret; +} +module_init(scmi_telemetry_init); + +static void __exit scmi_telemetry_exit(void) +{ + scmi_unregister(&scmi_telemetry_driver); + + class_unregister(&scmi_telemetry_class); +} +module_exit(scmi_telemetry_exit); + +MODULE_AUTHOR("Cristian Marussi "); +MODULE_DESCRIPTION("ARM SCMI Telemetry Driver"); +MODULE_LICENSE("GPL v2"); --=20 2.47.0 From nobody Thu Oct 9 02:16:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5DCC12080E8; Fri, 20 Jun 2025 19:29:06 +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=1750447748; cv=none; b=cETmetvx3x9AkNApcP/x8zrr5fVCCA08H/Ks4R85aGCPaIoTQpErUlqbcaw4yuwfH7vYh10BmXG2daWtr0QI4DxpX6erfzuyeFJOFtRrm//wvYwFJoDJxlk1bUXcpBXUH2R19KZ9iuRQ6xWIklQKJEIRRBv9WZUpl5R0PpM3gWI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750447748; c=relaxed/simple; bh=YRiU5FXB0wfYAoe2e3ofO+hKU2ldE2qB5AVI8IHooHA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L5Em8M4SLmq6MA/eKyi8PZQSGvvbPNaObLHvP0aa7Pl7DPBdkZkEngJc5Bq1HkVLOO7bRJ4mupRcBuaYw0ELwRQHLnUjioHW+Rco1pcYcCFcilYZvDDwxigMeBL1+Zuvl5GECcODGD2KrNLfkUfU2/vkdZ3qH07XFxkt43Hzg8o= 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; 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 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 29C9D16A3; Fri, 20 Jun 2025 12:28:46 -0700 (PDT) Received: from pluto.fritz.box (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 65C313F673; Fri, 20 Jun 2025 12:29:03 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org Cc: sudeep.holla@arm.com, 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, quic_sibis@quicinc.com, dan.carpenter@linaro.org, d-gole@ti.com, souvik.chakravarty@arm.com, Cristian Marussi Subject: [RFC PATCH 5/7] firmware: arm_scmi: Add System Telemetry chardev/ioctls API Date: Fri, 20 Jun 2025 20:28:11 +0100 Message-ID: <20250620192813.2463367-6-cristian.marussi@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250620192813.2463367-1-cristian.marussi@arm.com> References: <20250620192813.2463367-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 to the SCMI Telemetry driver an alternative custom userspace API based on a character device and ioctls. Still lacking: - a few groups related ioctls - a telemetry-reset ioctl - mmap support for raw telenetry data access Signed-off-by: Cristian Marussi --- .../firmware/arm_scmi/scmi_system_telemetry.c | 496 +++++++++++++++++- include/uapi/linux/scmi.h | 253 +++++++++ 2 files changed, 747 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/scmi.h diff --git a/drivers/firmware/arm_scmi/scmi_system_telemetry.c b/drivers/fi= rmware/arm_scmi/scmi_system_telemetry.c index a2f59001747d..c354dbe8a0f7 100644 --- a/drivers/firmware/arm_scmi/scmi_system_telemetry.c +++ b/drivers/firmware/arm_scmi/scmi_system_telemetry.c @@ -6,6 +6,7 @@ */ =20 #include +#include #include #include #include @@ -15,6 +16,8 @@ #include #include =20 +#include + #define MAX_BULK_LINE_CHAR_LENGTH 64 =20 struct scmi_tlm_setup; @@ -39,13 +42,22 @@ struct scmi_tlm_de_dev { #define to_tlm_de_dev(d) \ (container_of((d), struct scmi_tlm_de_dev, dev)) =20 +struct scmi_tlm_ioctls_db { + struct scmi_tlm_info tlm_info; + struct scmi_tlm_intervals *tlm_intervals; + struct scmi_tlm_intervals **tlm_grp_intervals; + struct scmi_tlm_des_list *tlm_des_list; +}; + struct scmi_tlm_instance { struct device dev; + struct cdev cdev; struct device des_dev; struct device groups_dev; struct scmi_tlm_de_dev **des; struct scmi_tlm_setup *tsp; const struct scmi_telemetry_info *info; + struct scmi_tlm_ioctls_db io_db; }; =20 #define dev_to_tlm_instance(d) \ @@ -71,6 +83,8 @@ struct scmi_tlm_setup { const void *priv; }; =20 +static int scmi_tlm_major; + static void scmi_telemetry_release(struct device *dev) { } @@ -326,7 +340,6 @@ static ssize_t intervals_discrete_show(struct device *d= ev, return __intervals_discrete_show(buf, ti->info->intervals.discrete); } =20 -//TODO Review available interval show #define BUF_SZ 1024 static inline ssize_t __available_update_show(char *buf, @@ -532,6 +545,31 @@ scmi_telemetry_dev_register(struct device *dev, struct= device *parent, return ret; } =20 +static int +scmi_telemetry_cdev_register(struct device *dev, struct device *parent, + struct cdev *cdev, const struct file_operations *fops, + const char *name, unsigned int minor) +{ + int ret; + + dev->parent =3D parent; + dev->release =3D scmi_tlm_dev_release; + dev_set_name(dev, "%s", name); + device_set_pm_not_required(dev); + dev_set_uevent_suppress(dev, true); + + device_initialize(dev); + + dev->devt =3D MKDEV(scmi_tlm_major, minor); + cdev_init(cdev, fops); + + ret =3D cdev_device_add(cdev, dev); + if (ret) + put_device(dev); + + return ret; +} + static int scmi_des_iter(struct device *dev, void *data) { device_unregister(dev); @@ -807,6 +845,448 @@ static int scmi_telemetry_des_initialize(struct devic= e *dev, return ret; } =20 +struct scmi_tlm_priv { + char *buf; + size_t buf_sz; + int buf_len; + struct scmi_tlm_instance *ti; +}; + +static int scmi_tlm_open(struct inode *ino, struct file *filp) +{ + struct scmi_tlm_instance *ti; + struct scmi_tlm_priv *tp; + + tp =3D kzalloc(sizeof(*tp), GFP_KERNEL); + if (!tp) + return -ENOMEM; + + ti =3D container_of(ino->i_cdev, struct scmi_tlm_instance, cdev); + tp->ti =3D ti; + + filp->private_data =3D tp; + + return 0; +} + +static int scmi_tlm_bulk_buffer_allocate_and_fill(struct scmi_tlm_priv *tp) +{ + struct scmi_tlm_instance *ti =3D tp->ti; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_telemetry_de_sample *samples; + int ret, num_samples; + + tp->buf_sz =3D ti->info->num_de * MAX_BULK_LINE_CHAR_LENGTH; + tp->buf =3D kzalloc(tp->buf_sz, GFP_KERNEL); + if (!tp->buf) + return -ENOMEM; + + num_samples =3D ti->info->num_de; + samples =3D kcalloc(num_samples, sizeof(*samples), GFP_KERNEL); + if (!samples) { + kfree(tp->buf); + return -ENOMEM; + } + + ret =3D tsp->ops->des_bulk_read(tsp->ph, SCMI_TLM_GRP_INVALID, + &num_samples, samples); + if (ret) { + kfree(samples); + kfree(tp->buf); + return ret; + } + + ret =3D scmi_tlm_buffer_fill(&ti->dev, tp->buf, tp->buf_sz, &tp->buf_len, + num_samples, samples); + kfree(samples); + + return ret; +} + +static ssize_t scmi_tlm_read(struct file *filp, char __user *buf, size_t c= ount, + loff_t *ppos) +{ + struct scmi_tlm_priv *tp =3D filp->private_data; + int ret; + + if (!tp->buf) { + ret =3D scmi_tlm_bulk_buffer_allocate_and_fill(tp); + if (ret) + return ret; + } + + return simple_read_from_buffer(buf, count, ppos, tp->buf, tp->buf_len); +} + +static __poll_t scmi_tlm_poll(struct file *, struct poll_table_struct *) +{ + return 0; +} + +static long +scmi_tlm_info_get_ioctl(struct scmi_tlm_instance *ti, unsigned long arg) +{ + void * __user uptr =3D (void * __user)arg; + + if (copy_to_user(uptr, &ti->io_db.tlm_info, + sizeof(ti->io_db.tlm_info))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_intervals_ioctl(struct scmi_tlm_instance *ti, unsigned long arg, + bool group) +{ + void * __user uptr =3D (void * __user)arg; + struct scmi_tlm_intervals ivs, *tlm_ivs; + + if (copy_from_user(&ivs, uptr, sizeof(ivs))) + return -EFAULT; + + if (!group) { + tlm_ivs =3D ti->io_db.tlm_intervals; + } else { + if (ivs.grp_id >=3D ti->info->num_groups) + return -EINVAL; + + tlm_ivs =3D ti->io_db.tlm_grp_intervals[ivs.grp_id]; + } + + if (ivs.num !=3D tlm_ivs->num) + return -EINVAL; + + if (copy_to_user(uptr, tlm_ivs, + sizeof(*tlm_ivs) + sizeof(u32) * ivs.num)) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_de_config_set_ioctl(struct scmi_tlm_instance *ti, unsigned long a= rg, + bool all) +{ + 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_config tcfg =3D {}; + int ret; + + if (copy_from_user(&tcfg, uptr, sizeof(tcfg))) + return -EFAULT; + + if (!all) + return tsp->ops->state_set(tsp->ph, false, tcfg.id, + (bool *)&tcfg.enable, + (bool *)&tcfg.t_enable); + + for (int i =3D 0; i < ti->info->num_de; i++) { + de =3D ti->info->des[i]; + + ret =3D tsp->ops->state_set(tsp->ph, false, de->id, + (bool *)&tcfg.enable, + (bool *)&tcfg.t_enable); + if (ret) + return ret; + } + + return 0; +} + +static long +scmi_tlm_de_config_get_ioctl(struct scmi_tlm_instance *ti, unsigned long a= rg) +{ + void * __user uptr =3D (void * __user)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_de_config tcfg =3D {}; + int ret; + + if (copy_from_user(&tcfg, uptr, sizeof(tcfg))) + return -EFAULT; + + ret =3D tsp->ops->state_get(tsp->ph, tcfg.id, + (bool *)&tcfg.enable, (bool *)&tcfg.t_enable); + if (ret) + return ret; + + if (copy_to_user(uptr, &tcfg, sizeof(tcfg))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_config_get_ioctl(struct scmi_tlm_instance *ti, unsigned long arg) +{ + void * __user uptr =3D (void * __user)arg; + struct scmi_tlm_config cfg; + + cfg.enable =3D !!ti->info->enabled; + cfg.current_update_interval =3D + ti->info->intervals.active_update_interval; + + if (copy_to_user(uptr, &cfg, sizeof(cfg))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_config_set_ioctl(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_config cfg =3D {}; + + if (copy_from_user(&cfg, uptr, sizeof(cfg))) + return -EFAULT; + + return tsp->ops->collection_configure(tsp->ph, SCMI_TLM_GRP_INVALID, + true, (bool *)&cfg.enable, + &cfg.current_update_interval, + NULL); +} + +static long +scmi_tlm_des_list_get_ioctl(struct scmi_tlm_instance *ti, unsigned long ar= g) +{ + void * __user uptr =3D (void * __user)arg; + struct scmi_tlm_des_list dsl; + + if (copy_from_user(&dsl, uptr, sizeof(dsl))) + return -EFAULT; + + if (dsl.num_des < ti->io_db.tlm_des_list->num_des) + return -EFAULT; + + if (copy_to_user(uptr, ti->io_db.tlm_des_list, + sizeof(*ti->io_db.tlm_des_list) + + ti->io_db.tlm_des_list->num_des * sizeof(ti->io_db.tlm_des_list->des[0= ]))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_de_value_get_ioctl(struct scmi_tlm_instance *ti, unsigned long ar= g) +{ + void * __user uptr =3D (void * __user)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_de_sample sample; + int ret; + + if (copy_from_user(&sample, uptr, sizeof(sample))) + return -EFAULT; + + 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_des_read_ioctl(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_bulk_read bulk, *bulk_ptr; + int ret; + + if (copy_from_user(&bulk, uptr, sizeof(bulk))) + return -EFAULT; + + bulk_ptr =3D kzalloc(sizeof(*bulk_ptr) + + bulk.num_samples * sizeof(bulk_ptr->samples[0]), + GFP_KERNEL); + if (!bulk_ptr) + return -ENOMEM; + + bulk_ptr->grp_id =3D bulk.grp_id; + bulk_ptr->num_samples =3D bulk.num_samples; + if (!single) + ret =3D tsp->ops->des_bulk_read(tsp->ph, bulk_ptr->grp_id, + &bulk_ptr->num_samples, + (struct scmi_telemetry_de_sample *)bulk_ptr->samples); + else + ret =3D tsp->ops->des_sample_get(tsp->ph, bulk_ptr->grp_id, + &bulk_ptr->num_samples, + (struct scmi_telemetry_de_sample *)bulk_ptr->samples); + if (ret) + goto out; + + if (copy_to_user(uptr, bulk_ptr, sizeof(*bulk_ptr) + + bulk_ptr->num_samples * sizeof(bulk_ptr->samples[0]))) + ret =3D -EFAULT; + +out: + kfree(bulk_ptr); + + return ret; +} + +static long scmi_tlm_unlocked_ioctl(struct file *filp, unsigned int cmd, + unsigned long arg) +{ + struct scmi_tlm_priv *tp =3D filp->private_data; + struct scmi_tlm_instance *ti =3D tp->ti; + + switch (cmd) { + case SCMI_TLM_GET_INFO: + return scmi_tlm_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_ioctl(ti, arg, false); + case SCMI_TLM_GET_DE_CFG: + return scmi_tlm_de_config_get_ioctl(ti, arg); + case SCMI_TLM_SET_DE_CFG: + return scmi_tlm_de_config_set_ioctl(ti, arg, false); + case SCMI_TLM_GET_DE_INFO: + return -EOPNOTSUPP; + case SCMI_TLM_GET_DE_LIST: + return scmi_tlm_des_list_get_ioctl(ti, arg); + case SCMI_TLM_GET_DE_VALUE: + return scmi_tlm_de_value_get_ioctl(ti, arg); + case SCMI_TLM_GET_GRP_CFG: + return -EOPNOTSUPP; + case SCMI_TLM_SET_GRP_CFG: + return -EOPNOTSUPP; + case SCMI_TLM_GET_GRP_INTRVS: + return scmi_tlm_intervals_ioctl(ti, arg, true); + case SCMI_TLM_GET_GRP_INFO: + return -EOPNOTSUPP; + case SCMI_TLM_GET_GRP_LIST: + return -EOPNOTSUPP; + case SCMI_TLM_SINGLE_SAMPLE: + 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_SET_ALL_CFG: + return scmi_tlm_de_config_set_ioctl(ti, arg, true); + default: + return -ENOTTY; + } +} + +static long scmi_tlm_compat_ioctl(struct file *, unsigned int, unsigned lo= ng) +{ + return 0; +} + +static int scmi_tlm_mmap(struct file *, struct vm_area_struct *) +{ + return 0; +} + +static int scmi_tlm_release(struct inode *ino, struct file *filp) +{ + struct scmi_tlm_priv *tp =3D filp->private_data; + + kfree(tp->buf); + kfree(tp); + + return 0; +} + +static const struct file_operations scmi_tlm_fops =3D { + .owner =3D THIS_MODULE, + .open =3D scmi_tlm_open, + .read =3D scmi_tlm_read, + .poll =3D scmi_tlm_poll, + .unlocked_ioctl =3D scmi_tlm_unlocked_ioctl, + .compat_ioctl =3D scmi_tlm_compat_ioctl, + .mmap =3D scmi_tlm_mmap, + .release =3D scmi_tlm_release, +}; + +static int scmi_tlm_setup_ioctl_data(struct device *dev, + struct scmi_tlm_instance *ti) +{ + ti->io_db.tlm_info.version =3D ti->info->version; + for (int i =3D 0; i < SCMI_TLM_DE_IMPL_VERS; i++) + ti->io_db.tlm_info.de_impl_version[i] =3D ti->info->de_impl_version[i]; + ti->io_db.tlm_info.num_des =3D ti->info->num_de; + ti->io_db.tlm_info.num_groups =3D ti->info->num_groups; + ti->io_db.tlm_info.num_intervals =3D ti->info->intervals.num; + if (ti->info->reset_support) + ti->io_db.tlm_info.flags =3D SCMI_TLM_CAN_RESET; + + ti->io_db.tlm_intervals =3D devm_kzalloc(dev, sizeof(*ti->io_db.tlm_inter= vals) + + ti->info->intervals.num * sizeof(__u32), + GFP_KERNEL); + if (!ti->io_db.tlm_intervals) + return -ENOMEM; + + ti->io_db.tlm_intervals->grp_id =3D 0; + ti->io_db.tlm_intervals->discrete =3D ti->info->intervals.discrete; + ti->io_db.tlm_intervals->num =3D ti->info->intervals.num; + for (int i =3D 0; i < ti->info->intervals.num; i++) + ti->io_db.tlm_intervals->available[i] =3D + ti->info->intervals.update_intervals[i]; + + ti->io_db.tlm_grp_intervals =3D devm_kcalloc(dev, ti->info->num_groups, + sizeof(ti->io_db.tlm_grp_intervals), + GFP_KERNEL); + if (!ti->io_db.tlm_grp_intervals) + return -ENOMEM; + + for (int i =3D 0; i < ti->info->num_groups; i++) { + struct scmi_tlm_intervals *ivs; + struct scmi_telemetry_group *grp =3D &ti->info->des_groups[i]; + + ivs =3D devm_kzalloc(dev, sizeof(*ivs) + + grp->intervals.num * sizeof(__u32), + GFP_KERNEL); + if (!ivs) + return -ENOMEM; + + ivs->grp_id =3D i; + ivs->discrete =3D grp->intervals.discrete; + ivs->num =3D grp->intervals.num; + for (int j =3D 0; j < ivs->num; j++) + ivs->available[i] =3D grp->intervals.update_intervals[i]; + + ti->io_db.tlm_grp_intervals[i] =3D ivs; + } + + ti->io_db.tlm_des_list =3D devm_kzalloc(dev, sizeof(*ti->io_db.tlm_des_li= st) + + ti->info->num_de * sizeof(ti->io_db.tlm_des_list->des[0]), + GFP_KERNEL); + if (!ti->io_db.tlm_des_list) + return -ENOMEM; + + ti->io_db.tlm_des_list->num_des =3D ti->info->num_de; + for (int i =3D 0; i < ti->info->num_de; i++) { + ti->io_db.tlm_des_list->des[i].id =3D ti->info->des[i]->id; + ti->io_db.tlm_des_list->des[i].grp_id =3D + ti->info->des[i]->grp ? ti->info->des[i]->grp->id : SCMI_TLM_GRP_INVALI= D; + ti->io_db.tlm_des_list->des[i].data_sz =3D ti->info->des[i]->data_sz; + ti->io_db.tlm_des_list->des[i].type =3D ti->info->des[i]->type; + ti->io_db.tlm_des_list->des[i].unit =3D ti->info->des[i]->unit; + ti->io_db.tlm_des_list->des[i].unit_exp =3D ti->info->des[i]->unit_exp; + ti->io_db.tlm_des_list->des[i].tstamp_exp =3D ti->info->des[i]->tstamp_e= xp; + ti->io_db.tlm_des_list->des[i].instance_id =3D ti->info->des[i]->instanc= e_id; + ti->io_db.tlm_des_list->des[i].compo_instance_id =3D + ti->info->des[i]->compo_instance_id; + ti->io_db.tlm_des_list->des[i].compo_type =3D ti->info->des[i]->compo_ty= pe; + ti->io_db.tlm_des_list->des[i].persistent =3D ti->info->des[i]->persiste= nt; + if (ti->info->des[i]->name) + strscpy(ti->io_db.tlm_des_list->des[i].name, ti->info->des[i]->name, + SCMI_SHORT_NAME_MAX_SIZE); + } + + return 0; +} + static int scmi_tlm_root_instance_initialize(struct device *dev, struct scmi_tlm_instance *ti, int instance_id) @@ -814,11 +1294,16 @@ scmi_tlm_root_instance_initialize(struct device *dev, char name[16]; int ret; =20 + ret =3D scmi_tlm_setup_ioctl_data(dev, ti); + if (ret) + return ret; + ti->dev.class =3D &scmi_telemetry_class; ti->dev.groups =3D scmi_telemetry_groups; =20 snprintf(name, 16, "scmi_tlm_%d", instance_id); - ret =3D scmi_telemetry_dev_register(&ti->dev, NULL, name); + ret =3D scmi_telemetry_cdev_register(&ti->dev, NULL, &ti->cdev, + &scmi_tlm_fops, name, instance_id); if (ret) return ret; =20 @@ -940,8 +1425,15 @@ static struct scmi_driver scmi_telemetry_driver =3D { =20 static int __init scmi_telemetry_init(void) { + dev_t devt; int ret; =20 + ret =3D alloc_chrdev_region(&devt, 0, 1024, "scmi-tlm"); + if (ret) + return ret; + + scmi_tlm_major =3D MAJOR(devt); + ret =3D class_register(&scmi_telemetry_class); if (ret) return ret; diff --git a/include/uapi/linux/scmi.h b/include/uapi/linux/scmi.h new file mode 100644 index 000000000000..8a0f365fca52 --- /dev/null +++ b/include/uapi/linux/scmi.h @@ -0,0 +1,253 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Copyright (C) 2025 ARM Ltd. + */ +#ifndef _UAPI_LINUX_SCMI_H +#define _UAPI_LINUX_SCMI_H + +/* + * Userspace interface SCMI Telemetry + */ + +#include +#include + +#define SCMI_TLM_DE_IMPL_VERS 4 +#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF + +/** + * scmi_tlm_info - Basic info about an instance + * + * @version: SCMI Telemetry protocol version + * @de_impl_version: SCMI Telemetry DE implementation revision + * @num_desi: Number of defined DEs + * @num_groups Number of defined DEs groups + * @num_intervals: Number of update intervals available (instance-level) + * @flags: Instance specific feature-support bitmap + * + * Used by: + * RO - SCMI_TLM_GET_INFO + */ +struct scmi_tlm_info { + __u32 version; + __u32 de_impl_version[SCMI_TLM_DE_IMPL_VERS]; + __u32 num_des; + __u32 num_groups; + __u32 num_intervals; + __u32 flags; +#define SCMI_TLM_CAN_RESET (1 << 0) +}; + +/** + * scmi_tlm_config - Basic instance configuration + * + * @enable: Enable/Disable Telemetry for the whole instance + * @current_update_interval: Get/Set currently active update interval + * (periodic tick for SHMTIs and Notifications) + * + * Used by: + * RO - SCMI_TLM_GET_CFG + * WO - SCMI_TLM_SET_CFG + */ +struct scmi_tlm_config { + __u32 enable; + __u32 current_update_interval; +}; + +/** + * scmi_tlm_intervals - Update intervals descriptor + * + * @grp_id: Group identifier (ignored by SCMI_TLM_GET_INTRVS) + * @discrete: Flag to indicate the nature of the intervals described in + * @available. When 'false' @available is a triplet: min/max/step + * @num: Number of entries of @available + * @available: A variably-sized array containing the update intervals + * + * Used by: + * RO - SCMI_TLM_GET_INTRVS + * RW - SCMI_TLM_GET_GRP_INTRVS + */ +struct scmi_tlm_intervals { + __u32 grp_id; + __u32 discrete; + __u32 num; + __u32 available[]; +}; + +/** + * scmi_tlm_de_config - DE configuration + * + * @id: Identifier of the DE to act upon (ignored by SCMI_TLM_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) + * + * Used by: + * RW - SCMI_TLM_GET_DE_CFG + * RW - SCMI_TLM_SET_DE_CFG + * WO - SCMI_TLM_SET_ALL_CFG + */ +struct scmi_tlm_de_config { + __u32 id; + __u32 enable; + __u32 t_enable; +}; + +/** + * scmi_tlm_de_info - DE Descriptor + * + * @id: DE identifier + * @grp_id: Identifier of the group which this DE belongs to; reported as + * SCMI_TLM_GRP_INVALID when not part of any group + * @data_sz: DE data size in bytes + * @type: DE type + * @unit: DE unit of measurements + * @unit_exp: Power-of-10 multiplier for DE unit + * @tstamp_exp: Power-of-10 multiplier for DE timestamp (if supported) + * @instance_id: DE instance ID + * @compo_instance_id: DE component instance ID + * @compo_type: Type of component which is associated to this DE + * @peristent: Data value for this DE survives reboot (non-cold ones) + * @name: Optional name of this DE + * + * Used to get the full description of a DE: it reflects DE Descriptors + * definitions in 3.12.4.6. + * + * Used by: + * RW - SCMI_TLM_GET_DE_INFO + * RO - SCMI_TLM_GET_DE_LIST + */ +struct scmi_tlm_de_info { + __u32 id; + __u32 grp_id; + __u32 data_sz; + __u32 type; + __u32 unit; + __s32 unit_exp; + __s32 tstamp_exp; + __u32 instance_id; + __u32 compo_instance_id; + __u32 compo_type; + __u32 persistent; + __u8 name[16]; +}; + +/** + * scmi_tlm_des_list - List of all defined DEs + * + * @num_des: Number of entries in @des + * @des: An array containing descriptors for all defined DEs + * + * Used by: + * RO - SCMI_TLM_GET_DE_LIST + */ +struct scmi_tlm_des_list { + __u32 num_des; + struct scmi_tlm_de_info des[]; +}; + +/** + * scmi_tlm_de_sample - A DE reading + * + * @id: DE identifier + * @tstamp: DE reading timestamp (equal 0 is NOT supported) + * @val: Reading of the DE data value + * + * Used by: + * RW - SCMI_TLM_GET_DE_VALUE + * RO - SCMI_TLM_SINGLE_READ + */ +struct scmi_tlm_de_sample { + __u32 id; + __u64 tstamp; + __u64 val; +}; + +/** + * scmi_tlm_bulk_read - Bulk read of multiple DEs + * + * @grp_id: The identifier of the group to query with a single asynchronous + * sample read. Set to SCMI_TLM_GRP_INVALID to ignore. + * @num_samples: Number of entries returned in @samples + * @samples: An array of samples containing an entry for each DE that was + * enabled when the single sample read request was issued. + * + * Used by: + * RW - SCMI_TLM_SINGLE_SAMPLE + * RW - SCMI_TLM_BULK_READ + */ +struct scmi_tlm_bulk_read { + __u32 grp_id; + __u32 num_samples; + struct scmi_tlm_de_sample samples[]; +}; + +/** + * scmi_tlm_grp_info - DE-group descriptor + * + * @id: Group identifier + * @flags: Group capabilities + * @num_intervals: Number of update intervals supported + * @num_des: Number of DEs part of this group + * + * Used by: + * WR - SCMI_TLM_GET_GRP_INFO + */ +struct scmi_tlm_grp_info { + __u32 id; + __u32 flags; +#define SCMI_TLM_GRP_HAS_UPDATE (1 << 0) + __u32 num_intervals; + __u32 num_des; +}; + +/** + * scmi_tlm_grps_list - DE-groups List + * + * @num_grps: Number of entries returned in @grps + * @grps: An array containing descriptors for all defined DE Groups + */ +struct scmi_tlm_grps_list { + __u32 num_grps; + struct scmi_tlm_grp_info grps[]; +}; + +/** + * scmi_tlm_grp_config - Group config + * + * @id: Identifier of the DEs-group to act upon + * @enable: A boolean to enable/disable the group + * @t_enable: A boolean to enable/disable the timestamp for this group + * + * Used by: + * RW - SCMI_TLM_GET_GRP_CFG + * WO - SCMI_TLM_SET_GRP_CFG + */ +struct scmi_tlm_grp_config { + __u32 id; + __u32 enable; + __u32 t_enable; + __u32 current_update_interval; +}; + +#define SCMI 0xF1 + +#define SCMI_TLM_GET_INFO _IOR(SCMI, 0x00, struct scmi_tlm_info) +#define SCMI_TLM_GET_CFG _IOR(SCMI, 0x01, struct scmi_tlm_config) +#define SCMI_TLM_SET_CFG _IOW(SCMI, 0x02, struct scmi_tlm_config) +#define SCMI_TLM_GET_INTRVS _IOR(SCMI, 0x03, struct scmi_tlm_intervals) +#define SCMI_TLM_GET_DE_CFG _IOWR(SCMI, 0x04, struct scmi_tlm_de_config) +#define SCMI_TLM_SET_DE_CFG _IOW(SCMI, 0x05, struct scmi_tlm_de_config) +#define SCMI_TLM_GET_DE_INFO _IOWR(SCMI, 0x06, struct scmi_tlm_de_info) +#define SCMI_TLM_GET_DE_LIST _IOWR(SCMI, 0x07, struct scmi_tlm_des_list) +#define SCMI_TLM_GET_DE_VALUE _IOWR(SCMI, 0x08, struct scmi_tlm_de_sample) +#define SCMI_TLM_GET_GRP_CFG _IOWR(SCMI, 0x09, struct scmi_tlm_grp_config) +#define SCMI_TLM_SET_GRP_CFG _IOW(SCMI, 0x0A, struct scmi_tlm_grp_config) +#define SCMI_TLM_GET_GRP_INTRVS _IOWR(SCMI, 0x0B, struct scmi_tlm_interval= s) +#define SCMI_TLM_GET_GRP_INFO _IOWR(SCMI, 0x0C, struct scmi_tlm_grp_info) +#define SCMI_TLM_GET_GRP_LIST _IOR(SCMI, 0x0D, struct scmi_tlm_grps_list) +#define SCMI_TLM_SINGLE_SAMPLE _IOWR(SCMI, 0x0E, struct scmi_tlm_bulk_read) +#define SCMI_TLM_BULK_READ _IOWR(SCMI, 0x0F, struct scmi_tlm_bulk_read) +#define SCMI_TLM_SET_ALL_CFG _IOW(SCMI, 0x10, struct scmi_tlm_de_config) + +#endif /* _UAPI_LINUX_SCMI_H */ --=20 2.47.0 From nobody Thu Oct 9 02:16:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C7B3D2EBDEE; Fri, 20 Jun 2025 19:29:08 +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=1750447750; cv=none; b=NYaoxWAgN6hJzjMPuQR6B2AXZstGaYLOrjDlPAkQp53lFGndO1YS39cuiwz7lbedoaV/WA+/Thd24QdIku7xeO/hluhb8v8Kj9wOyQLp38TYd616sWMVDLqbjELJkhZqBKYzkFBkcUlpkfxWBXuPjUFk6ekcoSDzSTCr195hINA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750447750; c=relaxed/simple; bh=/B8UcsaoV7Vi/z/5l2WdivMer4DZbo4+2QI0Zs+clM4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IeVsGrczx1zHboQrKvwWnDFUDEU0nGXqqHBQwAHzPUVYsPwIhP/iiwOxJbaZWBxdlG/zOxZ6O9eWMEgTip0ZLoprDrkBKz4IidWCQ6bJjDuFjroPG0qLhMEjQVouxoZ2JHXafHnTj8TJq60yLmZEqoHUeFotsCJaFVFQbplfmfg= 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; 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 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 A2D0F16F2; Fri, 20 Jun 2025 12:28:48 -0700 (PDT) Received: from pluto.fritz.box (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DCE7E3F673; Fri, 20 Jun 2025 12:29:05 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org Cc: sudeep.holla@arm.com, 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, quic_sibis@quicinc.com, dan.carpenter@linaro.org, d-gole@ti.com, souvik.chakravarty@arm.com, Cristian Marussi Subject: [RFC PATCH 6/7] include: trace: Add Telemetry trace events Date: Fri, 20 Jun 2025 20:28:12 +0100 Message-ID: <20250620192813.2463367-7-cristian.marussi@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250620192813.2463367-1-cristian.marussi@arm.com> References: <20250620192813.2463367-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 127300481123..471f028f36db 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), @@ -176,6 +177,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.47.0 From nobody Thu Oct 9 02:16:11 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3254C2ECEB8; Fri, 20 Jun 2025 19:29:10 +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=1750447752; cv=none; b=ICKjbdxCf1yl3OSexTAvbCo4vgJKrU8EUFPEUxERTCRZdOVMzi+si0qdmsEaYdlvecnihNb3D7R/mvVSiQESgyvCuoGliZuUtSrafABxMfQK8kHP1SIiMedlLF/ZFxSIm57QanXu+K1ITbpfaIAIzpgN+EmtaeRT69N7w2xHOlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750447752; c=relaxed/simple; bh=t+QuqQ+qtw3HRlrQJM9FCveIvct5aAupfcpOJz1MILA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mpnXkMh5e8UsuOgrTnLP/3JExYfvHu81Czz+KkBqa28nZDP/aVrWhHQwS792UWdyxhg5MFv10Zr6br4ZrNJOkKw2RD3nsNkwvTsAR0IR+TjRj2kVSG8fxXY/zvz6broy2c/tfk8nJ/16jwVb6xzuN6rqiCx1HL1zfmgYGSX9lBo= 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; 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 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 28A8816A3; Fri, 20 Jun 2025 12:28:51 -0700 (PDT) Received: from pluto.fritz.box (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6390F3F673; Fri, 20 Jun 2025 12:29:08 -0700 (PDT) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org Cc: sudeep.holla@arm.com, 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, quic_sibis@quicinc.com, dan.carpenter@linaro.org, d-gole@ti.com, souvik.chakravarty@arm.com, Cristian Marussi Subject: [RFC PATCH 7/7] firmware: arm_scmi: Use new Telemetry traces Date: Fri, 20 Jun 2025 20:28:13 +0100 Message-ID: <20250620192813.2463367-8-cristian.marussi@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250620192813.2463367-1-cristian.marussi@arm.com> References: <20250620192813.2463367-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" Track failed SHMTI accesses and notification updates. Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/telemetry.c | 35 +++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 3cbad06251a9..7843ff802bd0 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -16,6 +16,8 @@ #include "protocols.h" #include "notify.h" =20 +#include + /* Updated only after ALL the mandatory features for that version are merg= ed */ #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x10000 =20 @@ -813,8 +815,10 @@ static int scmi_telemetry_tdcf_parse_one(struct teleme= try_info *ti, int used_qwords; =20 de =3D xa_load(&ti->xa_des, le32_to_cpu(payld->id)); - if (!de || DATA_INVALID(payld)) + if (!de || DATA_INVALID(payld)) { + trace_scmi_tlm_access(de->id, "DE_INVALID", 0, 0); return -EINVAL; + } =20 used_qwords =3D 4; =20 @@ -840,6 +844,8 @@ static int scmi_telemetry_tdcf_parse_one(struct telemet= ry_info *ti, else tde->last_ts =3D 0; =20 + trace_scmi_tlm_collect(0, de->id, tde->last_val, "SHMTI_UPDATE"); + return used_qwords; } =20 @@ -864,8 +870,10 @@ static int scmi_telemetry_shmti_scan(struct telemetry_= info *ti, fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * 1000); =20 startm =3D TDCF_START_SEQ_GET(tdcf); - if (IS_BAD_START_SEQ(startm)) + if (IS_BAD_START_SEQ(startm)) { + trace_scmi_tlm_access(0, "MSEQ_BADSTART", startm, 0); continue; + } =20 qwords =3D tdcf->prlg.num_qwords; next =3D tdcf->payld; @@ -874,14 +882,18 @@ static int scmi_telemetry_shmti_scan(struct telemetry= _info *ti, =20 used_qwords =3D scmi_telemetry_tdcf_parse_one(ti, next, update ? shmti : NULL); - if (qwords < used_qwords) + if (qwords < used_qwords) { + trace_scmi_tlm_access(0, "BAD_QWORDS", 0, 0); return -EINVAL; + } =20 next +=3D used_qwords * 4; qwords -=3D used_qwords; } =20 endm =3D TDCF_END_SEQ_GET(eplg); + if (startm !=3D endm) + trace_scmi_tlm_access(0, "MSEQ_MISMATCH", startm, endm); } while (startm !=3D endm && --retries); =20 if (startm !=3D endm) @@ -1252,12 +1264,17 @@ static int scmi_telemetry_de_tdcf_parse(struct tele= metry_de *tde, fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * 1000); =20 startm =3D TDCF_START_SEQ_GET(tdcf); - if (IS_BAD_START_SEQ(startm)) + if (IS_BAD_START_SEQ(startm)) { + trace_scmi_tlm_access(tde->de.id, "MSEQ_BADSTART", + startm, 0); continue; + } =20 payld =3D tde->base + tde->offset; - if (le32_to_cpu(payld->id) !=3D tde->de.id || DATA_INVALID(payld)) + if (le32_to_cpu(payld->id) !=3D tde->de.id || DATA_INVALID(payld)) { + trace_scmi_tlm_access(tde->de.id, "DE_INVALID", 0, 0); return -EINVAL; + } =20 //TODO BLK_TS if (tstamp && USE_LINE_TS(payld) && TS_VALID(payld)) @@ -1266,6 +1283,9 @@ static int scmi_telemetry_de_tdcf_parse(struct teleme= try_de *tde, *val =3D LINE_DATA_GET(&payld->tsl); =20 endm =3D TDCF_END_SEQ_GET(tde->eplg); + if (startm !=3D endm) + trace_scmi_tlm_access(tde->de.id, "MSEQ_MISMATCH", + startm, endm); } while (startm !=3D endm && --retries); =20 if (startm !=3D endm) @@ -1412,6 +1432,9 @@ scmi_telemetry_msg_payld_process(struct telemetry_inf= o *ti, tde->last_ts =3D LINE_TSTAMP_GET(&payld->tsl); else tde->last_ts =3D 0; + + trace_scmi_tlm_collect(timestamp, tde->de.id, tde->last_val, + "MESSAGE"); } } =20 @@ -1622,6 +1645,8 @@ static void scmi_telemetry_scan_update(struct telemet= ry_info *ti, u64 ts) tde->last_ts =3D tstamp; else tde->last_ts =3D 0; + + trace_scmi_tlm_collect(ts, de->id, tde->last_val, "FC_UPDATE"); } } =20 --=20 2.47.0