From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304609; b=YkvGzzDSC5+BEAR9Mh0mbaUG2wg76dtAQ6bsIOr5LpQIIBeGgLe6ku+qOcoVYT6e1oIQ B4eNNHY31d4orrEahBXPSPKmoIFYskY0eH2uGL0lT20wXj9ZugOo/K+/WVap1jLWeMy74 EIFrzmClqKhpoAUuiOzlmwmg0LbKijgigx//vmPvDbQXEacQ2ZvmFVmYHyBSHMY4tzkzV ydAYdArZzVmr6+Hipd9xe1rYuFkH/ky/iJE/Ryl3rAm5iM2rw4zSb0J1aD8lRNqeWZQhV 6iZyMx7qOTNade+r4AhWLW+aIC6/4Y/sax+hL/KThsZdvFCKqbXp7mUNPqeP8+zzwVA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304609; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=vtM2MEBmduOcZZltNQ7NOe1dSUbwO9go+8cdTtGYRPE=; b=HPhNDeBsDbxuLjXMEav2E+uVIf/WQeEbgZwm7bpMp8XJs/Y5UFTHXbw7QcyEeaA/+5o8 bViE07nvkCPk9rWsY7XVY8rJaQ72dnDg9b5sg9DWOo/mbSDJlFkJNJLG28ATomKTiO108 sEo76dtyNS3CMMVZQU5Qi+tpGm8xrRfZoCOl1FjMkL9Z6zk5qZNHd+3YbquKpMQDkRwMG si7hABFt7qEZnMZT8w+51IU+v29OmxOa7Kaq0Oj3CrrCajZeNZIgasjOpFgjlgDOUJfa8 r173XrHkbs//4cEqO/M6gbNcWMnSvtt2joNAvJftrojvwg6IRnL2jIul1GVBakLsWUw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b="khB94L+/"; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 76FAF606A332; Sat, 21 Nov 2026 23:43:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 76FAF606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=khB94L+/ 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 D59F616F2; Sat, 21 Nov 2026 15:43:25 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E1AF93F632; Sat, 21 Nov 2026 15:43:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304608; bh=hg/2DnWI8gLzOZWWZXz8Xss4EyuZZ/xSdaUsIAEYlfM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=khB94L+/wrwZF+A06ZEvugtQ+fZpCE6NF/neZq7rZW2XV4NVCIsGL8HqAUmktn3HV u82wcho8LUZ+Ymasrx8aH4qJEweFK2N0NRjo64vZfB82IM3ercnvYJnZ0EIVwEHXZx MwZXyasKnAyXBT/O3D7m7LO4Od5QqisVxO5aa8Is= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 01/31] firmware: arm_scmi: Harden iterators state parsing Date: Sat, 21 Nov 2026 23:39:35 +0000 Message-ID: <20261121234005.1525419-2-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add checks in the core multi-part iterators support to detect generally malformed and inconsistent multi-part replies. Fixes: 36b6ea0fc6bc ("firmware: arm_scmi: Add iterators for multi-part comm= ands") Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/driver.c | 10 ++++++++++ drivers/firmware/arm_scmi/protocols.h | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi= /driver.c index 05092301db29..55d9cc487916 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -1873,11 +1873,21 @@ static int __scmi_iterator_run(void *iter, unsigned= int *start, unsigned int *en if (ret) return ret; =20 + if (i->t->rx.len < SCMI_MULTIPART_REPLY_MIN_SZ) { + dev_err(ph->dev, "Invalid multi-part reply.\n"); + return -EINVAL; + } + st->rx_len =3D i->t->rx.len; ret =3D iops->update_state(st, i->resp, i->priv); if (ret) return ret; =20 + if (!st->num_returned && st->num_remaining) { + dev_err(ph->dev, "Inconsistent multi-part reply.\n"); + return -EINVAL; + } + if (st->num_returned > st->max_resources - st->desc_index) { dev_err(ph->dev, "No. of resources can't exceed %d\n", diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_s= cmi/protocols.h index 8583159059e6..c15ec188291a 100644 --- a/drivers/firmware/arm_scmi/protocols.h +++ b/drivers/firmware/arm_scmi/protocols.h @@ -33,6 +33,13 @@ =20 #define MSG_SUPPORTS_FASTCHANNEL(x) ((x) & BIT(0)) =20 +/* + * Common minimum size required by all the existing standard and vendor + * protocols to hold number of returned and remaining items in a multi-part + * reply. + */ +#define SCMI_MULTIPART_REPLY_MIN_SZ (sizeof(u32)) + enum scmi_common_cmd { PROTOCOL_VERSION =3D 0x0, PROTOCOL_ATTRIBUTES =3D 0x1, --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304613; b=tF57R/XhI+0tDldjRtDn+FvPvo3gFnvm63xv5jbOR1ShKejErcT1BaE9q2P2ZaymATC7 I2hODV2VIFyWwpX6TmJdV4FR8FfHW4qJYPHjsMJcHOEpu1mYKY5dh/ekoSuHqzBIm/3Uk rKoPR6D5fN6RSmaxGRxhQ5OHunPvVzOzcNHqJxd+7iGVzt/8+qRI4zfRjK1fSAdYr77Od 6cJle+BPiDk2jfF2ViBFKVeO0GL8jmGHe6ZwPKJaw2Fcz/XrWZwRTdc+Q3JoGgO2A3BhK cHVH4Sqi2Bb+k4QD1QpnsW3FT/pxr36NyR9eCwfSstNx4z8TJ0iBHZRRlAPzRuZwxAA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304613; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=UP2PfH8d8VCNmy/9UBHp2fW+iJh9ONjNf0LSHyozQHU=; b=igFFyy7TNWGRSMF7mmau+ZY0T3bfVFNz1DSfHJEDo4Ycth4M4OvN864DOO7zIQPstDJ/ xF0ZZKne9b2LSvSphiuzDl64mW2+frhcmYfPfxKX8AFRbG4rEco411+X6yUom8Slmfr4M gSegAgoNho2rJrloszuZHYdI97FBdXCm5C2aWI/ZhHVcJzmcrx2+pgTvFn+khL8XsGibY DfQ4mXKh/+g2MEAOEEwbK9ygtXTMSk5rhY5Je5SVlJYXB96W8CzEebc5ebvSEujJlcHhg Lw9cORx6Ho+h/DFQUBMXgCJNwrAmpgx+/hprPeItTwmT7bslrBYrA8CJLXepeoTGaAQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=oo+UnUFY; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 914BA606A331; Sat, 21 Nov 2026 23:43:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 914BA606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=oo+UnUFY 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 05DE71575; Sat, 21 Nov 2026 15:43:29 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 13DE03F632; Sat, 21 Nov 2026 15:43:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304611; bh=/n2EWMdhO6kRAQYILC79T8bC3i1LKVlfeGgq74DQarY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oo+UnUFY/GW7vo313JKvwf3sD/EJo5YhDQvHJDjfv2SCO7B6vhg3WsxEt7g22Unr9 DsfQRzRI9TJlOvluIrI+IWWSjxG9vNtPXaGpgTHgYymr57/N9aZkrl0HJ5taSa2S0f MlZMOwGkKWqmpI3CPObLWHgO5Nk2h5C/Q37KJBQs= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 02/31] [RFC] firmware: arm_scmi: Additional iterators consistency checks Date: Sat, 21 Nov 2026 23:39:36 +0000 Message-ID: <20261121234005.1525419-3-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a few more consistency checks around the number of returned and remaining items during the life of an iteration: this is useful to spot prematurely terminated iterations that would produce shorter than expected results. Handling these anomalies in the platform multi-part replies within the core SCMI iterators machinery avoids the need to address such issues multiple times at the protocol layer. Signed-off-by: Cristian Marussi --- RFC because this is another fix that is potentially disrupting for any non compliant SCMI platform deployed in the wild. --- drivers/firmware/arm_scmi/driver.c | 38 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi= /driver.c index 55d9cc487916..a9b0f704c39b 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -1852,7 +1852,7 @@ static int __scmi_iterator_run(void *iter, unsigned i= nt *start, unsigned int *en const struct scmi_protocol_handle *ph; struct scmi_iterator_state *st; struct scmi_iterator *i; - unsigned int n; + unsigned int n, i_start, i_max, i_returned; =20 if (!iter) return -EINVAL; @@ -1862,10 +1862,15 @@ static int __scmi_iterator_run(void *iter, unsigned= int *start, unsigned int *en ph =3D i->ph; st =3D &i->state; =20 + /* Initialize iteration counters used to check consistency */ + i_max =3D 0; + i_returned =3D 0; + i_start =3D start ? *start : 0; + /* Reinitialize state for next run */ st->num_returned =3D 0; st->num_remaining =3D 0; - st->desc_index =3D start ? *start : 0; + st->desc_index =3D i_start; =20 do { iops->prepare_message(i->msg, st->desc_index, i->priv); @@ -1883,12 +1888,35 @@ static int __scmi_iterator_run(void *iter, unsigned= int *start, unsigned int *en if (ret) return ret; =20 - if (!st->num_returned && st->num_remaining) { - dev_err(ph->dev, "Inconsistent multi-part reply.\n"); + /* + * Initialize max items expectation for this run based on + * values returned on first message exchange: during the + * following iterations consistency will be validated against + * these parameters, so as to spot FW anomalies like sudden + * termination or unplausible conditions. + */ + if (!i_max) + i_max =3D i_start + st->num_returned + st->num_remaining; + + if ((!st->num_returned && st->num_remaining) || + (i_max !=3D i_start + i_returned + st->num_returned + st->num_remain= ing)) { + dev_err(ph->dev, + "Inconsistent multi-part reply. max:%u i_max:%u i_start:%u i_return= ed:%u returned:%u remaining:%u desc_index:%u\n", + st->max_resources, i_max, i_start, i_returned, + st->num_returned, st->num_remaining, + st->desc_index); return -EINVAL; } =20 - if (st->num_returned > st->max_resources - st->desc_index) { + dev_dbg(ph->dev, + "Multi-part - max:%u i_max:%u i_start:%u i_returned:%u returned:%u = remaining:%u desc_index:%u\n", + st->max_resources, i_max, i_start, i_returned, + st->num_returned, st->num_remaining, st->desc_index); + + /* Account for last batch received */ + i_returned +=3D st->num_returned; + + if (st->num_returned + st->desc_index > st->max_resources) { dev_err(ph->dev, "No. of resources can't exceed %d\n", st->max_resources); --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304615; b=qXXy7KpOAAg9iTqUlbJ5UU09Vn3UALqNlOY94j3x4ufJMDckCKdcSgDZBxTXBYF4WYUY CiMlNRp+y4er4TS5H08rwelH9VGsz9zwA5zqDdR/GTUYlvH0VJ/JITDQ4HRfk1oBoHk5D VL7I12IwY56tV3RJsTqrTfFRGrS/GBf8+slaXD4VUR6k9FcKCVrkzXLocrhaCkdbkt7D9 BHgyTRIcIDIIe1WawXqOP5zi6jVGfw+Y5cKjx9c3qqa+NaTaMedo2LmbsGAq1JWnTtv87 UNl4Fbp4k/Tc+87A6acM7WXFSpKabZ5WGS8H05nb+xxOUcWIcCj3wbTGij/jgwGrFeg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304615; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=vFT5X4VScYFosTBP0L3XnNtdE1oB9AcZg2PDRicv4pI=; b=G9j54vJL4Vo6ODW5pFRJASgT6WQ5vNUG6LrK3CCJDGd+YtGfNfGa5AxEP8E9/o/i06fs 2oWPEmUkVFgnPsyP334awKClbNXCNldOW6I8Ao933SNTfZec5cz09yiEx7ZdDMAEq0z3p 8q3ziCxYSGcq2jJzRW6CHzyDoK3iCdagtU6dFj3KBb/iHjoU8h0iWXL94BBhds7gNC46i 1On4oFBMF1poFeTW4s6yTHB9MoTPnQ63mnFdCajyPbSV/O6VMen4PAl+BFamyvlz1xzdv /STP2DYgxH0mw37MUJqejwVRg0GwCRL0IYdZjqtIoKaCshms85PAMzk2OThBmq0AYSA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=r+MPzQBx; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7B8B6606A32C; Sat, 21 Nov 2026 23:43:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7B8B6606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=r+MPzQBx 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 2653A16F2; Sat, 21 Nov 2026 15:43:32 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 37C463F632; Sat, 21 Nov 2026 15:43:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304615; bh=kCb2vXk43N6EA15KnutMEDFVvCutj0Y3CtplbWRuKk4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r+MPzQBxYByHNiQNM7X4BWA7zmfy1JTT+yMVYj3ANPNiyreXPh+pRpWdhwB/QfHR+ Dzw0tLny6hLDVgXMf9Gti6xMb3hYv+p0dB84JtsYjizZEDaTDEF7qkFUxan7Zs4AtX G0T0OQx2NzP7lkKM8I9ma3lxaXfBnIiVi7LHFTD8= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 03/31] firmware: arm_scmi: Add centralized detection of truncated replies Date: Sat, 21 Nov 2026 23:39:37 +0000 Message-ID: <20261121234005.1525419-4-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When an xfer is allocated with a very well known fixed reply size by providing a non-zero rx_size parameter to xfer_get_init(), it becomes possible to verify if the effectively received reply was truncated. Add the logic to detect and report truncated replies. Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/driver.c | 12 ++++++++++++ drivers/firmware/arm_scmi/protocols.h | 8 ++++++++ drivers/firmware/arm_scmi/raw_mode.c | 1 + 3 files changed, 21 insertions(+) diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi= /driver.c index a9b0f704c39b..12c215d04d43 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -1356,6 +1356,16 @@ static int scmi_wait_for_reply(struct device *dev, c= onst struct scmi_desc *desc, } } =20 + /* Check for truncated messages..it will be logged by the caller */ + if (!ret && !xfer->hdr.status && xfer->expected_rx_len && + xfer->expected_rx_len !=3D xfer->rx.len) { + dev_dbg(dev, + "Truncated reply - HDR:%08X expected:%zd received:%zd\n", + pack_scmi_header(&xfer->hdr), xfer->expected_rx_len, + xfer->rx.len); + xfer->hdr.status =3D SCMI_ERR_COMMS; + } + return ret; } =20 @@ -1616,6 +1626,8 @@ static int xfer_get_init(const struct scmi_protocol_h= andle *ph, =20 xfer->tx.len =3D tx_size; xfer->rx.len =3D rx_size ? : info->desc->max_msg_size; + /* When a specific rx_size was specified, check for truncation */ + xfer->expected_rx_len =3D rx_size ? : 0; xfer->hdr.type =3D MSG_TYPE_COMMAND; xfer->hdr.id =3D msg_id; xfer->hdr.poll_completion =3D false; diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_s= cmi/protocols.h index c15ec188291a..50b89c56e309 100644 --- a/drivers/firmware/arm_scmi/protocols.h +++ b/drivers/firmware/arm_scmi/protocols.h @@ -106,6 +106,13 @@ struct scmi_msg_hdr { * @rx: Receive message, the buffer should be pre-allocated to store * message. If request-ACK protocol is used, we can reuse the same * buffer for the rx path as we use for the tx path. + * @expected_rx_len: The expected length of the payload in the reply, excl= uding + * the status field. When set to a non-zero value this is used + * to evaluate if a reply had been possibly truncated due to a + * misconfigured transport: it is only set to some meaningful + * non-zero value for replies having a very well known fixed + * size, definitely not for variable-sized xfers with an RX + * len initialized to max_msg_size. * @done: command message transmit completion event * @async_done: pointer to delayed response message received event complet= ion, * or NULL when no delayed response is expected. Protected by @@ -139,6 +146,7 @@ struct scmi_xfer { struct scmi_msg_hdr hdr; struct scmi_msg tx; struct scmi_msg rx; + size_t expected_rx_len; struct completion done; struct completion *async_done; bool pending; diff --git a/drivers/firmware/arm_scmi/raw_mode.c b/drivers/firmware/arm_sc= mi/raw_mode.c index 7e42dd6cc2af..45206edff446 100644 --- a/drivers/firmware/arm_scmi/raw_mode.c +++ b/drivers/firmware/arm_scmi/raw_mode.c @@ -574,6 +574,7 @@ static int scmi_xfer_raw_get_init(struct scmi_raw_mode_= info *raw, void *buf, xfer->hdr.status =3D SCMI_SUCCESS; xfer->tx.len =3D tx_size; xfer->rx.len =3D raw->desc->max_msg_size; + xfer->expected_rx_len =3D 0; /* Clear the whole TX buffer */ memset(xfer->tx.buf, 0x00, raw->desc->max_msg_size); if (xfer->tx.len) --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304619; b=v/KvCPlPbyvl/nV1oysv3ibTpYnD9Hywk65hkjZ3eJT8TDVXA9z6VnCAbkdXeUvzfQ+r Lwt/bH5qIZs22Xv/wsr/w+k9ooppthsFayoKyY0PqukoDCYYFKiFkxH+k4RHiSLJzQZUN W3kjP4L5HXzrRPxDxDf43WthoYaKd+wMXU67c2GkclJlEQALMu9vMtwnlNazWO1KrOpK0 6xgiKPCsA56oENlSu08NU+V8+714fPB5a/BGzUyx3lwQnyY1JjBDWtI9Gc4XIM5WuITO/ EmXt+Ge2y1LzAVez0ejIL+XS6NO6A9hQ6KL0W4x5CZGHbqJ7eSGJUFhBLwJsf57GsbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304619; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=5aHo1OGeOfOj5EfMcl+mo+WPC3cWBElxO5fk9EmyHrE=; b=Knv3hbNUVOzOxKh9y5duUZ7Ix+A/FxneJk2DZzRC7GgVOvHWkyUy2AgY5gb83Agb2zdt kciYRLVnfXocoLaDazuHy4w1G5w4LUbtBoAzGiz18Gc39FEdJ3boK+0wePLHoTSkaCda5 ONyHPQN5SMzfECWVsQfVccy7jQ3ssYQo0iS4rewtjv207strx1hHXwc0OjGYxU3zzR91A 9HifqfxeQTDxrOFSEobsrzY9Lbip1zwHDHJg+SwYHLiGq8LyZX2+CXOCZkhoA64lfinVL Rj7Mxfh9SpDOPq0In6hiKb5eLiaRklHVb97dzoU+mMSzBL0Con01iCdMAsD/sBLZN3g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=qrMNmfP1; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E4163606A336; Sat, 21 Nov 2026 23:43:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E4163606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=qrMNmfP1 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 42C011575; Sat, 21 Nov 2026 15:43:35 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5A70D3F632; Sat, 21 Nov 2026 15:43:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304618; bh=wPGFAz4GR+cJOiQT8ezN/i8t+DmkptVFngiVrZEgm7E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qrMNmfP1CmXYD/fHdZSTwdes8/kNOYkiMIm1Fy/6yn7xZES/cNbiCbDOmHmrrrfUT yGOeJtTHQ9T068zNAlex+L5JeHce2HI3FCKlzC9Q1bXmbAUTLrt3RLtUsrx2l9GEdo MiP+h34ItqDj47URczXU31fEy2skGgeD8jHQeOXw= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 04/31] firmware: arm_scmi: Introduce a dedicated SCMI Errors header Date: Sat, 21 Nov 2026 23:39:38 +0000 Message-ID: <20261121234005.1525419-5-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new dedicated header and move all of the errors definitions and mapping helpers into it and out of common.h so that SCMI errors definitions alone can be easily included in protocol files without all of the unrelated additional stuff contained in common.h Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/common.h | 39 +--------------------- drivers/firmware/arm_scmi/errors.h | 52 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 drivers/firmware/arm_scmi/errors.h diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi= /common.h index af5b025ee072..cdd6f887fae7 100644 --- a/drivers/firmware/arm_scmi/common.h +++ b/drivers/firmware/arm_scmi/common.h @@ -26,6 +26,7 @@ =20 #include =20 +#include "errors.h" #include "protocols.h" #include "notify.h" =20 @@ -37,44 +38,6 @@ =20 #define SCMI_TRANSPORT_DEVNAME_PREFIX "__scmi_transport_device" =20 -enum scmi_error_codes { - SCMI_SUCCESS =3D 0, /* Success */ - SCMI_ERR_SUPPORT =3D -1, /* Not supported */ - SCMI_ERR_PARAMS =3D -2, /* Invalid Parameters */ - SCMI_ERR_ACCESS =3D -3, /* Invalid access/permission denied */ - SCMI_ERR_ENTRY =3D -4, /* Not found */ - SCMI_ERR_RANGE =3D -5, /* Value out of range */ - SCMI_ERR_BUSY =3D -6, /* Device busy */ - SCMI_ERR_COMMS =3D -7, /* Communication Error */ - SCMI_ERR_GENERIC =3D -8, /* Generic Error */ - SCMI_ERR_HARDWARE =3D -9, /* Hardware Error */ - SCMI_ERR_PROTOCOL =3D -10,/* Protocol Error */ -}; - -static const int scmi_linux_errmap[] =3D { - /* better than switch case as long as return value is continuous */ - 0, /* SCMI_SUCCESS */ - -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */ - -EINVAL, /* SCMI_ERR_PARAM */ - -EACCES, /* SCMI_ERR_ACCESS */ - -ENOENT, /* SCMI_ERR_ENTRY */ - -ERANGE, /* SCMI_ERR_RANGE */ - -EBUSY, /* SCMI_ERR_BUSY */ - -ECOMM, /* SCMI_ERR_COMMS */ - -EIO, /* SCMI_ERR_GENERIC */ - -EREMOTEIO, /* SCMI_ERR_HARDWARE */ - -EPROTO, /* SCMI_ERR_PROTOCOL */ -}; - -static inline int scmi_to_linux_errno(int errno) -{ - int err_idx =3D -errno; - - if (err_idx >=3D SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap)) - return scmi_linux_errmap[err_idx]; - return -EIO; -} - #define MSG_ID_MASK GENMASK(7, 0) #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr)) #define MSG_TYPE_MASK GENMASK(9, 8) diff --git a/drivers/firmware/arm_scmi/errors.h b/drivers/firmware/arm_scmi= /errors.h new file mode 100644 index 000000000000..e1b265d5c148 --- /dev/null +++ b/drivers/firmware/arm_scmi/errors.h @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * System Control and Management Interface (SCMI) Message Protocol + * header containing SCMI errors definitions and Linux mapping. + * + * Copyright (C) 2018-2026 ARM Ltd. + */ +#ifndef _SCMI_ERRORS_H +#define _SCMI_ERRORS_H + +#include +#include + +enum scmi_error_codes { + SCMI_SUCCESS =3D 0, /* Success */ + SCMI_ERR_SUPPORT =3D -1, /* Not supported */ + SCMI_ERR_PARAMS =3D -2, /* Invalid Parameters */ + SCMI_ERR_ACCESS =3D -3, /* Invalid access/permission denied */ + SCMI_ERR_ENTRY =3D -4, /* Not found */ + SCMI_ERR_RANGE =3D -5, /* Value out of range */ + SCMI_ERR_BUSY =3D -6, /* Device busy */ + SCMI_ERR_COMMS =3D -7, /* Communication Error */ + SCMI_ERR_GENERIC =3D -8, /* Generic Error */ + SCMI_ERR_HARDWARE =3D -9, /* Hardware Error */ + SCMI_ERR_PROTOCOL =3D -10,/* Protocol Error */ +}; + +static const int scmi_linux_errmap[] =3D { + /* better than switch case as long as return value is continuous */ + 0, /* SCMI_SUCCESS */ + -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */ + -EINVAL, /* SCMI_ERR_PARAM */ + -EACCES, /* SCMI_ERR_ACCESS */ + -ENOENT, /* SCMI_ERR_ENTRY */ + -ERANGE, /* SCMI_ERR_RANGE */ + -EBUSY, /* SCMI_ERR_BUSY */ + -ECOMM, /* SCMI_ERR_COMMS */ + -EIO, /* SCMI_ERR_GENERIC */ + -EREMOTEIO, /* SCMI_ERR_HARDWARE */ + -EPROTO, /* SCMI_ERR_PROTOCOL */ +}; + +static inline int scmi_to_linux_errno(int errno) +{ + int err_idx =3D -errno; + + if (err_idx >=3D SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap)) + return scmi_linux_errmap[err_idx]; + return -EIO; +} + +#endif /* _SCMI_ERRORS_H */ --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304622; b=X+NsPaF1yuddZn1nj90oqJImkoUpq9xSEGszDhEQrYw/b3ZVnyNWBMxpCjBO1xPeqPoL TG9NsYfGOHwqBExgNjNT56fxOqdzKlR1kZPicSvcWTQm5/fp7LS2mYFzrh2naahRuJySi ERnTUelDHyZUElbr+GCO4TEd/tlphn10y98hOC1V5DTSrgVPYXmZxdeIORwZ/jM7uqk4t CADiNQyWJ2emvKqBxLdwlnLA0Xi6IO0HdP9oCxONPaaGYHZU5LOPBexq+RoXVcfAkE91Y a4YKLMXbJSwUCC4cqOxzlCrB7gb5JV573oufRIAV18mMMKSs4QFcruBvyJKsjDpllVw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304622; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=BzTXmv4/43QI+gEKg0wG102Avm+8UYVbizH4U07EQKk=; b=DnkNk59PD4RlAPuraqi2i0qFPsenTfAoebydudcufuFlzvAvOGgVtvfTIKDi1TFXGKHB VmggVVEQdqlxIjT0cF30JU5bKC1jxBDEmpKrXxcxe8mW/Bj8hYiRwf161UyY823B9zl5p MT33zrxgUf8gwjyDQijHue05x0rxQ32CjMU/rArG0KWEZuiorfgIr592XzEoqO/PlT0RH FkWDjAQO+6vhgss0IlQc6xSvuhdIyAu/7+U2E+ai67HfU3Nh/FCf+l3kyHmJGXEfsNy1O 9oEUYXo9VfCPrkly79WILkPCIqB8P6Sn2ujDMXdPt/K0MopDWTPIxD0j6hkBJez0RkA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=nb4bQAL1; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B2164606A33A; Sat, 21 Nov 2026 23:43:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B2164606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=nb4bQAL1 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 5F81C16F2; Sat, 21 Nov 2026 15:43:38 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 77F343F632; Sat, 21 Nov 2026 15:43:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304621; bh=hp6B0902OkqPJqA8hd1E1Is4PsF85zVkd8lR/8dLC48=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nb4bQAL1k29nfg3Anw98wV7W1AVkPJyrUuwsrPMiYRs4frb3faHrB+LRfCWdm3M1L U2zquT/U7Ir+hd18vwMpnRkGu5C52wG+FwrXfCZAiqpRxbLocnudQjn47dsuq/IPfd XGs3UENUAmVrZxVRSzTLVLqNkAQQnUrzO76I76o8= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 05/31] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Date: Sat, 21 Nov 2026 23:39:39 +0000 Message-ID: <20261121234005.1525419-6-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" SCMIv4.0 introduces a couple of new possible protocol error codes: add the needed definitions and mappings to Linux error values. Note that SCMI_PARTIAL_ERROR has to be handled specially and as such it needs to be distinguishable but, as of now, all of the available sensible error codes are already mapped to some other pre-existent SCMI error condition, so we settled on -EBADMSG which is far from ideal, but at least kind of captures the nature of the underlying issue. Reviewed-by: David Hildenbrand (Arm) Reviewed-by: Jonathan Cameron Signed-off-by: Cristian Marussi --- v12 --> v13 - use EBADMSG to map SCMI_PARTIAL_ERROR v4 --> v5 - map SCMI_ERR_IN_USE to EBUSY as natural --- drivers/firmware/arm_scmi/errors.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/firmware/arm_scmi/errors.h b/drivers/firmware/arm_scmi= /errors.h index e1b265d5c148..707761cd8d34 100644 --- a/drivers/firmware/arm_scmi/errors.h +++ b/drivers/firmware/arm_scmi/errors.h @@ -23,6 +23,8 @@ enum scmi_error_codes { SCMI_ERR_GENERIC =3D -8, /* Generic Error */ SCMI_ERR_HARDWARE =3D -9, /* Hardware Error */ SCMI_ERR_PROTOCOL =3D -10,/* Protocol Error */ + SCMI_ERR_IN_USE =3D -11, /* In Use Error */ + SCMI_ERR_PARTIAL =3D -12, /* Partial Error */ }; =20 static const int scmi_linux_errmap[] =3D { @@ -38,6 +40,8 @@ static const int scmi_linux_errmap[] =3D { -EIO, /* SCMI_ERR_GENERIC */ -EREMOTEIO, /* SCMI_ERR_HARDWARE */ -EPROTO, /* SCMI_ERR_PROTOCOL */ + -EBUSY, /* SCMI_ERR_IN_USE */ + -EBADMSG, /* SCMI_ERR_PARTIAL */ }; =20 static inline int scmi_to_linux_errno(int errno) --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304625; b=ms7tXKADpk165AD5DieCPoMAcuAqse6fjCScza0hw0d3UuP6aNqGp3pmiokfSNiSQuvC 3Wi38yrwpZVpRiHn1owrFr7LjA1AnPsXv5OilDqHSVSAgFCmm+tUZImHv8iC9E6Miryz5 GJzRaY4PcKUGxkzYIsLW3vnnqlIoB5el466+qVPng2mR5kbQS1XwSi1ZO/kt7md6z/s5a c+xrwFHgWmz2+DxP0tXqvHfAuX26yRQ9knADNLRKezY4wKF/AbwH5AQGEdWqXDFoFiT6D iHh7eadEJehe+PZX34myMeSxzKO9CjdXxYjIzN54bZLsxuZi+j8gg5GvURtgTGSqnBg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304625; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=h/z+X7iEkKV49oF6OxaGD6WKtFLpZqWt61lF6Ja+o6Q=; b=h5DnDWkDlfX4ymy2xED8EMmjdr+AxcvSq2NQHwpSLuCy4lZ6RwwYxAG7JUYu55nNAZNd ZzhkLXd3lq7mmcqomBILIGFqd65vtCZvdOTjo06y9/pDnP6IrWYpSGEh7u7Zi0hYLzyJC Q1t53fTRijvf3EvRAtexyYePP1uafJynjQwIDmllojggV9hGwSwhP+YQwzFy5o6B9fhHq iKFYTymhFqhyFPWLSwRJEpzT4CCs6Tx59qbbWybBScmrDza6bAH/PMQyeMoO5XQK42Yi2 VfOM4mBTWXT971N+FD+l4X+398LbAKwo3ebDGu7mWebfztwsk3lt5qcG0poIwkJPPcQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=MslGoSlt; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C5668606A687; Sat, 21 Nov 2026 23:43:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C5668606A687 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=MslGoSlt 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 7974316F2; Sat, 21 Nov 2026 15:43:41 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 92DBC3F632; Sat, 21 Nov 2026 15:43:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304624; bh=h7QGUUz5MSwC7hcc9IF0u/AdaY1xHifslTurPKhU3wE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MslGoSltGHAAPhnrDIDAJ57Ju5IPfPM9tk7L4XwQFkstjY4hW4AtiM5uD+PstvnAB mqQocMYzV9i22W0fejVwyVFt5idNTPHPZJZ4Zr9G+VXX9ZNFQnZULXtEh/oeysBW/u BpWqxrXqmBkI+Hfud4tPOf03NH+5vUpL6FedDfCY= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 06/31] firmware: arm_scmi: Allow registration of unknown-size events/reports Date: Sat, 21 Nov 2026 23:39:40 +0000 Message-ID: <20261121234005.1525419-7-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow protocols to register events with build-time unknown sizes: such events can be declared zero-sized and let the core SCMI stack perform the needed safe-net boundary checks based on the configured transport size. Reviewed-by: Jonathan Cameron Signed-off-by: Cristian Marussi --- v14 --> v15 - better commenting v12 --> v13 - improve variable naming v10 --> v11 - account for ktime_t field when checking boundary v2 --> v3 - split out of previous patch on protocol notifier - use max() instead of max_t() --- drivers/firmware/arm_scmi/notify.c | 28 +++++++++++++++++++++++----- drivers/firmware/arm_scmi/notify.h | 8 ++++++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi= /notify.c index bc046f2f83a6..a30589c9b377 100644 --- a/drivers/firmware/arm_scmi/notify.c +++ b/drivers/firmware/arm_scmi/notify.c @@ -595,7 +595,16 @@ int scmi_notify(const struct scmi_handle *handle, u8 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. + * Note that, though, while this protects from buffer overruns, it is + * no more possible at this layer of the stack to detect zero-sized + * events corrupted by truncation. + */ + if (r_evt->evt->max_payld_sz && len > r_evt->evt->max_payld_sz) { dev_err(handle->dev, "discard badly sized message\n"); return -EINVAL; } @@ -751,7 +760,7 @@ int scmi_register_protocol_events(const struct scmi_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, default_report_sz; unsigned int num_sources; size_t payld_sz =3D 0; struct scmi_registered_events_desc *pd; @@ -766,6 +775,9 @@ 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); + default_report_sz =3D max_msg_sz + sizeof(ktime_t); + /* num_sources cannot be <=3D 0 */ if (ee->num_sources) { num_sources =3D ee->num_sources; @@ -778,8 +790,13 @@ int scmi_register_protocol_events(const struct scmi_ha= ndle *handle, u8 proto_id, } =20 evt =3D ee->evts; - for (i =3D 0; i < ee->num_events; i++) - payld_sz =3D max_t(size_t, payld_sz, evt[i].max_payld_sz); + for (i =3D 0; i < ee->num_events; i++) { + if (evt[i].max_payld_sz =3D=3D 0) { + payld_sz =3D max_msg_sz; + break; + } + payld_sz =3D max(payld_sz, evt[i].max_payld_sz); + } payld_sz +=3D sizeof(struct scmi_event_header); =20 pd =3D scmi_allocate_registered_events_desc(ni, proto_id, ee->queue_sz, @@ -805,7 +822,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 ?: default_report_sz, + GFP_KERNEL); if (!r_evt->report) return -ENOMEM; =20 diff --git a/drivers/firmware/arm_scmi/notify.h b/drivers/firmware/arm_scmi= /notify.h index f18f98c5ab3b..ff4044fd7448 100644 --- a/drivers/firmware/arm_scmi/notify.h +++ b/drivers/firmware/arm_scmi/notify.h @@ -18,8 +18,12 @@ /** * struct scmi_event - Describes an event to be supported * @id: Event ID - * @max_payld_sz: Max possible size for the payload of a notification 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 message 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 message size allowed by the + * transport plus the additional ktime_t timestamp. * * Each SCMI protocol, during its initialization phase, can describe the e= vents * it wishes to support in a few struct scmi_event and pass them to the co= re --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304628; b=cHpvGc/YZjSOhNsqvcL0tu3EsSewaoD1W3fqYyibREcYoJnmanp6RT2nesVUK0/f0SZn h05PNlcVRjGjBir+/9NfQw5sSZ5RAU8rx24W/jqkyiHHMq19GaaWW4/gMmMrdt4iovbiE LIHhO0L2BjVqrpMBfZ/YjFBRAkhzP195a1wS4ui7zX2dKD+u5pKOMZq8WFsIS+2asBsCg YTURNETkmLD6EEXPXfNjWC3po+nvsxrdIMbcbvRmzZscm7y2yBaIm4Y65DxNR7huo2Wt+ ST7LsY1Du2jug1R0dGYVjZamX7pH3kCPwq0ATx9u50aGCCi441TawRXwVwyIFiOBmcg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304628; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=tjLoocJtgZp3d7VE01qKIf/b0AUZVceK9RAY/Ki1zEE=; b=wK42t8eAGnWtZ+PyFaozKgAKdvCU/RQ3nW5vi/sgsMMrzGXPuWRi53EGLGMPviLntMe3 0bURN5JTWfo+ATb51F9fZNxRq1DA8A9Ey/t3pKxrUNq6jnRChNd8rCjLCnH/+zhaBzXQ1 heSFs9u0qSK8gf0iVimDhbNdVfXR7mmARtQeYhl8vxYTFttV9slIop+Hrk5bxx7GV7ICh zEDGsm/1hjsU8GEVk1GEQCrTV6wPyMCFFIUOkPlhYV2N42P2QC0QcXPVwerFIx7j3hxHR HcIDb2em11f/acBtxgdhNQdEZ4fajXJHIkmDv0hJ6S2Mh4avoFX8lI9589+izxqn69Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=EGaeFloH; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3FC11606A33B; Sat, 21 Nov 2026 23:43:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3FC11606A33B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=EGaeFloH 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 925CC1575; Sat, 21 Nov 2026 15:43:44 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id ACF113F632; Sat, 21 Nov 2026 15:43:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304627; bh=8kDoZMhfijALz2xCyOhL8rkJdGvulmKZNLN3A2dJI+o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EGaeFloH5WM4EIObZA92srKUHie75qJyU5B9JDhqNOA6kc0dMfHatphuWgQ7wzUnP 4mFg7ysGWgpTTbk0wfGytcSIDzVeV0XoTM00A8mnnlgRnNrV/ET+lxQWmQhzHct/Zr VAbWnIMq76SC8iZDExC9r57Qk6ZIStoZsc15+es0= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 07/31] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Date: Sat, 21 Nov 2026 23:39:41 +0000 Message-ID: <20261121234005.1525419-8-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor the __scmi_event_handler_get_ops notification helper to simplify its logic and make use of guards for locking. Event handler allocation is split out of registration in a dedicated helper so as to allow for future reuse. Mostly no functional change, in the sense that while the functionality remained the same, a richer set of errors is returned where before was always a -EINVAL. Reviewed-by: Jonathan Cameron Signed-off-by: Cristian Marussi --- v14 --> v15 - reworked error path in scmi_allocate_pending_event_handler - improve commit message --- drivers/firmware/arm_scmi/notify.c | 74 +++++++++++++++++++----------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi= /notify.c index a30589c9b377..fa2339209a69 100644 --- a/drivers/firmware/arm_scmi/notify.c +++ b/drivers/firmware/arm_scmi/notify.c @@ -891,7 +891,6 @@ void scmi_deregister_protocol_events(const struct scmi_= handle *handle, =20 /** * scmi_allocate_event_handler() - Allocate Event handler - * @ni: A reference to the notification instance to use * @evt_key: 32bit key uniquely bind to the event identified by the tuple * (proto_id, evt_id, src_id) * @@ -901,21 +900,33 @@ void scmi_deregister_protocol_events(const struct scm= i_handle *handle, * associated to this handler descriptor (hndl->r_evt =3D=3D NULL), so the= handler * is initialized as pending. * - * Context: Assumes to be called with @pending_mtx already acquired. * Return: the freshly allocated structure on Success */ -static struct scmi_event_handler * -scmi_allocate_event_handler(struct scmi_notify_instance *ni, u32 evt_key) +static struct scmi_event_handler *scmi_allocate_event_handler(u32 evt_key) { struct scmi_event_handler *hndl; =20 hndl =3D kzalloc_obj(*hndl); if (!hndl) return NULL; + hndl->key =3D evt_key; BLOCKING_INIT_NOTIFIER_HEAD(&hndl->chain); refcount_set(&hndl->users, 1); - /* New handlers are created pending */ + + return hndl; +} + +static struct scmi_event_handler * +scmi_allocate_pending_event_handler(struct scmi_notify_instance *ni, u32 e= vt_key) +{ + struct scmi_event_handler *hndl; + + lockdep_assert_held(&ni->pending_mtx); + hndl =3D scmi_allocate_event_handler(evt_key); + if (!hndl) + return NULL; + hash_add(ni->pending_events_handlers, &hndl->hash, hndl->key); =20 return hndl; @@ -1085,7 +1096,8 @@ __scmi_event_handler_get_ops(struct scmi_notify_insta= nce *ni, u32 evt_key, bool create) { struct scmi_registered_event *r_evt; - struct scmi_event_handler *hndl =3D NULL; + struct scmi_event_handler *hndl; + int ret; =20 r_evt =3D SCMI_GET_REVT(ni, KEY_XTRACT_PROTO_ID(evt_key), KEY_XTRACT_EVT_ID(evt_key)); @@ -1093,39 +1105,47 @@ __scmi_event_handler_get_ops(struct scmi_notify_ins= tance *ni, if (r_evt && r_evt->not_supported_by_platform) return ERR_PTR(-EOPNOTSUPP); =20 - mutex_lock(&ni->pending_mtx); + guard(mutex)(&ni->pending_mtx); /* Search registered events at first ... if possible at all */ if (r_evt) { - mutex_lock(&r_evt->proto->registered_mtx); + guard(mutex)(&r_evt->proto->registered_mtx); hndl =3D KEY_FIND(r_evt->proto->registered_events_handlers, hndl, evt_key); - if (hndl) + if (hndl) { refcount_inc(&hndl->users); - mutex_unlock(&r_evt->proto->registered_mtx); + return hndl; + } } =20 /* ...then amongst pending. */ - if (!hndl) { - hndl =3D KEY_FIND(ni->pending_events_handlers, hndl, evt_key); - if (hndl) - refcount_inc(&hndl->users); + hndl =3D KEY_FIND(ni->pending_events_handlers, hndl, evt_key); + if (hndl) { + refcount_inc(&hndl->users); + return hndl; } =20 - /* Create if still not found and required */ - if (!hndl && create) { - hndl =3D scmi_allocate_event_handler(ni, evt_key); - if (hndl && scmi_register_event_handler(ni, hndl)) { - dev_dbg(ni->handle->dev, - "purging UNKNOWN handler - key:%X\n", - hndl->key); - /* this hndl can be only a pending one */ - scmi_put_handler_unlocked(ni, hndl); - hndl =3D ERR_PTR(-EINVAL); - } + if (!create) + return ERR_PTR(-ENODEV); + + /* + * Create if still not found and required, new handlers are + * created pending and transitioned to registered when bound to + * an event. + */ + hndl =3D scmi_allocate_pending_event_handler(ni, evt_key); + if (!hndl) + return ERR_PTR(-ENOMEM); + + ret =3D scmi_register_event_handler(ni, hndl); + if (ret) { + dev_dbg(ni->handle->dev, + "purging UNKNOWN handler - key:%X\n", hndl->key); + /* this hndl can be only a pending one */ + scmi_put_handler_unlocked(ni, hndl); + return ERR_PTR(ret); } - mutex_unlock(&ni->pending_mtx); =20 - return hndl ?: ERR_PTR(-ENODEV); + return hndl; } =20 static struct scmi_event_handler * --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304632; b=ECxTfKx/2ezvH7J6spZRf3cFGKUxwJ9KJ1f0Q4+clknjE5QqBFj6ZEdHiqaswdThQjL9 p2k5hATUXOsxCpHbacyYxPze72Il2dSQTvyI7KSMi7Ud46Ri52eRy5SRwP4G6gr7muScP dbEbeHWc7dzgA9ytClaKdYgcbGjOFuol+olgowPZbmXo9XYL6y191pXVxMJOyMQayBSw+ ff1kntOKqxNEA25w2RBRV3yznJ2qiDFnKI8b1sGnYZ3DpZYm7mfGRz9/6/8FO5lhibfmW 7yyAz0wv3VvhAlY4yh/LXDgn7Q9sFY3m2aqWH1UqqiWGvYRrx1O9Wx1ODXUioLGnTcg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304632; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=t7R2yoCiAeLQBhB6gJTWyBDgFqLo/L9FrePQ72IrN3c=; b=0gEI2ezqBIVGD4Uz5rwlI96ZCyaAz5GipRYMMVCzJsZGtaBk/hqnMuxwkm6T2V9yktNe TdqxvhIx1TL9UNUkCSgYkq8WYD6PNPVavCZTHcqh71YdL74Hm9q1Egr06h0gRipG1gFTr mNgSqqJdpRUcDs5Yx3fiOeFar3siloIMsktv/JFMqZyAhHrI0EexNSd0vPn4Rnw7h4CDg OGo+RecZ1/r67qKYWgftdtkUF/ax5ggwgtyZvaliuJMyO/E2N5vaeM/dvggc70c+G9u0U y0DlHe3I9KoKVFsHhRIZf4iYLifZWr5qpQskDr8u+XltFyNQc/657dGRMXTy1tIshzA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=BdrPFbed; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A3909606A334; Sat, 21 Nov 2026 23:43:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A3909606A334 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=BdrPFbed 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 2336A1575; Sat, 21 Nov 2026 15:43:48 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C6A3C3F632; Sat, 21 Nov 2026 15:43:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304631; bh=hfA0ap4Xo70CAlarCd2qZoILhqj61SvsIL72n1hY+fo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BdrPFbedUT68TTWy7vCZmFOcSK2bF9LKpVBKpZahm2E0YYtNnTOGE8A6K0JeJ0Z8v UH4SlT2eWxFNisnOlSYFkBUjcbTUBCLJ4QrrvmJi/eTehXeWt34AMnKoZ9t+jzoW5c L/UBQJmZ8DLdI4k4jCxn8LBM+iIHp3SCp7ycUDHc= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi , "Rob Herring (Arm)" , Krzysztof Kozlowski , Conor Dooley , devicetree@vger.kernel.org Subject: [PATCH v16 08/31] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Date: Sat, 21 Nov 2026 23:39:42 +0000 Message-ID: <20261121234005.1525419-9-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add new SCMI v4.0 Telemetry protocol bindings definitions. Acked-by: Rob Herring (Arm) Signed-off-by: Cristian Marussi --- v3 --> v4 - changed protocol number to lowercase 1b - fixed misplaced block for protocol 0x1b Cc: Rob Herring Cc: Krzysztof Kozlowski Cc: Conor Dooley Cc: devicetree@vger.kernel.org --- Documentation/devicetree/bindings/firmware/arm,scmi.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Documentation/devicetree/bindings/firmware/arm,scmi.yaml b/Doc= umentation/devicetree/bindings/firmware/arm,scmi.yaml index 6fc44105f1c3..b63543390fd9 100644 --- a/Documentation/devicetree/bindings/firmware/arm,scmi.yaml +++ b/Documentation/devicetree/bindings/firmware/arm,scmi.yaml @@ -324,6 +324,14 @@ properties: required: - reg =20 + protocol@1b: + $ref: '#/$defs/protocol-node' + unevaluatedProperties: false + + properties: + reg: + const: 0x1b + unevaluatedProperties: false =20 $defs: --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304635; b=hsiQlvCaDbmnP43OiXWXa1E24r+NqJEebHptgQlSi+viXNBQXCgj79Vf0zCZS20xWijl ieyX15XzIcu+fqaTz4GXT8NVUIWJVd4Uq203SXnE37vAdi5zTIU5ghp4NjAoOLHZaX5zv vz4U59Eg9xytYENSuh0UB6HepmJs20ReKqcws7xLpueBTrI3tijAcqoA3NooM7KSQZ3tJ WuLucgQT6OOEutMb0ySepGa7/Boj2jcFN5fddfmGvPFL5rdK4zYq0vhgpJydKy+Gs9MPT HIiXBw6jdPdBETGINwqZ0dTz90tte+ZgFgddh7vbYk7zlioTe5rFSv2wCFLiOa69xow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304635; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=qECIJQpjfuH1ZjZJ/r1/HeSCxlIyRoe7mrKRIpv1eYs=; b=msU0Mib5jFm1cH5MgO8tOL/bYOgKeDqnp7TIJ3Ek0HW2E6kIqeHuJga1Ms1YxIXHs62+ qJjwMYPM1/JFzsc+zlPfL7p3tr9diEny/Yxik7c58sHk8iINh0B+haQNg/0O6ArT9zQRf bW//w7Aqr/75vPfxC3Edymf4BZ4uw2oTKpMEUln1LWSqIGeVMWOuCzE9iRtRIxn0Lg8zB IJfz/gpg1yOGzUTs89taaGcgIcXu4CCwFQJpx+ThrPqqVSKEZCYrrsNdT4lcMCVy/Cc// lHJ9rKzSL7z0amoJnY3EHxe1Ro9vFpvRpqA6KjjPglCjjff+qk8e3JVg0RJham7fqNg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=cR8FF+96; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AAB5F606A687; Sat, 21 Nov 2026 23:43:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AAB5F606A687 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=cR8FF+96 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 454ED16F2; Sat, 21 Nov 2026 15:43:51 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 564433F632; Sat, 21 Nov 2026 15:43:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304634; bh=/SQydrJmcQbyexqcY63QR4hJ8qgiHzXl7qLMR3pL2Cc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cR8FF+96LBqVCQtbhkCnmeOIewlvtRwu9SiDT12p0XReFeyj4e26Zgzj35A8JB5dC EtH6naw9EOXrIIpI6BFszo9UI4lWJlXvlGelmvAX7KHUnuwY+CsCYwg6PylzDheFgq OIs7JMFvQgY7Y7lPhNOD/bWBAkYpzdTnYMiyOmKk= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 09/31] include: trace: Add Telemetry trace events Date: Sat, 21 Nov 2026 23:39:43 +0000 Message-ID: <20261121234005.1525419-10-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add custom traces to report Telemetry accesses and collection events. Reviewed-by: Jonathan Cameron Signed-off-by: Cristian Marussi --- v12 --> v13 - better commit message - use const char where required v7 --> v8 - use proper FTRACE helpers for variable strings --- include/trace/events/scmi.h | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/include/trace/events/scmi.h b/include/trace/events/scmi.h index b421c253df00..ae63a13fd141 100644 --- a/include/trace/events/scmi.h +++ b/include/trace/events/scmi.h @@ -178,6 +178,51 @@ TRACE_EVENT(scmi_msg_dump, __get_str(tag), __entry->msg_id, __entry->seq, __entry->status, __print_hex_str(__get_dynamic_array(cmd), __entry->len)) ); + +TRACE_EVENT(scmi_tlm_access, + TP_PROTO(u64 de_id, const char *tag, u64 startm, u64 endm), + TP_ARGS(de_id, tag, startm, endm), + + TP_STRUCT__entry( + __field(u64, de_id) + __string(tag, tag) + __field(u64, startm) + __field(u64, endm) + ), + + TP_fast_assign( + __entry->de_id =3D de_id; + __assign_str(tag); + __entry->startm =3D startm; + __entry->endm =3D endm; + ), + + TP_printk("[%s] de_id=3D0x%llX - startm=3D%016llX endm=3D%016llX", + __get_str(tag), __entry->de_id, __entry->startm, __entry->endm) +); + +TRACE_EVENT(scmi_tlm_collect, + TP_PROTO(u64 ts, u64 de_id, u64 value, const char *tag), + TP_ARGS(ts, de_id, value, tag), + + TP_STRUCT__entry( + __field(u64, ts) + __field(u64, de_id) + __field(u64, value) + __string(tag, tag) + ), + + TP_fast_assign( + __entry->ts =3D ts; + __entry->de_id =3D de_id; + __entry->value =3D value; + __assign_str(tag); + ), + + TP_printk("[%s] ts=3D%llu de_id=3D0x%04llX value=3D%016llu", + __get_str(tag), __entry->ts, __entry->de_id, __entry->value) +); + #endif /* _TRACE_SCMI_H */ =20 /* This part must be outside protection */ --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304638; b=IARzXiBKJ/RXuguhQHMF2e4VTNyHoLVaZbQpm7M9Kge6IjvLcHvzMHSa6ODDskEUTUXp 7gtD0+a9Pf4IHDjzGZqv/6W5l0B8EH8J3SnJAAI06C3BiKXbrN7jxkqkXICWgi4QOLYbZ SA9Ovq1NLleM1H/Hf4ZrZQbcRPU+4mrvc+ecAlXCI1kgybKOciL4dIJfKDiKQpmXKNVw4 tAHUe8xOa5LovCfMkRXLBjmDyw+Dc7gkDXTKEyfhhhUSmdz+nGeK0G3a3y4xdAV4K5ZV7 Kg7D2A8Odoxu+zRWXGI2r/STQqCa4O7m2gCU7ruG3FnOswO8pm3vj2zJPYReTqjlwug== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304638; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=7jwAg6WvATTTODw5BkfNj5ph2KGnhaujgXrXzXLvI1c=; b=BMv4zlJA6zUuHYG+IoRnES3b18uu+/bmlo8DYqgwHw4jpkEkQ06m5RQMz6oGb+HYrNCE U2t+bpP3e7nBgs0d1Eq7jIX18t1HRaWdw/Q6/HQL4l3NynHNPfbFQp9sPHnnbJ1pzcnCL DNGTcqUUssnadBzuVNYIJGo4HImupdTcp1mPLpdxVHOLanS7cyiLbqAwChTJ/rm9bJd5g 6zqDaicEpikcxmx56UsVUMRq4iIOfa+PTOQ5SyD85X0PjBlb5mUZQPn0NLyzes+yymMFq 3xyClMxpDrX0SfnlAD88tgYGWKd302JwKLX+axln1kyecHHy2yI6VRYbPKJMm0ak3BQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=UqSRWyKN; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8CAF606A32C; Sat, 21 Nov 2026 23:43:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D8CAF606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=UqSRWyKN 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 7A5E31575; Sat, 21 Nov 2026 15:43:54 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 76BF03F632; Sat, 21 Nov 2026 15:43:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304637; bh=MnQJsiSKdVcB2DHJQDYdHnoxWc1fDJi8H7e1i3dp38s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UqSRWyKNEXxum3h7qBwT7yegGewoAv+CA+3a5bzqAeVR1zb4XRZ1OqjvQr7bVUjpU yp71jCO1yhEMqEcLVcrNVO1CndDv+emlpRLlp0iyfyqG/hChkFmCPBzCSebkyFcICR JRVMHHhONL0ZtiRe+OOqGGg2AWbCsSFLkDOjcsAI= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 10/31] firmware: arm_scmi: Add Telemetry basic attributes enumeration Date: Sat, 21 Nov 2026 23:39:44 +0000 Message-ID: <20261121234005.1525419-11-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add SCMIv4.0 Telemetry basic support for minimal initialization and protocol attributes enumeration: expose telemetry instance descriptor via a simple information retrieval protocol operation. Signed-off-by: Cristian Marussi --- v15 --> v16 - ignore groups v14 --> v15 - add include uuid.h into scmi_protocol.h - dropped needless __must_check v13 --> v14 - split original basic Telemetry patch, reducing this patch to the basics of protocol attributes enumeration only - fix group unroll loop on groups_init failure - remove goto from de_desc_parse - renamed some fields to more strictly adhere to spec nomenclature v12 --> v13 - headers includes re-order - rework cleanup path for instance allocations - relocate tde->mtx guards higher up and add lockdep assertions - add more _RAW macros - rename DE_CONFIGURE defines - use *alloc_objs - remove TO_CPU64 and useless READ_ONCEs v11 --> v12 - dropped counted-by in smhtis - reworked barriers around res_get - added smp_store_release on TDE registration v10 -->v11 - relocated a few parsing macros from another patch - cleanup exit path on resource enumeration - remove 'naked' ti->rinfo accesses - walk xa_des to free DEs on removal v8 --> v9 - Fixes for some Sashiko issues on v8 - use counted_by_lle where appropriate - consider header in boudanry check in DE_DESCRIPTION iterators - bail out if SHMTI areas are NOT 32bit aligned - warn if SHMTI areas are NOT 64bit aligned - convert SCMI_TDE_VAR_SZ macro to an inline function - make some enum definition explicit v7 --> v8 - Fixes for some Sashiko issues on v7 - add proper msg boundary parsing checks on DE_DESCRIPTION multipart - add proper cleanup/rollback on groups_init err_path - update num_des_tstamp ONLY after a DE is successfully initialized - proper cleanup tde during tde_put (preserving info/mutex bits) - add membarriers in enumerate resources - add a check and a warn on SHMTI phys_addr alignment - reword a few dev_errs - use tde_free_put on resoucers_free v6 --> v7 - use uuid_t for proper Telemetry UUID DE_IMPL_VERSION endianity v5 --> v6 - define uuid_line dwords as BE - fixed Sashiko reviews where applicable: https://sashiko.dev/#/message/20260703123601.381275-8-cristian.marussi%4= 0arm.com - fix missing le32 and bound check in variable payload parsing - fix TDE duplicatin logic - fix intervals cleanup - fixes also KMEMLEAK report - implement proper freeinfg for group resources - proper SHMTI iterations - removed useless/stale static in scmi_telemetry_tde_get() v4 --> v5 - fix de->grp_id initialization for DEs not belonging to a group - removed UAPI structures dependency - expose discovered SHMTIs areas in scmi_protocol.h - fixes sparse warnings v3 -->v4 - bail-out on FW_BUG errors - count timestamp capable DE to track enables v2 --> v3 - split from monolithic Telemetry patch - fix checkpatch macros complaints - fix ACCESS_PRIVATE usage - add a few comments on allocation/enumeration lifetime - use interval.num_intervals - removed needless cleanup handler usage - simply return from scmi_telemetry_de_lookup() - fixed composing_des name length to 08X --- drivers/firmware/arm_scmi/Makefile | 2 +- drivers/firmware/arm_scmi/driver.c | 2 + drivers/firmware/arm_scmi/protocols.h | 1 + drivers/firmware/arm_scmi/telemetry.c | 149 ++++++++++++++++++++++++++ include/linux/scmi_protocol.h | 33 +++++- 5 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 drivers/firmware/arm_scmi/telemetry.c diff --git a/drivers/firmware/arm_scmi/Makefile b/drivers/firmware/arm_scmi= /Makefile index 780cd62b2f78..fe55b7aa0707 100644 --- a/drivers/firmware/arm_scmi/Makefile +++ b/drivers/firmware/arm_scmi/Makefile @@ -8,7 +8,7 @@ scmi-driver-$(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT) +=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 12c215d04d43..1e2bf1eef2a4 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -3683,6 +3683,7 @@ static int __init scmi_driver_init(void) scmi_system_register(); scmi_powercap_register(); scmi_pinctrl_register(); + scmi_telemetry_register(); =20 return platform_driver_register(&scmi_driver); } @@ -3701,6 +3702,7 @@ static void __exit scmi_driver_exit(void) scmi_system_unregister(); scmi_powercap_unregister(); scmi_pinctrl_unregister(); + scmi_telemetry_unregister(); =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 50b89c56e309..1aa3ee1fb2b0 100644 --- a/drivers/firmware/arm_scmi/protocols.h +++ b/drivers/firmware/arm_scmi/protocols.h @@ -409,5 +409,6 @@ DECLARE_SCMI_REGISTER_UNREGISTER(sensors); DECLARE_SCMI_REGISTER_UNREGISTER(voltage); DECLARE_SCMI_REGISTER_UNREGISTER(system); DECLARE_SCMI_REGISTER_UNREGISTER(powercap); +DECLARE_SCMI_REGISTER_UNREGISTER(telemetry); =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..402d9587046c --- /dev/null +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * System Control and Management Interface (SCMI) Telemetry Protocol + * + * Copyright (C) 2026 ARM Ltd. + */ + +#include +#include +#include +#include +#include +#include +#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_DE_IMPL_NUM_DWORDS 4 + +/* An enum listing the protocol specific commands definitions */ +enum scmi_telemetry_protocol_cmd { + TELEMETRY_LIST_SHMTI =3D 0x3, + TELEMETRY_DE_DESCRIPTION =3D 0x4, + TELEMETRY_LIST_UPDATE_INTERVALS =3D 0x5, + TELEMETRY_DE_CONFIGURE =3D 0x6, + TELEMETRY_DE_ENABLED_LIST =3D 0x7, + TELEMETRY_CONFIG_SET =3D 0x8, + TELEMETRY_READING_COMPLETE =3D TELEMETRY_CONFIG_SET, + TELEMETRY_CONFIG_GET =3D 0x9, + TELEMETRY_RESET =3D 0xA, +}; + +struct scmi_msg_resp_telemetry_protocol_attributes { + __le32 de_num; + __le32 groups_num; + __le32 de_implem_rev_dword[SCMI_TLM_DE_IMPL_NUM_DWORDS]; + __le32 attributes_1; +#define SUPPORTS_SINGLE_READ(x) (le32_get_bits((x), BIT(31))) +#define SUPPORTS_CONTINUOS_UPDATE(x) (le32_get_bits((x), BIT(30))) +#define SUPPORTS_PER_GROUP_CONFIG(x) (le32_get_bits((x), BIT(18))) +#define SUPPORTS_RESET(x) (le32_get_bits((x), BIT(17))) +#define SUPPORTS_FC(x) (le32_get_bits((x), BIT(16))) + __le32 default_blk_ts_rate; +}; + +struct telemetry_info { + bool streaming_mode; + unsigned int num_shmti; + unsigned int num_des_tstamp; + unsigned int default_blk_ts_rate; + const struct scmi_protocol_handle *ph; + struct scmi_telemetry_info info; +}; + +static int +scmi_telemetry_protocol_attributes_get(struct telemetry_info *ti) +{ + struct scmi_msg_resp_telemetry_protocol_attributes *resp; + const struct scmi_protocol_handle *ph =3D ti->ph; + struct scmi_xfer *t; + int ret; + + ret =3D ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0, + sizeof(*resp), &t); + if (ret) + return ret; + + resp =3D t->rx.buf; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + __le32 attr =3D resp->attributes_1; + __be32 de_impl_version[SCMI_TLM_DE_IMPL_NUM_DWORDS]; + + ti->info.base.num_des =3D le32_to_cpu(resp->de_num); + ti->info.base.num_groups =3D 0; + for (int i =3D 0; i < SCMI_TLM_DE_IMPL_NUM_DWORDS; i++) + de_impl_version[i] =3D + cpu_to_be32(le32_to_cpu(resp->de_implem_rev_dword[i])); + import_uuid(&ti->info.base.primary_revision, (const __u8 *)&de_impl_vers= ion); + ti->info.single_read_support =3D SUPPORTS_SINGLE_READ(attr); + ti->info.continuous_update_support =3D SUPPORTS_CONTINUOS_UPDATE(attr); + ti->info.per_group_config_support =3D SUPPORTS_PER_GROUP_CONFIG(attr); + ti->info.reset_support =3D SUPPORTS_RESET(attr); + ti->info.fc_support =3D SUPPORTS_FC(attr); + ti->num_shmti =3D le32_get_bits(attr, GENMASK(15, 0)); + ti->default_blk_ts_rate =3D le32_to_cpu(resp->default_blk_ts_rate); + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + +static const struct scmi_telemetry_info * +scmi_telemetry_info_get(const struct scmi_protocol_handle *ph) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + + return &ti->info; +} + +static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { + .info_get =3D scmi_telemetry_info_get, +}; + +static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle = *ph) +{ + struct device *dev =3D ph->dev; + struct telemetry_info *ti; + int ret; + + dev_dbg(dev, "Telemetry Version %d.%d\n", + PROTOCOL_REV_MAJOR(ph->version), PROTOCOL_REV_MINOR(ph->version)); + + ti =3D devm_kzalloc(dev, sizeof(*ti), GFP_KERNEL); + if (!ti) + return -ENOMEM; + + ti->ph =3D ph; + + ret =3D scmi_telemetry_protocol_attributes_get(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot retrieve protocol attributes. Abort.\n"); + return ret; + } + + ti->info.base.version =3D ph->version; + + return ph->set_priv(ph, ti); +} + +static const struct scmi_protocol scmi_telemetry =3D { + .id =3D SCMI_PROTOCOL_TELEMETRY, + .owner =3D THIS_MODULE, + .instance_init =3D &scmi_telemetry_protocol_init, + .ops =3D &tlm_proto_ops, + .supported_version =3D SCMI_PROTOCOL_SUPPORTED_VERSION, +}; + +DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(telemetry, scmi_telemetry) diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 3f7cc35eb861..f076ce5601cf 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -2,17 +2,19 @@ /* * SCMI Message Protocol driver header * - * Copyright (C) 2018-2021 ARM Ltd. + * Copyright (C) 2018-2026 ARM Ltd. */ =20 #ifndef _LINUX_SCMI_PROTOCOL_H #define _LINUX_SCMI_PROTOCOL_H =20 #include +#include #include #include #include #include +#include =20 #define SCMI_MAX_STR_SIZE 64 #define SCMI_SHORT_NAME_MAX_SIZE 16 @@ -828,6 +830,35 @@ struct scmi_pinctrl_proto_ops { int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin); }; =20 +struct scmi_telemetry_base_info { + unsigned int version; + uuid_t primary_revision; + unsigned int num_des; + unsigned int num_groups; + unsigned int num_intervals; + unsigned int num_shmtis; +}; + +struct scmi_telemetry_info { + bool single_read_support; + bool continuous_update_support; + bool per_group_config_support; + bool reset_support; + bool fc_support; + struct scmi_telemetry_base_info base; +}; + +/** + * struct scmi_telemetry_proto_ops - represents the various operations pro= vided + * by SCMI Telemetry Protocol + * + * @info_get: get the general Telemetry information. + */ +struct scmi_telemetry_proto_ops { + const struct scmi_telemetry_info *(*info_get) + (const struct scmi_protocol_handle *ph); +}; + /** * struct scmi_notify_ops - represents notifications' operations provided= by * SCMI core --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304641; b=CWNDCgS8gVAZR5DpsOdpqoYv2FxmsrtJ/1oq3Xv6FOFOQd318d4+4+NA3kKjYxZhxIC2 thALquFNaLFWWuRQSMpkisD1vJFkWmEVLduk5sXnJnt5eFRYgr6AvjJafGQ5ws+EhQWuX K66fhcyFNnLQbWpXbD5saBXqb2l4kuT9O803JB7hwYK+fJfONxAbbZdWag0eke1fWXVRg iyaPFI2THYyFAqPnct8W5ReoTcWf9xds7B+XpVdFg5Gn5z76IBZgA0b4ID0iOk0IaKhoC VRJoVqXM/jt1ge2sfqMb8HZUGyaXPMxvXueYDEby9yxAkvMjhjsuy35a3wZeIYxvcig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304641; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=1g9jp5YhHfpZqqjNpeqxMtcVh/aFg6qlOiO9EtQpU3E=; b=JFhzfysTfDsazH7vleCYepwbfyJyLL+zquzo9AyYWTtKMHDbSxTOV+bcbYmu9zjA77P5 Z+u8bXP2jTO+CobFehj1GmMhn+emxNrb/nYl8rovcrKA7sFrgLmXcgOI6eOwqF/QG9RRT nX1n2ih8XpN2o7H1kYn+NjcncA2wDUOCrlvv64TUjj1VbfQfHlUVw9FVfuCEzI71XoLGL OObfz3/DVutVh6ugAVeIIfkdDqLbhTu8qtVgvYQJrD2KCQczTFHSpN7I+y522Y7ySvplB +t1jewyhFUSxl+qWG7FAcwFHleZTDH9CEc9WfMUaFtOWSG97iZsY+4Lin1KujxLS7nA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=rJfNU4gc; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 15297606A337; Sat, 21 Nov 2026 23:44:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 15297606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=rJfNU4gc 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 96B9F1575; Sat, 21 Nov 2026 15:43:57 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id ABF853F632; Sat, 21 Nov 2026 15:43:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304640; bh=l15Xy1hLDhzj6zen38T9a5GQ3FU1eU8c6opCKcEZ0j4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rJfNU4gcAdM5SsnVJNYgJ98WhlNEopk3Y+uciZAco4IgxKyYgymfh9cnTSdmWq8eq jxJ6+QPVbpDFKQPqPUr5N1RWOsMa7Ou4hm3kn6flU8+DtB8IYpxhJDRsCILowSdLZO 67XIxH8sAcnjQbIUVsiZxvgNWKiv4Wm1UqH5i2y0= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 11/31] firmware: arm_scmi: Add Telemetry resources enumeration Date: Sat, 21 Nov 2026 23:39:45 +0000 Message-ID: <20261121234005.1525419-12-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add basic Telemetry allocation and initialization routines, introducing and wiring-up the logic needed to enumerate all the SCMI Telemetry resources optionally using a lazy on-demand scheme: this approach factors in the flexibility needed to minimize the amount of initialization work based on the effective access pattern used by the upper layer ABI. Signed-off-by: Cristian Marussi --- v15 --> v16 - add tdes_cleanup as devm_action after enumeration - use phys_addr_t and proper format string - drop TDE_FC_SIZE and introduce better checks - unlink FC TDEs from fcs_des list on free - bailout on unknown TDE types - reject FCs smaller than 8 bytes v14 --> v15 - drop usage of devres for FC iomapping - trigger full enumeration of DE lookups - new IS_TDE_TS_ macro helpers - add a dedicated scmi_telemetry_free_tdes_add helper - better commenting - dropping needless __must_check - flag as fully enumerated only when ALL declared des were found - drop redundant iounmap - check FC size on enumeration v13 --> v14 - splitted out of the old Basic Telemetry patch, contains the basic instance allocations and the DEs enumeration process - renamed some fields to more strictly adhere to spec nomenclature --- drivers/firmware/arm_scmi/telemetry.c | 745 ++++++++++++++++++++++++++ include/linux/scmi_protocol.h | 37 ++ 2 files changed, 782 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 402d9587046c..64c92d9ab900 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -8,15 +8,19 @@ #include #include #include +#include +#include #include #include #include #include #include +#include #include #include #include #include +#include =20 #include "protocols.h" #include "notify.h" @@ -52,13 +56,213 @@ struct scmi_msg_resp_telemetry_protocol_attributes { __le32 default_blk_ts_rate; }; =20 +struct de_desc_fastchannel { + __le32 addr_low; + __le32 addr_high; + __le32 size; +}; + +struct scmi_de_desc { + __le32 de_id; + __le32 group_id; + __le32 de_data_sz; + __le32 de_attributes_1; +#define IS_NAME_SUPPORTED(d) (le32_get_bits((d)->de_attributes_1, BIT(31))) +#define IS_FC_SUPPORTED(d) (le32_get_bits((d)->de_attributes_1, BIT(30))) +#define GET_DE_TYPE(d) (le32_get_bits((d)->de_attributes_1, GENMASK(29, 2= 2))) +#define IS_PERSISTENT(d) (le32_get_bits((d)->de_attributes_1, BIT(21))) +#define GET_DE_UNIT_EXP(d) \ + ({ \ + __u32 __signed_exp =3D \ + le32_get_bits((d)->de_attributes_1, GENMASK(20, 13)); \ + \ + sign_extend32(__signed_exp, 7); \ + }) +#define GET_DE_UNIT(d) (le32_get_bits((d)->de_attributes_1, GENMASK(12, 5= ))) +#define TSTAMP_SUPPORT(d) (le32_get_bits((d)->de_attributes_1, GENMASK(1, = 0))) + __le32 de_attributes_2; +#define GET_DE_INSTA_ID(d) (le32_get_bits((d)->de_attributes_2, GENMASK(31= , 24))) +#define GET_COMPO_INSTA_ID(d) (le32_get_bits((d)->de_attributes_2, GENMASK= (23, 8))) +#define GET_COMPO_TYPE(d) (le32_get_bits((d)->de_attributes_2, GENMASK(7, = 0))) + __le32 de_attributes_3; +}; + +struct scmi_msg_resp_telemetry_de_description { + __le32 num_desc; /* This does NOT count desc[] by the spec */ + struct scmi_de_desc desc[]; +}; + +#define IS_TDE_TSTAMP_TYPE(__t, __ttype) ((__t)->ts_type =3D=3D (__ttype)) +#define IS_TDE_TS_NONE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE)) +#define IS_TDE_TS_LINE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE)) +#define IS_TDE_TS_BLK(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_BLK)) + +enum timestamps { + TSTAMP_NONE =3D 0, + TSTAMP_LINE =3D 1, + TSTAMP_BLK =3D 2, +}; + +#define SHMTI_ID_INVALID GENMASK(31, 0) + +struct telemetry_de { + enum timestamps ts_type; + u32 ts_rate; + bool enumerated; + void __iomem *base; + u32 sid; + u32 offset; + /* NOTE THAT DE data_sz is registered in scmi_telemetry_de */ + u32 fc_size; + struct list_head item; + struct scmi_telemetry_de de; + /* Protect last_val/ts/magic accesses - MUST BE KEPT LAST */ + struct mutex mtx; +}; + +#define to_tde(d) container_of(d, struct telemetry_de, de) + struct telemetry_info { bool streaming_mode; unsigned int num_shmti; unsigned int num_des_tstamp; unsigned int default_blk_ts_rate; const struct scmi_protocol_handle *ph; + struct telemetry_de *tdes; + struct xarray xa_des; + /* Mutex to protect access to @free_des */ + struct mutex free_mtx; + struct list_head free_des; + struct list_head fcs_des; struct scmi_telemetry_info info; + atomic_t rinfo_initializing; + struct completion rinfo_initdone; + struct scmi_telemetry_res_info *rinfo; + struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti); +}; + +static struct scmi_telemetry_res_info * +__scmi_telemetry_resources_get(struct telemetry_info *ti); + +static struct telemetry_de * +scmi_telemetry_free_tde_get(struct telemetry_info *ti) +{ + struct telemetry_de *tde; + + guard(mutex)(&ti->free_mtx); + + tde =3D list_first_entry_or_null(&ti->free_des, struct telemetry_de, item= ); + if (!tde) + return tde; + + list_del(&tde->item); + + return tde; +} + +static inline void scmi_telemetry_free_tdes_add(struct telemetry_info *ti, + struct telemetry_de *tde) +{ + guard(mutex)(&ti->free_mtx); + list_add_tail(&tde->item, &ti->free_des); +} + +static void scmi_telemetry_free_tde_put(struct telemetry_info *ti, + struct telemetry_de *tde) +{ + struct scmi_telemetry_de_info *info; + + lockdep_assert_held(&tde->mtx); + + /* Unlink and unmap any valid FC */ + if (tde->de.fc_support) { + /* + * During normal system operation this can be triggered only + * once by the DE description iteraror while enumerating + * resources, so the fcs_des list is not manipulated elsewhere + * when we remove this TDE from that list. + */ + list_del(&tde->item); + if (tde->base) + iounmap(tde->base); + } + + /* Save clear and restore */ + info =3D tde->de.info; + memset(info, 0, sizeof(*info)); + memset(tde, 0, offsetof(struct telemetry_de, mtx)); + tde->de.info =3D info; + + scmi_telemetry_free_tdes_add(ti, tde); +} + +static struct telemetry_de *scmi_telemetry_tde_lookup(struct telemetry_inf= o *ti, + unsigned int de_id) +{ + struct scmi_telemetry_de *de; + + de =3D xa_load(&ti->xa_des, de_id); + if (!de) + return NULL; + + return to_tde(de); +} + +static struct telemetry_de *scmi_telemetry_tde_get(struct telemetry_info *= ti, + unsigned int de_id) +{ + struct telemetry_de *tde; + + tde =3D scmi_telemetry_free_tde_get(ti); + if (!tde) { + dev_err(ti->ph->dev, "Cannot get TDE for ID:0x%08X\n", de_id); + return ERR_PTR(-ENOSPC); + } + + tde->sid =3D SHMTI_ID_INVALID; + + return tde; +} + +static int scmi_telemetry_tde_register(struct telemetry_info *ti, + struct telemetry_de *tde) +{ + struct scmi_telemetry_res_info *rinfo; + int ret; + + lockdep_assert_held(&tde->mtx); + + /* Get rinfo without triggering a recursive enumeration */ + rinfo =3D __scmi_telemetry_resources_get(ti); + + if (rinfo->num_des >=3D ti->info.base.num_des) { + ret =3D -ENOSPC; + goto err; + } + + /* Store DE pointer by de_id ... */ + ret =3D xa_insert(&ti->xa_des, tde->de.info->id, &tde->de, GFP_KERNEL); + if (ret) + goto err; + + /* ... and in the general array */ + rinfo->des[rinfo->num_des] =3D &tde->de; + /* Make sure the freshly registered DE is visible before the index update= */ + smp_store_release(&rinfo->num_des, rinfo->num_des + 1); + + return 0; + +err: + dev_err(ti->ph->dev, "Cannot register TDE for ID:0x%08X - ret:%d\n", + tde->de.info->id, ret); + + return ret; +} + +struct scmi_tlm_de_priv { + struct telemetry_info *ti; + void *next; + size_t rx_len; }; =20 static int @@ -100,6 +304,320 @@ scmi_telemetry_protocol_attributes_get(struct telemet= ry_info *ti) return ret; } =20 +static void iter_tlm_prepare_message(void *message, + unsigned int desc_index, const void *priv) +{ + put_unaligned_le32(desc_index, message); +} + +static int iter_de_descr_update_state(struct scmi_iterator_state *st, + const void *response, void *priv) +{ + const struct scmi_msg_resp_telemetry_de_description *r =3D response; + struct scmi_tlm_de_priv *p =3D priv; + + st->num_returned =3D le32_get_bits(r->num_desc, GENMASK(15, 0)); + st->num_remaining =3D le32_get_bits(r->num_desc, GENMASK(31, 16)); + + if (st->rx_len < (sizeof(*r) + sizeof(r->desc[0]) * st->num_returned)) + return -EINVAL; + + /* Initialized to first descriptor and initial payload size */ + p->next =3D (void *)r->desc; + p->rx_len =3D st->rx_len - sizeof(*r); + + return 0; +} + +static inline size_t scmi_telemetry_tde_var_size_calc(struct telemetry_de = *tde) +{ + size_t sz =3D 0; + + sz +=3D IS_TDE_TS_LINE(tde) ? sizeof(__le32) : 0; + sz +=3D tde->de.fc_support ? sizeof(struct de_desc_fastchannel) : 0; + sz +=3D tde->de.name_support ? SCMI_SHORT_NAME_MAX_SIZE : 0; + + return sz; +} + +static inline int +scmi_telemetry_area_check(struct device *dev, const char *tag, + u32 low, u32 high, u32 size, + phys_addr_t *phys_addr, size_t *len) +{ + u64 addr =3D ((u64)high << 32) | low; + phys_addr_t pa =3D (phys_addr_t)addr; + phys_addr_t end_phys; + + /* Does the returned pointer fits architetural size ? */ + if ((u64)pa !=3D addr) { + dev_err(dev, FW_BUG "%s phys_addr (%pa) pointer too big.\n", + tag, &pa); + return -EOVERFLOW; + } + + if (!IS_ALIGNED(pa, 4)) { + dev_err(dev, FW_BUG "%s phys_addr (%pa) misaligned.\n", + tag, &pa); + return -EINVAL; + } + + if (!IS_ALIGNED(size, 4)) { + dev_err(dev, FW_BUG "%s area length (%u) misaligned\n", + tag, size); + return -EINVAL; + } + + if (check_add_overflow(pa, size, &end_phys)) { + dev_err(dev, FW_BUG "%s area (%pa/%u) wrap-around.\n", + tag, &pa, size); + return -EOVERFLOW; + } + + *phys_addr =3D pa; + *len =3D size; + + return 0; +} + +static inline int +scmi_telemetry_fc_size_check(struct telemetry_de *tde, size_t size) +{ + size_t fc_size, fc_base_sz =3D tde->de.info->data_sz; + + if (fc_base_sz < 8 || !size) + return -EINVAL; + + if (check_add_overflow(fc_base_sz, + tde->de.tstamp_support ? sizeof(u32) * 2 : 0, + &fc_size)) + return -EOVERFLOW; + + if (size < fc_size) + return -ENOSPC; + + return 0; +} + +static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti, + struct telemetry_de *tde, + void **next, size_t *rx_len) +{ + const struct scmi_de_desc *desc =3D *next; + size_t payld_sz =3D sizeof(*desc); + + lockdep_assert_held(&tde->mtx); + + if (*rx_len < payld_sz) + return -ENOSPC; + + tde->de.info->id =3D le32_to_cpu(desc->de_id); + tde->de.info->grp_id =3D le32_to_cpu(desc->group_id); + tde->de.info->data_sz =3D le32_to_cpu(desc->de_data_sz); + tde->de.info->type =3D GET_DE_TYPE(desc); + tde->de.info->unit =3D GET_DE_UNIT(desc); + tde->de.info->unit_exp =3D GET_DE_UNIT_EXP(desc); + tde->de.info->instance_id =3D GET_DE_INSTA_ID(desc); + tde->de.info->compo_instance_id =3D GET_COMPO_INSTA_ID(desc); + tde->de.info->compo_type =3D GET_COMPO_TYPE(desc); + tde->de.info->persistent =3D IS_PERSISTENT(desc); + tde->ts_type =3D TSTAMP_SUPPORT(desc); + tde->de.tstamp_support =3D !!tde->ts_type; + tde->de.fc_support =3D IS_FC_SUPPORTED(desc); + tde->de.name_support =3D IS_NAME_SUPPORTED(desc); + /* Update DE_DESCRIPTOR size for the next iteration */ + *next +=3D sizeof(*desc); + + payld_sz +=3D scmi_telemetry_tde_var_size_calc(tde); + if (*rx_len < payld_sz) + return -ENOSPC; + + if (IS_TDE_TS_LINE(tde)) { + __le32 *line_ts_rate =3D *next; + + tde->de.info->ts_rate =3D le32_to_cpu((*line_ts_rate)); + + /* Variably sized depending on TS support */ + *next +=3D sizeof(*line_ts_rate); + } else if (IS_TDE_TS_BLK(tde)) { + /* Setup default BLK TS value at first */ + tde->de.info->ts_rate =3D ti->default_blk_ts_rate; + } else if (!IS_TDE_TS_NONE(tde)) { + dev_warn(ti->ph->dev, + "Unknown TDE timestamp type %u\n", tde->ts_type); + return -EPROTO; + } + + if (tde->de.fc_support) { + struct de_desc_fastchannel *dfc; + phys_addr_t phys_addr; + void __iomem *addr; + size_t size; + int ret; + + dfc =3D *next; + + ret =3D scmi_telemetry_area_check(ti->ph->dev, "FC", + le32_to_cpu(dfc->addr_low), + le32_to_cpu(dfc->addr_high), + le32_to_cpu(dfc->size), + &phys_addr, &size); + if (ret) + return ret; + + /* Does it fit ? */ + if (scmi_telemetry_fc_size_check(tde, size)) { + dev_err(ti->ph->dev, + FW_BUG "TDE FC size inconsistent (%zu)\n", size); + return -EPROTO; + } + + addr =3D ioremap(phys_addr, size); + if (!addr) + return -EADDRNOTAVAIL; + + tde->base =3D addr; + tde->offset =3D 0; + tde->fc_size =3D size; + + /* Variably sized depending on FC support */ + *next +=3D sizeof(*dfc); + } + + if (tde->de.name_support) { + const char *de_name =3D *next; + + strscpy(tde->de.info->name, de_name, SCMI_SHORT_NAME_MAX_SIZE); + /* Variably sized depending on name support */ + *next +=3D SCMI_SHORT_NAME_MAX_SIZE; + } + + *rx_len -=3D payld_sz; + + return 0; +} + +static int iter_de_descr_process_response(const struct scmi_protocol_handl= e *ph, + const void *response, + struct scmi_iterator_state *st, + void *priv) +{ + struct scmi_tlm_de_priv *p =3D priv; + struct telemetry_info *ti =3D p->ti; + const struct scmi_de_desc *desc =3D p->next; + struct telemetry_de *tde; + bool discovered =3D false; + unsigned int de_id; + int ret; + + /* Is at least reading de_id safe */ + if (p->rx_len < sizeof(desc->de_id)) + return -ENOSPC; + + de_id =3D le32_to_cpu(desc->de_id); + /* + * Check if this DE was already been discovered by other means, + * such as during boot if Telemetry was detected as already enabled + * and the list of enabled DEs was enumerated separately. + */ + tde =3D scmi_telemetry_tde_lookup(ti, de_id); + if (!tde) { + /* Create a new one */ + tde =3D scmi_telemetry_tde_get(ti, de_id); + if (IS_ERR(tde)) + return PTR_ERR(tde); + + discovered =3D true; + } + + guard(mutex)(&tde->mtx); + if (tde->enumerated) { + /* Cannot be a duplicate of a DE already created by enumeration */ + dev_err(ph->dev, + "Discovered INVALID DE with DUPLICATED ID:0x%08X\n", + de_id); + return -EINVAL; + } + + ret =3D scmi_telemetry_de_descriptor_parse(ti, tde, &p->next, &p->rx_len); + if (ret) { + dev_err(ph->dev, "Malformed DE Descriptor - ret:%d\n", ret); + /* DE not enumerated at this point were created in this call */ + if (discovered) + scmi_telemetry_free_tde_put(ti, tde); + return ret; + } + + /* Now fully enumerated */ + tde->enumerated =3D true; + + /* Register if it was not already ... */ + if (discovered) { + ret =3D scmi_telemetry_tde_register(ti, tde); + if (ret) { + /* DE not enumerated at this point were created in this call */ + scmi_telemetry_free_tde_put(ti, tde); + return ret; + } + } + + /* Count timestamped DEs */ + ti->num_des_tstamp +=3D !!tde->de.tstamp_support; + + /* Add to FastChannels list */ + if (tde->de.fc_support) + list_add(&tde->item, &ti->fcs_des); + + return 0; +} + +static void scmi_telemetry_tdes_cleanup(void *arg) +{ + struct telemetry_info *ti =3D arg; + struct scmi_telemetry_de *de; + unsigned long idx; + + xa_for_each(&ti->xa_des, idx, de) { + struct telemetry_de *tde =3D to_tde(de); + + guard(mutex)(&tde->mtx); + + scmi_telemetry_free_tde_put(ti, tde); + } +} + +static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti) +{ + const struct scmi_protocol_handle *ph =3D ti->ph; + + struct scmi_iterator_ops ops =3D { + .prepare_message =3D iter_tlm_prepare_message, + .update_state =3D iter_de_descr_update_state, + .process_response =3D iter_de_descr_process_response, + }; + struct scmi_tlm_de_priv tpriv =3D { + .ti =3D ti, + .next =3D NULL, + }; + void *iter; + int ret; + + if (!ti->info.base.num_des) + return 0; + + iter =3D ph->hops->iter_response_init(ph, &ops, ti->info.base.num_des, + TELEMETRY_DE_DESCRIPTION, + sizeof(u32), &tpriv); + if (IS_ERR(iter)) + return PTR_ERR(iter); + + ret =3D ph->hops->iter_response_run(iter); + if (ret) + return ret; + + return devm_add_action_or_reset(ph->dev, scmi_telemetry_tdes_cleanup, ti); +} + static const struct scmi_telemetry_info * scmi_telemetry_info_get(const struct scmi_protocol_handle *ph) { @@ -108,10 +626,231 @@ scmi_telemetry_info_get(const struct scmi_protocol_h= andle *ph) return &ti->info; } =20 +static const struct scmi_telemetry_res_info * +scmi_telemetry_resources_get(const struct scmi_protocol_handle *ph) +{ + struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti); + struct telemetry_info *ti =3D ph->get_priv(ph); + + /* Ensure the latest installed res_get helper is visible */ + res_get =3D smp_load_acquire(&ti->res_get); + + return res_get(ti); +} + +static const struct scmi_telemetry_de * +scmi_telemetry_de_lookup(const struct scmi_protocol_handle *ph, u32 id) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + + /* Trigger a full proper enumeration */ + scmi_telemetry_resources_get(ph); + + return xa_load(&ti->xa_des, id); +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, + .de_lookup =3D scmi_telemetry_de_lookup, + .res_get =3D scmi_telemetry_resources_get, }; =20 +/** + * scmi_telemetry_resources_alloc - Resources allocation + * @ti: A reference to the telemetry info descriptor for this instance + * + * This allocates and initializes dedicated resources for the maximum poss= ible + * number of needed telemetry resources, based on information gathered from + * the initial enumeration: these allocations represent an upper bound on + * the number of discoverable telemetry resources and they will be later + * populated during late deferred further discovery phases. + * + * Note that some of the allocations down below are designed to keep some + * class of data packed together since it eases retrieval from the ABI lay= er. + * + * Return: 0 on Success, errno otherwise + */ +static int scmi_telemetry_resources_alloc(struct telemetry_info *ti) +{ + /* Array to hold pointers to discovered DEs */ + struct scmi_telemetry_de **des __free(kfree) =3D + kzalloc_objs(*des, ti->info.base.num_des); + if (!des) + return -ENOMEM; + + /* The allocated DE descriptors */ + struct telemetry_de *tdes __free(kfree) =3D + kzalloc_objs(*tdes, ti->info.base.num_des); + if (!tdes) + return -ENOMEM; + + /* Allocate a set of contiguous DE info descriptors. */ + struct scmi_telemetry_de_info *dei_store __free(kfree) =3D + kzalloc_objs(*dei_store, ti->info.base.num_des); + if (!dei_store) + return -ENOMEM; + + struct scmi_telemetry_res_info *rinfo __free(kfree) =3D + kzalloc_obj(*rinfo); + if (!rinfo) + return -ENOMEM; + + mutex_init(&ti->free_mtx); + INIT_LIST_HEAD(&ti->free_des); + for (int i =3D 0; i < ti->info.base.num_des; i++) { + mutex_init(&tdes[i].mtx); + /* Bind contiguous DE info structures */ + tdes[i].de.info =3D &dei_store[i]; + scmi_telemetry_free_tdes_add(ti, &tdes[i]); + } + + INIT_LIST_HEAD(&ti->fcs_des); + + ti->tdes =3D no_free_ptr(tdes); + xa_init(&ti->xa_des); + + rinfo->des =3D no_free_ptr(des); + rinfo->dei_store =3D no_free_ptr(dei_store); + + /* Ensure all of the above assignments are visible */ + smp_store_release(&ti->rinfo, no_free_ptr(rinfo)); + + return 0; +} + +static struct scmi_telemetry_res_info * +__scmi_telemetry_resources_get(struct telemetry_info *ti) +{ + /* Ensure rinfo descriptor is visible */ + return smp_load_acquire(&ti->rinfo); +} + +static void scmi_telemetry_resources_free(struct telemetry_info *ti) +{ + struct scmi_telemetry_res_info *rinfo; + + /* Get rinfo without triggering a recursive enumeration, cannot be NULL */ + rinfo =3D __scmi_telemetry_resources_get(ti); + /* Ensure rinfo is no more accessible upfront */ + smp_store_release(&ti->rinfo, NULL); + + xa_destroy(&ti->xa_des); + + kfree(ti->tdes); + + kfree(rinfo->des); + kfree(rinfo->dei_store); + + kfree(rinfo); + + dev_dbg(ti->ph->dev, "SCMI Telemetry resources freed for instance\n"); +} + +static void scmi_telemetry_instance_cleanup(void *arg) +{ + struct telemetry_info *ti =3D arg; + + scmi_telemetry_resources_free(ti); +} + +/** + * scmi_telemetry_resources_enumerate - Enumeration helper + * @ti: A reference to the telemetry info descriptor for this instance + * + * This helper is configured to be called once on the first enumeration + * attempt, when triggered by invoking ti->res_get() from somewhere else. + * + * Once run it substitues itself in ti->res_get() with the simple accessor + * __scmi_telemetry_resources_get, which returns a descriptor to the resou= rces + * that were possibly discovered. + * + * Note that, while it attempts to fully enumerate Data Events and Groups,= it + * does NOT fail when such enumerations fail, instead it simply gives up w= ith + * the end result that only a partially populated, but consistent, resourc= es + * descriptor will be returned; in such a case the incomplete descriptor w= ill + * be marked as NOT fully_enumerated: this design enables the kernel to de= al + * with badly implemented out-of-spec firmware support while keep on provi= ding + * a minimal sane, albeit possibly incomplete, set of telemetry respources. + * + * Return: A reference to a fully or partially populated resources descrip= tor + */ +static struct scmi_telemetry_res_info * +scmi_telemetry_resources_enumerate(struct telemetry_info *ti) +{ + struct scmi_telemetry_res_info *rinfo; + struct device *dev =3D ti->ph->dev; + int ret; + + /* + * Ensure the following initialization can be called only once + * from one thread of execution. + */ + if (atomic_cmpxchg(&ti->rinfo_initializing, 0, 1)) { + /* + * When initialization is already ongoing in another thread, + * just wait for its completion and return the fully or partially + * populated rinfo. + */ + if (!completion_done(&ti->rinfo_initdone)) + wait_for_completion(&ti->rinfo_initdone); + + /* Ensure rinfo descriptor is visible */ + return smp_load_acquire(&ti->rinfo); + } + + /* Note that this code below can be run only once by one thread */ + ret =3D scmi_telemetry_de_descriptors_get(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot fully enumerate DEs resources. Degraded syst= em.\n"); + goto done; + } + + /* Get rinfo without triggering a recursive enumeration */ + rinfo =3D __scmi_telemetry_resources_get(ti); + rinfo->fully_enumerated =3D rinfo->num_des =3D=3D ti->info.base.num_des; +done: + /* Disable initialization permanently */ + smp_store_release(&ti->res_get, __scmi_telemetry_resources_get); + + /* Unblock concurrent threads that have been stalled */ + complete_all(&ti->rinfo_initdone); + + /* Ensure local rinfo is visible before returning it */ + smp_mb(); + return ti->rinfo; +} + +/** + * scmi_telemetry_instance_init - Instance initializer + * @ti: A reference to the telemetry info descriptor for this instance + * + * Note that this allocates and initialize all the resources possibly need= ed + * and then setups the @scmi_telemetry_resources_enumerate helper as the + * default method for the first call to ti->res_get(): this mechanism enab= les + * the possibility of optionally implementing deferred enumeration policies + * which optionally delay the discovery phase and related SCMI message exc= hanges + * to a later point in time. + * + * Return: 0 on Success, errno otherwise + */ +static int scmi_telemetry_instance_init(struct telemetry_info *ti) +{ + int ret; + + ret =3D scmi_telemetry_resources_alloc(ti); + if (ret) + return ret; + + /* Setup resources lazy initialization */ + atomic_set(&ti->rinfo_initializing, 0); + init_completion(&ti->rinfo_initdone); + /* Ensure the new res_get() operation is visible after this point */ + smp_store_mb(ti->res_get, scmi_telemetry_resources_enumerate); + + return devm_add_action_or_reset(ti->ph->dev, + scmi_telemetry_instance_cleanup, ti); +} + static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle = *ph) { struct device *dev =3D ph->dev; @@ -133,6 +872,12 @@ static int scmi_telemetry_protocol_init(const struct s= cmi_protocol_handle *ph) return ret; } =20 + ret =3D scmi_telemetry_instance_init(ti); + if (ret) { + dev_err(dev, "Cannot initialize instance. Abort.\n"); + return ret; + } + ti->info.base.version =3D ph->version; =20 return ph->set_priv(ph, ti); diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index f076ce5601cf..cc5bd87705ed 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -830,6 +830,37 @@ struct scmi_pinctrl_proto_ops { int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin); }; =20 +struct scmi_telemetry_de_info { + unsigned int id; + unsigned int grp_id; + unsigned int data_sz; + unsigned int type; + unsigned int unit; + int unit_exp; + unsigned int ts_rate; + unsigned int instance_id; + unsigned int compo_instance_id; + unsigned int compo_type; + bool persistent; + char name[16]; +}; + +struct scmi_telemetry_de { + bool tstamp_support; + bool fc_support; + bool name_support; + bool enabled; + bool tstamp_enabled; + struct scmi_telemetry_de_info *info; +}; + +struct scmi_telemetry_res_info { + bool fully_enumerated; + unsigned int num_des; + struct scmi_telemetry_de **des; + struct scmi_telemetry_de_info *dei_store; +}; + struct scmi_telemetry_base_info { unsigned int version; uuid_t primary_revision; @@ -853,10 +884,16 @@ struct scmi_telemetry_info { * by SCMI Telemetry Protocol * * @info_get: get the general Telemetry information. + * @de_lookup: get a specific DE descriptor from the DE id. + * @res_get: get a reference to the Telemetry resources descriptor. */ struct scmi_telemetry_proto_ops { const struct scmi_telemetry_info *(*info_get) (const struct scmi_protocol_handle *ph); + const struct scmi_telemetry_de __must_check *(*de_lookup) + (const struct scmi_protocol_handle *ph, u32 id); + const struct scmi_telemetry_res_info *(*res_get) + (const struct scmi_protocol_handle *ph); }; =20 /** --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304645; b=JBZqVE5CWEWAeiDQEha36SOHL9KQVXNzlXvpDXHWSueyV+pSAuwT4UdT7zYKjJU2xwXv vdxY4632CuzczOrC55KIzYGHD4IOEShw091fesDUa+73UqNIWe7hEXLjgOUzbPpDf1btl kL5GmEZ7lAKSijzFonC8gWfaxHHyl9CNwWTncst9MWZayp03oBOhdmg6ePVVnxHg8s29S CvLWG4Ibcsu5VSWqnGnmpTj1nIrJpLjUo2PjBB+IISn5DqHg4onEnb7T/+Gz2UGkays16 xqZWgaIDxLC88++hzjHRFQEFBMmeSJybXP2C837L1Kb1dNWScZbzz2DrHK3UTyOzlbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304645; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=hb9T5ArgrAzr/JClJPi4G2cB6w0raB9303L+BYr/rpA=; b=YnogXrs2SWz7GgKog4Eb59k9yy9xKBu7tDGYr6UrwvLiw1sYobmDPvEAQrrV6+GnbM27 eWofhdFkvGmWhQAnmDSMJtA7CGc/wO0UdiMaQdXQSRaPsZuRa1F3cpjAFLtYMxL/rmX4I 7xYvNZU/sw1WtK96shrvQGNj7Sb2eB68iB2fTrrkILZBwjVsfFLSA62x44ACLh2oxh2OT WEgeOTWrA1mSQe75NUxvWf9RRxnBcHmuqYBO4ayiASSD34c8sZaemjI5FO9wafr6l60wb gfI0bSoxGViaKVPFVIGHrQ0WypKAsHyxYjT3RasKywoqq53VSi7KDJIbIFUfPctVH+A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=TSCZ+NiD; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 38B4C606A331; Sat, 21 Nov 2026 23:44:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 38B4C606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=TSCZ+NiD 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 B053D16F2; Sat, 21 Nov 2026 15:44:00 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C80AA3F632; Sat, 21 Nov 2026 15:44:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304643; bh=3VSAcbVmbrlnSQIUOy2KA4THCCM+THqJTZo0o6GstzU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TSCZ+NiD3P5Mtfp7kfMAClGJ97vHgAbP9xskirDmgRqm1iSzzJgH1TaEftsGA4+bV debg2gnwjJMm+s2rrzPPiyE3ujwCXraxaXKfIU6FBO8IEggqVx7EMQmE0JxH6TvEAY q6lhNHZECWzhvCN5srDiNe6mLFZthVd7bilsQlLA= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 12/31] firmware: arm_scmi: Add Telemetry global update intervals enumeration Date: Sat, 21 Nov 2026 23:39:46 +0000 Message-ID: <20261121234005.1525419-13-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support to enumerate global available Telemetry update intervals. Signed-off-by: Cristian Marussi --- v15 --> v16 - split out group update interval enumeration v14 --> v15 - removed unused dev reference in interval parsing - added missing initialization of intrvs->grp_id v13 --> v14 - splitted out of the old Basic Telemetry patch, contains the update_intervals enumeration routines --- drivers/firmware/arm_scmi/telemetry.c | 164 ++++++++++++++++++++++++++ include/linux/scmi_protocol.h | 21 ++++ 2 files changed, 185 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 64c92d9ab900..115aebcc918f 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -92,6 +92,21 @@ struct scmi_msg_resp_telemetry_de_description { struct scmi_de_desc desc[]; }; =20 +struct scmi_msg_telemetry_update_intervals { + __le32 index; + __le32 group_identifier; +#define ALL_DES_NO_GROUP 0x0 +#define SPECIFIC_GROUP_DES 0x1 +#define ALL_DES_ANY_GROUP 0x2 + __le32 flags; +}; + +struct scmi_msg_resp_telemetry_update_intervals { + __le32 flags; +#define INTERVALS_DISCRETE(x) (le32_get_bits((x), BIT(12)) =3D=3D 0) + __le32 intervals[]; +}; + #define IS_TDE_TSTAMP_TYPE(__t, __ttype) ((__t)->ts_type =3D=3D (__ttype)) #define IS_TDE_TS_NONE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE)) #define IS_TDE_TS_LINE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE)) @@ -618,6 +633,149 @@ static int scmi_telemetry_de_descriptors_get(struct t= elemetry_info *ti) return devm_add_action_or_reset(ph->dev, scmi_telemetry_tdes_cleanup, ti); } =20 +struct scmi_tlm_ivl_priv { + struct scmi_telemetry_intervals **intrvs; + unsigned int grp_id; + unsigned int flags; +}; + +static void iter_intervals_prepare_message(void *message, + unsigned int desc_index, + const void *priv) +{ + struct scmi_msg_telemetry_update_intervals *msg =3D message; + const struct scmi_tlm_ivl_priv *p =3D priv; + + msg->index =3D cpu_to_le32(desc_index); + msg->group_identifier =3D cpu_to_le32(p->grp_id); + msg->flags =3D cpu_to_le32(FIELD_PREP(GENMASK(3, 0), p->flags)); +} + +static int iter_intervals_update_state(struct scmi_iterator_state *st, + const void *response, void *priv) +{ + const struct scmi_msg_resp_telemetry_update_intervals *r =3D response; + + st->num_returned =3D le32_get_bits(r->flags, GENMASK(11, 0)); + st->num_remaining =3D le32_get_bits(r->flags, GENMASK(31, 16)); + + if (st->rx_len < (sizeof(*r) + sizeof(r->intervals[0]) * st->num_returned= )) + return -EINVAL; + + /* + * total intervals is not declared previously anywhere so we + * assume it's returned+remaining on first call. + */ + if (!st->max_resources) { + struct scmi_tlm_ivl_priv *p =3D priv; + struct scmi_telemetry_intervals *intrvs; + bool discrete; + int inum; + + discrete =3D INTERVALS_DISCRETE(r->flags); + /* Check consistency on first call */ + if (!discrete && (st->num_returned !=3D 3 || st->num_remaining !=3D 0)) + return -EINVAL; + + inum =3D st->num_returned + st->num_remaining; + intrvs =3D kzalloc_flex(*intrvs, update_intervals, inum); + if (!intrvs) + return -ENOMEM; + + intrvs->num_intervals =3D inum; + intrvs->discrete =3D discrete; + intrvs->grp_id =3D p->grp_id; + st->max_resources =3D intrvs->num_intervals; + + *p->intrvs =3D intrvs; + } + + return 0; +} + +static int +iter_intervals_process_response(const struct scmi_protocol_handle *ph, + const void *response, + struct scmi_iterator_state *st, void *priv) +{ + const struct scmi_msg_resp_telemetry_update_intervals *r =3D response; + struct scmi_tlm_ivl_priv *p =3D priv; + struct scmi_telemetry_intervals *intrvs =3D *p->intrvs; + unsigned int idx =3D st->loop_idx; + + intrvs->update_intervals[st->desc_index + idx] =3D + le32_to_cpu(r->intervals[idx]); + + return 0; +} + +static int +scmi_tlm_enumerate_update_intervals(struct telemetry_info *ti, + struct scmi_telemetry_intervals **intervals, + int grp_id, unsigned int flags) +{ + struct scmi_iterator_ops ops =3D { + .prepare_message =3D iter_intervals_prepare_message, + .update_state =3D iter_intervals_update_state, + .process_response =3D iter_intervals_process_response, + }; + const struct scmi_protocol_handle *ph =3D ti->ph; + struct scmi_telemetry_intervals *intrvs =3D NULL; + struct scmi_tlm_ivl_priv ipriv =3D { + .grp_id =3D grp_id, + .intrvs =3D &intrvs, + .flags =3D flags, + }; + void *iter; + int ret; + + iter =3D ph->hops->iter_response_init(ph, &ops, 0, + TELEMETRY_LIST_UPDATE_INTERVALS, + sizeof(struct scmi_msg_telemetry_update_intervals), + &ipriv); + if (IS_ERR(iter)) + return PTR_ERR(iter); + + /* Cleanup iterator inner allocations on failure */ + ret =3D ph->hops->iter_response_run(iter); + if (ret) { + kfree(intrvs); + return ret; + } + + *intervals =3D intrvs; + + return 0; +} + +static void scmi_telemetry_intervals_free(void *interval) +{ + kfree(interval); +} + +static int +scmi_telemetry_enumerate_common_intervals(struct telemetry_info *ti) +{ + unsigned int flags; + int ret; + + flags =3D !ti->info.per_group_config_support ? + ALL_DES_ANY_GROUP : ALL_DES_NO_GROUP; + + ret =3D scmi_tlm_enumerate_update_intervals(ti, &ti->info.intervals, + SCMI_TLM_GRP_INVALID, flags); + if (ret) + return ret; + + /* A copy for UAPI access... */ + ti->info.base.num_intervals =3D ti->info.intervals->num_intervals; + + /* Delegate freeing of allocated intervals to unbind time */ + return devm_add_action_or_reset(ti->ph->dev, + scmi_telemetry_intervals_free, + ti->info.intervals); +} + static const struct scmi_telemetry_info * scmi_telemetry_info_get(const struct scmi_protocol_handle *ph) { @@ -878,6 +1036,12 @@ static int scmi_telemetry_protocol_init(const struct = scmi_protocol_handle *ph) return ret; } =20 + ret =3D scmi_telemetry_enumerate_common_intervals(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot enumerate update intervals. Abort.\n"); + return ret; + } + ti->info.base.version =3D ph->version; =20 return ph->set_priv(ph, ti); diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index cc5bd87705ed..74ed6411ff76 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -830,6 +830,25 @@ struct scmi_pinctrl_proto_ops { int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin); }; =20 +#define SCMI_TLM_GET_UPDATE_INTERVAL_SECS(x) (FIELD_GET(GENMASK(20, 5), (x= ))) +#define SCMI_TLM_GET_UPDATE_INTERVAL_EXP(x) (sign_extend32((x), 4)) + +#define SCMI_TLM_GET_UPDATE_INTERVAL(x) (FIELD_GET(GENMASK(20, 0), (x))) +#define SCMI_TLM_BUILD_UPDATE_INTERVAL(s, e) \ + (FIELD_PREP(GENMASK(20, 5), (s)) | FIELD_PREP(GENMASK(4, 0), (e))) + +#define SCMI_TLM_GRP_INVALID 0xFFFFFFFF + +struct scmi_telemetry_intervals { + unsigned int grp_id; + bool discrete; + unsigned int num_intervals; +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW 0 +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH 1 +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP 2 + unsigned int update_intervals[] __counted_by(num_intervals); +}; + struct scmi_telemetry_de_info { unsigned int id; unsigned int grp_id; @@ -877,6 +896,8 @@ struct scmi_telemetry_info { bool reset_support; bool fc_support; struct scmi_telemetry_base_info base; + unsigned int active_update_interval; + struct scmi_telemetry_intervals *intervals; }; =20 /** --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304648; b=pORKaiJlmpScwx7+ZVczSh1wrnONVWO1vNJQ+p46uFm8L3axSo9UdRYDAiBuRkIws9wK VBEmHebYOfcQXGqX2kT4VCOo9BDLhkKWO4I9PknAL7TswcIRPdbU2fb4DjS5YvQma/am2 vt5bTdOu6T1h+ZjDk5b1rtR4H8xv8DxzoPWLBRxNOob0CsI+/PKx1O9S5HKUpe9KyLLYG NfDowuBBveyequMlaJGIwS0U1feRLWyBprmsIblvlKECfZQoraekVugaWpYDwgd1kh/2k FnT8YNEUvqGyNycmphjhdpRfJ2XwGF84e9JR6DTuquHlffPDZZvDk0fRz1Al0M1E6sA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304648; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=LiSpTXxPK8nxxj28ukr0BpK+ZZTAtADGXsHJRdahjIM=; b=Dhaudv4wGjyKeSMBCh4vL1ignkfguZ6CfefAe7uEBwoNw6HpKKmdGpzHXggLG8gOvIfH nfo+g+6cDdLSvIQyOO6b6LaS9rQX7ZkGf5eCIYV0ZohCCEmGUiMNymW6qnTfpQpJ7MqMd qNS9jh1u9JLf561/ktR1YOVlvtOUtf0yJsObIykk+rqcvSy10w890TNaCrV9lPFf0rK5s 2sh+zsrIiSsZtZNS4kQBGQ2Rh/mIFKn6PDafeXJtPCdvGtvtOm4E/bAmHOE1TFOjdyXUu nw6gtFf1phfN1G/dr6pNAL3Vp+faeEoVsWoHKcjFevrhWm1pHhyQMczGjAlKNCd/4Cw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=fwqLNj4c; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6581D606A330; Sat, 21 Nov 2026 23:44:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6581D606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=fwqLNj4c 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 D984D1758; Sat, 21 Nov 2026 15:44:03 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E3A0F3F632; Sat, 21 Nov 2026 15:44:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304646; bh=7kwwxEho6yxsgEKSzAsVWSKh4kMBRFykje/UQlHb2e0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fwqLNj4c4bx/iHRyQFwevwPzJdX2FVcRZut4iRryPxENTS1eGWURteiLw3rGcl1id KrhNAtiev1YCoTcBh52otfVjswVdncdALZdNXaCvkCSxIw01PYsQ9p3LMWx86l3w1F 52VtTbkVPDswQCR/XWVdxpBG9PAwdnCqjclky+nM= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 13/31] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration Date: Sat, 21 Nov 2026 23:39:47 +0000 Message-ID: <20261121234005.1525419-14-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add Telemetry support to enumerate SHMTI areas. Signed-off-by: Cristian Marussi --- v15 --> v16 - fixed missing \n in dev_err v14 --> v15 - fix prologue/epilogue endianity fields - rework SHMTI initialization and allocation - add snapshot suppport data structures - added alignment checks on SHMTII length size - fixed error string formatting - warn if less than expected SHMTIs are enumerated - improve memory area sanity checks v13 --> v14 - splitted out of the old Basic Telemetry patch, contains the SHMTI areas enumeration routines --- drivers/firmware/arm_scmi/telemetry.c | 312 ++++++++++++++++++++++++++ include/linux/scmi_protocol.h | 59 +++++ 2 files changed, 371 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 115aebcc918f..75844cac5900 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -56,6 +57,19 @@ struct scmi_msg_resp_telemetry_protocol_attributes { __le32 default_blk_ts_rate; }; =20 +struct scmi_shmti_desc { + __le32 id; + __le32 addr_low; + __le32 addr_high; + __le32 length; + __le32 flags; +}; + +struct scmi_msg_resp_telemetry_shmti_list { + __le32 num_shmti; + struct scmi_shmti_desc desc[]; +}; + struct de_desc_fastchannel { __le32 addr_low; __le32 addr_high; @@ -107,6 +121,72 @@ struct scmi_msg_resp_telemetry_update_intervals { __le32 intervals[]; }; =20 +/* TDCF */ + +#define _I(__a) (ioread32((void __iomem *)(__a))) + +struct prlg { + __le32 sign_start; +#define SIGNATURE_START 0x5442474E /* TBGN */ + __le32 match_start; + __le32 num_qwords; + __le32 hdr_meta_1; +#define TDCF_REVISION_GET(x) (le32_get_bits((x)->hdr_meta_1, GENMASK(7, 0)= )) +}; + +struct eplg { + __le32 match_end; + __le32 sign_end; +#define SIGNATURE_END 0x54454E44 /* TEND */ +}; + +#define TDCF_EPLG_SZ (sizeof(struct eplg)) + +struct tdcf { + struct prlg prlg; + unsigned char payld[]; +}; + +#define SHMTI_MIN_SIZE (sizeof(struct tdcf) + TDCF_EPLG_SZ) +/* An arbitrary upper bound to avoid crazy allocations driven by buggy FW = */ +#define SHMTI_MAX_SIZE SZ_64K + +#define TDCF_START_SIGNATURE(x) (_I(&((x)->prlg.sign_start))) +#define TDCF_END_SIGNATURE(e) (_I(&((e)->sign_end))) + +struct telemetry_shmti; + +struct shmti_snap { + struct telemetry_shmti *shmti; + u32 magic; + u8 base[] __aligned(8); +}; + +struct telemetry_shmti { + u32 flags; + void __iomem *base; + struct scmi_telemetry_shmti_info info; + /* Protect srcu operation */ + struct mutex lock; + struct srcu_struct srcu; + struct shmti_snap *snaps[2]; + struct shmti_snap __rcu *cur; + /* Mutex to protect @xa_lines */ + struct mutex lines_mtx; + struct xarray xa_lines; + #define PAYLD_HASH_BITS 4 + struct mutex payld_locks[1 << PAYLD_HASH_BITS]; +}; + +#define SHMTI_EPLG(s) \ + ({ \ + struct telemetry_shmti *_s =3D (s); \ + struct eplg __iomem *_eplg; \ + \ + _eplg =3D _s->base + _s->info.len - TDCF_EPLG_SZ; \ + (_eplg); \ + }) + #define IS_TDE_TSTAMP_TYPE(__t, __ttype) ((__t)->ts_type =3D=3D (__ttype)) #define IS_TDE_TS_NONE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE)) #define IS_TDE_TS_LINE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE)) @@ -125,6 +205,7 @@ struct telemetry_de { u32 ts_rate; bool enumerated; void __iomem *base; + struct eplg __iomem *eplg; u32 sid; u32 offset; /* NOTE THAT DE data_sz is registered in scmi_telemetry_de */ @@ -143,6 +224,7 @@ struct telemetry_info { unsigned int num_des_tstamp; unsigned int default_blk_ts_rate; const struct scmi_protocol_handle *ph; + struct telemetry_shmti *shmti; struct telemetry_de *tdes; struct xarray xa_des; /* Mutex to protect access to @free_des */ @@ -776,6 +858,211 @@ scmi_telemetry_enumerate_common_intervals(struct tele= metry_info *ti) ti->info.intervals); } =20 +static int iter_shmti_update_state(struct scmi_iterator_state *st, + const void *response, void *priv) +{ + const struct scmi_msg_resp_telemetry_shmti_list *r =3D response; + + st->num_returned =3D le32_get_bits(r->num_shmti, GENMASK(15, 0)); + st->num_remaining =3D le32_get_bits(r->num_shmti, GENMASK(31, 16)); + + if (st->rx_len < (sizeof(*r) + sizeof(r->desc[0]) * st->num_returned)) + return -EINVAL; + + return 0; +} + +static inline int +scmi_telemetry_shmti_validate(struct device *dev, struct telemetry_shmti *= shmti) +{ + struct tdcf __iomem *tdcf =3D shmti->base; + u32 sign_start, sign_end; + + sign_start =3D TDCF_START_SIGNATURE(tdcf); + sign_end =3D TDCF_END_SIGNATURE(SHMTI_EPLG(shmti)); + + if (sign_start !=3D SIGNATURE_START || sign_end !=3D SIGNATURE_END) { + dev_err(dev, + "BAD signature for SHMTI ID:%u @phys:%pa - START:0x%08X END:0x%08X\n", + shmti->info.sid, &shmti->info.phys, sign_start, sign_end); + return -EINVAL; + } + + return 0; +} + +static int iter_shmti_process_response(const struct scmi_protocol_handle *= ph, + const void *response, + struct scmi_iterator_state *st, + void *priv) +{ + const struct scmi_msg_resp_telemetry_shmti_list *r =3D response; + struct telemetry_info *ti =3D priv; + struct telemetry_shmti *shmti; + const struct scmi_shmti_desc *desc; + void __iomem *addr; + phys_addr_t phys_addr; + size_t len; + int ret; + + desc =3D &r->desc[st->loop_idx]; + shmti =3D &ti->shmti[st->desc_index + st->loop_idx]; + + shmti->info.sid =3D le32_to_cpu(desc->id); + shmti->flags =3D le32_to_cpu(desc->flags); + + ret =3D scmi_telemetry_area_check(ph->dev, "SHMTI", + le32_to_cpu(desc->addr_low), + le32_to_cpu(desc->addr_high), + le32_to_cpu(desc->length), + &phys_addr, &len); + if (ret) { + dev_err(ph->dev, "Malformed SHMTI ID:%u. Abort.\n", + shmti->info.sid); + return ret; + } + + if (len < SHMTI_MIN_SIZE || len > SHMTI_MAX_SIZE) { + dev_err(ph->dev, + FW_BUG "Invalid length for SHMTI ID:%u len:%zu. Abort.\n", + shmti->info.sid, len); + return -EINVAL; + } + + /* SHMTI base phys_addr SHOULD be 64bit aligned by the SCMI spec */ + if (!IS_ALIGNED(phys_addr, 8)) + dev_warn(ph->dev, + "SHMTI ID:%u is NOT 64bit aligned (paddr:0x%llX)\n", + shmti->info.sid, phys_addr); + + addr =3D devm_ioremap(ph->dev, phys_addr, len); + if (!addr) + return -EADDRNOTAVAIL; + + shmti->base =3D addr; + shmti->info.len =3D len; + shmti->info.phys =3D phys_addr; + shmti->info.offset =3D shmti->info.phys & ~PAGE_MASK; + + ret =3D scmi_telemetry_shmti_validate(ph->dev, shmti); + if (ret) + return ret; + + ti->num_shmti++; + + return 0; +} + +static int scmi_telemetry_shmti_list(const struct scmi_protocol_handle *ph, + struct telemetry_info *ti) +{ + struct scmi_iterator_ops ops =3D { + .prepare_message =3D iter_tlm_prepare_message, + .update_state =3D iter_shmti_update_state, + .process_response =3D iter_shmti_process_response, + }; + int num_shmti; + void *iter; + int ret; + + /* Save and zero...calculate real number of returned */ + num_shmti =3D ti->num_shmti; + ti->num_shmti =3D 0; + iter =3D ph->hops->iter_response_init(ph, &ops, num_shmti, + TELEMETRY_LIST_SHMTI, + sizeof(u32), ti); + if (IS_ERR(iter)) + return PTR_ERR(iter); + + ret =3D ph->hops->iter_response_run(iter); + if (ret) + return ret; + + if (num_shmti !=3D ti->num_shmti) + dev_warn(ph->dev, + FW_BUG "SHMTI enumeration returned %u items, expected %u\n", + ti->num_shmti, num_shmti); + + return 0; +} + +static void scmi_telemetry_shmti_cleanup(void *arg) +{ + struct telemetry_shmti *shmti =3D arg; + + cleanup_srcu_struct(&shmti->srcu); + + kvfree(shmti->snaps[0]); + kvfree(shmti->snaps[1]); + + xa_destroy(&shmti->xa_lines); +} + +static int scmi_telemetry_shmti_init(struct device *dev, + struct telemetry_shmti *shmti) +{ + int ret; + + struct shmti_snap *s0 __free(kvfree) =3D + kvzalloc_flex(*s0, base, shmti->info.len); + if (!s0) + return -ENOMEM; + + struct shmti_snap *s1 __free(kvfree) =3D + kvzalloc_flex(*s1, base, shmti->info.len); + if (!s1) + return -ENOMEM; + + ret =3D init_srcu_struct(&shmti->srcu); + if (ret) + return ret; + + for (int i =3D 0; i < 1 << PAYLD_HASH_BITS; i++) + mutex_init(&shmti->payld_locks[i]); + + mutex_init(&shmti->lock); + RCU_INIT_POINTER(shmti->cur, NULL); + + mutex_init(&shmti->lines_mtx); + xa_init(&shmti->xa_lines); + + s0->shmti =3D shmti; + shmti->snaps[0] =3D no_free_ptr(s0); + + s1->shmti =3D shmti; + shmti->snaps[1] =3D no_free_ptr(s1); + + return devm_add_action_or_reset(dev, scmi_telemetry_shmti_cleanup, shmti); +} + +static int scmi_telemetry_enumerate_shmti(struct telemetry_info *ti) +{ + const struct scmi_protocol_handle *ph =3D ti->ph; + int ret; + + if (!ti->num_shmti) + return 0; + + ret =3D scmi_telemetry_shmti_list(ph, ti); + if (ret) { + dev_err(ph->dev, "Cannot get SHMTI list descriptors\n"); + return ret; + } + + for (int i =3D 0; i < ti->num_shmti; i++) { + ret =3D scmi_telemetry_shmti_init(ph->dev, &ti->shmti[i]); + if (ret) + return ret; + + ti->info.shmtis[i] =3D &ti->shmti[i].info; + } + + /* Expose the fully initialized SHMTI infos */ + smp_store_release(&ti->info.base.num_shmtis, ti->num_shmti); + + return 0; +} + static const struct scmi_telemetry_info * scmi_telemetry_info_get(const struct scmi_protocol_handle *ph) { @@ -829,7 +1116,20 @@ static const struct scmi_telemetry_proto_ops tlm_prot= o_ops =3D { * Return: 0 on Success, errno otherwise */ static int scmi_telemetry_resources_alloc(struct telemetry_info *ti) + { + /* Array to hold SHMTIs descriptors */ + struct telemetry_shmti *shmtis __free(kfree) =3D + kzalloc_objs(*shmtis, ti->num_shmti); + if (!shmtis) + return -ENOMEM; + + /* Array of initialized SHMTIs references */ + struct scmi_telemetry_shmti_info **shmtis_info __free(kfree) =3D + kzalloc_objs(*shmtis_info, ti->num_shmti); + if (!shmtis_info) + return -ENOMEM; + /* Array to hold pointers to discovered DEs */ struct scmi_telemetry_de **des __free(kfree) =3D kzalloc_objs(*des, ti->info.base.num_des); @@ -864,6 +1164,9 @@ static int scmi_telemetry_resources_alloc(struct telem= etry_info *ti) =20 INIT_LIST_HEAD(&ti->fcs_des); =20 + ti->shmti =3D no_free_ptr(shmtis); + ti->info.shmtis =3D no_free_ptr(shmtis_info); + ti->tdes =3D no_free_ptr(tdes); xa_init(&ti->xa_des); =20 @@ -894,6 +1197,9 @@ static void scmi_telemetry_resources_free(struct telem= etry_info *ti) =20 xa_destroy(&ti->xa_des); =20 + kfree(ti->info.shmtis); + kfree(ti->shmti); + kfree(ti->tdes); =20 kfree(rinfo->des); @@ -1042,6 +1348,12 @@ static int scmi_telemetry_protocol_init(const struct= scmi_protocol_handle *ph) return ret; } =20 + ret =3D scmi_telemetry_enumerate_shmti(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot enumerate SHMTIs. Abort.\n"); + return ret; + } + ti->info.base.version =3D ph->version; =20 return ph->set_priv(ph, ti); diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 74ed6411ff76..cf097c135d39 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -830,6 +830,57 @@ struct scmi_pinctrl_proto_ops { int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin); }; =20 +enum scmi_telemetry_de_type { + SCMI_TLM_DE_TYPE_USPECIFIED, + SCMI_TLM_DE_TYPE_ACCUMUL_IDLE_RESIDENCY, + SCMI_TLM_DE_TYPE_ACCUMUL_IDLE_COUNTS, + SCMI_TLM_DE_TYPE_ACCUMUL_OTHERS, + SCMI_TLM_DE_TYPE_INSTA_IDLE_STATE, + SCMI_TLM_DE_TYPE_INSTA_OTHERS, + SCMI_TLM_DE_TYPE_AVERAGE, + SCMI_TLM_DE_TYPE_STATUS, + SCMI_TLM_DE_TYPE_RESERVED_START, + SCMI_TLM_DE_TYPE_RESERVED_END =3D 0xef, + SCMI_TLM_DE_TYPE_OEM_START =3D 0xf0, + SCMI_TLM_DE_TYPE_OEM_END =3D 0xff, +}; + +enum scmi_telemetry_compo_type { + SCMI_TLM_COMPO_TYPE_USPECIFIED, + SCMI_TLM_COMPO_TYPE_CPU, + SCMI_TLM_COMPO_TYPE_CLUSTER, + SCMI_TLM_COMPO_TYPE_GPU, + SCMI_TLM_COMPO_TYPE_NPU, + SCMI_TLM_COMPO_TYPE_INTERCONNECT, + SCMI_TLM_COMPO_TYPE_MEM_CNTRL, + SCMI_TLM_COMPO_TYPE_L1_CACHE, + SCMI_TLM_COMPO_TYPE_L2_CACHE, + SCMI_TLM_COMPO_TYPE_L3_CACHE, + SCMI_TLM_COMPO_TYPE_LL_CACHE, + SCMI_TLM_COMPO_TYPE_SYS_CACHE, + SCMI_TLM_COMPO_TYPE_DISP_CNTRL, + SCMI_TLM_COMPO_TYPE_IPU, + SCMI_TLM_COMPO_TYPE_CHIPLET, + SCMI_TLM_COMPO_TYPE_PACKAGE, + SCMI_TLM_COMPO_TYPE_SOC, + SCMI_TLM_COMPO_TYPE_SYSTEM, + SCMI_TLM_COMPO_TYPE_SMCU, + SCMI_TLM_COMPO_TYPE_ACCEL, + SCMI_TLM_COMPO_TYPE_BATTERY, + SCMI_TLM_COMPO_TYPE_CHARGER, + SCMI_TLM_COMPO_TYPE_PMIC, + SCMI_TLM_COMPO_TYPE_BOARD, + SCMI_TLM_COMPO_TYPE_MEMORY, + SCMI_TLM_COMPO_TYPE_PERIPH, + SCMI_TLM_COMPO_TYPE_PERIPH_SUBC, + SCMI_TLM_COMPO_TYPE_LID, + SCMI_TLM_COMPO_TYPE_DISPLAY, + SCMI_TLM_COMPO_TYPE_RESERVED_START =3D 0x1d, + SCMI_TLM_COMPO_TYPE_RESERVED_END =3D 0xdf, + SCMI_TLM_COMPO_TYPE_OEM_START =3D 0xe0, + SCMI_TLM_COMPO_TYPE_OEM_END =3D 0xff, +}; + #define SCMI_TLM_GET_UPDATE_INTERVAL_SECS(x) (FIELD_GET(GENMASK(20, 5), (x= ))) #define SCMI_TLM_GET_UPDATE_INTERVAL_EXP(x) (sign_extend32((x), 4)) =20 @@ -889,6 +940,13 @@ struct scmi_telemetry_base_info { unsigned int num_shmtis; }; =20 +struct scmi_telemetry_shmti_info { + unsigned int sid; + unsigned int len; + unsigned long offset; + phys_addr_t phys; +}; + struct scmi_telemetry_info { bool single_read_support; bool continuous_update_support; @@ -898,6 +956,7 @@ struct scmi_telemetry_info { struct scmi_telemetry_base_info base; unsigned int active_update_interval; struct scmi_telemetry_intervals *intervals; + struct scmi_telemetry_shmti_info **shmtis; }; =20 /** --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304651; b=4sF4fYB2OUODKlR8tP7uLpCzuh/duEwPEj1ZELteNp0hFOkkcMakV7p//s2PIDOIZt11 H3ft2vSguI8VN6Ive6ddoe/7S6E+Dhhf44XbcOMz1J79T3vl2aXz3zXBtWOC7MbhdG7Er z6EY2C8D0ybhMA2pUmuWGvqyf/UYvJsrrg8XjtA7XakIYIAp+htOPMlPuaRVLNVR2aOH8 Y4Y7TDCIFRZuXD8IPxKNFnHvR160EyZ3g402+xSDK9wPbtWfmmDZnc6PHswMU98Od6XSe J57KQU5TIoK+rydr+oqIzGnx2IwOrySow4btVcQFm07B+dnWtXodo57iknBOS+mAjHw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304651; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=IZlsZ/mc9BZ7fgnjlNYRBn+MRT9cHrNgcNAMaN9wn3c=; b=VytiD0rQtjLDvV75xwTP7UBxZv6g/Y5B1QCsaSp4+oaV/7YHjYPK7r4HF52WrSiK/Z6z FlS9/DmY6fddabWKg6HxUa57Ne9fq3zh2zhgYVmEbqsqTrkIDAHHzFqnYMQyjUkoQMPpI FTOc4nDYCvw7DtHlHrjBQkJwL1nuvh4JeCwDpbGIAwfoQ0dF2/hnH/Xz4xfsipk5rSFQm IQvZqJtzq70HZSWnIYmWd+FBmXPnl3JTGKh4h30JFC4OCTjMgJzTdV2NaGquD7kJtpB1n M+72hqbXP3DhN7/fqd337RfDm0FXb9wAqELx/duOKoDD7fa+CFrlAOAykeowubm9LZg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=bVyCy1Fi; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 71107606A336; Sat, 21 Nov 2026 23:44:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 71107606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=bVyCy1Fi 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 1CFD5175D; Sat, 21 Nov 2026 15:44:07 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1AAEC3F632; Sat, 21 Nov 2026 15:44:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304650; bh=l1TSzz6cKazjnJ/JfJ+SuC/7sVQQViL5XZHcdFJyJWA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bVyCy1FisVBVzY4RcVgvBtgQLbtzxm6zytmv6orh/05BgdtKD4nbD6DX/30bM02E3 mHfb4uUwrebVeF9ClbwoAZD1P6R+XHWDGXajMZ/Eg3zGIjlr9zEBVpPS3jDgqVQq0D JYmmYDfPsyOaj82epUVk0DMI+H2ynFdY7/YdFMp4= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 14/31] firmware: arm_scmi: Add Telemetry TDCF areas parsing support Date: Sat, 21 Nov 2026 23:39:48 +0000 Message-ID: <20261121234005.1525419-15-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add logic to scan the SHMTI areas, parsing the TDCF descriptors and collect DataEvent contained in TDCF DATA lines. Ignore TDCF BLK_TS and UUID lines. Signed-off-by: Cristian Marussi --- v15 --> v16 - fix des_enabled format string - make magic const in caching helpers - drop useless __iomem useless cast in de_offs - improve qwords overflow check on qwords in data_line_parse - consider cache_stale BAD_START_SEQ(last_magic) - fix offset_safe_calc helper - discard and WARN when DE/FC are found on SHMTI - remove unused SCAN_DISCOVERY - shmti_oveflow use eplg directly v14 --> v15 - account for tstamped TDE during discovery - correct SHMTI overflow off-by-one - rework TDE cache hit/miss logic - introduce snapshots on full scan reads - validate DE location on SCAN_UPDATE - add custom SNAP eplg and overflow helpers - moved here definition of scmi_telemetry_payld_offset_safe_calc v13 --> v14 - fixed AB-BA dealock on blkts_link - relocate xa_destroy for xa_lines after uuids cleanup - report errors on scmi_telemetry_line_data_parse() - moved some common protocol definitions here from previous patches - split out of this patch all of the BLK_TS and UUID lines management, keeping only DATA line processing and delegating the other line types to following smaller patches - using a less regular time interval for TDCF scan retries v12 --> v13 - refactored cleanup resources - relocate tde->mtx guards higher up and add lockdep assertions - drop refcount on previous active lines when scanning - harden freeing path of UUIDs RCU items - better DATA LINE size-validation while parsing - reset active lines trackers on scan retries - validate used_qwords - redefine startm/endm bad sentinels and error path - add more BLKTS_INVALID checks on parsing - unlink stale BTS line when finding a new actibe for the TDE - retry scans on TDCF sanity failures..could be a torn read - use *alloc_objs - remove TO_CPU64 v11 --> v12 - reject zero as MATCHSEQ - reworked locking around line_get/put - reworked locking around blkts_read - a few comments on locking acquisition order - cleanup active line refs even when bailing out of a scan in error - added hashed locks for fine grain locking around blkts/uuid line creation - added RCU protection on UUIDs access v10 --> v11 - relocated some line handlers from another patch - reworked TDE and lines locking logic - fix UUID/BLKTS line refcounting to properly drop unused lines - add a mutex to protect des cache accesses v7 --> v9 - avoid referencing a NULL in trace_scmi_tlm_access O_o - refactor scmi_telemetry_line_put to free its container on release - convert SHMTI_OVERFLOW macro to an inline function v7 --> v8 - Fixes for some Sashiko issues on v7 - reword a few dev_errs - removed a few unused function (moved to next patch) - add more safeguards and checks to SHMTI_OVERFLOWS - fix UUIDs copy and allocation - fix UUID reference store and cleanup - cleanup all UNBOUND lines and their containers in resources_free - remove mutex on primary init v6 --> v7 - add _RAW helpers to manage non MMIO accesses - sparse fixes - use uuid_t for UUID lines - fix proper UUID line parsing and binding - adding a few traces for TDCF line handling - add new dynamically sized UUID DB to ease discovered UUIDs lookup - fix endianity handling in UUID TDCF lines - handle Primary UUID lines with regular uuid_line v5 --> v6 - consider UUID line in TDCF as BE when scanning - use const booleans in scmi_telemetry_de_state_update() - fix Sashiko review https://sashiko.dev/#/patchset/20260703123601.381275-= 1-cristian.marussi%40arm.com?part=3D8 - check SHMTI overflow while parsing - use proper bytes increment while parsing line_by_type - use mutex guard when link BLK_TS lines v4 --> v5 - converted TDE_HAS_TSTAMP to inline scmi_tde_has_tstamp - fixes sparse warnings v3 --> v4 - use kzalloc_obj - track SHMTI-discovered Telemetry DE states - refactor TDE cache with dedicated helpers - force tstamp to zero when timestamp is NOT supported or disabled v2 --> v3 - split from monolithic Telemetry patch - avoid devres allocation for resources that are added to the xa_lines XAr= ray - simplify prototype of line parsing helpers to drop unneeded dev - flip tstmap logic in scmi_telemetry_line_data_parse() to properly emit a TLM ftrace event - use ternary ops to simplify quite a few expressions --- drivers/firmware/arm_scmi/telemetry.c | 751 +++++++++++++++++++++++++- 1 file changed, 750 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 75844cac5900..4790929d10be 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -14,21 +14,29 @@ #include #include #include +#include #include #include +#include #include #include #include #include +#include #include #include =20 #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 +#define SCMI_TLM_TDCF_MAX_RETRIES 15 +#define SCMI_TLM_TDCF_RETRY_GAP_US 1234 + #define SCMI_TLM_DE_IMPL_NUM_DWORDS 4 =20 /* An enum listing the protocol specific commands definitions */ @@ -125,6 +133,157 @@ struct scmi_msg_resp_telemetry_update_intervals { =20 #define _I(__a) (ioread32((void __iomem *)(__a))) =20 +/* + * Define the behaviour of a SHMTI scan defining what information will + * be gathered and which Telemetry items can be updated. + */ +enum scan_mode { + SCAN_LOOKUP, /* Update only value/tstamp */ + SCAN_UPDATE, /* Update also location offset */ +}; + +struct fc_line { + __le32 data_low; + __le32 data_high; +}; + +struct fc_tsline { + __le32 data_low; + __le32 data_high; + __le32 ts_low; + __le32 ts_high; +}; + +struct line { + __le32 data_low; + __le32 data_high; +}; + +struct tsline { + __le32 data_low; + __le32 data_high; + __le32 ts_low; + __le32 ts_high; +}; + +#define LINE_DATA_GET_RAW(f) \ +({ \ + typeof(f) _f =3D (f); \ + u32 _high =3D le32_to_cpu(_f->data_high); \ + u32 _low =3D le32_to_cpu(_f->data_low); \ + u64 _res; \ + \ + _res =3D (((u64)(_high)) << 32) | (_low); \ + _res; \ +}) + +#define LINE_DATA_GET(f) \ +({ \ + typeof(f) _f =3D (f); \ + u32 _high =3D _I(&_f->data_high); \ + u32 _low =3D _I(&_f->data_low); \ + u64 _res; \ + \ + _res =3D (((u64)(_high)) << 32) | (_low); \ + _res; \ +}) + +#define LINE_TSTAMP_GET_RAW(f) \ +({ \ + typeof(f) _f =3D (f); \ + u32 _high =3D le32_to_cpu(_f->ts_high); \ + u32 _low =3D le32_to_cpu(_f->ts_low); \ + u64 _res; \ + \ + _res =3D (((u64)(_high)) << 32) | (_low); \ + _res; \ +}) + +#define LINE_TSTAMP_GET(f) \ +({ \ + typeof(f) _f =3D (f); \ + u32 _high =3D _I(&_f->ts_high); \ + u32 _low =3D _I(&_f->ts_low); \ + u64 _res; \ + \ + _res =3D (((u64)(_high)) << 32) | (_low); \ + _res; \ +}) + +#define BLK_TS_STAMP(f) LINE_TSTAMP_GET(f) +#define BLK_TS_STAMP_RAW(f) LINE_TSTAMP_GET_RAW(f) +#define BLK_TS_RATE(p) PAYLD_ID(p) +#define BLK_TS_RATE_RAW(p) PAYLD_ID_RAW(p) + +enum tdcf_line_types { + TDCF_DATA_LINE =3D 0, + TDCF_BLK_TS_LINE =3D 1, + TDCF_UUID_LINE =3D 2, +}; + +struct payload { + __le32 meta; +#define PAYLD_META(x) (_I(&((x)->meta))) +#define PAYLD_META_RAW(x) (le32_to_cpu((x)->meta)) +#define LINE_TYPE_RAW(m) (FIELD_GET(GENMASK(7, 4), (m))) +#define LINE_TYPE(x) (LINE_TYPE_RAW(PAYLD_META(x))) +#define IS_DATA_LINE(x) (LINE_TYPE(x) =3D=3D TDCF_DATA_LINE) +#define IS_DATA_LINE_RAW(x) (LINE_TYPE_RAW(x) =3D=3D TDCF_DATA_LINE) +#define IS_BLK_TS_LINE(x) (LINE_TYPE(x) =3D=3D TDCF_BLK_TS_LINE) +#define IS_BLK_TS_LINE_RAW(x) (LINE_TYPE_RAW(x) =3D=3D TDCF_BLK_TS_LINE) +#define IS_UUID_LINE(x) (LINE_TYPE(x) =3D=3D TDCF_UUID_LINE) +#define USE_BLK_TS_RAW(m) ((m) & BIT(3)) +#define USE_BLK_TS(x) (USE_BLK_TS_RAW(PAYLD_META(x))) +#define HAS_LINE_EXT_RAW(m) ((m) & BIT(2)) +#define HAS_LINE_EXT(x) (HAS_LINE_EXT_RAW(PAYLD_META(x))) +#define LINE_TS_VALID_RAW(m) ((m) & BIT(1)) +#define LINE_TS_VALID(x) (LINE_TS_VALID_RAW(PAYLD_META(x))) +#define DATA_INVALID_RAW(m) ((m) & BIT(0)) +#define DATA_INVALID(x) (DATA_INVALID_RAW(PAYLD_META(x))) +#define BLK_TS_INVALID_RAW(m) \ +({ \ + bool invalid; \ + \ + invalid =3D LINE_TS_VALID_RAW(m) || HAS_LINE_EXT_RAW(m) || \ + USE_BLK_TS_RAW(m) || DATA_INVALID_RAW(m); \ + invalid; \ +}) +#define BLK_TS_INVALID(p) BLK_TS_INVALID_RAW(PAYLD_META(p)) + +#define UUID_INVALID_RAW(m) \ +({ \ + bool invalid; \ + \ + invalid =3D LINE_TS_VALID_RAW(m) || USE_BLK_TS_RAW(m) || \ + DATA_INVALID_RAW(m) || !HAS_LINE_EXT_RAW(m); \ + invalid; \ +}) +#define UUID_INVALID(p) UUID_INVALID_RAW(PAYLD_META(p)) + __le32 id; + union { + struct line l; + struct tsline tsl; + }; +}; + +#define PAYLD_ID(x) (_I(&(((struct payload __iomem *)(x))->id))) +#define PAYLD_ID_RAW(x) (le32_to_cpu((((struct payload *)(x))->id))) + +#define LINE_DATA_PAYLD_WORDS \ + ((sizeof(u32) + sizeof(u32) + sizeof(struct line)) / sizeof(u32)) +#define EXT_LINE_DATA_PAYLD_WORDS \ + ((sizeof(u32) + sizeof(u32) + sizeof(struct tsline)) / sizeof(u32)) + +#define LINE_LENGTH_WORDS_RAW(m) \ + (HAS_LINE_EXT_RAW((m)) ? EXT_LINE_DATA_PAYLD_WORDS : LINE_DATA_PAYLD_WORD= S) + +#define LINE_LENGTH_WORDS(x) LINE_LENGTH_WORDS_RAW(PAYLD_META(x)) +#define LINE_LENGTH_QWORDS(x) ((LINE_LENGTH_WORDS(x)) / 2) +#define LINE_LENGTH_QWORDS_RAW(x) ((LINE_LENGTH_WORDS_RAW(x)) / 2) + +#define LINE_LENGTH_BYTES_RAW(m) ((LINE_LENGTH_WORDS_RAW(m)) * sizeof(u32)) +#define LINE_LENGTH_BYTES(x) (LINE_LENGTH_BYTES_RAW(PAYLD_META(x))) + struct prlg { __le32 sign_start; #define SIGNATURE_START 0x5442474E /* TBGN */ @@ -147,12 +306,24 @@ struct tdcf { unsigned char payld[]; }; =20 +#define QWORDS(_t) (_I(&(_t)->prlg.num_qwords)) +#define QWORDS_RAW(_t) (le32_to_cpu((_t)->prlg.num_qwords)) + #define SHMTI_MIN_SIZE (sizeof(struct tdcf) + TDCF_EPLG_SZ) /* An arbitrary upper bound to avoid crazy allocations driven by buggy FW = */ #define SHMTI_MAX_SIZE SZ_64K =20 -#define TDCF_START_SIGNATURE(x) (_I(&((x)->prlg.sign_start))) +#define TDCF_START_SIGNATURE(x) (_I(&((x)->prlg.sign_start))) +#define TDCF_START_SEQ_GET(x) (_I(&((x)->prlg.match_start))) +#define TDCF_START_SEQ_GET_RAW(x) (le32_to_cpu((x)->prlg.match_start)) +/* TDCF MatchSequence cannot be zero by the SCMI v4.0 Specification */ +#define TDCF_INVALID_MATCH_SEQ 0 +#define IS_BAD_START_SEQ(s) ((s) & 0x1 || (s) =3D=3D TDCF_INVALID_MATCH_SE= Q) + +#define TDCF_END_SEQ_GET(e) (_I(&((e)->match_end))) +#define TDCF_END_SEQ_GET_RAW(e) (le32_to_cpu((e)->match_end)) #define TDCF_END_SIGNATURE(e) (_I(&((e)->sign_end))) +#define TDCF_BAD_END_SEQ GENMASK(31, 0) =20 struct telemetry_shmti; =20 @@ -178,6 +349,9 @@ struct telemetry_shmti { struct mutex payld_locks[1 << PAYLD_HASH_BITS]; }; =20 +#define TDCF_OFFS(_b, _p) \ + ((unsigned long)((__force void *)(_p) - (__force void *)(_b))) + #define SHMTI_EPLG(s) \ ({ \ struct telemetry_shmti *_s =3D (s); \ @@ -187,6 +361,44 @@ struct telemetry_shmti { (_eplg); \ }) =20 +#define SNAP_EPLG(s) \ + ({ \ + struct shmti_snap *_s =3D (s); \ + struct eplg *_eplg; \ + \ + _eplg =3D (void *)_s->base + _s->shmti->info.len - TDCF_EPLG_SZ; \ + (_eplg); \ + }) + +struct telemetry_info; + +struct telemetry_line { + enum tdcf_line_types type; + u32 last_magic; + unsigned long offset; + refcount_t users; + struct telemetry_shmti *shmti; + /* Protect line accesses */ + struct mutex mtx; +}; + +struct telemetry_block_ts { + u64 last_ts; + u32 last_rate; + struct telemetry_line line; +}; + +#define to_blkts(l) container_of(l, struct telemetry_block_ts, line) + +struct telemetry_uuid { + u32 key; + struct hlist_node node_ht; + struct hlist_node node_lst; + uuid_t uuid; +}; + +#define to_uuid_from_uuid_t(u) container_of(u, struct telemetry_uuid, uuid) + #define IS_TDE_TSTAMP_TYPE(__t, __ttype) ((__t)->ts_type =3D=3D (__ttype)) #define IS_TDE_TS_NONE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE)) #define IS_TDE_TS_LINE(_t) (IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE)) @@ -210,7 +422,14 @@ struct telemetry_de { u32 offset; /* NOTE THAT DE data_sz is registered in scmi_telemetry_de */ u32 fc_size; + u64 last_val; + u64 last_ts; + u32 last_magic; struct list_head item; + struct telemetry_block_ts *bts; +#define BLK_TS_CHANGED(t, s, o) \ + (!((t)->bts) || (t)->bts->line.shmti !=3D (s) || (t)->bts->line.offset != =3D (o)) + struct telemetry_uuid *uuid; struct scmi_telemetry_de de; /* Protect last_val/ts/magic accesses - MUST BE KEPT LAST */ struct mutex mtx; @@ -218,10 +437,24 @@ struct telemetry_de { =20 #define to_tde(d) container_of(d, struct telemetry_de, de) =20 +static inline bool scmi_tde_has_tstamp(struct telemetry_de *t) +{ + return t->de.tstamp_support && t->de.tstamp_enabled; +} + +enum de_state { + ENA_STATE, + ENA_TSTAMP, + ENA_MAX +}; + struct telemetry_info { bool streaming_mode; unsigned int num_shmti; unsigned int num_des_tstamp; + /* Mutex to protect des_enabled_mtx */ + struct mutex des_enabled_mtx; + atomic_t des_enabled[ENA_MAX]; unsigned int default_blk_ts_rate; const struct scmi_protocol_handle *ph; struct telemetry_shmti *shmti; @@ -241,6 +474,27 @@ struct telemetry_info { static struct scmi_telemetry_res_info * __scmi_telemetry_resources_get(struct telemetry_info *ti); =20 +static inline struct tdcf *snap_tdcf(struct shmti_snap *s); + +static int scmi_telemetry_shmti_scan(struct telemetry_info *ti, + unsigned int shmti_id, enum scan_mode mode); + +static inline void +scmi_telemetry_de_state_update(struct telemetry_info *ti, enum de_state st= ate, + bool *current_state, const bool next_state) +{ + guard(mutex)(&ti->des_enabled_mtx); + if (!current_state || *current_state !=3D next_state) + atomic_add(next_state ? 1 : -1, &ti->des_enabled[state]); + + if (current_state) + *current_state =3D next_state; + + dev_dbg(ti->ph->dev, "Telemetry des_enabled[%s]:%d\n", + state =3D=3D ENA_STATE ? "STATE" : "TSTAMP", + atomic_read(&ti->des_enabled[state])); +} + static struct telemetry_de * scmi_telemetry_free_tde_get(struct telemetry_info *ti) { @@ -356,6 +610,36 @@ static int scmi_telemetry_tde_register(struct telemetr= y_info *ti, return ret; } =20 +static inline bool +scmi_telemetry_tde_cache_stale(struct telemetry_de *tde, const u32 magic) +{ + lockdep_assert_held(&tde->mtx); + + /* last_magic < magic */ + return IS_BAD_START_SEQ(tde->last_magic) || + time_before32(tde->last_magic, magic); +} + +static void +scmi_telemetry_tde_cache_update(struct telemetry_de *tde, u64 val, + u64 *tstamp, const u32 *magic) +{ + lockdep_assert_held(&tde->mtx); + + /* + * Update the cache only if stale compared to the current + * sample, so bail-out when last_magic >=3D *magic + */ + if (magic && !scmi_telemetry_tde_cache_stale(tde, *magic)) + return; + + tde->last_magic =3D magic ? *magic : TDCF_BAD_END_SEQ; + tde->last_val =3D val; + tde->last_ts =3D tstamp && scmi_tde_has_tstamp(tde) ? *tstamp : 0; + if (tstamp) + *tstamp =3D tde->last_ts; +} + struct scmi_tlm_de_priv { struct telemetry_info *ti; void *next; @@ -1094,6 +1378,468 @@ scmi_telemetry_de_lookup(const struct scmi_protocol= _handle *ph, u32 id) return xa_load(&ti->xa_des, id); } =20 +static inline struct payload __iomem * +scmi_telemetry_payld_offset_safe_calc(struct telemetry_shmti *shmti, u32 o= ffs) +{ + struct payload __iomem *payld; + unsigned long p; + u32 end; + + if (offs < sizeof(struct tdcf)) + return IOMEM_ERR_PTR(-EPROTO); + + /* Check if accessing payld->meta is safe. */ + if (check_add_overflow(offs, sizeof(u32), &end) || + end > shmti->info.len) + return IOMEM_ERR_PTR(-EPROTO); + + if (check_add_overflow((__force unsigned long)shmti->base, offs, &p)) + return IOMEM_ERR_PTR(-EPROTO); + + payld =3D shmti->base + offs; + /* Check line boundary */ + if (check_add_overflow(offs, LINE_LENGTH_BYTES(payld), &end) || + end > shmti->info.len - TDCF_EPLG_SZ) + return IOMEM_ERR_PTR(-EPROTO); + + return payld; +} + +static u64 +scmi_telemetry_blkts_read(u32 magic, struct telemetry_de *tde) +{ + lockdep_assert_held(&tde->mtx); + + return 0; +} + +static inline int +scmi_telemetry_line_data_parse(struct telemetry_de *tde, struct shmti_snap= *snap, + u64 *val, u64 *tstamp, struct payload *payld, + struct payload *bts_payld, u32 magic) +{ + u32 meta =3D PAYLD_META_RAW(payld); + + lockdep_assert_held(&tde->mtx); + + /* Clear returned values in case of errors */ + *val =3D 0; + + if (tstamp) { + *tstamp =3D 0; + if (USE_BLK_TS_RAW(meta)) { + /* Read out the actual BLK_TS */ + *tstamp =3D scmi_telemetry_blkts_read(magic, tde); + } else if (LINE_TS_VALID_RAW(meta)) { + /* + * Note that LINE_TS_VALID implies HAS_LINE_EXT and that + * the per DE line_ts_rate is advertised in the DE + * descriptor, BUT we check anyway against buggy FW. + */ + if (!HAS_LINE_EXT_RAW(meta)) { + trace_scmi_tlm_access(tde->de.info->id, + "SHMTI_BAD_TS_LINE", 0, 0); + return -EPROTO; + } + + *tstamp =3D LINE_TSTAMP_GET_RAW(&payld->tsl); + } + } + + /* Data is always valid since we are NOT handling BLK TS lines here */ + *val =3D LINE_DATA_GET_RAW(&payld->l); + + trace_scmi_tlm_collect(tstamp ? *tstamp : 0, tde->de.info->id, + *val, "SHMTI_DE_READ"); + + scmi_telemetry_tde_cache_update(tde, *val, tstamp, &magic); + + return 0; +} + +/** + * scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing + * @ti: A reference to the telemetry info descriptor + * @payld: Line payload to parse + * @snap: A reference to the working SHMTI snapshot that will be processed + * @mode: A flag to determine the behaviour of the scan + * @active_bts: A pointer to keep track and report any found BLK timestamp= line + * @active_uuid: A pointer to keep track and report any found UUID line + * + * This routine takes care to: + * - verify line consistency in relation to the used flags and the current + * context: e.g. is there an active preceding BLK_TS line if the DataLi= ne + * sports a USE_BLKTS flag ? + * - verify the related Data Event ID exists OR create a brand new DE + * (depending on the @mode of operation) + * - links any active BLK_TS or UUID line to the current DE + * - read and save value/tstamp for the DE ONLY if anything has changed (= by + * tracking the last TDCF magic) and update related magic: this allows = to + * minimize future needs of single-DE reads + * + * Modes of operation. + * + * The scan behaviour depends on the chosen @mode: + * - SCAN_LOOKUP: the basic scan which aims to update value associated = to + * existing DEs. Any discovered DataLine that could NOT be + * matched to an existing, previously discovered, DE is + * discarded. This is the normal scan behaviour. + * - SCAN_UPDATE: a more advanced scan which provides all the SCAN_LOOK= UP + * features plus takes care to update the DEs location + * coordinates inside the SHMTI: note that the related DEs are + * still supposed to have been previously discovered when + * this scan runs. This is used to update location + * coordinates for DEs contained in a Group when such group + * is enabled. + */ +static void scmi_telemetry_tdcf_data_parse(struct telemetry_info *ti, + struct payload *payld, + struct shmti_snap *snap, + enum scan_mode mode, + struct telemetry_block_ts *active_bts, + struct telemetry_uuid *active_uuid) +{ + struct telemetry_shmti *shmti =3D snap->shmti; + struct telemetry_de *tde; + u64 val, tstamp =3D 0; + u32 de_id; + + de_id =3D PAYLD_ID_RAW(payld); + /* Is this DE ID known ? */ + tde =3D scmi_telemetry_tde_lookup(ti, de_id); + if (!tde) { + trace_scmi_tlm_access(de_id, "DE_INVALID", 0, 0); + return; + } + + guard(mutex)(&tde->mtx); + + if (WARN_ON(tde->de.fc_support)) { + trace_scmi_tlm_access(de_id, "DE_INVALID_FC_ON_SHMTI", 0, 0); + return; + } + + /* + * Update DE location refs if requested: normally done only on group + * enable. + * Note that all the DE location information is relative to the + * original SHMTI device-memory area, NOT to the snapshot we are parsing. + */ + if (mode >=3D SCAN_UPDATE) { + u32 de_offs; + void __iomem *p; + + /* Offest is the same in the SHMTI and in the snapshot indeed */ + de_offs =3D (void *)payld - (void *)snap->base; + /* Validate location before update */ + p =3D scmi_telemetry_payld_offset_safe_calc(shmti, de_offs); + if (IS_ERR(p)) { + dev_err(ti->ph->dev, + "TDE 0x%08X updated location invalid - shmti:%u offs:%u\n", + de_id, shmti->info.sid, de_offs); + trace_scmi_tlm_access(de_id, "DE_LOCATION_INVALID", 0, 0); + return; + } + + tde->sid =3D shmti->info.sid; + tde->base =3D shmti->base; + tde->eplg =3D SHMTI_EPLG(shmti); + tde->offset =3D de_offs; + + trace_scmi_tlm_access(de_id, "DE_LOCATION_UPDATED", 0, 0); + + dev_dbg(ti->ph->dev, + "TDCF-updated DE_ID:0x%08X - shmti:%pK offset:%u\n", + tde->de.info->id, tde->base, tde->offset); + } + + /* + * Has any value/tstamp really changed ? + * Note that, if the magic currently associated with this TDE has + * NOT changed compared to the current SHMTI magic, nothing could have + * been changed across the whole TDCF, including any relation to + * BLK_TS or UUID lines. + */ + if (!scmi_telemetry_tde_cache_stale(tde, snap->magic)) + return; + + /* + * Parse data words ... only for the sake of updating TDE cache, + * any error is logged by ignored. + */ + scmi_telemetry_line_data_parse(tde, snap, &val, &tstamp, payld, NULL, + snap->magic); +} + +/** + * scmi_telemetry_tdcf_line_parse - Line parser + * @ti: A reference to the Telemetry instance + * @avail_qwords: Number of remaining qwords + * @payld: A reference to the payload representing the line to parse + * @snap: A reference to the working SHMTI snapshot that will be processed + * @mode: Specification of the scanning mode + * @active_bts: A reference used to track the last BLK_TS line found + * @active_uuid: A reference used to track the last UUID line found + * + * Note that @active_bts and @active_uuid references are propagated back a= nd + * forth from the line parsers to keep track of the nearest active BLK_TS = or + * UUID line found in the SHMTI areas and, while doing that, such descript= ors + * are refcounted and tracked in the xa_lines XArray indexed by @payld. + * BLK_TS and UUID descriptors are dropped when no more linked to any DATA= line, + * during the normal system operation OR later on when the protocol is + * de-initialized following a driver removal. + * + * Return: Number of QWORDS consumed from @payld by this line parsing; note + * that buggy firmware could return bad, out of range values, so this + * value MUST be carefully checked for SHMTI boundary overflows by the + * caller. + */ +static int scmi_telemetry_tdcf_line_parse(struct telemetry_info *ti, + unsigned int avail_qwords, + struct payload *payld, + struct shmti_snap *snap, + enum scan_mode mode, + struct telemetry_block_ts **active_bts, + struct telemetry_uuid **active_uuid) +{ + u32 meta =3D le32_to_cpu(payld->meta); + int used_qwords; + + used_qwords =3D LINE_LENGTH_QWORDS_RAW(meta); + /* Invalid lines are not an error, could simply be disabled DEs */ + if (DATA_INVALID_RAW(meta)) { + trace_scmi_tlm_access(le32_to_cpu(payld->id), "TDCF_INVALID", 0, 0); + return used_qwords; + } + + /* Does they fit in the snapshot (sane) availabel qwords ? */ + if (used_qwords > avail_qwords) + return -EINVAL; + + /* + * Problematic lines are ignored, their information discarded, and such + * issues are logged, but the scan process continues. + */ + switch (LINE_TYPE_RAW(meta)) { + case TDCF_DATA_LINE: + scmi_telemetry_tdcf_data_parse(ti, payld, snap, mode, + *active_bts, *active_uuid); + break; + default: + trace_scmi_tlm_access(PAYLD_ID_RAW(payld), "TDCF_LINE_UNKNOWN", 0, 0); + break; + } + + return used_qwords; +} + +static inline bool +scmi_telemetry_shmti_overflow(struct eplg *eplg, void __iomem *next) +{ + void __iomem *limit =3D eplg; + + /* Is payld->meta accessible ? */ + if (next >=3D limit - sizeof(u32)) + return true; + + next +=3D LINE_LENGTH_BYTES((struct payload __iomem *)next); + + return next > limit; +} + +static inline struct tdcf *snap_tdcf(struct shmti_snap *s) +{ + return (struct tdcf *)s->base; +} + +static inline bool +scmi_telemetry_snap_overflow(struct shmti_snap *snap, const void *next) +{ + void *limit =3D SNAP_EPLG(snap); + + /* Is payld->meta accessible ? */ + if (next >=3D limit - sizeof(u32)) + return true; + + next +=3D LINE_LENGTH_BYTES_RAW(PAYLD_META_RAW((struct payload *)next)); + + return next > limit; +} + +static int scmi_telemetry_shmti_snapshot(struct telemetry_shmti *shmti, + struct shmti_snap *work) +{ + struct tdcf __iomem *tdcf =3D shmti->base; + int retries =3D SCMI_TLM_TDCF_MAX_RETRIES; + struct tdcf *snap =3D snap_tdcf(work); + u32 startm =3D 0, endm =3D 0, live; + + lockdep_assert_held(&shmti->lock); + + do { + /* A bit of exponential backoff between retries */ + fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP_U= S); + + memcpy_fromio(snap, tdcf, shmti->info.len); + /* Assure the TDCF snapshot has been fully taken before checking seqnum = */ + rmb(); + /* Used to detect changes while copying */ + live =3D TDCF_START_SEQ_GET(tdcf); + + startm =3D TDCF_START_SEQ_GET_RAW(snap); + if (IS_BAD_START_SEQ(startm)) { + trace_scmi_tlm_access(0, "MSEQ_BADSTART", startm, 0); + continue; + } + + endm =3D TDCF_END_SEQ_GET_RAW(SNAP_EPLG(work)); + if (startm !=3D endm || startm !=3D live) { + trace_scmi_tlm_access(0, "MSEQ_MISMATCH", startm, + startm !=3D endm ? endm : live); + continue; + } + + trace_scmi_tlm_access(0, "TDCF_SNAPSHOT_OK", startm, endm); + + /* Ensure any snapshot is visible before updating magic */ + smp_store_release(&work->magic, startm); + + return 0; + } while (--retries); + + trace_scmi_tlm_access(0, "TDCF_SNAPSHOT_FAIL", startm, endm); + + return -EPROTO; +} + +/* + * True if neither TDCF marker has moved since the cached snapshot was tak= en. + * Any platform update must write one marker before touching data (start f= irst + * or end first, depending on the spec's order), so checking both is + * independent of that order. + */ +static bool scmi_telemetry_shmti_unchanged(struct telemetry_shmti *shmti, = u32 magic) +{ + struct tdcf __iomem *tdcf =3D shmti->base; + + return TDCF_START_SEQ_GET(tdcf) =3D=3D magic && + TDCF_END_SEQ_GET(SHMTI_EPLG(shmti)) =3D=3D magic; +} + +/* Returns with the SRCU read lock held (index in *idx) on a snapshot that= matched at check time */ +static struct shmti_snap * +scmi_telemetry_shmti_refresh(struct telemetry_shmti *shmti, int *idx) +{ + struct shmti_snap *cur, *work; + int ret; + + guard(mutex)(&shmti->lock); + + cur =3D rcu_dereference_protected(shmti->cur, lockdep_is_held(&shmti->loc= k)); + /* Someone else may have refreshed while we waited for the mutex */ + if (cur && scmi_telemetry_shmti_unchanged(shmti, cur->magic)) { + /* Safe: recycling cur requires shmti->lock, which we hold */ + *idx =3D srcu_read_lock(&shmti->srcu); + return cur; + } + + /* Pick the spare buffer */ + work =3D (cur =3D=3D shmti->snaps[0]) ? shmti->snaps[1] : shmti->snaps[0]; + /* + * Wait for readers that may still be parsing 'work' from its time + * as current + */ + synchronize_srcu(&shmti->srcu); + + ret =3D scmi_telemetry_shmti_snapshot(shmti, work); + if (ret) + return ERR_PTR(ret); + + /* + * Take our read lock BEFORE publishing so 'work' cannot be + * recycled under us + */ + *idx =3D srcu_read_lock(&shmti->srcu); + rcu_assign_pointer(shmti->cur, work); + + return work; +} + +/** + * scmi_telemetry_shmti_scan - Full SHMTI scan + * @ti: A reference to the telemetry info descriptor + * @shmti_id: ID of the SHMTI area that has to be scanned + * @mode: A flag to determine the behaviour of the scan + * + * Return: 0 on Success + */ +static int scmi_telemetry_shmti_scan(struct telemetry_info *ti, + unsigned int shmti_id, enum scan_mode mode) +{ + struct telemetry_shmti *shmti =3D &ti->shmti[shmti_id]; + struct telemetry_block_ts *active_bts =3D NULL; + struct telemetry_uuid *active_uuid =3D NULL; + unsigned int qwords; + struct shmti_snap *snap; + struct tdcf *tdcf; + int idx, ret =3D 0; + void *next; + + if (!shmti->base) + return -ENODEV; + + idx =3D srcu_read_lock(&shmti->srcu); + snap =3D srcu_dereference(shmti->cur, &shmti->srcu); + if (!snap || !scmi_telemetry_shmti_unchanged(shmti, snap->magic)) { + /* MUST drop the read lock: refresh calls synchronize_srcu() */ + srcu_read_unlock(&shmti->srcu, idx); + snap =3D scmi_telemetry_shmti_refresh(shmti, &idx); + if (IS_ERR(snap)) + return PTR_ERR(snap); + } + + tdcf =3D snap_tdcf(snap); + next =3D tdcf->payld; + qwords =3D QWORDS_RAW(tdcf); + while (qwords) { + int used_qwords; + + /* + * Bailout on any error since we are processing a supposedly + * stable snapshot at this point, so any further error is on + * firmware-side and NOT recoverable: this would mean having to + * drop the remaining part of the scan results after having + * cached the initial good snapshot: log this down below. + */ + if (scmi_telemetry_snap_overflow(snap, next)) { + trace_scmi_tlm_access(0, "SHMTI_OVERFLOW", snap->magic, 0); + ret =3D -EPROTO; + break; + } + + used_qwords =3D scmi_telemetry_tdcf_line_parse(ti, qwords, + next, snap, mode, + &active_bts, &active_uuid); + if (used_qwords <=3D 0) { + /* Stop scanning when qwords are inconsistent. */ + trace_scmi_tlm_access(PAYLD_ID_RAW(next), "BAD_QWORDS", + snap->magic, 0); + ret =3D -EPROTO; + break; + } + + next +=3D used_qwords * 8; + qwords -=3D used_qwords; + } + + srcu_read_unlock(&shmti->srcu, idx); + + if (ret) + dev_err(ti->ph->dev, FW_BUG "Inconsistent TDCF. Scan aborted.\n"); + + return ret; +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, @@ -1305,6 +2051,9 @@ static int scmi_telemetry_instance_init(struct teleme= try_info *ti) if (ret) return ret; =20 + atomic_set(&ti->des_enabled[ENA_STATE], 0); + atomic_set(&ti->des_enabled[ENA_TSTAMP], 0); + mutex_init(&ti->des_enabled_mtx); /* Setup resources lazy initialization */ atomic_set(&ti->rinfo_initializing, 0); init_completion(&ti->rinfo_initdone); --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304654; b=XE+Hb+93gpyfhgAsl09dr1mujsWeHXFdJ8ubnDry22TL/4kSkhZZlrCGceFAl5OS+6tM xWKx5LX8UfxO7OobLQ1mf2dRpFjFajlFXLujhaEKrSo7ihdR5AeJVDVgYRf/LUCbQPZ43 PMB9tXqDUwaF+4dwHwwL+YIoQFvDAtETO9b0jSlypr8LoICKTi+abuU/6vmyi8/nXwR3b GyQAB3Rmo308b2jWzWxLwvbWVx7uy1SyXXxHh5LDnu8fkMLwoEp0aKI8g7Vs1LRFkiWrg FDfid9ag+rb1w2l6BLaTNOUSCNiejEAlPOJFwbzKGRO6ZRSHUvhsdjpePFnJch00eDg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304654; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=jaBjYmBdElqa4mMzlABtn/SoY1106vU3rQxUBC2ZKsE=; b=fJ+5MNbitbxzXgu2UnEG3YepitPpvGZj/3PucuipPDEB+YcSngCUCgGjPgJr7155RDEh UUgrzyC+QM2aAx+c37jKMxkSWQPPUeFqVh9JNQMmPf6P0QnJ7hFc/MAAJtXAwFqt2Xb74 fGFGta+ouvWmJvgFlcz4nqT0O/XAhnKoeKDc/R/794xZ0NEBPykRFHLVErvFzPLckBb3J o1RBCFwEwZEhM5Rx98wQKDKM+QJWybYTm5gTzS6Dl+WjbqnVOCzaztQfbU0fw4KInwAa5 2xf5McMiybUUaDn7FecC6A3UjOHhz4yeOXxN7+GbsFRkWguWf1g0rXuhbgie3i9XGXA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=gLoCOfkg; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C56D2606A339; Sat, 21 Nov 2026 23:44:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C56D2606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=gLoCOfkg 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 38A0A176B; Sat, 21 Nov 2026 15:44:10 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5134E3F632; Sat, 21 Nov 2026 15:44:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304653; bh=yPXlbyxfPszypBVLAU4x3PWP87zfD02iDfGeiOOrzpg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gLoCOfkgm+2gEHJZc2jE+eYtvDywMaDCYLZSBPd245vYUtQcnkj++/il2brRfRErC mRakaXyYiZCTYHtP4e5Qfg2SG5LsJoW1N63OvZOD3rPbGXIHKg5cKCN+XscsjBvgyX 6qKc3C3iLClJKe0wJ7F+4bCF1P20GmPxgl1MWz5U= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 15/31] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines Date: Sat, 21 Nov 2026 23:39:49 +0000 Message-ID: <20261121234005.1525419-16-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support to parse, track and link TDCF BLK_TS lines to the related discovered DataEvents. Signed-off-by: Cristian Marussi --- v15 --> v16 - WARN_ON(BLK_TS_INVALID) but keep active_bts valid - invalidate TDE cache on blkts_unlin - cleanup stale lines on shmti cleanup v14 --> v15 - use proper cast on tlm_collect - use snapshots to track and collect BLK_TS during full scan bulk reads - fix too early invocation xa_destroy(xa_lines) v13 --> v14 - split out of previous single patch implementing parse SHMTI routines --- drivers/firmware/arm_scmi/telemetry.c | 341 +++++++++++++++++++++++++- 1 file changed, 334 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 4790929d10be..eea070fd0cd1 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -159,6 +160,11 @@ struct line { __le32 data_high; }; =20 +struct blk_tsline { + __le32 ts_low; + __le32 ts_high; +}; + struct tsline { __le32 data_low; __le32 data_high; @@ -263,6 +269,7 @@ struct payload { union { struct line l; struct tsline tsl; + struct blk_tsline blk_tsl; }; }; =20 @@ -471,6 +478,10 @@ struct telemetry_info { struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti); }; =20 +static void __scmi_telemetry_line_put(struct telemetry_line *line); + +static void scmi_telemetry_de_unlink(struct scmi_telemetry_de *de); + static struct scmi_telemetry_res_info * __scmi_telemetry_resources_get(struct telemetry_info *ti); =20 @@ -479,6 +490,12 @@ static inline struct tdcf *snap_tdcf(struct shmti_snap= *s); static int scmi_telemetry_shmti_scan(struct telemetry_info *ti, unsigned int shmti_id, enum scan_mode mode); =20 +static inline +struct mutex *lock_for_payld(struct mutex *locks, unsigned long offset) +{ + return &locks[hash_long(offset, PAYLD_HASH_BITS)]; +} + static inline void scmi_telemetry_de_state_update(struct telemetry_info *ti, enum de_state st= ate, bool *current_state, const bool next_state) @@ -610,6 +627,14 @@ static int scmi_telemetry_tde_register(struct telemetr= y_info *ti, return ret; } =20 +static inline void scmi_telemetry_tde_cache_invalidate(struct telemetry_de= *tde) +{ + lockdep_assert_held(&tde->mtx); + + /* Invalidate cache */ + tde->last_magic =3D TDCF_BAD_END_SEQ; +} + static inline bool scmi_telemetry_tde_cache_stale(struct telemetry_de *tde, const u32 magic) { @@ -958,11 +983,18 @@ static void scmi_telemetry_tdes_cleanup(void *arg) struct scmi_telemetry_de *de; unsigned long idx; =20 + /* + * Unlinking all the BLK_TS/UUID lines related to a DE triggers also + * the deallocation of such lines when the embedded refcount hits zero. + * Note that the UUID lines are also referenced and refcounted by the + * global UUID RCU array, so they won't be deallocated here. + */ xa_for_each(&ti->xa_des, idx, de) { struct telemetry_de *tde =3D to_tde(de); =20 guard(mutex)(&tde->mtx); =20 + scmi_telemetry_de_unlink(&tde->de); scmi_telemetry_free_tde_put(ti, tde); } } @@ -996,7 +1028,7 @@ static int scmi_telemetry_de_descriptors_get(struct te= lemetry_info *ti) if (ret) return ret; =20 - return devm_add_action_or_reset(ph->dev, scmi_telemetry_tdes_cleanup, ti); + return 0; } =20 struct scmi_tlm_ivl_priv { @@ -1270,16 +1302,29 @@ static int scmi_telemetry_shmti_list(const struct s= cmi_protocol_handle *ph, return 0; } =20 +static void scmi_telemetry_shmti_lines_cleanup(struct telemetry_shmti *shm= ti) +{ + struct telemetry_line *line; + unsigned long idx; + + guard(mutex)(&shmti->lines_mtx); + /* Drop all remaining currently unbound lines and their containers */ + xa_for_each(&shmti->xa_lines, idx, line) + __scmi_telemetry_line_put(line); + + xa_destroy(&shmti->xa_lines); +} + static void scmi_telemetry_shmti_cleanup(void *arg) { struct telemetry_shmti *shmti =3D arg; =20 + scmi_telemetry_shmti_lines_cleanup(shmti); + cleanup_srcu_struct(&shmti->srcu); =20 kvfree(shmti->snaps[0]); kvfree(shmti->snaps[1]); - - xa_destroy(&shmti->xa_lines); } =20 static int scmi_telemetry_shmti_init(struct device *dev, @@ -1344,7 +1389,7 @@ static int scmi_telemetry_enumerate_shmti(struct tele= metry_info *ti) /* Expose the fully initialized SHMTI infos */ smp_store_release(&ti->info.base.num_shmtis, ti->num_shmti); =20 - return 0; + return devm_add_action_or_reset(ph->dev, scmi_telemetry_tdes_cleanup, ti); } =20 static const struct scmi_telemetry_info * @@ -1406,11 +1451,242 @@ scmi_telemetry_payld_offset_safe_calc(struct telem= etry_shmti *shmti, u32 offs) } =20 static u64 -scmi_telemetry_blkts_read(u32 magic, struct telemetry_de *tde) +scmi_telemetry_blkts_read(u32 magic, struct telemetry_de *tde, + struct payload *bts_payld, struct shmti_snap *snap) { + struct telemetry_block_ts *bts; + + /* Acquiring order is: tde -> line */ lockdep_assert_held(&tde->mtx); + if (WARN_ON(!tde->bts || (!bts_payld && !snap))) + return 0; =20 - return 0; + bts =3D tde->bts; + guard(mutex)(&bts->line.mtx); + + if (bts->line.last_magic =3D=3D magic) + return bts->last_ts; + + /* Unless specified otherwise, use the snapshot BLK_TS paylod */ + if (!bts_payld) { + void *base =3D snap_tdcf(snap); + + bts_payld =3D base + bts->line.offset; + } + + /* Note that the bts->last_rate can change ONLY on creation */ + bts->last_ts =3D BLK_TS_STAMP_RAW(&bts_payld->blk_tsl); + bts->line.last_magic =3D magic; + + return bts->last_ts; +} + +static void +scmi_telemetry_blkts_update_snap(struct telemetry_info *ti, + struct telemetry_block_ts *bts, + struct payload *bts_payld, u32 magic) +{ + guard(mutex)(&bts->line.mtx); + if (bts->line.last_magic !=3D magic) { + bts->last_ts =3D BLK_TS_STAMP_RAW(&bts_payld->blk_tsl); + bts->last_rate =3D BLK_TS_RATE_RAW(bts_payld); + /* BLK_TS clock rate value can change ONLY here on creation */ + if (!bts->last_rate) + bts->last_rate =3D ti->default_blk_ts_rate; + bts->line.last_magic =3D magic; + + trace_scmi_tlm_access(0, "BLK_TS_UPDATE", magic, 0); + } +} + +static inline void scmi_telemetry_line_free(struct telemetry_line *line) +{ + void *blob; + + switch (line->type) { + case TDCF_BLK_TS_LINE: + blob =3D to_blkts(line); + kfree(blob); + break; + default: + WARN_ON(1); + break; + } +} + +static void __scmi_telemetry_line_put(struct telemetry_line *line) +{ + lockdep_assert_held(&line->shmti->lines_mtx); + + mutex_lock(&line->mtx); + if (refcount_dec_and_test(&line->users)) { + xa_erase(&line->shmti->xa_lines, line->offset); + + /* Unlock and free the containing structure: blkts or uuid */ + mutex_unlock(&line->mtx); + + trace_scmi_tlm_access(0, "DROP_LINE", line->type, line->last_magic); + scmi_telemetry_line_free(line); + + return; + } + mutex_unlock(&line->mtx); +} + +static void scmi_telemetry_line_put(struct telemetry_line *line) +{ + /* Acquiring coarse grain first ... */ + guard(mutex)(&line->shmti->lines_mtx); + __scmi_telemetry_line_put(line); +} + +static void scmi_telemetry_blkts_unlink(struct telemetry_de *tde) +{ + /* Acquiring order is: tde -> line */ + lockdep_assert_held(&tde->mtx); + if (!tde->bts) + return; + + scmi_telemetry_line_put(&tde->bts->line); + + scmi_telemetry_tde_cache_invalidate(tde); + tde->bts =3D NULL; + + trace_scmi_tlm_access(tde->de.info->id, "BLKTS_UNLINK", 0, 0); +} + +static void scmi_telemetry_de_unlink(struct scmi_telemetry_de *de) +{ + struct telemetry_de *tde =3D to_tde(de); + + lockdep_assert_held(&tde->mtx); + + /* Unlink all related lines triggering their deallocation */ + scmi_telemetry_blkts_unlink(tde); +} + +static struct telemetry_line * +scmi_telemetry_line_get(struct telemetry_shmti *shmti, unsigned long offse= t) +{ + struct telemetry_line *line; + + /* Acquiring coarse grain first ... */ + guard(mutex)(&shmti->lines_mtx); + line =3D xa_load(&shmti->xa_lines, offset); + if (!line) + return NULL; + + guard(mutex)(&line->mtx); + refcount_inc(&line->users); + + return line; +} + +static int +scmi_telemetry_line_init(struct telemetry_shmti *shmti, struct telemetry_l= ine *line, + unsigned long offset, enum tdcf_line_types type) +{ + line->type =3D type; + refcount_set(&line->users, 1); + line->offset =3D offset; + line->shmti =3D shmti; + mutex_init(&line->mtx); + + guard(mutex)(&shmti->lines_mtx); + return xa_insert(&shmti->xa_lines, offset, line, GFP_KERNEL); +} + +static struct telemetry_block_ts * +scmi_telemetry_blkts_create(struct telemetry_shmti *shmti, unsigned long o= ffset) +{ + struct telemetry_block_ts *bts; + int ret; + + bts =3D kzalloc_obj(*bts); + if (!bts) + return NULL; + + ret =3D scmi_telemetry_line_init(shmti, &bts->line, offset, TDCF_BLK_TS_L= INE); + if (ret) { + kfree(bts); + return NULL; + } + + trace_scmi_tlm_collect(0, shmti->info.sid, offset, "SHMTI_NEW_BLKTS"); + + return bts; +} + +static struct telemetry_block_ts * +scmi_telemetry_blkts_get_or_create(struct telemetry_shmti *shmti, + unsigned long offset) +{ + struct telemetry_line *line; + + /* Avoid races on line creation for this payld */ + guard(mutex)(lock_for_payld(shmti->payld_locks, offset)); + + line =3D scmi_telemetry_line_get(shmti, offset); + if (line) + return to_blkts(line); + + return scmi_telemetry_blkts_create(shmti, offset); +} + +/** + * scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser + * + * @ti: A reference to the telemetry_info descriptor + * @bts_payld: TDCF payld line to process + * @snap: A reference to the working SHMTI snapshot that will be processed + * @active_bts: Input/output reference to keep track of the last blk_ts fo= und + * + * Process a valid TDCF BLK_TS line and, after having looked up or created= a + * blk_ts descriptor, update the related data and return it as the current= ly + * active blk_ts, given that it is effectively the last found during this + * scan. + * + * Note that the current BLK_TS line could be malformed due to garbage in + * the TDCF: in such a case we warn/log and bailout BUT we don't throw away + * the currently active_bts (if any) since the current malformed line could + * be bogus and never intended to be a BLK_TS line. + */ +static void scmi_telemetry_tdcf_blkts_parse(struct telemetry_info *ti, + struct payload *bts_payld, + struct shmti_snap *snap, + struct telemetry_block_ts **active_bts) +{ + struct telemetry_block_ts *bts, *previous =3D *active_bts; + struct telemetry_shmti *shmti =3D snap->shmti; + u32 meta =3D PAYLD_META_RAW(bts_payld); + unsigned long offset; + + /* Check for spec compliance. */ + if (WARN_ON(BLK_TS_INVALID_RAW(meta))) { + trace_scmi_tlm_access(0, "BLK_TS_INVALID", 0, meta); + return; + } + + offset =3D TDCF_OFFS(snap->base, bts_payld); + /* A BLK_TS descriptor MUST be returned: it is found or it is created */ + bts =3D scmi_telemetry_blkts_get_or_create(shmti, offset); + /* + * On error processing the new BLK_TS bailout taking care to clear + * the previous last active BLK_TS to avoid mistakenly binding to the + * next data lines. + */ + if (WARN_ON(!bts)) + goto out; + + /* Update the descriptor with the lastest TS */ + scmi_telemetry_blkts_update_snap(ti, bts, bts_payld, snap->magic); + +out: + /* Drop one refcount on the previous active since we found a new one */ + if (previous) + scmi_telemetry_line_put(&previous->line); + + *active_bts =3D bts; } =20 static inline int @@ -1429,7 +1705,8 @@ scmi_telemetry_line_data_parse(struct telemetry_de *t= de, struct shmti_snap *snap *tstamp =3D 0; if (USE_BLK_TS_RAW(meta)) { /* Read out the actual BLK_TS */ - *tstamp =3D scmi_telemetry_blkts_read(magic, tde); + *tstamp =3D scmi_telemetry_blkts_read(magic, tde, + bts_payld, snap); } else if (LINE_TS_VALID_RAW(meta)) { /* * Note that LINE_TS_VALID implies HAS_LINE_EXT and that @@ -1457,6 +1734,26 @@ scmi_telemetry_line_data_parse(struct telemetry_de *= tde, struct shmti_snap *snap return 0; } =20 +static inline void scmi_telemetry_blkts_link(struct telemetry_de *tde, + struct telemetry_block_ts *bts) +{ + /* Acquiring order is: tde -> line */ + lockdep_assert_held(&tde->mtx); + + /* Unlink old stale BTS if existent */ + scmi_telemetry_blkts_unlink(tde); + + guard(mutex)(&bts->line.mtx); + refcount_inc(&bts->line.users); + + /* Update TS clock rate if provided by the BLK_TS */ + if (bts->last_rate) + tde->de.info->ts_rate =3D bts->last_rate; + + tde->bts =3D bts; + trace_scmi_tlm_access(tde->de.info->id, "BLKTS_LINK", 0, 0); +} + /** * scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing * @ti: A reference to the telemetry info descriptor @@ -1500,11 +1797,19 @@ static void scmi_telemetry_tdcf_data_parse(struct t= elemetry_info *ti, struct telemetry_uuid *active_uuid) { struct telemetry_shmti *shmti =3D snap->shmti; + u32 meta =3D PAYLD_META_RAW(payld); + bool use_blk_ts =3D USE_BLK_TS_RAW(meta); struct telemetry_de *tde; u64 val, tstamp =3D 0; u32 de_id; =20 de_id =3D PAYLD_ID_RAW(payld); + /* Discard malformed lines...a preceding BLK_TS must exist */ + if (use_blk_ts && !active_bts) { + trace_scmi_tlm_access(de_id, "BAD_USE_BLK_TS", 0, 0); + return; + } + /* Is this DE ID known ? */ tde =3D scmi_telemetry_tde_lookup(ti, de_id); if (!tde) { @@ -1563,6 +1868,15 @@ static void scmi_telemetry_tdcf_data_parse(struct te= lemetry_info *ti, if (!scmi_telemetry_tde_cache_stale(tde, snap->magic)) return; =20 + /* + * Link the related BTS when needed, it's unlinked on disable. + * Note that each DEs timestamp types is statically defined and + * enumerated once for all at init: a Data line cannot suddenly + * 'migrate' to a different timestamp type. + */ + if (use_blk_ts && BLK_TS_CHANGED(tde, shmti, active_bts->line.offset)) + scmi_telemetry_blkts_link(tde, active_bts); + /* * Parse data words ... only for the sake of updating TDE cache, * any error is logged by ignored. @@ -1625,6 +1939,9 @@ static int scmi_telemetry_tdcf_line_parse(struct tele= metry_info *ti, scmi_telemetry_tdcf_data_parse(ti, payld, snap, mode, *active_bts, *active_uuid); break; + case TDCF_BLK_TS_LINE: + scmi_telemetry_tdcf_blkts_parse(ti, payld, snap, active_bts); + break; default: trace_scmi_tlm_access(PAYLD_ID_RAW(payld), "TDCF_LINE_UNKNOWN", 0, 0); break; @@ -1765,6 +2082,15 @@ scmi_telemetry_shmti_refresh(struct telemetry_shmti = *shmti, int *idx) return work; } =20 +static inline void +scmi_telemetry_reset_active_lines(struct telemetry_block_ts **active_bts) +{ + if (*active_bts) { + scmi_telemetry_line_put(&(*active_bts)->line); + *active_bts =3D NULL; + } +} + /** * scmi_telemetry_shmti_scan - Full SHMTI scan * @ti: A reference to the telemetry info descriptor @@ -1832,6 +2158,7 @@ static int scmi_telemetry_shmti_scan(struct telemetry= _info *ti, qwords -=3D used_qwords; } =20 + scmi_telemetry_reset_active_lines(&active_bts); srcu_read_unlock(&shmti->srcu, idx); =20 if (ret) --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304657; b=GvR09Ty/71yuzS7jkWo+DZrytA424m2axn0+QAp5YEwmmaMkegn9isq1ltKxEPh+3jr1 xLDSYD+fDARG53jn6eMDP3hTEKYFaGNaKfIhGYaesP/1cPyHw7RP3RhTrp+gWJ15fqBSM HNFYsUfM/3dnLm/Rje3f1d2DbCNaL2XVj6YEKqD/rd4m4x+q8VAZLxRgbUtWvYThlqlPl PQaiqMeGLz5wSDWJNpRveKJB94fp/xHbSn3GzPubB8M/cCtmCmVEgyBQQ7i5Aep2G4cC3 4TDZIqm6kRk3tk5hfIxrpkMcyykcTaSjZyZpt+kpeaaDpvberuZeYpXBzCZd2operWg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304657; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=lfcRXial+jQGNgfn9F2fdEOcNt9oewQA/9nieollPlk=; b=wH4qiriHOT/nyM+4PioRjaPJFyNH2LVyJz3z1e8LKyZ/bFpnLX4nfEc/fOs4S6Ly6ReG glIbftr9YeBNMegSMQXJqeV2DhDEgNI3ok0Th9FipWuAJL4HlZnEGSRjD9rV0cj/lJiLA P9csY4J9iFbLe/5jmf76G0AHGgghra9Pl/h3gqp+xnpykRrSuiLXyS3EW0hiLUrIXcXxB ZGqpm0WOjsahwBBvLhjz+4DCNcJuEOyet8v1y9msc9Q9XukDTkbYWC5Aoi2ueX9fg1tFN fqwGghp1hbvllKmNhm3CgyF+roFoLfZNNL8a8toabxJ9ZZRlHpp9C68peDZmQ5XRx/g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=JYiBjEX0; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E94AB606A32C; Sat, 21 Nov 2026 23:44:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E94AB606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=JYiBjEX0 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 54899176C; Sat, 21 Nov 2026 15:44:13 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6C4773F632; Sat, 21 Nov 2026 15:44:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304656; bh=T3iEQ/pD3SmBj/6naVHDlk6QEKFb5K6kn5U5gVPoQZQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JYiBjEX0OI/8D1YkcN0SdAEyZeKFKFiS/WFmtJGKMJV+eUioktAFS0p4cBDzaQpFp X8VwevWv6C92tgZe/yYA1miqAR03TuSiIyPNahrf6shMFH09MgfsVkN2gQ/2rwPtMe 3C1wU7gKGt8BcriTraNttiSOPCV5bP9n86uoGpJM= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 16/31] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines Date: Sat, 21 Nov 2026 23:39:50 +0000 Message-ID: <20261121234005.1525419-17-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support to parse, track and link TDCF UUID lines to the related discovered DataEvents. Maintain also a central list of discovered UUID lines. Signed-off-by: Cristian Marussi --- v15 --> v16 - WARN_ON invalid UUID line - uuid array drop counted_by v14 --> v15 - use proper casting in tlm_collect - use snapshots to collect UUIDs during full scans - stop tracking UUIDs lines singularly..it's useless - use UUIDs line hashing for lookup - clarified UUID/DE binding are permanent - fixed WARN_ON invocation on missing active_uuid v13 --> v14 - split out of previous single patch implementing parse SHMTI routines --- drivers/firmware/arm_scmi/telemetry.c | 280 +++++++++++++++++++++++++- include/linux/scmi_protocol.h | 2 + 2 files changed, 280 insertions(+), 2 deletions(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index eea070fd0cd1..bb32db493bda 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -13,11 +13,14 @@ #include #include #include +#include +#include #include #include #include #include #include +#include #include #include #include @@ -172,6 +175,10 @@ struct tsline { __le32 ts_high; }; =20 +struct uuid_line { + __be32 dwords[SCMI_TLM_DE_IMPL_NUM_DWORDS]; +}; + #define LINE_DATA_GET_RAW(f) \ ({ \ typeof(f) _f =3D (f); \ @@ -270,6 +277,7 @@ struct payload { struct line l; struct tsline tsl; struct blk_tsline blk_tsl; + struct uuid_line uuid_l; }; }; =20 @@ -398,7 +406,6 @@ struct telemetry_block_ts { #define to_blkts(l) container_of(l, struct telemetry_block_ts, line) =20 struct telemetry_uuid { - u32 key; struct hlist_node node_ht; struct hlist_node node_lst; uuid_t uuid; @@ -459,6 +466,12 @@ struct telemetry_info { bool streaming_mode; unsigned int num_shmti; unsigned int num_des_tstamp; +#define SCMI_UUID_DB_THRESH 3 + /* Protect uuids_len */ + struct mutex uuids_mtx; + unsigned int uuids_len; +#define SCMI_UUID_HASH_SZ 5 + DECLARE_HASHTABLE(uuids_ht, SCMI_UUID_HASH_SZ); /* Mutex to protect des_enabled_mtx */ struct mutex des_enabled_mtx; atomic_t des_enabled[ENA_MAX]; @@ -467,6 +480,7 @@ struct telemetry_info { struct telemetry_shmti *shmti; struct telemetry_de *tdes; struct xarray xa_des; + struct telemetry_uuid *primary_uuid; /* Mutex to protect access to @free_des */ struct mutex free_mtx; struct list_head free_des; @@ -490,6 +504,9 @@ static inline struct tdcf *snap_tdcf(struct shmti_snap = *s); static int scmi_telemetry_shmti_scan(struct telemetry_info *ti, unsigned int shmti_id, enum scan_mode mode); =20 +static inline void scmi_telemetry_uuid_link(struct telemetry_de *tde, + struct telemetry_uuid *uuid); + static inline struct mutex *lock_for_payld(struct mutex *locks, unsigned long offset) { @@ -971,8 +988,10 @@ static int iter_de_descr_process_response(const struct= scmi_protocol_handle *ph, ti->num_des_tstamp +=3D !!tde->de.tstamp_support; =20 /* Add to FastChannels list */ - if (tde->de.fc_support) + if (tde->de.fc_support) { + scmi_telemetry_uuid_link(tde, ti->primary_uuid); list_add(&tde->item, &ti->fcs_des); + } =20 return 0; } @@ -1555,6 +1574,17 @@ static void scmi_telemetry_blkts_unlink(struct telem= etry_de *tde) trace_scmi_tlm_access(tde->de.info->id, "BLKTS_UNLINK", 0, 0); } =20 +static void scmi_telemetry_uuid_unlink(struct telemetry_de *tde) +{ + /* Acquiring order is: tde -> line */ + lockdep_assert_held(&tde->mtx); + if (!tde->uuid) + return; + + tde->uuid =3D NULL; + trace_scmi_tlm_access(tde->de.info->id, "UUID_UNLINK", 0, 0); +} + static void scmi_telemetry_de_unlink(struct scmi_telemetry_de *de) { struct telemetry_de *tde =3D to_tde(de); @@ -1563,6 +1593,7 @@ static void scmi_telemetry_de_unlink(struct scmi_tele= metry_de *de) =20 /* Unlink all related lines triggering their deallocation */ scmi_telemetry_blkts_unlink(tde); + scmi_telemetry_uuid_unlink(tde); } =20 static struct telemetry_line * @@ -1633,6 +1664,154 @@ scmi_telemetry_blkts_get_or_create(struct telemetry= _shmti *shmti, return scmi_telemetry_blkts_create(shmti, offset); } =20 +static int scmi_telemetry_uuids_update(struct telemetry_info *ti, + struct telemetry_uuid *uuid) +{ + uuid_t **cur_uuids; + + /* Resize array if needed ... */ + if (ti->info.num_uuids + SCMI_UUID_DB_THRESH >=3D ti->uuids_len) { + uuid_t **new_uuids, **old_uuids; + unsigned int new_len =3D ti->uuids_len * 2; + + new_uuids =3D kzalloc_objs(*new_uuids, new_len); + if (!new_uuids) + return -ENOMEM; + + old_uuids =3D rcu_dereference_protected(ti->info.uuids, + lockdep_is_held(&ti->uuids_mtx)); + /* Copy/move old allocated UUIDs */ + for (int i =3D 0; i < ti->info.num_uuids; i++) + new_uuids[i] =3D old_uuids[i]; + + /* Expose new UUID pointer */ + rcu_assign_pointer(ti->info.uuids, new_uuids); + ti->uuids_len =3D new_len; + + kfree_rcu_mightsleep(old_uuids); + } + + /* Store new UUID reference into the globally accessible UUIDS */ + cur_uuids =3D rcu_dereference_protected(ti->info.uuids, + lockdep_is_held(&ti->uuids_mtx)); + cur_uuids[ti->info.num_uuids] =3D &uuid->uuid; + rcu_assign_pointer(ti->info.uuids, cur_uuids); + /* Update UUIDs count */ + smp_store_release(&ti->info.num_uuids, ti->info.num_uuids + 1); + + return 0; +} + +static int scmi_telemetry_uuid_register(struct telemetry_info *ti, u32 key, + struct telemetry_uuid *uuid) +{ + int ret; + + lockdep_assert_held(&ti->uuids_mtx); + + ret =3D scmi_telemetry_uuids_update(ti, uuid); + if (ret) + return ret; + + /* Register by key for easy lookup */ + hash_add(ti->uuids_ht, &uuid->node_ht, key); + + trace_scmi_tlm_collect(0, key, 0, "SHMTI_NEW_UUID"); + + return 0; +} + +static struct telemetry_uuid * +scmi_telemetry_uuid_create(struct telemetry_info *ti, __be32 *dwords) +{ + u32 key; + int ret; + + lockdep_assert_held(&ti->uuids_mtx); + + struct telemetry_uuid *uuid __free(kfree) =3D kzalloc_obj(*uuid); + if (!uuid) + return NULL; + + /* + * Fetch BigEndian in-memory UUID fields as per SCMIv4.0 + * specification 3.12.2.2 + */ + import_uuid(&uuid->uuid, (__force const __u8 *)dwords); + key =3D jhash2((__force const u32 *)dwords, SCMI_TLM_DE_IMPL_NUM_DWORDS, = 1); + ret =3D scmi_telemetry_uuid_register(ti, key, uuid); + if (ret) + return NULL; + + return no_free_ptr(uuid); +} + +static struct telemetry_uuid * +scmi_telemetry_uuid_lookup(struct telemetry_info *ti, __be32 *dwords) +{ + struct telemetry_uuid *uuid; + u32 key; + + lockdep_assert_held(&ti->uuids_mtx); + + key =3D jhash2((__force const u32 *)dwords, SCMI_TLM_DE_IMPL_NUM_DWORDS, = 1); + hash_for_each_possible(ti->uuids_ht, uuid, node_ht, key) + if (!memcmp(&uuid->uuid, dwords, sizeof(uuid->uuid))) + return uuid; + + return NULL; +} + +static struct telemetry_uuid * +scmi_telemetry_uuid_get_or_create(struct telemetry_info *ti, __be32 *dword= s) +{ + struct telemetry_uuid *uuid; + + guard(mutex)(&ti->uuids_mtx); + + uuid =3D scmi_telemetry_uuid_lookup(ti, dwords); + if (uuid) + return uuid; + + return scmi_telemetry_uuid_create(ti, dwords); +} + +/** + * scmi_telemetry_tdcf_uuid_parse - A UUID line parser + * + * @ti: A reference to the telemetry_info descriptor + * @uuid_payld: UUID payld line to process + * @snap: A reference to the working SHMTI snapshot that will be processed + * @active_uuid: Input/output reference to keep track of the last uuid fou= nd + * + * Note that by the SCMI v4.0 Telemetry specification the associations bet= ween + * DEs and UUIDs, both primary and secondary, are permanent across the same + * session (boot): once discovered they cannot change. + * + * Moreover, since the current UUID line could be malformed due to garbage= in + * the TDCF, in such a case we warn/log and bailout BUT we don't throw away + * the currently active_uuid (if any) since the current malformed line cou= ld + * be bogus and never intended to be a UUID line. + */ +static void scmi_telemetry_tdcf_uuid_parse(struct telemetry_info *ti, + struct payload *uuid_payld, + struct shmti_snap *snap, + struct telemetry_uuid **active_uuid) +{ + __be32 *dwords =3D uuid_payld->uuid_l.dwords; + u32 meta =3D PAYLD_META_RAW(uuid_payld); + + if (WARN_ON(UUID_INVALID_RAW(meta))) { + trace_scmi_tlm_access(0, "UUID_INVALID", + snap->magic, snap->magic); + return; + } + + *active_uuid =3D scmi_telemetry_uuid_get_or_create(ti, dwords); + + WARN_ON(!*active_uuid); +} + /** * scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser * @@ -1754,6 +1933,18 @@ static inline void scmi_telemetry_blkts_link(struct = telemetry_de *tde, trace_scmi_tlm_access(tde->de.info->id, "BLKTS_LINK", 0, 0); } =20 +static inline void scmi_telemetry_uuid_link(struct telemetry_de *tde, + struct telemetry_uuid *uuid) +{ + lockdep_assert_held(&tde->mtx); + if (tde->uuid) + return; + + /* Update UUID association */ + tde->uuid =3D uuid; + trace_scmi_tlm_access(tde->de.info->id, "UUID_LINK", 0, 0); +} + /** * scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing * @ti: A reference to the telemetry info descriptor @@ -1877,6 +2068,9 @@ static void scmi_telemetry_tdcf_data_parse(struct tel= emetry_info *ti, if (use_blk_ts && BLK_TS_CHANGED(tde, shmti, active_bts->line.offset)) scmi_telemetry_blkts_link(tde, active_bts); =20 + /* Link the active UUID or the primary, it's unlinked on disable */ + scmi_telemetry_uuid_link(tde, active_uuid ?: ti->primary_uuid); + /* * Parse data words ... only for the sake of updating TDE cache, * any error is logged by ignored. @@ -1942,6 +2136,9 @@ static int scmi_telemetry_tdcf_line_parse(struct tele= metry_info *ti, case TDCF_BLK_TS_LINE: scmi_telemetry_tdcf_blkts_parse(ti, payld, snap, active_bts); break; + case TDCF_UUID_LINE: + scmi_telemetry_tdcf_uuid_parse(ti, payld, snap, active_uuid); + break; default: trace_scmi_tlm_access(PAYLD_ID_RAW(payld), "TDCF_LINE_UNKNOWN", 0, 0); break; @@ -2236,6 +2433,7 @@ static int scmi_telemetry_resources_alloc(struct tele= metry_info *ti) } =20 INIT_LIST_HEAD(&ti->fcs_des); + hash_init(ti->uuids_ht); =20 ti->shmti =3D no_free_ptr(shmtis); ti->info.shmtis =3D no_free_ptr(shmtis_info); @@ -2283,10 +2481,42 @@ static void scmi_telemetry_resources_free(struct te= lemetry_info *ti) dev_dbg(ti->ph->dev, "SCMI Telemetry resources freed for instance\n"); } =20 +static void scmi_telemetry_uuids_cleanup(struct telemetry_info *ti) +{ + uuid_t **cur_uuids; + + /* Drop reference to UUID lines kept in the growable array */ + scoped_guard(mutex, &ti->uuids_mtx) { + int num_uuids; + + /* Ensure looking up an updated counter */ + num_uuids =3D smp_load_acquire(&ti->info.num_uuids); + /* Ensure zeroing is visible */ + smp_store_release(&ti->info.num_uuids, 0); + /* Inhibit further accesses to cur_uuids */ + cur_uuids =3D rcu_replace_pointer(ti->info.uuids, NULL, + lockdep_is_held(&ti->uuids_mtx)); + synchronize_rcu(); + + for (int i =3D 0; i < num_uuids; i++) { + struct telemetry_uuid *uuid; + + uuid =3D to_uuid_from_uuid_t(cur_uuids[i]); + hash_del(&uuid->node_ht); + kfree(uuid); + + trace_scmi_tlm_access(0, "DROP_LINE", TDCF_UUID_LINE, 0); + } + } + + kfree_rcu_mightsleep(cur_uuids); +} + static void scmi_telemetry_instance_cleanup(void *arg) { struct telemetry_info *ti =3D arg; =20 + scmi_telemetry_uuids_cleanup(ti); scmi_telemetry_resources_free(ti); } =20 @@ -2357,6 +2587,23 @@ scmi_telemetry_resources_enumerate(struct telemetry_= info *ti) return ti->rinfo; } =20 +static int scmi_telemetry_primary_uuid_init(struct telemetry_info *ti) +{ + __be32 *dwords =3D (__be32 *)&ti->info.base.primary_revision.b; + struct telemetry_uuid *uuid; + + uuid =3D scmi_telemetry_uuid_get_or_create(ti, dwords); + if (!uuid) { + dev_err(ti->ph->dev, "Failed to register Primary UUID\n"); + return -ENOMEM; + } + + /* Ensure Primary UUID association is visible */ + smp_store_release(&ti->primary_uuid, uuid); + + return 0; +} + /** * scmi_telemetry_instance_init - Instance initializer * @ti: A reference to the telemetry info descriptor for this instance @@ -2372,12 +2619,41 @@ scmi_telemetry_resources_enumerate(struct telemetry= _info *ti) */ static int scmi_telemetry_instance_init(struct telemetry_info *ti) { + unsigned int uuids_len; + uuid_t **cur_uuids; int ret; =20 ret =3D scmi_telemetry_resources_alloc(ti); if (ret) return ret; =20 + /* + * Always allocate at least one slot for the primary and anyway at + * least enough to avoid immediate resizing, assuring uuids_len + * always greater or equal to one. + */ + uuids_len =3D max(ti->num_shmti * 2, SCMI_UUID_DB_THRESH + 1); + cur_uuids =3D kzalloc_objs(*cur_uuids, uuids_len); + if (!cur_uuids) { + scmi_telemetry_resources_free(ti); + return -ENOMEM; + } + + mutex_init(&ti->uuids_mtx); + /* Expose new UUID pointer */ + scoped_guard(mutex, &ti->uuids_mtx) { + rcu_assign_pointer(ti->info.uuids, cur_uuids); + ti->uuids_len =3D uuids_len; + } + + /* Initialize the first UUIDs in the global list with the primary */ + ret =3D scmi_telemetry_primary_uuid_init(ti); + if (ret) { + scmi_telemetry_uuids_cleanup(ti); + scmi_telemetry_resources_free(ti); + return ret; + } + atomic_set(&ti->des_enabled[ENA_STATE], 0); atomic_set(&ti->des_enabled[ENA_TSTAMP], 0); mutex_init(&ti->des_enabled_mtx); diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index cf097c135d39..55fecc17afe9 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -957,6 +957,8 @@ struct scmi_telemetry_info { unsigned int active_update_interval; struct scmi_telemetry_intervals *intervals; struct scmi_telemetry_shmti_info **shmtis; + unsigned int num_uuids; + uuid_t * __rcu *uuids; }; =20 /** --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304660; b=s72gb8R3qtL5YEm5g4NVfTXVUe1LFWIlDF5H9yVlU95le6u15tJC1CfndPSdyF2P0wub 1FdKEiFMqzQsO7Nqce5e0/JJ2uOjzOoRnHkjlcXBK7Xvt3BGoyKfl4mdqm6x/gK0asmlU lXPP9uD0iZSYLzs2q64tgN6xh2Pm2M8WGZPENmT4nn7AAR2jajnuQO/jHhVbc7ksRsA0q /lJxZ4wMNlY1zRkjQ88gix62sr62R7qDz67knlk1liFHkLQ2BFTAOUDyGKdI1RmMMLbm7 kPsuep6EHuTIABWKs07iimh1JUZKfJoplQIwqsfWzR+imr6FPB5FarTKTjgMhoyFtNg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304660; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=634vqp2NB8PJ9DiTUhWI4kIVVOb2M4OWmtmj8nljax0=; b=gUD3tSYG+mCKJO3tQQ1mUS4Zw0x7n0myxce70ZmygZMPYJoMv0Rkbz9W7EXik6o52DXf cnijNDnJSQO3QSocL8SJqTgxwH8EU6moCRTGn64bxJargY1vP9qcI5ESLA55VMxgY+2iS Y7/7TUi7wh8FQFrvAsqqvFmY358xSovJaA0zQYs7Qe9YFlC7+rwa2m101KEpVmcfIpX1P g5fwNnuBvmbmam1eZJuxw5SEhUtX78p4emd0/w9LhSdHkpn8UdFoSTBSeCe6WX50W7G8A OWCxhGt8TZSLPsH+0Qb6MDC3ENFKNMHbcsivwbkYU9GDGRaUNUJE4UoJ6tkUVYUJ0sg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=luN0yZBc; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F1204606A680; Sat, 21 Nov 2026 23:44:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F1204606A680 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=luN0yZBc 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 75F481575; Sat, 21 Nov 2026 15:44:16 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 87D9C3F632; Sat, 21 Nov 2026 15:44:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304659; bh=kL62an8xJPpQq+eJl9qme4AuA21W/Yz3IfducugKsXs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=luN0yZBcoNDsQf4GAO1/rxW/c7kzScQumq6jdGMEascWObtKrX/mSl7PVSjKCmXOZ qK+txOsyloiaQsvQzwkJ9DPTjsJhr5MtlH8WFUg06S5ko4b+reqe7SnscRJhZuZ4X5 piBzfIMTqKXPRjVxLj9YOSEkWOEypwY5PqkfNpd0= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 17/31] firmware: arm_scmi: Add Telemetry DEs configuration support Date: Sat, 21 Nov 2026 23:39:51 +0000 Message-ID: <20261121234005.1525419-18-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for basic Telemetry DEs configuration operations to selectively enable or disable DataEvents monitoring. Signed-off-by: Cristian Marussi --- v15 --> v16 - split out group config support - split out global collection configure - check liveness on blkts_snapshot too - refatcor blkts_update on DE enable and update de.ts_rate even when BLK_TS line is the same - on disable do NOT assume SHMTI_ID_INVALID means disable - fix remote_update_needed logic - invalidate cache on DE disable - invalidate base/offset on disable if NOT FC - use new shmti_overflow with weplg directly v14 --> v15 - unlink BLK_TS on DE disable - update linked BLK_TS on enable - update TDE location ONLY after successfull linked lines processing - skip tstamp state update when tstamp NOT supported - fix TDE location returned value on error path - trigger resource enumeration on state_get - trigger resource enumeration on colletion configure only for groups - use rinfo->num_groups to check groups sanity - fix per-group collection_configure calls - fix streaming_mode variable handling - widen tde->mtx locking section on state_set - refactor UUID/BLK_TS lines processing in enable - introduce BLK_TS localized snapshot during enable - check line type at de_offs - sanitize requested mode on collection conf - missing err init - moved away definition of scmi_telemetry_payload_offset_safe_calc v13 --> v14 - move BLK_ST_CHANGED check when bts_payld is really known - check lso LINE_TYPE when processing BLK_TS on enable - grab individual tde->mtx while updating group state - clear returned sid/offset/uid on error path - do NOT mutate global config when configuring groups - call state_update on do_xfer success even if response process fails - moved some definitions from earlier split patch - renamed some fields to more strictly adhere to spec nomenclature v12 --> v13 - refactored cleanup path - generalize payload_offset_safe_calc - harden nearest_line_type scanner - full refactoring of SET/GET helpers: keep DE and group in distinct code path - relocate tde->mtx guards higher up and add lockdep assertions - cleanup xa_lines under mutex_lock on cleanup - unlink stale BTS line when finding a new actibe for the TDE - use *alloc_objs v11 --> v12 - added a few locking order acquisition comments - added more checks on wraparounds and alignment - added proper locking around TDE access on state_get - added a config_mtx to serialize configuration accesses between collection configure and state_set - refactored state_cache_update - added locking around sid/offset/uuid updates on enable v10 --> v11 - refactor reply processing code on DE enable/disable - use the new des_ennabled mutex to protect des cache updates - clear sid on group disable for each composing DEs - add dev_warn_once on ZERO blk_ts offset v8 --> v9 - use new scmi_telemetry_shmti_overflow() inline - review DE offsets boundary and wraparound checks (Sashiko v8 - update per-DE ts_rate for DE using BLK_TS too v7 --> v8 - Fixes for some Sashiko issues on v7 - re-add functions unused in previous patch - add proper barriers to TDE/UUID binding process - update tde->sid on SCAN_UPDATE too - check wrap-arounds on shmti_de_offset manipulation - fix GROUP enable pollutes status update (was polluting global state) - remove .all_disable telemetry_operation since unused - add WARN on if a valid SHMTI ID is reported on enabling a FastChannel/DE v6 --> v7 - expose per-DE tracked sid/offset/UUIDs - trigger a full SHMTI scan when a SHMTI-based is enabled and it still has= NO associated UUID v5 --> v6 - constify a few booleans used as inpurt in telemetry_operations - fixes (some) Sashiko reviews https://sashiko.dev/#/patchset/20260703123601.381275-1-cristian.marussi%= 40arm.com?part=3D9 - underflow on de_offs - SHMTI overflow checks - bail out on failed group scan - unlink comprising DEs on group disable - add boudnary check on message response parsing v4 --> v5 - use renamed grp_id instead of old id in group descriptors - remove useless grp_ignore param from collection_configure - fixes sparse warnings v3 --> v4 - track configured Telemetry DE states - add aggregate Telemetry state query v2 --> v3 - split from monolithic Telemetry patch - simplify clenaup with scmi_telemetry_de_unlink --- drivers/firmware/arm_scmi/telemetry.c | 689 +++++++++++++++++++++++++- include/linux/scmi_protocol.h | 11 + 2 files changed, 698 insertions(+), 2 deletions(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index bb32db493bda..1453adee393c 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -133,6 +133,49 @@ struct scmi_msg_resp_telemetry_update_intervals { __le32 intervals[]; }; =20 +struct scmi_msg_telemetry_de_configure { + __le32 identifier; + __le32 flags; +#define CFG_ENABLE_NO_TSTAMP BIT(0) +#define CFG_ENABLE_WTH_TSTAMP BIT(1) +#define CFG_DISABLE_ALL BIT(2) +#define CFG_GROUP_SELECTOR BIT(3) +#define CFG_EVENT_DE 0 +#define CFG_EVENT_GROUP 1 +#define CFG_DISABLE_ONE 0x0 +}; + +struct scmi_msg_resp_telemetry_de_configure { + __le32 shmti_id; +#define SHMTI_ID_INVALID GENMASK(31, 0) +#define IS_SHMTI_ID_VALID(x) ((x) !=3D SHMTI_ID_INVALID) + __le32 shmti_de_offset; + __le32 blk_ts_offset; +}; + +struct scmi_msg_telemetry_config_set { + __le32 group_identifier; + __le32 control; +#define TELEMETRY_ENABLE (BIT(0)) +#define TELEMETRY_MODE GENMASK(4, 1) +#define TELEMETRY_MODE_SET(x) (FIELD_PREP(TELEMETRY_MODE, (x))) +#define TLM_ONDEMAND (0) +#define TLM_NOTIFS (1) +#define TLM_SINGLE (2) +#define TELEMETRY_MODE_ONDEMAND TELEMETRY_MODE_SET(TLM_ONDEMAND) +#define TELEMETRY_MODE_NOTIFS TELEMETRY_MODE_SET(TLM_NOTIFS) +#define TELEMETRY_MODE_SINGLE TELEMETRY_MODE_SET(TLM_SINGLE) + +#define TLM_ORPHANS (0) +#define TLM_GROUP (1) +#define TLM_ALL (2) +#define TELEMETRY_SET_SELECTOR(x) (FIELD_PREP(GENMASK(8, 5), (x))) +#define TELEMETRY_SET_SELECTOR_ORPHANS TELEMETRY_SET_SELECTOR(TLM_ORPHANS) +#define TELEMETRY_SET_SELECTOR_GROUP TELEMETRY_SET_SELECTOR(TLM_GROUP) +#define TELEMETRY_SET_SELECTOR_ALL TELEMETRY_SET_SELECTOR(TLM_ALL) + __le32 sampling_rate; +}; + /* TDCF */ =20 #define _I(__a) (ioread32((void __iomem *)(__a))) @@ -424,8 +467,6 @@ enum timestamps { TSTAMP_BLK =3D 2, }; =20 -#define SHMTI_ID_INVALID GENMASK(31, 0) - struct telemetry_de { enum timestamps ts_type; u32 ts_rate; @@ -485,6 +526,8 @@ struct telemetry_info { struct mutex free_mtx; struct list_head free_des; struct list_head fcs_des; + /* Protect global and per-group configuration data */ + struct mutex config_mtx; struct scmi_telemetry_info info; atomic_t rinfo_initializing; struct completion rinfo_initdone; @@ -1812,6 +1855,70 @@ static void scmi_telemetry_tdcf_uuid_parse(struct te= lemetry_info *ti, WARN_ON(!*active_uuid); } =20 +static struct payload __iomem * +scmi_telemetry_nearest_line_by_type(struct telemetry_shmti *shmti, + void __iomem *last, enum tdcf_line_types ltype) +{ + struct tdcf __iomem *tdcf =3D shmti->base; + void __iomem *next, *found =3D NULL; + + /* Scan from start of the first TDCF payload up to last payld */ + next =3D tdcf->payld; + while (next < last) { + struct payload __iomem *payld; + enum tdcf_line_types cur_type; + u32 meta, offs; + + if (check_sub_overflow((__force unsigned long)next, + (__force unsigned long)tdcf, &offs)) { + trace_scmi_tlm_access(ltype, "LINE_OFFSET_OUT_OF_RANGE", 0, 0); + return NULL; + } + + payld =3D scmi_telemetry_payld_offset_safe_calc(shmti, offs); + if (IS_ERR(payld)) { + trace_scmi_tlm_access(ltype, "LINE_PAYLD_MALFORMED", 0, 0); + return NULL; + } + + meta =3D PAYLD_META(payld); + cur_type =3D LINE_TYPE_RAW(meta); + + /* + * Bail-out on malformed lines, since the whole TDCF scan + * could be affected. + */ + if ((cur_type =3D=3D TDCF_BLK_TS_LINE && BLK_TS_INVALID_RAW(meta)) || + (cur_type =3D=3D TDCF_UUID_LINE && UUID_INVALID_RAW(meta))) { + trace_scmi_tlm_access(ltype, "LINE_TYPE_MALFORMED", 0, 0); + return NULL; + } + + if (cur_type =3D=3D ltype) + found =3D next; + + next +=3D LINE_LENGTH_BYTES_RAW(meta); + } + + return found; +} + +static struct telemetry_block_ts * +scmi_telemetry_blkts_bind(struct telemetry_info *ti, + struct telemetry_shmti *shmti, + struct payload *bts_payld, + unsigned long offset, u32 magic) +{ + struct telemetry_block_ts *bts; + + bts =3D scmi_telemetry_blkts_get_or_create(shmti, offset); + /* Update the descriptor with the lastest TS */ + if (bts) + scmi_telemetry_blkts_update_snap(ti, bts, bts_payld, magic); + + return bts; +} + /** * scmi_telemetry_tdcf_blkts_parse - A BLK_TS line parser * @@ -1945,6 +2052,32 @@ static inline void scmi_telemetry_uuid_link(struct t= elemetry_de *tde, trace_scmi_tlm_access(tde->de.info->id, "UUID_LINK", 0, 0); } =20 +static struct telemetry_uuid * +scmi_telemetry_uuid_bind(struct telemetry_info *ti, + struct telemetry_shmti *shmti, + struct payload __iomem *payld) +{ + __be32 dwords[SCMI_TLM_DE_IMPL_NUM_DWORDS]; + struct payload __iomem *uuid_payld; + struct uuid_line __iomem *uuid_l; + + uuid_payld =3D scmi_telemetry_nearest_line_by_type(shmti, payld, + TDCF_UUID_LINE); + if (!uuid_payld) + return NULL; + + /* + * Use proper mem accessors BUT no swapping, maintain UUID + * in memory BE layout. + */ + uuid_l =3D &uuid_payld->uuid_l; + for (int i =3D 0; i < SCMI_TLM_DE_IMPL_NUM_DWORDS; i++) + dwords[i] =3D (__force __be32)__raw_readl(&uuid_l->dwords[i]); + + /* Here the nearest UUID line just found has been already validated */ + return scmi_telemetry_uuid_get_or_create(ti, dwords); +} + /** * scmi_telemetry_tdcf_data_parse - TDCF DataLine parsing * @ti: A reference to the telemetry info descriptor @@ -2364,10 +2497,561 @@ static int scmi_telemetry_shmti_scan(struct teleme= try_info *ti, return ret; } =20 +static struct payload __iomem * +scmi_telemetry_blkts_payld_locate(struct device *dev, + struct telemetry_shmti *shmti, u32 *bts_offs, + struct payload __iomem *payld, u32 de_id) +{ + struct payload __iomem *bts_payld =3D NULL; + + if (!bts_offs) + return IOMEM_ERR_PTR(-EINVAL); + + if (*bts_offs && + check_add_overflow((__force unsigned long)shmti->base, *bts_offs, + (__force unsigned long *)&bts_payld)) + return IOMEM_ERR_PTR(-EPROTO); + + /* Check SHMTI overflow on returned offset */ + if (bts_payld) { + if (scmi_telemetry_shmti_overflow(SHMTI_EPLG(shmti), bts_payld)) { + trace_scmi_tlm_access(de_id, "SHMTI_OVERFLOW", 0, 0); + return IOMEM_ERR_PTR(-EINVAL); + } + } else { + dev_warn_once(dev, "Received BLK_TS with zero offset.\n"); + trace_scmi_tlm_access(de_id, "BLKTS_WITH_ZERO_OFFSET", 0, 0); + + /* + * Find the BLK_TS immediately preceding this DE payld by + * scanning. + */ + bts_payld =3D scmi_telemetry_nearest_line_by_type(shmti, payld, + TDCF_BLK_TS_LINE); + if (WARN_ON(!bts_payld)) + return IOMEM_ERR_PTR(-EPROTO); + + /* Calculate offset of found blkts */ + *bts_offs =3D (void __iomem *)bts_payld - shmti->base; + } + + return bts_payld; +} + +static int +scmi_telemetry_tdcf_bts_payld_check(struct payload *bts_snap, u32 de_id) +{ + u32 meta =3D PAYLD_META_RAW(bts_snap); + + if (!IS_BLK_TS_LINE_RAW(meta)) { + trace_scmi_tlm_access(de_id, "BAD_BLK_TS_LINE_OFFSET", 0, meta); + return -EPROTO; + } + + /* Check for spec compliance */ + if (BLK_TS_INVALID_RAW(meta)) { + trace_scmi_tlm_access(de_id, "BLK_TS_INVALID", 0, meta); + return -EINVAL; + } + + return 0; +} + +static int scmi_telemetry_blkts_snapshot(struct telemetry_shmti *shmti, + struct payload __iomem *bts_payld, + struct payload *bts_snap, u32 *magic, + u32 de_id) +{ + int retries =3D SCMI_TLM_TDCF_MAX_RETRIES; + struct tdcf __iomem *tdcf =3D shmti->base; + u32 startm, endm, live; + int err =3D 0; + + do { + /* A bit of exponential backoff between retries */ + fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP_U= S); + + startm =3D TDCF_START_SEQ_GET(tdcf); + /* Build a default endm certainly different from startm */ + endm =3D startm + 1; + if (IS_BAD_START_SEQ(startm)) { + trace_scmi_tlm_access(de_id, "MSEQ_BADSTART", startm, 0); + continue; + } + + memcpy_fromio(bts_snap, bts_payld, LINE_LENGTH_BYTES(bts_payld)); + /* + * In this context any anomaly detected on the payload could + * due to a torn read, and it is worth retrying a snapshot. + */ + err =3D scmi_telemetry_tdcf_bts_payld_check(bts_snap, de_id); + if (err) + continue; + + /* Assure the snapshot has been fully taken before checking seqnum */ + rmb(); + + /* Used to detect changes while copying */ + live =3D TDCF_START_SEQ_GET(tdcf); + + endm =3D TDCF_END_SEQ_GET(SHMTI_EPLG(shmti)); + if (startm !=3D endm || startm !=3D live) + trace_scmi_tlm_access(de_id, "MSEQ_MISMATCH", startm, endm); + } while ((startm !=3D endm || err) && --retries); + + if (startm !=3D endm || err) { + trace_scmi_tlm_access(de_id, "TDCF_SNAPSHOT_BLKTS_FAIL", startm, endm); + return err ?: -EPROTO; + } + + /* Ensure any snapshot is visible before updating magic */ + smp_store_release(magic, startm); + + trace_scmi_tlm_access(de_id, "TDCF_SNAPSHOT_BLKTS_OK", startm, endm); + + return 0; +} + +static struct telemetry_block_ts * +scmi_telemetry_blkts_update(struct telemetry_info *ti, + struct telemetry_shmti *shmti, + struct payload __iomem *bts_payld, + u32 bts_offs, u32 de_id) +{ + struct telemetry_block_ts *bts; + struct payload bts_snap =3D {}; + u32 magic =3D 0; + int ret; + + ret =3D scmi_telemetry_blkts_snapshot(shmti, bts_payld, &bts_snap, + &magic, de_id); + if (ret) + return ERR_PTR(ret); + + /* Look for a viable BLK_TS line */ + bts =3D scmi_telemetry_blkts_bind(ti, shmti, &bts_snap, bts_offs, magic); + if (WARN_ON(!bts)) { + trace_scmi_tlm_access(de_id, "INVALID_USE_BLK_TS", 0, 0); + return ERR_PTR(-EPROTO); + } + + return bts; +} + +static int +scmi_telemetry_tde_linked_lines_process(struct telemetry_info *ti, + struct telemetry_de *tde, + struct telemetry_shmti *shmti, + struct payload __iomem *payld, + u32 bts_offs) +{ + struct payload __iomem *bts_payld; + + lockdep_assert_held(&ti->config_mtx); + lockdep_assert_held(&tde->mtx); + + /* + * On enable, if a UUID was NOT already assigned, scan looking for + * possible Secondary UUID to bind: if NO secondary could be found + * embedded in the preceding SHMTI lines, install the primary UUID + * for this DE. + * NOTE that UUID lines content is immutable once created and no + * concurrent configuration request is possible at this point since + * config_mtx is held, so while the platform can still concurrently + * update all the existing SHMTI lines variable values, existing lines + * CANNOT be shuffled around: as a consequence a full snapshot is NOT + * needed as a pre-requisite to perform a scan when looking for + * UUID lines. + */ + if (!tde->uuid) { + struct telemetry_uuid *uuid; + + uuid =3D scmi_telemetry_uuid_bind(ti, shmti, payld); + if (uuid) + tde->uuid =3D uuid; + else + scmi_telemetry_uuid_link(tde, ti->primary_uuid); + } + + /* Process related BLK_TS lines if any */ + if (!USE_BLK_TS(payld)) { + /* Unlink old stale BTS if existent */ + if (IS_TDE_TS_BLK(tde) && tde->bts) + scmi_telemetry_blkts_unlink(tde); + return 0; + } + + if (!IS_TDE_TS_BLK(tde)) { + trace_scmi_tlm_access(tde->de.info->id, "BAD_DE_TSTAMP_TYPE", 0, 0); + return -EPROTO; + } + + bts_payld =3D scmi_telemetry_blkts_payld_locate(ti->ph->dev, shmti, + &bts_offs, payld, + tde->de.info->id); + if (IS_ERR(bts_payld)) + return PTR_ERR(bts_payld); + + /* Has associated BLK_TS line changed at all ? */ + if (BLK_TS_CHANGED(tde, shmti, bts_offs)) { + struct telemetry_block_ts *bts; + + bts =3D scmi_telemetry_blkts_update(ti, shmti, bts_payld, + bts_offs, tde->de.info->id); + if (IS_ERR(bts)) + return PTR_ERR(bts); + + /* Unlink old stale BTS if existent */ + scmi_telemetry_blkts_unlink(tde); + + /* Store freshly discovered sane BLK_TS */ + tde->bts =3D bts; + } + + /* Update per-DE BLK_TS rate if any */ + if (tde->bts->last_rate) + tde->de.info->ts_rate =3D tde->bts->last_rate; + + return 0; +} + +static int +scmi_telemetry_state_set_tde_resp(struct telemetry_info *ti, + struct scmi_telemetry_de *de, + u32 sid, u32 de_offs, u32 bts_offs) +{ + struct telemetry_de *tde =3D to_tde(de); + struct telemetry_shmti *shmti; + struct payload __iomem *payld; + int ret; + + lockdep_assert_held(&tde->mtx); + + /* A FastChannel DE MUST return an INVALID SHMTI ID on enable */ + if (WARN_ON(de->fc_support)) { + trace_scmi_tlm_access(de->info->id, "INVALID_DE_FC_ON_SHMTI", 0, 0); + return -EPROTO; + } + + /* Update SHMTI/offset while skipping non-SHMTI based DES */ + shmti =3D &ti->shmti[sid]; + /* Check sanity of received location... */ + payld =3D scmi_telemetry_payld_offset_safe_calc(shmti, de_offs); + if (IS_ERR(payld)) + return PTR_ERR(payld); + + /* + * Note that here we are processing a configuration request on a DE + * while holding @config_mtx: in this scenario platform cannot relocate + * or mutate this line freely, so we can directly access the TDCF and + * lookup the line sitting at @de_offs to check its line type without + * having to worry about consistency or taking snapshots. + */ + if (!IS_DATA_LINE(payld)) + return -EPROTO; + + ret =3D scmi_telemetry_tde_linked_lines_process(ti, tde, shmti, payld, + bts_offs); + if (ret) + return ret; + + /* ...then assign once validated */ + tde->sid =3D sid; + tde->base =3D shmti->base; + tde->offset =3D de_offs; + /* A handy reference to the Epilogue updated */ + tde->eplg =3D SHMTI_EPLG(shmti); + + return 0; +} + +static int +scmi_telemetry_tde_state_set_resp_process(struct telemetry_info *ti, + struct scmi_telemetry_de *de, + struct scmi_msg_resp_telemetry_de_configure *r) +{ + u32 sid =3D le32_to_cpu(r->shmti_id); + + /* + * 3.12.4.8 TELEMETRY_DE_CONFIGURE - SHMTI ID + * This field is optional and is set to 0xFFFFFFFF if any of the + * following are true: + * - no DE or event group is being enabled. + * - No SHMTI is supported. + * - SHMTI is not used for this DE or event group. This could + * be the case for example, if this DE is associated with a + * FastChannel. + * - the platform does not support returning this information. + */ + if (IS_SHMTI_ID_VALID(sid)) { + u32 de_offs, bts_offs; + + if (sid >=3D ti->num_shmti) + return -EPROTO; + + de_offs =3D le32_to_cpu(r->shmti_de_offset); + bts_offs =3D le32_to_cpu(r->blk_ts_offset); + if (!IS_ALIGNED(de_offs, 4) || !IS_ALIGNED(bts_offs, 4)) { + dev_err(ti->ph->dev, + "Returned DE/BTS offsets are NOT 32-bit aligned - de_offs:%X bts_offs= :%X\n", + de_offs, bts_offs); + return -EINVAL; + } + + return scmi_telemetry_state_set_tde_resp(ti, de, sid, + de_offs, bts_offs); + } else if (!de->enabled) { + /* + * Unlink the related BLK_TS lines on disable and update + * sid/offset, unless unsupported by platform or the + * current DE flavour (FCs/Notifs) + * UUID is never unlinked once discovered. + */ + struct telemetry_de *tde =3D to_tde(de); + + lockdep_assert_held(&tde->mtx); + + tde->sid =3D SHMTI_ID_INVALID; + if (!tde->de.fc_support) { + tde->base =3D 0; + tde->offset =3D 0; + tde->eplg =3D 0; + + scmi_telemetry_blkts_unlink(tde); + } + + + /* Consider that a TDE could be re-located on next re-enable */ + scmi_telemetry_tde_cache_invalidate(tde); + } + + return 0; +} + +static void +scmi_telemetry_state_cache_update(struct telemetry_info *ti, + const bool *enable, bool *enabled_state, + const bool *tstamp, bool *tstamp_enabled_state, + struct scmi_telemetry_de *de) +{ + if (!de) + return; + + if (enable) + scmi_telemetry_de_state_update(ti, ENA_STATE, + enabled_state, *enable); + + if (tstamp && de->tstamp_support) + scmi_telemetry_de_state_update(ti, ENA_TSTAMP, + tstamp_enabled_state, *tstamp); +} + +static inline bool +scmi_telemetry_remote_state_update_needed(struct telemetry_info *ti, + const bool *enable, bool *enabled_state, + const bool *tstamp, + bool *tstamp_enabled_state, + bool is_group, void *obj) +{ + /* Is anything to do at all on this object ? */ + if ((!enable || *enable =3D=3D *enabled_state) && + (!tstamp || *tstamp =3D=3D *tstamp_enabled_state)) + return false; + + /* + * When the object is currently disabled AND no enable state change was + * requested, BUT timestamp is being changed, update only local state... + * no need to send a message now, it will be sent later on enable. + */ + if ((!enable || !*enable) && !*enabled_state) { + scmi_telemetry_state_cache_update(ti, enable, + enabled_state, tstamp, + tstamp_enabled_state, obj); + + return false; + } + + return true; +} + +static inline void +scmi_telemetry_state_message_prepare(struct scmi_msg_telemetry_de_configur= e *msg, + unsigned int id, bool is_group, + const bool *enable, bool *enabled_state, + const bool *tstamp, bool *tstamp_enabled_state) +{ + msg->identifier =3D cpu_to_le32(id); + /* Default to disable mode */ + msg->flags =3D CFG_DISABLE_ONE; + msg->flags |=3D cpu_to_le32(FIELD_PREP(GENMASK(3, 3), + !is_group ? CFG_EVENT_DE : CFG_EVENT_GROUP)); + + if ((!enable && *enabled_state) || (enable && *enable)) { + /* Already enabled but tstamp_enabled state changed */ + if (tstamp) { + /* Here, tstamp cannot be NULL too */ + msg->flags |=3D cpu_to_le32(*tstamp ? + CFG_ENABLE_WTH_TSTAMP : CFG_ENABLE_NO_TSTAMP); + } else { + msg->flags |=3D cpu_to_le32(*tstamp_enabled_state ? + CFG_ENABLE_WTH_TSTAMP : CFG_ENABLE_NO_TSTAMP); + } + } +} + +static inline void +scmi_telemetry_tde_location_update(struct telemetry_de *tde, + u32 *sid, u32 *offset, uuid_t *uuid) +{ + if (tde) + lockdep_assert_held(&tde->mtx); + + if (sid) + *sid =3D tde ? tde->sid : SHMTI_ID_INVALID; + if (offset) + *offset =3D tde ? tde->offset : 0; + if (uuid) { + if (tde && tde->uuid) + uuid_copy(uuid, &tde->uuid->uuid); + else + memset(uuid, 0x0, sizeof(*uuid)); + } +} + +static int scmi_telemetry_tde_state_set(const struct scmi_protocol_handle = *ph, + const bool *enable, const bool *tstamp, + struct scmi_telemetry_de *de, + u32 *sid, u32 *offset, uuid_t *uuid) +{ + struct scmi_msg_resp_telemetry_de_configure *resp; + struct scmi_msg_telemetry_de_configure *msg; + struct telemetry_info *ti =3D ph->get_priv(ph); + struct telemetry_de *tde =3D to_tde(de); + struct scmi_xfer *t; + int ret; + + /* Stall concurrent configuration request */ + guard(mutex)(&ti->config_mtx); + + /* + * Note that the TDE lock must be taken early on, before the SCMI + * configuration command is even issued to avoid a de-sync between the + * TDE state and the effective HW state enforced by the platform in + * response to our request. + */ + guard(mutex)(&tde->mtx); + + /* Initialize return values with current TDE in case of error */ + scmi_telemetry_tde_location_update(tde, sid, offset, uuid); + + if (!scmi_telemetry_remote_state_update_needed(ti, enable, &de->enabled, + tstamp, &de->tstamp_enabled, + false, de)) + return 0; + + ret =3D ph->xops->xfer_get_init(ph, TELEMETRY_DE_CONFIGURE, + sizeof(*msg), sizeof(*resp), &t); + if (ret) + return ret; + + msg =3D t->tx.buf; + scmi_telemetry_state_message_prepare(msg, de->info->id, false, + enable, &de->enabled, + tstamp, &de->tstamp_enabled); + + resp =3D t->rx.buf; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + /* + * Update cached state on command success anyway, despite any + * possible later response validation issue: once the command + * has succedeed keep our state in sync with HW. + */ + scmi_telemetry_state_cache_update(ti, enable, &de->enabled, + tstamp, &de->tstamp_enabled, de); + /* Process response, update DEs location tracking */ + ret =3D scmi_telemetry_tde_state_set_resp_process(ti, de, resp); + if (!ret) + scmi_telemetry_tde_location_update(tde, sid, offset, uuid); + else + dev_err(ph->dev, + "Could NOT process DE ID 0x%08X state response - ret:%d\n", + tde->de.info->id, ret); + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + +static int scmi_telemetry_state_get(const struct scmi_protocol_handle *ph, + const u32 *id, bool *enabled, bool *tstamp_enabled, + u32 *sid, u32 *offset, uuid_t *uuid) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + struct scmi_telemetry_de *de; + struct telemetry_de *tde; + + if (!enabled || !tstamp_enabled) + return -EINVAL; + + ti->res_get(ti); + if (!id) { + guard(mutex)(&ti->des_enabled_mtx); + /* Returning the all_des_* state */ + *enabled =3D + (atomic_read(&ti->des_enabled[ENA_STATE]) =3D=3D ti->info.base.num_des); + *tstamp_enabled =3D + (atomic_read(&ti->des_enabled[ENA_TSTAMP]) =3D=3D ti->num_des_tstamp); + + return 0; + } + + de =3D xa_load(&ti->xa_des, *id); + if (!de) + return -ENODEV; + + tde =3D to_tde(de); + guard(mutex)(&tde->mtx); + + *enabled =3D de->enabled; + *tstamp_enabled =3D de->tstamp_enabled; + + scmi_telemetry_tde_location_update(tde, sid, offset, uuid); + + return 0; +} + +static int scmi_telemetry_state_set(const struct scmi_protocol_handle *ph, + bool is_group, u32 id, const bool *enable, + const bool *tstamp, u32 *sid, u32 *offset, + uuid_t *uuid) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + + ti->res_get(ti); + if (!is_group) { + struct scmi_telemetry_de *de; + + de =3D xa_load(&ti->xa_des, id); + if (!de) + return -ENODEV; + + /* Filter out unplausible timestamp requests */ + return scmi_telemetry_tde_state_set(ph, enable, + de->tstamp_support ? tstamp : NULL, + de, sid, offset, uuid); + } + + return 0; +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, .res_get =3D scmi_telemetry_resources_get, + .state_get =3D scmi_telemetry_state_get, + .state_set =3D scmi_telemetry_state_set, }; =20 /** @@ -2440,6 +3124,7 @@ static int scmi_telemetry_resources_alloc(struct tele= metry_info *ti) =20 ti->tdes =3D no_free_ptr(tdes); xa_init(&ti->xa_des); + mutex_init(&ti->config_mtx); =20 rinfo->des =3D no_free_ptr(des); rinfo->dei_store =3D no_free_ptr(dei_store); diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 55fecc17afe9..c111b8ff87db 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -968,6 +968,10 @@ struct scmi_telemetry_info { * @info_get: get the general Telemetry information. * @de_lookup: get a specific DE descriptor from the DE id. * @res_get: get a reference to the Telemetry resources descriptor. + * @state_get: retrieve the specific DE state, if NULL returns the + * cumulative state of all DEs. Global group state can be looked-up + * directly from @scmi_telemetry_info via @info_get. + * @state_set: enable/disable the specific DE or GROUP with or without tim= estamps. */ struct scmi_telemetry_proto_ops { const struct scmi_telemetry_info *(*info_get) @@ -976,6 +980,13 @@ struct scmi_telemetry_proto_ops { (const struct scmi_protocol_handle *ph, u32 id); const struct scmi_telemetry_res_info *(*res_get) (const struct scmi_protocol_handle *ph); + int (*state_get)(const struct scmi_protocol_handle *ph, + const u32 *id, bool *enabled, bool *tstamp_enabled, + u32 *sid, u32 *offset, uuid_t *uuid); + int (*state_set)(const struct scmi_protocol_handle *ph, + bool is_group, u32 id, const bool *enable, + const bool *tstamp, u32 *sid, u32 *offset, + uuid_t *uuid); }; =20 /** --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304663; b=QFvetTMFhII/CxaBEk1KH4FJN+W7fdK5fsWm1CmZcTVWs41WE4ipsBcH3kqV8wGOXNzX wP9zB4KMQ3GpP0s9VhKQ8WtduVNPw1d1/7siHclDl3caq+8kOxesTNV9NfTSFaNRsUrHH PRmnktco5BysqpsmPnSaoxkRAIduUgW3nswkjJk9yfLt7ipsZTbOc0vpzz+2KpcW+U60f fbyNs83CA79+s1O08K3+Rb6PFdU8qcBCL2fGtYbClkDH5bt492LLZFoSWAfLQNNRYWHH/ +w9T3wq5aqalgI71wikahVOUV22TqrqzMcwmHFvcHT1L5Z84yCg8CxhFJHsJ2cZQiGQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304663; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=1ks1qtON9t7AUvsQWNFxu5MU0gY/VQveKsMo3JPBMgg=; b=SgRWOS2cj7iUweBryibZfMT6bmiACHBJK6ogr4/QdwVUlIgjbendIiJyt/YBiM8amiJV qc8vFDHnDB5cigJg9ZsuSKGZc7CqUaP5pPHwie4yd7vRZEhjVOZMNywaSMRmpt035RSvq gAfqDyap2KTvg5Eq7yg9FzfCemryO1XoGft+wCw6BCskAwdgu/nxSeshmO0o4F5zr+4Js sBQwckitXlFyy4MCdDNOx3s5XCNKpIhK3J2LieVwUfDYoc+lGm68BOSM65ybx7kApgk43 66n4ma+lp3C5rYJE0Z5XCv8UiGmarKj//KNpR8F66OA7hS3pTisg/c4HxLozUCvRxVg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=ts21B2lR; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 18AC9606A33A; Sat, 21 Nov 2026 23:44:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 18AC9606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=ts21B2lR 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 9573516F2; Sat, 21 Nov 2026 15:44:19 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A81603F632; Sat, 21 Nov 2026 15:44:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304662; bh=7ASed0irKhbquWxwateY6WQZIArl0OMm5dYsGNK5iXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ts21B2lRNxp91M7iItVxv+6S68+hgws6nU5xhSaKtBgp33J+BG8I3M+vdnj/hRLJ8 W1YYNSC/tcae94V33I/bjpLIhjKdHrcTw77XK5OK44NSouB4RkqSwYZHcz7di3Uvy0 nfvk+P8opgya5EbYFYb12zWFCmrICRs7Bj0zAlFs= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 18/31] firmware: arm_scmi: Add Telemetry global configuration support Date: Sat, 21 Nov 2026 23:39:52 +0000 Message-ID: <20261121234005.1525419-19-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support to configure Telemetry globally by setting its enabled state and the requested update interval. Signed-off-by: Cristian Marussi --- v15 --> v16 - fix mode check logic --- drivers/firmware/arm_scmi/telemetry.c | 68 +++++++++++++++++++++++++++ include/linux/scmi_protocol.h | 15 ++++++ 2 files changed, 83 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 1453adee393c..2c71c090588d 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -3046,12 +3046,80 @@ static int scmi_telemetry_state_set(const struct sc= mi_protocol_handle *ph, return 0; } =20 +static int +scmi_telemetry_collection_configure(const struct scmi_protocol_handle *ph, + unsigned int res_id, const bool *enable, + const unsigned int *update_interval_ms, + const enum scmi_telemetry_collection *mode) +{ + enum scmi_telemetry_collection *current_mode, next_mode; + struct telemetry_info *ti =3D ph->get_priv(ph); + struct scmi_msg_telemetry_config_set *msg; + bool tlm_enable, *current_state, is_group; + unsigned int *active_update_interval; + struct scmi_xfer *t; + __le32 interval; + u32 control; + int ret; + + /* Stall concurrent configuration request */ + guard(mutex)(&ti->config_mtx); + + if (mode && (*mode !=3D SCMI_TLM_ONDEMAND)) + return -EOPNOTSUPP; + + is_group =3D res_id !=3D SCMI_TLM_GRP_INVALID; + if (is_group) + return -EOPNOTSUPP; + + active_update_interval =3D &ti->info.active_update_interval; + current_mode =3D &ti->info.current_mode; + current_state =3D &ti->info.enabled; + + if (!enable && !update_interval_ms && (!mode || *mode =3D=3D *current_mod= e)) + return 0; + + ret =3D ph->xops->xfer_get_init(ph, TELEMETRY_CONFIG_SET, + sizeof(*msg), 0, &t); + if (ret) + return ret; + + if (!update_interval_ms) + interval =3D cpu_to_le32(*active_update_interval); + else + interval =3D cpu_to_le32(*update_interval_ms); + + tlm_enable =3D enable ? *enable : *current_state; + next_mode =3D mode ? *mode : *current_mode; + + msg =3D t->tx.buf; + msg->group_identifier =3D cpu_to_le32(res_id); + control =3D tlm_enable ? TELEMETRY_ENABLE : 0; + control |=3D !is_group ? TELEMETRY_SET_SELECTOR_ALL : TELEMETRY_SET_SELEC= TOR_GROUP; + control |=3D TELEMETRY_MODE_SET(next_mode); + msg->control =3D cpu_to_le32(control); + msg->sampling_rate =3D interval; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + *current_mode =3D next_mode; + *current_state =3D tlm_enable; + ti->streaming_mode =3D *current_mode =3D=3D SCMI_TLM_NOTIFICATION; + if (update_interval_ms) + *active_update_interval =3D le32_to_cpu(interval); + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, .res_get =3D scmi_telemetry_resources_get, .state_get =3D scmi_telemetry_state_get, .state_set =3D scmi_telemetry_state_set, + .collection_configure =3D scmi_telemetry_collection_configure, }; =20 /** diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index c111b8ff87db..9a1257185309 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -888,6 +888,12 @@ enum scmi_telemetry_compo_type { #define SCMI_TLM_BUILD_UPDATE_INTERVAL(s, e) \ (FIELD_PREP(GENMASK(20, 5), (s)) | FIELD_PREP(GENMASK(4, 0), (e))) =20 +enum scmi_telemetry_collection { + SCMI_TLM_ONDEMAND, + SCMI_TLM_NOTIFICATION, + SCMI_TLM_SINGLE_READ, +}; + #define SCMI_TLM_GRP_INVALID 0xFFFFFFFF =20 struct scmi_telemetry_intervals { @@ -959,6 +965,8 @@ struct scmi_telemetry_info { struct scmi_telemetry_shmti_info **shmtis; unsigned int num_uuids; uuid_t * __rcu *uuids; + bool enabled; + enum scmi_telemetry_collection current_mode; }; =20 /** @@ -972,6 +980,9 @@ struct scmi_telemetry_info { * cumulative state of all DEs. Global group state can be looked-up * directly from @scmi_telemetry_info via @info_get. * @state_set: enable/disable the specific DE or GROUP with or without tim= estamps. + * @collection_configure: choose a sampling rate and enable SHMTI/FC sampl= ing + * for on demand collection via @de_data_read or async + * notificatioins for all the enabled DEs. */ struct scmi_telemetry_proto_ops { const struct scmi_telemetry_info *(*info_get) @@ -987,6 +998,10 @@ struct scmi_telemetry_proto_ops { bool is_group, u32 id, const bool *enable, const bool *tstamp, u32 *sid, u32 *offset, uuid_t *uuid); + int (*collection_configure)(const struct scmi_protocol_handle *ph, + unsigned int res_id, const bool *enable, + const unsigned int *update_interval_ms, + const enum scmi_telemetry_collection *mode); }; =20 /** --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304667; b=CLaByEFHcPpKtbnOlYfdANJIDgPbcAdi8u4jHuEt9klT0n839zJ7pXNh6XyH9XqyFrdL 2UKHrQhyWuO2kdfkTgIuuHIThbZNdmwuA6EXggvu692RN7BNUtv8gPilFn2g941oNIBF2 t9Kc79TZAAlrSzEfCXDFRU66nKLmNiBMoUT3PgnaBSQCO7ImcGOL+63sjpxlnheQX1ows Hc6PNlQqoh4BVCbPL4LHfNFdyTcPYiRw6xvKO+HYY3nzliET9pfw63uOB7V+H1EDH2Ztl 0EoCy7Q6ilZWF1kFwZdN6xjxmzYtKLyLj1dhKQjefbTujP+p0lc204u913bmEn9FPkA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304667; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=3IZ5qzRbl6w3s65pSH0LQA9SGE19s4V6UTDNQpB1bak=; b=xgQ97dw7VVusC4CEKX7mTAfmtaJNXT3uEM4BFp/iYwKlNRoNaMRuU+cdhw2+1B9VhTfk TCpGzCTKVOapx5JXEraaHWdcTlLTqdcLdgSNHu8CGnLOG4grGGNB9rRkthPoziEt8haia 0Mp5r2v+9BZ46vd33zyhwEN5pGXRbsIJq7HNsxOIbAs/XGOAlLofnQAHHhXG2/Dg4EJO1 LwTMhBw8SwSpRazRlIJepNew7hk8fZnV8aLX6J1JI+fg7YMKd6UjwsSxtKakfdroHpoMr t/jwWkIV3NMGZiqF3w7OWeeclWYpOpzS37xLlF7O7iE6yomYxf1OezVh3oYVgpkZT2Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=XvaHRC4A; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F7FC606A69E; Sat, 21 Nov 2026 23:44:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3F7FC606A69E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=XvaHRC4A 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 AF2771758; Sat, 21 Nov 2026 15:44:22 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C948B3F632; Sat, 21 Nov 2026 15:44:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304665; bh=MX3fXyOyMVKF7IyP4JAm7nbCpFnreV3P/0dR623E7Fk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XvaHRC4AnmEZebl6QWyMYqUWLcQBOzBOE/SBlNDDtV9tvCPHgkKWXcl6jFlfjJvWo bfGIH8VfMux4TqptY7BmFkpKejQ50Wy2E+6Q3d/IEXMUjpWP6mGwZW6C9hFL3jNNtX 4WvR05cESpPn7OmAa8EiEM7TOZhcyane5GK4wd8c= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 19/31] firmware: arm_scmi: Add Telemetry Single/Bulk DEs reads Date: Sat, 21 Nov 2026 23:39:53 +0000 Message-ID: <20261121234005.1525419-20-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for Telemetry operations needed to read DataEvent values and timestamps as single entities or all together in a single bulk buffer. The returned values are effectiely retrieved from the platform only when strictly needed, i.e. when no fresh recent cached value was already available. The DataEvent values are fetched transparently from the platform origins using the proper synchronization and consistency primitives, directly from the SHMTIs areas or the FastChannels memory depending on the configuration. Signed-off-by: Cristian Marussi --- v15 --> v16 - removed SINGLE SAMPLE delayed response reads capabilities - removed groups references - check liveness in de_snapshot - add shmti_overflow check on snap v14 --> v15 - use IS_TDE_TS_LINE macro - use NOT IS_DATA_LINE in tdcf_de_payld_get - use better startm/endm initial markers in tdcf_de_payld_get - retry scmi_telemetry_tdcf_de_parse on error too - trigger resource enumeration also on de_data_read - skip unitialized FCs on reads and return -EPROTO - introduce localized single DE snapshot on single DE data read - widen tde->mtx locking in msg_payld_process - use unsigned int samples - better des_sample_get sanity checks - refactor msg_payld_process v13 --> v14 - report scmi_telemetry_line_data_parse errors - add traces for CACHED_DE_UPDATED - moved some definitions from earlier patches - using a less regular time interval for TDCF scan retries - renamed some fields to more strictly adhere to spec nomenclature v12 --> v13 - use new RAW macros in line_data_parse and check for line validity - handle PARTIAL_ERROR by logging and carrying on - relocate tde->mtx guards higher up and add lockdep assertions - better tdcf/eplg init within tde->mtx - do not cache tde->base between DE read retries - retry scan on payld sanity errors v11 --> v12 - using new TDCF_INVALID_MATCH_SEQ macro - reporting transient DATA_INVALID lines as -EINVAL - added cacheable param to msg_payld_process v10 --> v11 - split out and relocate unrelated code to previous patches - add TDE mutex_lock when getting payload address - check LINE_TYPE while parsing payloads - check overflows and boundary on single sample replies v8 -->v9 - Fix Sashiko reports for v8 - check if TS supported on FC line before using it - check for unexpected shorter reply on sample_get - use non-LE accessors in msg_payld where appropriate v7 --> v8 - Fixes for some Sashiko issues on v7 - added CACHED_DE_READ trace - reworked locking around line handling: blkts/uuid v6 -->v7 - usse proper le32 accesors, fix sparse warns v5 --> v6 - fixed some Sashiko review - add new macros parse TDCF-like content on regular ram (non IO) https://sashiko.dev/#/patchset/20260703123601.381275-1-cristian.marussi%40= arm.com?part=3D10 - fix retry loop in DE parse to avoid exiting on transient errors - use proper non-IO accessors for msg payld parsing - add msg payld boundary check v4 --> v5 - removed UAPI structures dependency - cleaned up sample_get logic around groups - fixes sparse warnings v3 --> v4 - fix typos in comments - extend TDE cache helpers with cache lookup routine and use cumulative scmi_telemetry_de_sample param v2 --> v3 - split from monolithic Telemetry patch - simplity using a few assignement using ternary ops - remove useless ts param from scanning function - use a compound literal to simplify samples init - add a missing __must_check on telemetry_ops - changed errno on DE read troubles: - ENODEV/ENOENT: DE is UNKNOWN - EINVAL: DE is marked as DATA_INVALID - ENODATA: TLM susbsystem or the specific DE is OFF --- drivers/firmware/arm_scmi/telemetry.c | 531 ++++++++++++++++++++++++-- include/linux/scmi_protocol.h | 24 ++ 2 files changed, 519 insertions(+), 36 deletions(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 2c71c090588d..21fbd6a4578d 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -176,6 +176,11 @@ struct scmi_msg_telemetry_config_set { __le32 sampling_rate; }; =20 +struct scmi_msg_resp_telemetry_reading_complete { + __le32 num_dwords; + __le32 array[] __counted_by_le(num_dwords); +}; + /* TDCF */ =20 #define _I(__a) (ioread32((void __iomem *)(__a))) @@ -383,6 +388,12 @@ struct tdcf { #define TDCF_END_SIGNATURE(e) (_I(&((e)->sign_end))) #define TDCF_BAD_END_SEQ GENMASK(31, 0) =20 +struct telemetry_de_snapshot { + u32 magic; + struct payload payld; + struct payload bts_payld; +}; + struct telemetry_shmti; =20 struct shmti_snap { @@ -471,6 +482,7 @@ struct telemetry_de { enum timestamps ts_type; u32 ts_rate; bool enumerated; + bool cached_msg; void __iomem *base; struct eplg __iomem *eplg; u32 sid; @@ -706,8 +718,9 @@ scmi_telemetry_tde_cache_stale(struct telemetry_de *tde= , const u32 magic) } =20 static void -scmi_telemetry_tde_cache_update(struct telemetry_de *tde, u64 val, - u64 *tstamp, const u32 *magic) +scmi_telemetry_tde_cache_update(struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample, + const u32 *magic) { lockdep_assert_held(&tde->mtx); =20 @@ -719,10 +732,37 @@ scmi_telemetry_tde_cache_update(struct telemetry_de *= tde, u64 val, return; =20 tde->last_magic =3D magic ? *magic : TDCF_BAD_END_SEQ; - tde->last_val =3D val; - tde->last_ts =3D tstamp && scmi_tde_has_tstamp(tde) ? *tstamp : 0; - if (tstamp) - *tstamp =3D tde->last_ts; + tde->last_val =3D sample->val; + tde->last_ts =3D scmi_tde_has_tstamp(tde) ? sample->tstamp : 0; + /* Update returned value too */ + sample->tstamp =3D tde->last_ts; + + trace_scmi_tlm_collect(tde->last_ts, tde->de.info->id, + tde->last_val, "CACHED_DE_UPDATED"); +} + +static int +scmi_telemetry_tde_cache_lookup(struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample, + u32 *magic) +{ + lockdep_assert_held(&tde->mtx); + + /* + * Bailout without providing any sample when the cache is + * stale, i.e. last_magic < *magic + */ + if (magic && scmi_telemetry_tde_cache_stale(tde, *magic)) + return -EAGAIN; + + sample->id =3D tde->de.info->id; + sample->val =3D tde->last_val; + sample->tstamp =3D tde->last_ts; + + trace_scmi_tlm_collect(sample->tstamp, sample->id, + sample->val, "CACHED_DE_READ"); + + return 0; } =20 struct scmi_tlm_de_priv { @@ -1977,45 +2017,45 @@ static void scmi_telemetry_tdcf_blkts_parse(struct = telemetry_info *ti, =20 static inline int scmi_telemetry_line_data_parse(struct telemetry_de *tde, struct shmti_snap= *snap, - u64 *val, u64 *tstamp, struct payload *payld, - struct payload *bts_payld, u32 magic) + struct scmi_telemetry_de_sample *sample, + struct payload *payld, struct payload *bts_payld, + u32 magic) { u32 meta =3D PAYLD_META_RAW(payld); =20 lockdep_assert_held(&tde->mtx); =20 /* Clear returned values in case of errors */ - *val =3D 0; - - if (tstamp) { - *tstamp =3D 0; - if (USE_BLK_TS_RAW(meta)) { - /* Read out the actual BLK_TS */ - *tstamp =3D scmi_telemetry_blkts_read(magic, tde, - bts_payld, snap); - } else if (LINE_TS_VALID_RAW(meta)) { - /* - * Note that LINE_TS_VALID implies HAS_LINE_EXT and that - * the per DE line_ts_rate is advertised in the DE - * descriptor, BUT we check anyway against buggy FW. - */ - if (!HAS_LINE_EXT_RAW(meta)) { - trace_scmi_tlm_access(tde->de.info->id, - "SHMTI_BAD_TS_LINE", 0, 0); - return -EPROTO; - } - - *tstamp =3D LINE_TSTAMP_GET_RAW(&payld->tsl); + sample->tstamp =3D 0; + sample->val =3D 0; + + if (USE_BLK_TS_RAW(meta)) { + /* Read out the actual BLK_TS */ + sample->tstamp =3D scmi_telemetry_blkts_read(magic, tde, + bts_payld, snap); + } else if (LINE_TS_VALID_RAW(meta)) { + /* + * Note that LINE_TS_VALID implies HAS_LINE_EXT and that + * the per DE line_ts_rate is advertised in the DE + * descriptor, BUT we check anyway against buggy FW. + */ + if (!HAS_LINE_EXT_RAW(meta)) { + trace_scmi_tlm_access(tde->de.info->id, + "SHMTI_BAD_TS_LINE", 0, 0); + return -EPROTO; } + + sample->tstamp =3D LINE_TSTAMP_GET_RAW(&payld->tsl); } =20 /* Data is always valid since we are NOT handling BLK TS lines here */ - *val =3D LINE_DATA_GET_RAW(&payld->l); + sample->val =3D LINE_DATA_GET_RAW(&payld->l); =20 - trace_scmi_tlm_collect(tstamp ? *tstamp : 0, tde->de.info->id, - *val, "SHMTI_DE_READ"); + /* Trace originally read tstamp */ + trace_scmi_tlm_collect(sample->tstamp, tde->de.info->id, + sample->val, "SHMTI_DE_READ"); =20 - scmi_telemetry_tde_cache_update(tde, *val, tstamp, &magic); + scmi_telemetry_tde_cache_update(tde, sample, &magic); =20 return 0; } @@ -2121,10 +2161,10 @@ static void scmi_telemetry_tdcf_data_parse(struct t= elemetry_info *ti, struct telemetry_uuid *active_uuid) { struct telemetry_shmti *shmti =3D snap->shmti; + struct scmi_telemetry_de_sample sample =3D {}; u32 meta =3D PAYLD_META_RAW(payld); bool use_blk_ts =3D USE_BLK_TS_RAW(meta); struct telemetry_de *tde; - u64 val, tstamp =3D 0; u32 de_id; =20 de_id =3D PAYLD_ID_RAW(payld); @@ -2208,8 +2248,7 @@ static void scmi_telemetry_tdcf_data_parse(struct tel= emetry_info *ti, * Parse data words ... only for the sake of updating TDE cache, * any error is logged by ignored. */ - scmi_telemetry_line_data_parse(tde, snap, &val, &tstamp, payld, NULL, - snap->magic); + scmi_telemetry_line_data_parse(tde, snap, &sample, payld, NULL, snap->mag= ic); } =20 /** @@ -3113,6 +3152,424 @@ scmi_telemetry_collection_configure(const struct sc= mi_protocol_handle *ph, return ret; } =20 +static inline int +scmi_telemetry_de_data_fc_read(struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample) +{ + struct fc_tsline __iomem *fc; + + lockdep_assert_held(&tde->mtx); + + /* + * A FastChannel DE that was discovered as enabled at boot but later + * failed to be enumerated cannot be accessed. + */ + if (!tde->base) + return -EPROTO; + + fc =3D tde->base + tde->offset; + sample->val =3D LINE_DATA_GET(fc); + sample->tstamp =3D IS_TDE_TS_LINE(tde) ? LINE_TSTAMP_GET(fc) : 0; + + /* Trace originally read tstamp */ + trace_scmi_tlm_collect(sample->tstamp, tde->de.info->id, sample->val, + "FC_READ"); + + /* Cache anyway since TDE caches are on the read path anyway */ + scmi_telemetry_tde_cache_update(tde, sample, NULL); + + return 0; +} + +static void scmi_telemetry_scan_update(struct telemetry_info *ti) +{ + struct telemetry_de *tde; + + /* Scan all SHMTIs ... */ + for (int id =3D 0; id < ti->num_shmti; id++) { + int ret; + + ret =3D scmi_telemetry_shmti_scan(ti, id, SCAN_LOOKUP); + if (ret) + dev_warn(ti->ph->dev, + "Failed update-scan of SHMTI ID:%d - ret:%d\n", + id, ret); + } + + if (!ti->info.fc_support) + return; + + /* Need to enumerate resources to access fastchannels */ + ti->res_get(ti); + list_for_each_entry(tde, &ti->fcs_des, item) { + struct scmi_telemetry_de_sample sample =3D {}; + + guard(mutex)(&tde->mtx); + if (!tde->de.enabled) + continue; + + /* + * Only for the sake of updating TDE cache. + * Ignoring errors deriving from malformed FCs. + */ + scmi_telemetry_de_data_fc_read(tde, &sample); + } +} + +/* + * TDCF and TS Line Management Notes + * --------------------------------- + * + * TDCF Payload Metadata notable bits: + * - Bit[3]: USE BLK Tstamp + * - Bit[2]: Line-Extension Field present (LineTstamp) + * - Bit[1]: Tstamp VALID + * - Bit[0]: Data INVALID + * + * CASE_1: + * ------- + * + A DE is enabled with timestamp disabled, so the TS fields are + * NOT present + * -> BIT[3:0] =3D 0000b + * + * - 1/A LINE_TSTAMP + * ------------------ + * + that DE is then 're-enabled' with TS: so it was ON, it remains + * ON but using DE_CONFIGURE I now enabled also TS, so the + * platform relocates it at the end of the SHMTI and return the + * new offset + * -> BIT[3:0] =3D 0110b + * + * - 1/B BLK_TSTAMP + * ------------------ + * + that DE is then 're-enabled' with BLK TS: so it was ON, it + * remains ON but using DE_CONFIGURE, we now also enabled the TS, + * so the platform will: + * - IF a preceding BLK_TS line exist (with same clk rate) + * it relocates the DE at the end of the SHMTI and return the + * new offset (if there is enough room, if not in another SHMTI) + * - IF a preceding BLK_TS line DOES NOT exist (with same clk rate) + * it creates a new BLK_TS line at the end of the SHMTI and then + * relocates the DE after the new BLK_TS and return the + * new offset (if there is enough room, if not in another SHMTI) + * -> BIT[3:1] =3D 1010b + * + * + the hole left from the relocated DE can be reused by the platform + * to fit another equally sized DE. (i.e. without shuffling around any + * other enabled DE, since that would cause a change of the known offset) + * anyway it will be marked as: + * -> BIT[3:0] =3D 0101b iff it was a LINE_TSTAMP + * -> BIT[3:0] =3D 0001b iff it was a BLK_TSTAMP + * + * CASE_2: + * ------- + * + A DE is enabled with LINE timestamp enabled, so the TS_Line is there + * -> BIT[3:0] =3D 0110b + * + that DE has its timestamp disabled: again, you can do this without + * disabling it fully but just disabling the TS, so now that TS_line + * fields are still physically there but NOT valid + * -> BIT[3:0] =3D 0100b + * + the hole from the timestamp remain there unused until + * - you enable again the TS so the hole is used again + * -> BIT[3:0] =3D 0110b + * OR + * - you disable fully the DE and then re-enable it with the TS + * -> potentially CASE_1 the DE is relocated on enable + * + same kind of dynamic applies if the DE had a BLK_TS line + */ +static int scmi_telemetry_tdcf_de_payld_check(struct telemetry_de *tde, + struct payload *payld) +{ + u32 meta =3D PAYLD_META_RAW(payld); + + lockdep_assert_held(&tde->mtx); + + if (DATA_INVALID_RAW(meta)) { + trace_scmi_tlm_access(PAYLD_ID_RAW(payld), "DE_DATA_INVALID", 0, 0); + return -EINVAL; + } + + if (!IS_DATA_LINE_RAW(meta)) { + trace_scmi_tlm_access(tde->de.info->id, "BAD_DE_LINE_TYPE", 0, 0); + return -EPROTO; + } + + if (PAYLD_ID_RAW(payld) !=3D tde->de.info->id) { + trace_scmi_tlm_access(tde->de.info->id, "DE_ID_MISMATCH", 0, 0); + return -ENODEV; + } + + /* + * A valid line using BLK_TS should have been initialized with the + * related BLK_TS when enabled, unless is a torn read. + */ + if ((USE_BLK_TS_RAW(meta) && !tde->bts)) { + trace_scmi_tlm_access(tde->de.info->id, "BAD_USE_BLK_TS", 0, 0); + return -EPROTO; + } + + return 0; +} + +static int scmi_telemetry_de_snapshot(struct tdcf __iomem *tdcf, + struct telemetry_de *tde, + struct telemetry_de_snapshot *snap) +{ + int retries =3D SCMI_TLM_TDCF_MAX_RETRIES; + u32 startm, endm, live; + int err =3D 0; + + lockdep_assert_held(&tde->mtx); + + do { + struct payload __iomem *payld, *bts_payld; + + /* A bit of exponential backoff between retries */ + fsleep((SCMI_TLM_TDCF_MAX_RETRIES - retries) * SCMI_TLM_TDCF_RETRY_GAP_U= S); + + startm =3D TDCF_START_SEQ_GET(tdcf); + /* Build a default endm certainly different from startm */ + endm =3D startm + 1; + if (IS_BAD_START_SEQ(startm)) { + trace_scmi_tlm_access(tde->de.info->id, "MSEQ_BADSTART", + startm, 0); + continue; + } + + /* + * In this context any anomaly detected on the payload could + * due to a torn read, and it is worth retrying a snapshot. + * + * Note that TDE base/offset have been sanitized when + * initialized and cannot have changed: accessing them here + * is safe. + */ + payld =3D tde->base + tde->offset; + /* Sanitize length before snapshot */ + if (scmi_telemetry_shmti_overflow(tde->eplg, payld)) + continue; + memcpy_fromio(&snap->payld, payld, LINE_LENGTH_BYTES(payld)); + err =3D scmi_telemetry_tdcf_de_payld_check(tde, &snap->payld); + if (err) + continue; + + /* + * Here the associated BLK_TS line, if any, has been verified + * as sane when linked/bound: no need of further validation. + */ + if (tde->bts) { + bts_payld =3D tde->base + tde->bts->line.offset; + /* Sanitize length before snapshot */ + if (scmi_telemetry_shmti_overflow(tde->eplg, bts_payld)) + continue; + memcpy_fromio(&snap->bts_payld, bts_payld, + LINE_LENGTH_BYTES(bts_payld)); + } + + /* Assure the snapshot has been fully taken before checking seqnum */ + rmb(); + + /* Used to detect changes while copying */ + live =3D TDCF_START_SEQ_GET(tdcf); + + endm =3D TDCF_END_SEQ_GET(tde->eplg); + if (startm !=3D endm || startm !=3D live) + trace_scmi_tlm_access(tde->de.info->id, "MSEQ_MISMATCH", + startm, endm); + } while ((startm !=3D endm || err) && --retries); + + if (startm !=3D endm || err) { + trace_scmi_tlm_access(tde->de.info->id, + "TDCF_SNAPSHOT_DE_FAIL", startm, endm); + return err ?: -EPROTO; + } + + /* Ensure any snapshot is visible before updating magic */ + smp_store_release(&snap->magic, startm); + + trace_scmi_tlm_access(tde->de.info->id, "TDCF_SNAPSHOT_DE_OK", + startm, endm); + + return 0; +} + +static int +scmi_telemetry_tdcf_de_sample_cached(struct tdcf __iomem *tdcf, + struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample) +{ + lockdep_assert_held(&tde->mtx); + + u32 startm; + + /* If updating cannot be cached */ + startm =3D TDCF_START_SEQ_GET(tdcf); + if (IS_BAD_START_SEQ(startm)) + return -EAGAIN; + + return scmi_telemetry_tde_cache_lookup(tde, sample, &startm); +} + +/** + * scmi_telemetry_tdcf_de_parse - Helper to read one SHMTI/DE + * @tde: A reference to the DE descriptor to read + * @sample: A reference to the sample used as a container for output + * + * This helper can be used to retrieve the data value of a SHMTI based DE + * and any related BLK_TS line: unless the values are found already availa= ble + * and up to date in the TDE cache, a limited snapshot of the involved lin= es + * is taken at first so that all the subsequent DE parsing and processing = can + * happen on a stable in RAM copy. + * + * Taking a limited snapshot does not carry relevant overhead and has the + * benefit of allowing the reuse of the same RAM-baased data accessors used + * during a full scan and related full snapshot, instead of a set of dedic= ated + * __iomem helpers. + * + * NOTE that since each TDE has its own cache, which is accessed and updat= ed + * both during a single DE read and while performing a bulk read, while + * caching consistency assures to provide always the 'freshest' value + * retrieved, mixing single and bulk reads can lead to the values produced= by + * the bulk reads to contain 'fresher' late values derived from concurrent + * single DE reads: IOW mixing single and bulk reads can produce a set of = bulk + * DEs values that are NOT all from the same moment in time. + * + * Moreover in general, since no assumption can be made about the TDCF seq= uence + * numbers across different SHMTIs and related optional DE timestamps, it = is NOT + * possible to guarantee anyway that data values retrieved from DEs locate= d in + * distinct SHMTIs areas comes from the same platform update cycle at all. + */ +static int +scmi_telemetry_tdcf_de_parse(struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample) +{ + struct telemetry_de_snapshot snap =3D {}; + struct tdcf __iomem *tdcf; + int ret; + + lockdep_assert_held(&tde->mtx); + + /* + * TDEs can be relocated by platform only during enable phase, + * thing that cannot happen until tde->mtx is released. + */ + tdcf =3D tde->base; + if (!tdcf) + return -ENODEV; + + if (!scmi_telemetry_tdcf_de_sample_cached(tdcf, tde, sample)) + return 0; + + /* Attempt a localized DE snapshot: DATA_LINE + optionally BLK_TS */ + ret =3D scmi_telemetry_de_snapshot(tdcf, tde, &snap); + if (ret) + return ret; + + /* Now parse the stable line snapshot */ + return scmi_telemetry_line_data_parse(tde, NULL, sample, &snap.payld, + &snap.bts_payld, snap.magic); +} + +static int scmi_telemetry_de_access(struct telemetry_de *tde, + struct scmi_telemetry_de_sample *sample) +{ + lockdep_assert_held(&tde->mtx); + + if (!tde->de.fc_support) + return scmi_telemetry_tdcf_de_parse(tde, sample); + + return scmi_telemetry_de_data_fc_read(tde, sample); +} + +static int scmi_telemetry_de_collect(struct telemetry_info *ti, + struct scmi_telemetry_de *de, + struct scmi_telemetry_de_sample *sample) +{ + struct telemetry_de *tde =3D to_tde(de); + + guard(mutex)(&tde->mtx); + + if (!de->enabled) + return -ENODATA; + + /* + * DE readings returns cached values when: + * - DE data value was retrieved via notification/delayed_response + */ + if (tde->cached_msg) + return scmi_telemetry_tde_cache_lookup(tde, sample, NULL); + + return scmi_telemetry_de_access(tde, sample); +} + +static int scmi_telemetry_de_data_read(const struct scmi_protocol_handle *= ph, + struct scmi_telemetry_de_sample *sample) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + struct scmi_telemetry_de *de; + + if (!ti->info.enabled || !sample) + return -ENODATA; + + ti->res_get(ti); + de =3D xa_load(&ti->xa_des, sample->id); + if (!de) + return -ENOENT; + + return scmi_telemetry_de_collect(ti, de, sample); +} + +static int +scmi_telemetry_samples_collect(struct telemetry_info *ti, int grp_id, + unsigned int *num_samples, + struct scmi_telemetry_de_sample *samples) +{ + struct scmi_telemetry_res_info *rinfo; + unsigned int max_samples; + + if (grp_id !=3D SCMI_TLM_GRP_INVALID) + return -EOPNOTSUPP; + + max_samples =3D *num_samples; + *num_samples =3D 0; + + rinfo =3D ti->res_get(ti); + for (int i =3D 0; i < rinfo->num_des; i++) { + struct telemetry_de *tde =3D to_tde(rinfo->des[i]); + + guard(mutex)(&tde->mtx); + + /* Skip disabled DEs */ + if (!tde->de.enabled) + continue; + + /* Must be checked here to skip disabled and non selected */ + if (*num_samples =3D=3D max_samples) + return -ENOSPC; + + /* Note that this cannot fail when passing a NULL magic */ + scmi_telemetry_tde_cache_lookup(tde, &samples[(*num_samples)++], NULL); + } + + return 0; +} + +static int scmi_telemetry_des_bulk_read(const struct scmi_protocol_handle = *ph, + int grp_id, unsigned int *num_samples, + struct scmi_telemetry_de_sample *samples) +{ + struct telemetry_info *ti =3D ph->get_priv(ph); + + if (!ti->info.enabled || !num_samples || !samples) + return -EINVAL; + + /* Trigger a full SHMTIs & FCs scan */ + scmi_telemetry_scan_update(ti); + + /* Collect all last cached values */ + return scmi_telemetry_samples_collect(ti, grp_id, num_samples, samples); +} + static const struct scmi_telemetry_proto_ops tlm_proto_ops =3D { .info_get =3D scmi_telemetry_info_get, .de_lookup =3D scmi_telemetry_de_lookup, @@ -3120,6 +3577,8 @@ static const struct scmi_telemetry_proto_ops tlm_prot= o_ops =3D { .state_get =3D scmi_telemetry_state_get, .state_set =3D scmi_telemetry_state_set, .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, }; =20 /** diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 9a1257185309..a2f46167883d 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -969,6 +969,13 @@ struct scmi_telemetry_info { enum scmi_telemetry_collection current_mode; }; =20 +struct scmi_telemetry_de_sample { + unsigned int id; + unsigned int pad; + unsigned long long tstamp; + unsigned long long val; +}; + /** * struct scmi_telemetry_proto_ops - represents the various operations pro= vided * by SCMI Telemetry Protocol @@ -983,6 +990,15 @@ struct scmi_telemetry_info { * @collection_configure: choose a sampling rate and enable SHMTI/FC sampl= ing * for on demand collection via @de_data_read or async * notificatioins for all the enabled DEs. + * @de_data_read: on-demand read of a single DE and related optional times= tamp: + * the value will be retrieved at the proper SHMTI offset OR + * from the dedicated FC area (if supported by that DE). + * @des_bulk_read: on-demand read of all the currently enabled DEs, or just + * the ones belonging to a specific group when provided. + * @des_sample_get: on-demand read of all the currently enabled DEs, or ju= st + * the ones belonging to a specific group when provided. + * This causes an immediate update platform-side of all the + * enabled DEs. */ struct scmi_telemetry_proto_ops { const struct scmi_telemetry_info *(*info_get) @@ -1002,6 +1018,14 @@ struct scmi_telemetry_proto_ops { unsigned int res_id, const bool *enable, const unsigned int *update_interval_ms, const enum scmi_telemetry_collection *mode); + int __must_check (*de_data_read)(const struct scmi_protocol_handle *ph, + struct scmi_telemetry_de_sample *sample); + int __must_check (*des_bulk_read)(const struct scmi_protocol_handle *ph, + int grp_id, unsigned int *num_samples, + struct scmi_telemetry_de_sample *samples); + int __must_check (*des_sample_get)(const struct scmi_protocol_handle *ph, + int grp_id, unsigned int *num_samples, + struct scmi_telemetry_de_sample *samples); }; =20 /** --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304669; b=tD1pTruU991RZJQmGTF6/vW5jMZB6hTSLso2uNMEuac+A4jTUc+qxT4agQNICJdC1eLG WOD009OseTuC47IrpbNBQ5U2q56GNF7WmAXe6pASFxvQY3EZyBIE+B8dmFQGNNLjcUiLL ePuIpt2DITEhC1WwhsTVot3rXCH3OWp3xP52uUHEP+TV2dayQhp+/cQxAcgbBM4YAVzNn GvkdmrpO/+PksoXfVhhAk5QjCZYqvu+RAgzkEvePPgPr8+DgH/ddHNZsqx++HnFqOlbHz Ho7hRERf+9lQ002TcXQdugl/rXXz/WpMplUrN1fc+cTitgZWJLcW3/icpuroxOOXUxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304669; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=FQsmtXBpj9g7Vc2MnK1s/XKa2XQ1wGnZHAswBU4OXjc=; b=roNuGvibFSI2WaxVgVKqRWBtQY2i5lMecC3FafLZQe2tsqSQ6iwaV+y4nXubUaIQB2Gz qe3NQtn2/C6bQ7BxfU+xBbyY5x/nJTC0OFIShkAPLWoleWdnP9lQsU1V0vDzoP+YYZbvA eNenSpsaDjyJoFqXWy+3ydUJZq8ktlks1g+huir3+sd8ye1uBzYmbtRSh9VZqpQ4/bvgi b8UOdChyGDd1UAq5Eh7/r8BosiYKuyHOROFwOfZAQ2OT19j7meq6YnmDb70x3oWn5PWYV kl7usJcrLS19NlUJlhZgs9bngZDIdB2mUQqfYzKoii/zXFsH6p/BciBL9T41A5gt7Bw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Zfrhk0pu; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D728606A682; Sat, 21 Nov 2026 23:44:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5D728606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Zfrhk0pu 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 CA2EB16F2; Sat, 21 Nov 2026 15:44:25 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E228F3FA21; Sat, 21 Nov 2026 15:44:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304668; bh=6NA3IHW0zaPrOvgsZWpIC6A/H3nptBWjkmO2buyDTig=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zfrhk0pu+AFEzqdKfXWiEcyAkAqlSitR35SyWsNIGGSd+X7mozDWyFKwyxEO8nsgr cYvjuJ0cI64LYolBOp+v5gO4P/DUh8VVJoy4DRM+FRrNKLE3bFHbFosCFqw+RsJUgz S6NRqPhPY6NwitB9Wba6PoH9bit6Eby+3xbmV+0E= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 20/31] firmware: arm_scmi: Add Telemetry boot-on support Date: Sat, 21 Nov 2026 23:39:54 +0000 Message-ID: <20261121234005.1525419-21-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the initialization and discovery logic needed to detect when the platform SCMI server is configured with telemetry enabled at boot and perform all the needed resource enumerations to keep the kernel telemetry subsystem state aligned with the platform boot-on configurations. Signed-off-by: Cristian Marussi --- v15 --> 16 - removed group support at boot-on - discard DE/FC at enumeration time that were found in SHMTI at boot - align DE local/global TS state after enumeration v14 --> v15 - use IS_TDE_TS_ macro - retrieve collection_mode at boot-on and force ignore groups - count only successfully initialized groups at boot-on v13 --> v14 - moved some definitions from earlier split patch - renamed some fields to more strictly adhere to spec nomenclature v12 --> v13 - greb tde->mtx on DE_ENABLED_LIST v11 --> v12 - fixed TS_RATE determination at boot-on v10 --> v11 - fix non-conformancy on CONFIG_GET when groups NON configurable - use SCAN_UPDATE also on BOOT-ON initial scan (NO discovery) - avoid 'naked' ti->rinfo accesses v8 -- > v9 - remove useless initial state lookup after RESET - properly set de->tstamp_support on DE_ENABLED_LIST v5 --> v6 - reporte properly an error on scmi_telemetry_config_lookup() failure (Sashiko) v4 --> v5 - use renamed grp_id instead of old id in group descriptors - fixes sparse warnings v3 --> v4 - bail-out FW_BUG errors - fix typos in comments - track boot-on Telemetry DE states v2 --> v3 - split from monolithic telemetry protocol patch - swap logic in scmi_telemetry_initial_state_lookup --- drivers/firmware/arm_scmi/telemetry.c | 283 +++++++++++++++++++++++++- 1 file changed, 277 insertions(+), 6 deletions(-) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index 21fbd6a4578d..b74371bc41d5 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -133,6 +133,21 @@ struct scmi_msg_resp_telemetry_update_intervals { __le32 intervals[]; }; =20 +struct scmi_msg_telemetry_de_enabled_list { + __le32 index; + __le32 flags; +}; + +struct scmi_enabled_de_desc { + __le32 id; + __le32 mode; +}; + +struct scmi_msg_resp_telemetry_de_enabled_list { + __le32 flags; + struct scmi_enabled_de_desc array[]; +}; + struct scmi_msg_telemetry_de_configure { __le32 identifier; __le32 flags; @@ -181,6 +196,20 @@ struct scmi_msg_resp_telemetry_reading_complete { __le32 array[] __counted_by_le(num_dwords); }; =20 +struct scmi_msg_telemetry_config_get { + __le32 group_identifier; + __le32 flags; +#define TELEMETRY_GET_SELECTOR(x) (FIELD_PREP(GENMASK(3, 0), (x))) +#define TELEMETRY_GET_SELECTOR_ORPHANS TELEMETRY_GET_SELECTOR(TLM_ORPHANS) +#define TELEMETRY_GET_SELECTOR_GROUP TELEMETRY_GET_SELECTOR(TLM_GROUP) +#define TELEMETRY_GET_SELECTOR_ALL TELEMETRY_GET_SELECTOR(TLM_ALL) +}; + +struct scmi_msg_resp_telemetry_config_get { + __le32 control; + __le32 sampling_rate; +}; + /* TDCF */ =20 #define _I(__a) (ioread32((void __iomem *)(__a))) @@ -509,6 +538,8 @@ static inline bool scmi_tde_has_tstamp(struct telemetry= _de *t) return t->de.tstamp_support && t->de.tstamp_enabled; } =20 +#define DE_ENABLED_WITH_TSTAMP 2 + enum de_state { ENA_STATE, ENA_TSTAMP, @@ -911,6 +942,7 @@ static int scmi_telemetry_de_descriptor_parse(struct te= lemetry_info *ti, { const struct scmi_de_desc *desc =3D *next; size_t payld_sz =3D sizeof(*desc); + bool fc_support; =20 lockdep_assert_held(&tde->mtx); =20 @@ -929,7 +961,7 @@ static int scmi_telemetry_de_descriptor_parse(struct te= lemetry_info *ti, tde->de.info->persistent =3D IS_PERSISTENT(desc); tde->ts_type =3D TSTAMP_SUPPORT(desc); tde->de.tstamp_support =3D !!tde->ts_type; - tde->de.fc_support =3D IS_FC_SUPPORTED(desc); + fc_support =3D IS_FC_SUPPORTED(desc); tde->de.name_support =3D IS_NAME_SUPPORTED(desc); /* Update DE_DESCRIPTOR size for the next iteration */ *next +=3D sizeof(*desc); @@ -938,6 +970,10 @@ static int scmi_telemetry_de_descriptor_parse(struct t= elemetry_info *ti, if (*rx_len < payld_sz) return -ENOSPC; =20 + /* Was already discovered as a SHMTI-based DE ? */ + if (fc_support && tde->base) + return -EPROTO; + if (IS_TDE_TS_LINE(tde)) { __le32 *line_ts_rate =3D *next; =20 @@ -946,15 +982,20 @@ static int scmi_telemetry_de_descriptor_parse(struct = telemetry_info *ti, /* Variably sized depending on TS support */ *next +=3D sizeof(*line_ts_rate); } else if (IS_TDE_TS_BLK(tde)) { - /* Setup default BLK TS value at first */ - tde->de.info->ts_rate =3D ti->default_blk_ts_rate; + /* + * Setup default BLK TS rate value at first, if it has NOT + * already been picked from the BLK_TS line by the boot-on + * SCAN_UPDATE. + */ + if (!tde->de.info->ts_rate) + tde->de.info->ts_rate =3D ti->default_blk_ts_rate; } else if (!IS_TDE_TS_NONE(tde)) { dev_warn(ti->ph->dev, "Unknown TDE timestamp type %u\n", tde->ts_type); return -EPROTO; } =20 - if (tde->de.fc_support) { + if (fc_support) { struct de_desc_fastchannel *dfc; phys_addr_t phys_addr; void __iomem *addr; @@ -985,6 +1026,7 @@ static int scmi_telemetry_de_descriptor_parse(struct t= elemetry_info *ti, tde->base =3D addr; tde->offset =3D 0; tde->fc_size =3D size; + tde->de.fc_support =3D fc_support; =20 /* Variably sized depending on FC support */ *next +=3D sizeof(*dfc); @@ -1067,6 +1109,11 @@ static int iter_de_descr_process_response(const stru= ct scmi_protocol_handle *ph, } } =20 + /* TS is NOT supported clear any enabled state possibly at boot-on */ + if (!tde->de.tstamp_support) + scmi_telemetry_de_state_update(ti, ENA_TSTAMP, + &tde->de.tstamp_enabled, false); + /* Count timestamped DEs */ ti->num_des_tstamp +=3D !!tde->de.tstamp_support; =20 @@ -1079,6 +1126,216 @@ static int iter_de_descr_process_response(const str= uct scmi_protocol_handle *ph, return 0; } =20 +static int scmi_telemetry_config_lookup(struct telemetry_info *ti, + unsigned int grp_id, bool *enabled, + unsigned int *active_update_interval, + enum scmi_telemetry_collection *mode, + bool ignore_groups) +{ + const struct scmi_protocol_handle *ph =3D ti->ph; + struct scmi_msg_telemetry_config_get *msg; + struct scmi_msg_resp_telemetry_config_get *resp; + bool is_group =3D grp_id !=3D SCMI_TLM_GRP_INVALID; + struct scmi_xfer *t; + u32 flags; + int ret; + + ret =3D ph->xops->xfer_get_init(ph, TELEMETRY_CONFIG_GET, + sizeof(*msg), sizeof(*resp), &t); + if (ret) + return ret; + + msg =3D t->tx.buf; + msg->group_identifier =3D cpu_to_le32(grp_id); + + /* Spec mandates the ALL selector when per-group config not available */ + if (!ti->info.per_group_config_support || ignore_groups) + flags =3D TELEMETRY_GET_SELECTOR_ALL; + else + flags =3D !is_group ? TELEMETRY_GET_SELECTOR_ORPHANS : + TELEMETRY_GET_SELECTOR_GROUP; + + msg->flags =3D cpu_to_le32(flags); + + resp =3D t->rx.buf; + ret =3D ph->xops->do_xfer(ph, t); + if (!ret) { + *enabled =3D le32_get_bits(resp->control, TELEMETRY_ENABLE); + *mode =3D le32_get_bits(resp->control, TELEMETRY_MODE); + if (!is_group) + ti->streaming_mode =3D *mode =3D=3D SCMI_TLM_NOTIFICATION; + *active_update_interval =3D + SCMI_TLM_GET_UPDATE_INTERVAL(le32_to_cpu(resp->sampling_rate)); + } + + ph->xops->xfer_put(ph, t); + + return ret; +} + +static void iter_enabled_list_prepare_message(void *message, + unsigned int desc_index, + const void *priv) +{ + struct scmi_msg_telemetry_de_enabled_list *msg =3D message; + + msg->index =3D cpu_to_le32(desc_index); + msg->flags =3D 0; +} + +static int iter_enabled_list_update_state(struct scmi_iterator_state *st, + const void *response, void *priv) +{ + const struct scmi_msg_resp_telemetry_de_enabled_list *r =3D response; + + st->num_returned =3D le32_get_bits(r->flags, GENMASK(15, 0)); + st->num_remaining =3D le32_get_bits(r->flags, GENMASK(31, 16)); + + if (st->rx_len < (sizeof(*r) + sizeof(r->array[0]) * st->num_returned)) + return -EINVAL; + + /* + * total enabled is not declared previously anywhere so we + * assume it's returned+remaining on first call. + */ + if (!st->max_resources) + st->max_resources =3D st->num_returned + st->num_remaining; + + return 0; +} + +static int +iter_enabled_list_process_response(const struct scmi_protocol_handle *ph, + const void *response, + struct scmi_iterator_state *st, void *priv) +{ + const struct scmi_msg_resp_telemetry_de_enabled_list *r =3D response; + const struct scmi_enabled_de_desc *desc; + struct telemetry_info *ti =3D priv; + struct telemetry_de *tde; + u32 de_id; + int ret; + + desc =3D &r->array[st->loop_idx]; + de_id =3D le32_to_cpu(desc->id); + if (scmi_telemetry_tde_lookup(ti, de_id)) { + dev_err(ph->dev, + "Found INVALID DE with DUPLICATED ID:0x%08X\n", de_id); + return -EINVAL; + } + + tde =3D scmi_telemetry_tde_get(ti, de_id); + if (IS_ERR(tde)) + return PTR_ERR(tde); + + guard(mutex)(&tde->mtx); + tde->de.info->id =3D de_id; + tde->de.enabled =3D true; + tde->de.tstamp_enabled =3D le32_to_cpu(desc->mode) =3D=3D DE_ENABLED_WITH= _TSTAMP; + /* + * Guessing that, if a DE is reported as enabled with timestamp, it has + * to support timestamp: NOTE THAT the timestamp type (tde->ts_type) can + * ONLY be discovered later by DE_DESCRIPTION. + */ + tde->de.tstamp_support =3D tde->de.tstamp_enabled; + + ret =3D scmi_telemetry_tde_register(ti, tde); + if (ret) { + scmi_telemetry_free_tde_put(ti, tde); + return ret; + } + + scmi_telemetry_de_state_update(ti, ENA_STATE, NULL, true); + if (tde->de.tstamp_enabled) + scmi_telemetry_de_state_update(ti, ENA_TSTAMP, NULL, true); + + dev_dbg(ph->dev, "Registered new ENABLED DE with ID:0x%08X\n", + tde->de.info->id); + + return 0; +} + +static int scmi_telemetry_enumerate_des_enabled_list(struct telemetry_info= *ti) +{ + const struct scmi_protocol_handle *ph =3D ti->ph; + struct scmi_telemetry_res_info *rinfo; + struct scmi_iterator_ops ops =3D { + .prepare_message =3D iter_enabled_list_prepare_message, + .update_state =3D iter_enabled_list_update_state, + .process_response =3D iter_enabled_list_process_response, + }; + void *iter; + int ret; + + iter =3D ph->hops->iter_response_init(ph, &ops, 0, + TELEMETRY_DE_ENABLED_LIST, + sizeof(u32) * 2, ti); + if (IS_ERR(iter)) + return PTR_ERR(iter); + + ret =3D ph->hops->iter_response_run(iter); + if (ret) + return ret; + + /* Get rinfo as it is...without triggering an enumeration */ + rinfo =3D __scmi_telemetry_resources_get(ti); + + dev_info(ti->ph->dev, "Found %u enabled DEs.\n", rinfo->num_des); + + return 0; +} + +static int scmi_telemetry_initial_state_lookup(struct telemetry_info *ti) +{ + struct device *dev =3D ti->ph->dev; + int ret; + + /* Discover the initial state ONLY for DEs, ignoring groups */ + ret =3D scmi_telemetry_config_lookup(ti, SCMI_TLM_GRP_INVALID, + &ti->info.enabled, + &ti->info.active_update_interval, + &ti->info.current_mode, true); + if (ret) + return ret; + + if (!ti->info.enabled) + return 0; + + /* + * When Telemetry is found already enabled on the platform, proceed with + * passive discovery using DE_ENABLED_LIST and TDCF scanning: note that + * this CAN only discover DEs exposed via SHMTIs. + * FastChannel DEs need a proper DE_DESCRIPTION enumeration, while, even + * though incoming Notifications could be used for passive discovery too, + * it would carry a considerable risk of assimilating trash as DEs. + */ + dev_info(dev, + "Telemetry found enabled with update interval %lux10^%d mode:%u\n", + SCMI_TLM_GET_UPDATE_INTERVAL_SECS(ti->info.active_update_interval), + SCMI_TLM_GET_UPDATE_INTERVAL_EXP(ti->info.active_update_interval), + ti->info.current_mode); + /* + * Query enabled DEs list: collect states. It will include DEs from any + * interface. Enabled groups still NOT enumerated. + */ + ret =3D scmi_telemetry_enumerate_des_enabled_list(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot query enabled DE list. Abort.\n"); + return ret; + } + + /* Discover DEs on SHMTis: collect states/offsets/values */ + for (int id =3D 0; id < ti->num_shmti; id++) { + ret =3D scmi_telemetry_shmti_scan(ti, id, SCAN_UPDATE); + if (ret) + dev_warn(dev, + "Failed discovery-scan of SHMTI ID:%d - ret:%d\n", + id, ret); + } + + return 0; +} + static void scmi_telemetry_tdes_cleanup(void *arg) { struct telemetry_info *ti =3D arg; @@ -2217,9 +2474,17 @@ static void scmi_telemetry_tdcf_data_parse(struct te= lemetry_info *ti, =20 trace_scmi_tlm_access(de_id, "DE_LOCATION_UPDATED", 0, 0); =20 + /* Just in case it was passively discovered boot-on */ + if (IS_TDE_TS_NONE(tde)) { + if (use_blk_ts) + tde->ts_type =3D TSTAMP_BLK; + else if (LINE_TS_VALID_RAW(meta)) + tde->ts_type =3D TSTAMP_LINE; + } + dev_dbg(ti->ph->dev, - "TDCF-updated DE_ID:0x%08X - shmti:%pK offset:%u\n", - tde->de.info->id, tde->base, tde->offset); + "TDCF-updated DE_ID:0x%08X - shmti:%pK offset:%u ts_type:%d\n", + tde->de.info->id, tde->base, tde->offset, tde->ts_type); } =20 /* @@ -3918,6 +4183,12 @@ static int scmi_telemetry_protocol_init(const struct= scmi_protocol_handle *ph) return ret; } =20 + ret =3D scmi_telemetry_initial_state_lookup(ti); + if (ret) { + dev_err(dev, FW_BUG "Cannot retrieve initial state. Abort.\n"); + return ret; + } + ti->info.base.version =3D ph->version; =20 return ph->set_priv(ph, ti); --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304673; b=ASTKoPYc/7zXR5xnE+F1sBTBfAQyacLwdtxYg5M4vNo2ldvnCDTnfqjFsiotCfaOxfOh URUjQzWQ0D+14dEfFOXDqeSRSAZe0QdQz0EFEmi7aLN+AAvRZqmD6XWsmRP4M0tZKuKwY 3zIyx7kwYknffB7ETRsbvNHobFgHnyIuRlAjnXgxvI5v7LiRS9D0xTAiAf5e2b96e1bZg yonKIpddUWZcdZ5XTS9Yir15pIA3X6ZsHPMlWepJSv/9NrDtNIgx+rGL/4MFB031pPQEA yXY6/8yNOp3L+WiOO30aK9GSz3cvo5ZZutdbMr3XoRgIrDhaiKBI+A8mdieCFClOUuw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304673; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=OKpDCNyl1moqPt1/HkWVp3v99rggGU7D2fGszC6D84Y=; b=QEYOxLOz8xFX953wT0ujJVoiTSPtFOkn1k+te0RizpQAvdCaiZ9qH2yvgAW6lrnHpErq R2YSnIwGrwrI74+9YLJOrtae6hWtWV9TXsyC+UhWSnUCajEJ2jq8rGbl93EL01iO46Pts pllzxeZqBS6FtMbng76xqzc2VS1zPT7c0NUDUoaIPHUoM1jfPc8QkcgI+JB5Fmot12A60 AEbLf6n3nPxAxjQ2WHqq5i/8+JXPKDz5igLPW+/2P/v3zAUSH0rCECLg9fTWY5mL3+CmN c/yHMmakbzefSaG7oGqdb62Y5OjJd5kaeFxFOCkDbiRoqS/S51IVpZ4jDeOQZE/7VnQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Htofl0ZR; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 74470606A33D; Sat, 21 Nov 2026 23:44:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 74470606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Htofl0ZR 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 E01701575; Sat, 21 Nov 2026 15:44:28 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 06B1A3F632; Sat, 21 Nov 2026 15:44:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304671; bh=ZrwV5GEgbTHD6z7lX0jVIStuH2ZZt3a6t5U6nc2ToL0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Htofl0ZRP6WfomXdNQ9yfEhDRA3qEE9CERVMNFDUVzlD3SHM3bZTQxd4hZHZRjpri QixxZMO2ZAWo8uwaJvCdF8edg1xueSTX6h/hkcExlZVBPhv8H1Gi+Gw+jeTIdcpbcf LOpYCsEyAKnMBfnPfbsz9pv5FYhOzpheqey/2yN8= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 21/31] firmware: arm_scmi: Add common per-protocol debugfs support Date: Sat, 21 Nov 2026 23:39:55 +0000 Message-ID: <20261121234005.1525419-22-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow interested SCMI protocols to register their own specific debugfs entries under a common per-instance and per-protocol subtree rooted at /sys/kernel/debug/scmi//protocols// Expose a helper to enable protocol initialization code to get access to such per-protocol/per-instance dentries in order to be able to install their own dedicated debugfs entries. Per-protocol debugfs support is configurable and default off. Signed-off-by: Cristian Marussi --- v15 --> v16 - cleanup protocol dbg root after devres group closure v14 --> v15 - fix debugfs_remove_recursive call-site...was too early v8 --> v9 - avoid NULL deref on DBG cleanup path v5 --> v6 - bail out of dentry_get helper when SCMI debug susbsytem failed to init - remove stale SCMI protocol operations - add a preoper clean up of debugfs when protocol init fails --- drivers/firmware/arm_scmi/Kconfig | 14 ++++++++++ drivers/firmware/arm_scmi/common.h | 2 ++ drivers/firmware/arm_scmi/driver.c | 39 +++++++++++++++++++++++++++ drivers/firmware/arm_scmi/protocols.h | 6 +++++ include/linux/scmi_protocol.h | 1 - 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/arm_scmi/Kconfig b/drivers/firmware/arm_scmi/= Kconfig index 783c24a20e29..f506c4d1d96a 100644 --- a/drivers/firmware/arm_scmi/Kconfig +++ b/drivers/firmware/arm_scmi/Kconfig @@ -69,6 +69,20 @@ config ARM_SCMI_DEBUG_COUNTERS such useful debug counters. This can be helpful for debugging and SCMI monitoring. =20 +config ARM_SCMI_DEBUG_PROTOCOLS + bool "Enable SCMI protocols debug" + select ARM_SCMI_NEED_DEBUGFS + depends on DEBUG_FS + default n + help + Enables per-protocol specific debug features, where available. + When provided, such per-protocol debugfs entries are grouped + inside a common a subtree named by the protocol number and rooted + under a per-instance 'protocols' directory. + + Such per-protocol entries subtree structure is freely defined + within the related protocol code. + config ARM_SCMI_QUIRKS bool "Enable SCMI Quirks framework" depends on JUMP_LABEL || COMPILE_TEST diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi= /common.h index cdd6f887fae7..ed1623da6563 100644 --- a/drivers/firmware/arm_scmi/common.h +++ b/drivers/firmware/arm_scmi/common.h @@ -307,6 +307,7 @@ enum debug_counters { /** * struct scmi_debug_info - Debug common info * @top_dentry: A reference to the top debugfs dentry + * @protos: A reference to the top debugfs protocols subdirectory * @name: Name of this SCMI instance * @type: Type of this SCMI instance * @is_atomic: Flag to state if the transport of this instance is atomic @@ -314,6 +315,7 @@ enum debug_counters { */ struct scmi_debug_info { struct dentry *top_dentry; + struct dentry *protos; const char *name; const char *type; bool is_atomic; diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi= /driver.c index 1e2bf1eef2a4..4b3f291e623a 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -101,6 +102,8 @@ struct scmi_xfers_info { * has completed. * @ph: An embedded protocol handle that will be passed down to protocol * initialization code to identify this instance. + * @dbg: An optional reference to this protocol top debugfs directory; it = will + * be automatically recursively removed on protocol de-initialization. * * Each protocol is initialized independently once for each SCMI platform = in * which is defined by fwnode and implemented by the SCMI server fw. @@ -114,6 +117,7 @@ struct scmi_protocol_instance { unsigned int version; unsigned int negotiated_version; struct scmi_protocol_handle ph; + struct dentry *dbg; }; =20 #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph) @@ -2154,6 +2158,25 @@ static void scmi_common_fastchannel_db_ring(struct s= cmi_fc_db_info *db) SCMI_PROTO_FC_RING_DB(64); } =20 +static struct dentry * +scmi_debugfs_proto_dentry_get(const struct scmi_protocol_handle *ph) +{ + struct scmi_protocol_instance *pi =3D ph_to_pi(ph); + struct scmi_info *info =3D handle_to_scmi_info(pi->handle); + + if (!IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS) || !info->dbg) + return ERR_PTR(-ENODEV); + + if (!pi->dbg) { + char proto_dir[8]; + + snprintf(proto_dir, 8, "0x%02X", pi->proto->id); + pi->dbg =3D debugfs_create_dir(proto_dir, info->dbg->protos); + } + + return pi->dbg; +} + static const struct scmi_proto_helpers_ops helpers_ops =3D { .extended_name_get =3D scmi_common_extended_name_get, .get_max_msg_size =3D scmi_common_get_max_msg_size, @@ -2164,6 +2187,7 @@ static const struct scmi_proto_helpers_ops helpers_op= s =3D { .protocol_msg_check =3D scmi_protocol_msg_check, .fastchannel_init =3D scmi_common_fastchannel_init, .fastchannel_db_ring =3D scmi_common_fastchannel_db_ring, + .debugfs_proto_dentry_get =3D scmi_debugfs_proto_dentry_get, }; =20 /** @@ -2315,6 +2339,7 @@ scmi_alloc_init_protocol_instance(struct scmi_info *i= nfo, { int ret =3D -ENOMEM; void *gid; + struct dentry *dbg; struct scmi_protocol_instance *pi; const struct scmi_handle *handle =3D &info->handle; =20 @@ -2377,7 +2402,9 @@ scmi_alloc_init_protocol_instance(struct scmi_info *i= nfo, clean: /* Take care to put the protocol module's owner before releasing all */ scmi_protocol_put(proto); + dbg =3D pi->dbg; devres_release_group(handle->dev, gid); + debugfs_remove_recursive(dbg); out: return ERR_PTR(ret); } @@ -2455,6 +2482,7 @@ void scmi_protocol_release(const struct scmi_handle *= handle, u8 protocol_id) goto out; =20 if (refcount_dec_and_test(&pi->users)) { + struct dentry *dbg =3D pi->dbg; void *gid =3D pi->gid; =20 if (pi->proto->events) @@ -2468,6 +2496,14 @@ void scmi_protocol_release(const struct scmi_handle = *handle, u8 protocol_id) scmi_protocol_put(pi->proto); =20 devres_release_group(handle->dev, gid); + + /* + * Remove per-protocol top dir only after protocol release in + * order to allow any protocol specific cleanup on the pi->dbg + * subtree. + */ + debugfs_remove_recursive(dbg); + dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n", protocol_id); } @@ -3224,6 +3260,9 @@ static struct scmi_debug_info *scmi_debugfs_common_se= tup(struct scmi_info *info) if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS)) scmi_debugfs_counters_setup(dbg, trans); =20 + if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS)) + dbg->protos =3D debugfs_create_dir("protocols", top_dentry); + dbg->top_dentry =3D top_dentry; =20 if (devm_add_action_or_reset(info->dev, diff --git a/drivers/firmware/arm_scmi/protocols.h b/drivers/firmware/arm_s= cmi/protocols.h index 1aa3ee1fb2b0..c39ef77770d1 100644 --- a/drivers/firmware/arm_scmi/protocols.h +++ b/drivers/firmware/arm_scmi/protocols.h @@ -11,6 +11,7 @@ =20 #include #include +#include #include #include #include @@ -292,6 +293,9 @@ struct scmi_fc_info { * gathering FC descriptions from the SCMI platform server. * @fastchannel_db_ring: A common helper to ring a FC doorbell. * @get_max_msg_size: A common helper to get the maximum message size. + * @debugfs_proto_dentry_get: A common helper to get a per-protocol debugf= s top + * directory to use as a root. It will be + * recursively removed on protocol de-initialization. */ struct scmi_proto_helpers_ops { int (*extended_name_get)(const struct scmi_protocol_handle *ph, @@ -315,6 +319,8 @@ struct scmi_proto_helpers_ops { u32 *rate_limit); void (*fastchannel_db_ring)(struct scmi_fc_db_info *db); int (*get_max_msg_size)(const struct scmi_protocol_handle *ph); + struct dentry *(*debugfs_proto_dentry_get) + (const struct scmi_protocol_handle *ph); }; =20 /** diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index a2f46167883d..4b4c7b336c71 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -1119,7 +1119,6 @@ struct scmi_handle { void (*devm_protocol_put)(struct scmi_device *sdev, u8 proto); bool (*is_transport_atomic)(const struct scmi_handle *handle, unsigned int *atomic_threshold); - const struct scmi_notify_ops *notify_ops; }; =20 --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304676; b=E5gKXBceYeAiEIJB1ojo3tr9jFOZZIs9l4tLcSAU/7UuJabD5LXbXVRLKfMfkia9whDc Bvv9nn92nZRjuheMKP6etUB+QYzge8TgUeu373n2QYg1mNhWfDzq0vytEECtZBjFlJ+W4 6jUn575zuEDEpuPAIhClkliuFfVzWxq9d49HM5vA4ef0e7qvknBLwK0HjkRUksmeRnz1G 7Ld4t4rgbCRx7QZ2L5k5EfTkx7K0MzOOUjrMzU8/qzUC7cmitNc1qobR4uZwpik6ABq1G nLLHEjRZ8SRzrCxQr2ykgGXJuEhh33WLnNN0vPEOjpqnIT4+wvdjCYXEQ/aeVi54G2A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304676; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=QGW1IxiDX5VdKnuif5DjoMpJwzIpz+5fMstOX4KKTaE=; b=XrVdfI7ar42bQuE2XS/+WHz4HZZlkzgFYXf9mSKJTk4Cv+B3neRzE2VeJL2DWrnsmUmz hXmeRdfdMUIrcLaHf3abALmqWzLDhpz0mDXY4+kek1nu040MgqejAAaGL5Ul98GgetooR ygnwhCiWj0Gn6LGdh/qJPWA+TLelUCP6jvSiC6vH/tx0eZPi74ieeJnNQOKO51dPTGVag vgkWKTgCad2pNnJY9kR4BsUbogR/uI6WeWxmcd8R90nX3a/N6aLph4eUo/ZQagbPZJlzW mnNNP8l0AV3M3n2dK9As0DeDkWBY7C/H1uDeQ4sbfIJm0BNSxYusTMASilt+ZK9+69w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=cHJc+Qwj; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7A67B606A693; Sat, 21 Nov 2026 23:44:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7A67B606A693 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=cHJc+Qwj 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 0DABA1758; Sat, 21 Nov 2026 15:44:32 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1FDB33F632; Sat, 21 Nov 2026 15:44:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304674; bh=VHK/Zaew+zy2/DLFlWWTABBhoHjNkucYnAaXxyLYZ54=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cHJc+QwjfIHxmc/OjB8WljmHU8lwTapMQ2yy7rGQf81wnFSDHzH4VVXkheXw1FofA Lf+wwvk3LVM4iAdKmLxsF0x637WzARJhKlFmt5HbbCuMDqBm81QO8ONJRMi89ndCiQ OfVaKuJCFNJ8cPEqGxS13IR/ifQrvYPUpe60QzYI= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 22/31] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Date: Sat, 21 Nov 2026 23:39:56 +0000 Message-ID: <20261121234005.1525419-23-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expose one debugfs entry for each discovered SHMTI that can be used to dump the related SHMTI in binary form from TBGN up to TEND markers. No processing is done kernel side, beside using proper accessors for device memory. Signed-off-by: Cristian Marussi --- v15 --> v16 - initialize/destroy debugFS around SHMTIs init v14 --> v15 - cleanup local debugfs subtree with debugfs_remove_recursive on remove v12 --> v13 - get inode lock on time update v11 --> v12 - fix debugfs shmti dump file to use the SID as name v8 --> v9 - use kvzalloc/kfree - use file_update_time to update debugfs file ts v5 --> v6 - added mutex to protect binary dump buffer during reads (Sashiko) - using debugfs_create_file_size() (Sashiko) --- drivers/firmware/arm_scmi/telemetry.c | 103 ++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_s= cmi/telemetry.c index b74371bc41d5..cd0bd0015c82 100644 --- a/drivers/firmware/arm_scmi/telemetry.c +++ b/drivers/firmware/arm_scmi/telemetry.c @@ -9,9 +9,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -450,6 +452,14 @@ struct telemetry_shmti { #define TDCF_OFFS(_b, _p) \ ((unsigned long)((__force void *)(_p) - (__force void *)(_b))) =20 +struct scmi_telemetry_dbg_shmti { + struct telemetry_shmti *shmti; + size_t buf_sz; + void *buf; + /* Protect this descriptor from concurrent readers using it */ + struct mutex mtx; +}; + #define SHMTI_EPLG(s) \ ({ \ struct telemetry_shmti *_s =3D (s); \ @@ -562,6 +572,7 @@ struct telemetry_info { unsigned int default_blk_ts_rate; const struct scmi_protocol_handle *ph; struct telemetry_shmti *shmti; + struct dentry *shmti_top; struct telemetry_de *tdes; struct xarray xa_des; struct telemetry_uuid *primary_uuid; @@ -1356,6 +1367,10 @@ static void scmi_telemetry_tdes_cleanup(void *arg) scmi_telemetry_de_unlink(&tde->de); scmi_telemetry_free_tde_put(ti, tde); } + + /* Safe removal of SHMTI debugfs tree before freeing underlying data */ + if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS)) + debugfs_remove_recursive(ti->shmti_top); } =20 static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti) @@ -1533,6 +1548,91 @@ scmi_telemetry_enumerate_common_intervals(struct tel= emetry_info *ti) ti->info.intervals); } =20 +static int scmi_telemetry_dbg_shmti_open(struct inode *inode, struct file = *filp) +{ + struct scmi_telemetry_dbg_shmti *sblob; + struct telemetry_shmti *shmti; + + if (!inode->i_private) + return -ENODEV; + + shmti =3D inode->i_private; + sblob =3D kzalloc_obj(*sblob); + if (!sblob) + return -ENOMEM; + + sblob->shmti =3D shmti; + sblob->buf =3D kvzalloc(shmti->info.len, GFP_KERNEL); + if (!sblob->buf) { + kfree(sblob); + return -ENOMEM; + } + + mutex_init(&sblob->mtx); + + filp->private_data =3D sblob; + + return 0; +} + +static ssize_t scmi_telemetry_dbg_shmti_read(struct file *filp, char __use= r *buf, + size_t count, loff_t *ppos) +{ + struct scmi_telemetry_dbg_shmti *sblob =3D filp->private_data; + + /* Dump on first read and again after each rewind to start pos */ + guard(mutex)(&sblob->mtx); + if (!sblob->buf_sz || *ppos =3D=3D 0) { + struct inode *ino =3D file_inode(filp); + + memcpy_fromio(sblob->buf, sblob->shmti->base, + sblob->shmti->info.len); + sblob->buf_sz =3D sblob->shmti->info.len; + /* update inode timestamps */ + inode_lock(ino); + file_update_time(filp); + inode_unlock(ino); + } + + return simple_read_from_buffer(buf, count, ppos, sblob->buf, sblob->buf_s= z); +} + +static int scmi_telemetry_dbg_shmti_release(struct inode *inode, struct fi= le *filp) +{ + struct scmi_telemetry_dbg_shmti *sblob =3D filp->private_data; + + kvfree(sblob->buf); + kfree(sblob); + + return 0; +} + +static const struct file_operations scmi_telemetry_dbg_shmti_fops =3D { + .open =3D scmi_telemetry_dbg_shmti_open, + .release =3D scmi_telemetry_dbg_shmti_release, + .read =3D scmi_telemetry_dbg_shmti_read, + .llseek =3D generic_file_llseek, + .owner =3D THIS_MODULE, +}; + +static void scmi_telemetry_debugfs_initialize(struct telemetry_info *ti) +{ + const struct scmi_protocol_handle *ph =3D ti->ph; + struct dentry *top; + + top =3D ph->hops->debugfs_proto_dentry_get(ph); + ti->shmti_top =3D debugfs_create_dir("shmtis", top); + + for (unsigned int i =3D 0; i < ti->num_shmti; i++) { + char id[16]; + + snprintf(id, 16, "%u", ti->shmti[i].info.sid); + debugfs_create_file_size(id, 0444, ti->shmti_top, &ti->shmti[i], + &scmi_telemetry_dbg_shmti_fops, + ti->shmti[i].info.len); + } +} + static int iter_shmti_update_state(struct scmi_iterator_state *st, const void *response, void *priv) { @@ -1748,6 +1848,9 @@ static int scmi_telemetry_enumerate_shmti(struct tele= metry_info *ti) /* Expose the fully initialized SHMTI infos */ smp_store_release(&ti->info.base.num_shmtis, ti->num_shmti); =20 + if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_PROTOCOLS)) + scmi_telemetry_debugfs_initialize(ti); + return devm_add_action_or_reset(ph->dev, scmi_telemetry_tdes_cleanup, ti); } =20 --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304678; b=2Zn8QWrwq066Ex8C2wZPAJWNSEC7C9KxnZuqRM3pyNA/EjiX+wIet3zQ6CCD6b+pxlIZ JXiO+ildgIQgWSTX3P7p5w0940Uc4lEXCwcOMe88TaxuOjtu0rlU5yngkFnijGVaItPdn 01DkV651ZouMkbOREiZ8jDFrPjAg/rsWBfkk5rwFMDjOHxxIMYdD6rtjKVqsyUf6elO0C ii4Si/O8a/Pj2x1opNjK8y6rbG9m6YAuJEEt1nTDlPhdbv40Ouu3fwy0HaIAeBebb5Ada knEZb41AzrOdklZgMpQi19TvUCpGHbRnsiE8PAAX0ZNgUBvyE76ZD12Bj9y2fTdV9kw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304678; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=UXv2USDLjxCmkBT7t673t/L9uechNCEsT7mnauJfqOk=; b=hvGSdnATGTGMXepeHbxjWv6XeNvw1dNzM6b48URboO5aNV9MyhKB+PT2cL8R7ur94Rnd WIKVZAM61gjvqtSzf1oiwNC2KtL/NxsgAnZY33FGD6mAMLfSk2DwBMej3va8MLJ+JyCDn 1f8qqxh+20NWkQLMPuyiQVCjgp/baIiSiVgZalqLoq5dEQmgHD6qIyLL9z255+EZhQfTy hr1uaRH1o6oDnqTDcN+A5QpSiJHatEFQHGyBqD8N6Q6Cy6ZVH6kBEuiPPdTRO3GM9Filp Wf8yYFwFqos6Z9FIDIt+PVmja4h2rbbHx4kqJ+N9wzzPSPIOdHd5Mz7WRU1EKVBXNqg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=GG0qy4bR; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D5A1606A6A5; Sat, 21 Nov 2026 23:44:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7D5A1606A6A5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=GG0qy4bR 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 2E80B176B; Sat, 21 Nov 2026 15:44:35 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3F96E3F632; Sat, 21 Nov 2026 15:44:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304678; bh=jI3f070raLj7/k00GZw1QBRqUT86GTz73dk4UnjUdbY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GG0qy4bR4J3mMbCO5QmC4XLLLwXuy5kkvLJaDsROFV68mOnswmQmp6B4K9lA7qITB SR9mXXsxvVy4ko35NQfSn8oYH9W1NTDYlzh9Wv3ji7xYZcrk4GJefIs9hjr3rpgBoW +boVd+sPL+ngxXBPkltixrNgDakBKYKVslWOOkvI= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 23/31] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Date: Sat, 21 Nov 2026 23:39:57 +0000 Message-ID: <20261121234005.1525419-24-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add description of the debugfs SCMI Telemetry protocol ABI. Signed-off-by: Cristian Marussi --- v15 --> v16 - fix Kernel version/date v5 --> v6 - fixed typos --- Documentation/ABI/testing/debugfs-scmi | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Documentation/ABI/testing/debugfs-scmi b/Documentation/ABI/tes= ting/debugfs-scmi index ee7179ab2edf..b3e09142fa08 100644 --- a/Documentation/ABI/testing/debugfs-scmi +++ b/Documentation/ABI/testing/debugfs-scmi @@ -68,3 +68,25 @@ Description: Max number of concurrently allowed in-fligh= t SCMI messages for the currently configured SCMI transport for instance on the RX channels. Users: Debugging, any userspace test suite + +What: /sys/kernel/debug/scmi//protocols/0x/ +Date: Feb 2027 +KernelVersion: 7.5 +Contact: cristian.marussi@arm.com +Description: A subdirectory grouping debug entries related to protocol + for instance . Each protocol owns and defines the subtree + of entries rooted under this directory. +Users: Debugging, any userspace test suite + +What: /sys/kernel/debug/scmi//protocols/0x1B/shmtis/ +Date: Feb 2027 +KernelVersion: 7.5 +Contact: cristian.marussi@arm.com +Description: A set of RO files exposed by the Telemetry protocol (0x1B) in + order to dump the latest snapshot of each SHMTI memory area in + binary format: each file is named by its SHMTI id. + File is seekable and a seek to position zero on an open file + causes the SHMTI snapshot to be refreshed; file timestamps are + updated after each snapshot. + This directory reports SHMTIs for instance . +Users: Debugging, any userspace test suite --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304682; b=13EOQ1ekmH2ZKu0Fun4cvfdi35k2VP70ONl0zoHzI/0Gh/kuhV6iFIZs6jGNktKWt5fH fiuyOLBap2/W2ahyHU26QilDHVlYIGN4bj5c2qmVqPvNIhatHxY/qURi3mxLY4FO1J/66 s7jEeNJMhex4Ycm6aiXEjpqNHhrIxaHnQ5jWzcAbfJd3svOXQZR07ROaA/QdX6q8sLKF/ RrhDSJ6Nh/tSRjAAzlcRIhCG0GetYHOfo738ZvafgMLXQE6a6t9aWtRZ+4f9U8W7hGkn+ UV/lX+8G3o71pm4ykbxikd/EhXk4kih6E2X5Yl8DvhsVT7u2QBx5zhSQMcvVWM7edFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304682; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Yfdde+/hJowzTbg0KjHhSlaVR/+nDe4W6soN7D1vscU=; b=xhFg9muliDWXdArGIyrCL9zwJWxi7ZV7+KejmXCf0qKl8xWQWEG7/bT8GlksM0m0ZwrA YSX5/1fqyP2DNRi55Kj1gX8lixOj3SL3CifmGEYARRzL0GoRtyvTcq53hG4r+tgCcoA0m ZPHsSQKF+0UP8onuXCLZJ2yQNf1Ht83a0j62Xg+l0Qvm1HISKN5SrUNavdu7t8pBliTB/ PHvWjdBOdsLAMxkdph4LuoJ0YoaUsiBEs453bwY4TbPRaZiiU18PDc78tNiDRNyNDEdyS Ll4ZHwXqmXnjUTgKPjcB9T7nDiFCBUxbZiSK2s/PwL3zpgFn2VX+PBIEDfnZD4XuHPQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=ZiNGVtjp; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E000B606A331; Sat, 21 Nov 2026 23:44:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E000B606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=ZiNGVtjp 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 4BB5E175D; Sat, 21 Nov 2026 15:44:38 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5FDAF3F632; Sat, 21 Nov 2026 15:44:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304681; bh=MXfhRpApggBTfwv/7/s4oWFtSJgBGz+mbDFJ2DLWHjI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZiNGVtjp/2ZT9xZrQL4FhHdZxNgCY+hrOBg60Dt9PyXrs6qFLUzd8Bu0p8ni3S/Ss 9HAbxks+eivZoHAhgqxDyHZDsJKknjZ6ziiKJK2w/frGSbV03Dkn+OpbrQWP4Gremn 5TvC/DugIYHTaGodq+N5yIC4AnosI7bCri46LtKU= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 24/31] firmware: arm_scmi: Expose per-instance identifier Date: Sat, 21 Nov 2026 23:39:58 +0000 Message-ID: <20261121234005.1525419-25-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When multiple SCMI instances are configured, the SCMI stack probes and it is initialized multiple times: one core stack for each defined instance. Each istance is simply identified internally with a number and such number is already exposed to the user to name the root of the per-instance debugfs subtree, if enabled. Expose such unique identifier to the SCMI drivers so that they can use that same identifier when in need to name their per-instance resources. Signed-off-by: Cristian Marussi --- v7 --> v8 - fixed Doxygen 'non-negative' integer --- drivers/firmware/arm_scmi/driver.c | 1 + include/linux/scmi_protocol.h | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi= /driver.c index 4b3f291e623a..5203872b57e1 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -3451,6 +3451,7 @@ static int scmi_probe(struct platform_device *pdev) idr_init(&info->rx_idr); =20 handle =3D &info->handle; + handle->id =3D info->id; handle->dev =3D info->dev; handle->version =3D &info->version; handle->devm_protocol_acquire =3D scmi_devm_protocol_acquire; diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index 4b4c7b336c71..12a980dbc7c8 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -1088,6 +1088,10 @@ struct scmi_notify_ops { /** * struct scmi_handle - Handle returned to ARM SCMI clients for usage. * + * @id: A unique non-negative natural integer identifying the SCMI Instance + * associated with this handle to be used across all drivers for + * naming purposes: same identifier used internally as the root for + * the debugfs per-instance tree. * @dev: pointer to the SCMI device * @version: pointer to the structure containing SCMI version information * @devm_protocol_acquire: devres managed method to get hold of a protocol, @@ -1108,6 +1112,7 @@ struct scmi_notify_ops { * @notify_ops: pointer to set of notifications related operations */ struct scmi_handle { + unsigned int id; struct device *dev; struct scmi_base_info *version; =20 --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304685; b=KCMl/kS/hQM0VsDbrr0le/An2OOlWtVQMF9GNgss0SBWKfN4RCvjyYdqG9ndypC3JPyH MXEEdTxBoAGRoZdDKGpCiSU61tESATQw38tIfT/qRppQ3kooC+6yj4SZlMvGKqC6OeULv dZZhEkkhBJVg/8pjD7igljvfLZrNEurGu+/QCEuf2qSR01e0xQasNXhTjeqT1SRUMdvMB HTgQee/Jk+Yddzv/iJ4VCtaxJAEulAOuK9+5EuIpa7l4GC8dlI7AEkx//YiOPlFLXV/xd P8V37r204fA11Ble8SDpbaDA0l12ASdSyiKVC1j2Qtlj6qv7m0quFl0df9d9lWP2zvg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304685; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=LEKvVJyAQjl3c1hfexhVXWOBop6fiwvVoyf2821FCU8=; b=TE09jysW2TC5UnFiYuytvRPfi6vW/2+9Uqyq+vbORKvRqsItY04XfAqM6Slem5HXLoF7 0kFilUZdZd7Wu+TLo2We/OUkQyxUTckTO+eCgSlTN6O9Xj2xxLwx1zeCj/K+OMy/rtaCZ VH8SK6V0hSMIz2KxGQG8ZtPRFRQX11lsQzYUI74wfRZvEBxuWsk+BKg/fg20ADnGWu70n 2xkQ/oheCuioViEQiCwk8BSKITQn+bF+VOdtoIHWCbPz25pkJ65pBnQlV831r3X+XOYTt TSwo8MxrzOFEVUjwl/ppxSsLfjCD6Xhu5yFjRMD2qTNRwZZrB2urDbny9n2qskQi3YQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=sSyMC+X5; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE446606A699; Sat, 21 Nov 2026 23:44:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BE446606A699 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=sSyMC+X5 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 68BE8176C; Sat, 21 Nov 2026 15:44:41 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7F1AE3F632; Sat, 21 Nov 2026 15:44:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304684; bh=42U57OWWpKUHsh+ow5101qZkL8rOieg7JRO1M+0Mbhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sSyMC+X50Irjxd7yCzynK6YiIp+gMYkNS4yZd+fCFIvXRbDHF7Wr4SeTz16/bmt+k 7IZuG7v8gU/ICjrizrktUagxvkfbOsvVVeotHWQdN6KGg92ZlOS81XR2PjpCvff4ch jTwTEw1+sng8fXsBDv+lnQv32zxm9qx++G+u5ISg= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 25/31] uapi: Add ARM SCMI Telemetry definitions Date: Sat, 21 Nov 2026 23:39:59 +0000 Message-ID: <20261121234005.1525419-26-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a number of structures and ioctls definitions used by the ARM SCMI Telemetry protocol. Signed-off-by: Cristian Marussi --- v15 --> v16 - added GROUPS/SINGLE_SAMPLE abi_features flags definitions v14 --> v15 - a couple of clarification in doxygen v11 --> v12 - fixed documentation around event lifetime cycle to align with the new implemented logic v10 --> v11 - typos fixing and comments fixup - more docs on BATCH IOCTLs and structures - more docs on SHMTI_LIST - more docs on EVENT_REGISTER - more docs on GET_INTRVS upper bounds and zero-query - more docs on BULK/SINGLE upper bounds and zero-query v8 --> v9 - Simplified ABI related to _LIST IOCTL, substituting all custom container structures in favour of the usage of scmi_tlm_batch, i.e. dropping: - scmi_tlm_des_list - scmi_tlm_uuid_list - scmi_tlm_grps_list - scmi_tlm_shmtis_list - Updated related _LIST documentation v7 --> v8 - Fixes for some Sashiko issues on v7 - fix typos and wrong struct reference in SCMI_TLM_BATCH_READ - move a couple of IOCTL to be _IOW - add a few more comments on usage and behaviour of BATCH ops v6 --> v7 - added per-DE UUID/SID/OFFS - IOCTL EVENTS: generation counter - generic BATCH support (scmi_tlm_batch) - BATCH READ with perf-DE status - BATCH DE GET/SET CONFIG - added reserved fields for grow - added more ABI features flags - better Doxygen desciption for IN/OUT params v5 --> v6 - Fixed interval exp to be __s32 as documented - Using a more unique name for IOCTL MAGIC define - Renamed a few IOCTLs to be consistent - redefine primary UUID as an array of u8 - add GET_ABI_INFO to gather ABI version and instance info - add RESET support - fixed typos in kdoc v4 --> v5 - Added more structure padding - Removed frowned upon flexible array in favour of u64 pointers to external userspace provided buffer - Added UAPI support to access SHMTIs from userspace - Cleaned up Doxygen v2 --> v3 - Change tstamp_exp tp ts_rate - renamed num -> num_intervals in scmi_tlm_interval - added padding in scmi_tlm_de_sample to avoid packing issues on 32bit v1 --> v2 - Added proper __counted_by marks - Fixed a few dox comments - Renamed reserved[] fields to pad[] --- MAINTAINERS | 1 + include/uapi/linux/scmi.h | 533 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 534 insertions(+) create mode 100644 include/uapi/linux/scmi.h diff --git a/MAINTAINERS b/MAINTAINERS index ccbcdc0799b9..8c203b2be066 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -27345,6 +27345,7 @@ F: drivers/reset/reset-scmi.c F: include/linux/device-id/scmi.h F: include/linux/sc[mp]i_protocol.h F: include/trace/events/scmi.h +F: include/uapi/linux/scmi.h F: include/uapi/linux/virtio_scmi.h =20 SYSTEM CONTROL MANAGEMENT INTERFACE (SCMI) i.MX Extension Message Protocol= drivers diff --git a/include/uapi/linux/scmi.h b/include/uapi/linux/scmi.h new file mode 100644 index 000000000000..726989fbfc76 --- /dev/null +++ b/include/uapi/linux/scmi.h @@ -0,0 +1,533 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Copyright (C) 2026 ARM Ltd. + */ +#ifndef _UAPI_LINUX_SCMI_H +#define _UAPI_LINUX_SCMI_H + +/* + * Userspace interface SCMI Telemetry + */ + +#include +#include + +#define SCMI_TLM_ABI_VERSION_V1 1 +#define SCMI_TLM_CURRENT_ABI_VERSION SCMI_TLM_ABI_VERSION_V1 +#define SCMI_TLM_DE_IMPL_UUID_SZ 16 + +#define SCMI_TLM_SHMTI_ID_INVALID 0xFFFFFFFF + +/** + * struct scmi_tlm_abi_info - Basic information on the ABI/instance + * + * SCMI Telemetry characteristics like number of DEs, Groups, Intervals and + * SHMTIs are statically determined by the platform and do not change with= in + * one boot/session; they are reported here so that they can be used to pr= operly + * size the requests on other IOCTLs, without having to discover dynamical= ly + * those sizes by issuing a zero-sized IOCTL request at first. + * + * @size: sizeof this structure - IN + * @abi_version: ABI Version - OUT + * @abi_features: ABI capabilities bitmap - OUT + * @primary_de_impl_version: SCMI Telemetry DE implementation revision - O= UT + * @num_des: Number of defined DEs - OUT + * @num_groups: Number of defined DEs groups - OUT + * @num_intervals: Number of available update intervals (instance-level) -= OUT + * @num_shmtis: Number of discovered SHMTI areas - OUT + * @features: Instance specific SCMI feature-support bitmap - OUT + * @reserved: Some room for future expansion without the need to resize. + * Must be zero. + * + * Used by: + * RW - SCMI_TLM_GET_ABI_INFO + */ +struct scmi_tlm_abi_info { + __u32 size; + __u32 abi_version; + __u32 abi_features; +#define SCMI_TLM_ABI_FEAT_RESET (1 << 0) +#define SCMI_TLM_ABI_FEAT_EVENT (1 << 1) +#define SCMI_TLM_ABI_FEAT_UUID_LIST (1 << 2) +#define SCMI_TLM_ABI_FEAT_BATCH_STATE (1 << 3) +#define SCMI_TLM_ABI_FEAT_BATCHED_CFG (1 << 4) +#define SCMI_TLM_ABI_FEAT_DE_TRACKING (1 << 5) +#define SCMI_TLM_ABI_FEAT_GROUPS (1 << 6) +#define SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE (1 << 7) + __u8 primary_de_impl_version[SCMI_TLM_DE_IMPL_UUID_SZ]; + __u32 num_des; + __u32 num_groups; + __u32 num_intervals; + __u32 num_shmtis; + __u32 features; +#define SCMI_TLM_SCMI_SUPPORT_RESET (1 << 0) +#define SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE (1 << 1) +#define SCMI_TLM_SCMI_SUPPORT_GROUP_CONFIG (1 << 2) +#define SCMI_TLM_SCMI_SUPPORT_UPDATE_NOTIFICATION (1 << 3) + __u64 reserved; +}; + +/** + * struct scmi_tlm_update_interval - Update interval descriptor + * + * @secs: integer representing seconds - OUT + * @exp: signed integer representing the base 10 exponent used as a multip= lier + * with @secs to represent the interval - OUT + * + * The resulting update interval is calculated as: * 10 ^ + */ +struct scmi_tlm_update_interval { + __u32 secs; + __s32 exp; +}; + +/** + * struct scmi_tlm_config - Whole instance or group configuration + * + * @enable: Get/Set Telemetry enabled state for the whole instance or the + * group specified in @grp_id - IN/OUT + * @t_enable: Get/Set timestamp state for the group specified in @grp_id -= IN/OUT + * @flags: Bitmask to represent special characteristics - IN + * @pad: Padding fields to enforce alignment + * @grp_id: Identifier of the target group upon which this configuration w= ill + * be applied: ignored if not marked as a group in @flags - IN/OUT + * @active: Get/Set currently active update interval for the whole instance + * or the group specified in @grp_id - IN/OUT + * @reserved: Some room for future expansion. Must be zero. + * + * Used by: + * RW - SCMI_TLM_GET_CFG + * WO - SCMI_TLM_SET_CFG + */ +struct scmi_tlm_config { + __u8 enable; + __u8 t_enable; + __u8 flags; +#define SCMI_TLM_CONFIG_GROUP (1 << 0) +#define SCMI_TLM_CONFIG_FLAGS (SCMI_TLM_CONFIG_GROUP) +#define SCMI_TLM_CONFIG_IS_GROUP(f) ((f) & SCMI_TLM_CONFIG_GROUP) + __u8 pad; + __u32 grp_id; + struct scmi_tlm_update_interval active; + __u64 reserved; +}; + +/** + * struct scmi_tlm_intervals - Update intervals descriptor + * + * @grp_id: Identifier of the target group upon which this configuration + * will be applied: ignored if not marked as a group in @flags - IN + * @flags: Bitmask to represent special characteristics. When the interval + * is NOT marked as SCMI_TLM_INTERV_DISCRETE, @intervals will + * contain a triplet: min/max/step - OUT + * @pad: Padding fields to enforce alignment + * @num_intervals: Number of entries of @intervals. On input this represen= ts the + * size of @intervals, while on output carries the effective + * number of entries filled-in. + * On input this has an upper-bound determined as: + * - @scmi_tlm_abi_info.num_intervals if NOT referring a group + * OR + * - @scmi_tlm_grp_info.num_intervals if referring a group + * When provided as zero it will return the upper-bound itself + * leaving @intervals untouched - IN/OUT + * @pad2: Padding fields to enforce alignment + * @reserved: Some room for future expansion. Must be zero. + * @intervals: A pointer to an array of struct scmi_tlm_update_interval - = IN/OUT + * + * Used by: + * RW - SCMI_TLM_GET_INTRVS + */ +struct scmi_tlm_intervals { + __u32 grp_id; + __u8 flags; +#define SCMI_TLM_INTERV_GROUP (1 << 0) +#define SCMI_TLM_INTERV_DISCRETE (1 << 1) +#define SCMI_TLM_INTERV_FLAGS \ + (SCMI_TLM_INTERV_GROUP | SCMI_TLM_INTERV_DISCRETE) +#define SCMI_TLM_INTERV_IS_GROUP(f) ((f) & SCMI_TLM_INTERV_GROUP) + __u8 pad[3]; + __u32 num_intervals; + __u32 pad2; + __u64 reserved; +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW 0 +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH 1 +#define SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP 2 + __u64 intervals; +}; + +/** + * struct scmi_tlm_de_config - DE configuration + * + * This descriptor is used directly by the aggregate SCMI_TLM_GET_ALL_CFG = and + * SCMI_TLM_SET_ALL_CFG IOCTLs, or embedded into an @scmi_tlm_batch reques= t for + * SCMI_TLM_GET_DE_CFG and SCMI_TLM_SET_DE_CFG IOCTLs. + * + * @id: Identifier of the DE, ignored by SCMI_TLM_GET/SET_ALL_CFG - IN + * @enable: Get/Set the enabled state of this single DE or the cumulative = ALL + * state - IN/OUT + * @t_enable: Get/Set the timestamp state of this single DE (if supported)= or + * the cumulative ALL state - IN/OUT + * @sid: An integer representing the SHMTI ID that contains this DE. Valid + * ONLY when the related DE is enabled - OUT + * @offset: An integer representing the offset in the SHMTI @sid that iden= tifies + * the start of the TDCF DataLine containing this DE. + * Valid ONLY when the related DE is enabled - OUT + * @pad: Padding fields to enforce alignment + * @uuid: An array containing the UUID, in BigEndian format, associated to + * this DE. Valid ONLY when the related DE is enabled: this UUID can + * be the primary UUID or one of the secondary UUIDs if any exist. + * Note that this UUID/DE association is permanent within the same + * boot/session - OUT + * @reserved: Some room for future expansion. Must be zero. + * + * Used by: + * RW - SCMI_TLM_GET_ALL_CFG + * WO - SCMI_TLM_SET_ALL_CFG + * RW - SCMI_TLM_GET_DE_CFG (within a BATCH) + * RW - SCMI_TLM_SET_DE_CFG (within a BATCH) + */ +struct scmi_tlm_de_config { + __u32 id; + __u32 enable; + __u32 t_enable; + __u32 sid; + __u32 offset; + __u32 pad; + __u8 uuid[SCMI_TLM_DE_IMPL_UUID_SZ]; + __u64 reserved; +}; + +/** + * struct scmi_tlm_de_info - DataEvent Descriptor + * + * @id: DE identifier - IN/OUT + * @grp_id: Identifier of the group which this DE belongs to; valid only if + * this DE is marked as belonging to a group in @flags - IN/OUT + * @data_sz: DE data size in bytes - OUT + * @type: DE type - OUT + * @unit: DE unit of measurements - OUT + * @unit_exp: Power-of-10 multiplier for DE unit - OUT + * @ts_rate: Clock rate in kHz used to generate the DE timestamp - OUT + * @instance_id: DE instance ID - OUT + * @compo_instance_id: DE component instance ID - OUT + * @compo_type: Type of component which is associated to this DE - OUT + * @persistent: Data value for this DE survives reboot (non-cold ones) - O= UT + * @flags: Bitmask to represent special characteristics - IN + * @pad: Padding fields to enforce alignment + * @pad2: Padding fields to enforce alignment + * @name: Name of this DE - OUT + * @reserved: Some room for future expansion. Must be zero. + * + * Used to get the full description of a DE: it reflects DE Descriptors + * definitions in SCMI V4.0 specification at 3.12.4.6. + * + * Used by: + * RW - SCMI_TLM_GET_DE_INFO + * RW - SCMI_TLM_GET_DE_LIST (within a @scmi_tlm_batch) + */ +struct scmi_tlm_de_info { + __u32 id; + __u32 grp_id; + __u32 data_sz; + __u32 type; + __u32 unit; + __s32 unit_exp; + __u32 ts_rate; + __u32 instance_id; + __u32 compo_instance_id; + __u32 compo_type; + __u8 persistent; + __u8 flags; +#define SCMI_TLM_DEINFO_GROUP (1 << 0) +#define SCMI_TLM_DEINFO_FLAGS (SCMI_TLM_DEINFO_GROUP) +#define SCMI_TLM_DEINFO_HAS_GROUP(f) ((f) & SCMI_TLM_DEINFO_GROUP) + __u8 pad[2]; + __u32 pad2; + __u8 name[16]; + __u64 reserved; +}; + +/** + * struct scmi_tlm_de_sample - A DataEvent reading + * + * @id: DE identifier - IN + * @pad: Padding fields to enforce alignment. + * @tstamp: DE reading timestamp (0 if timestamp NOT supported) - OUT + * @val: Reading of the DE data value - OUT + * + * Used by: + * RW - SCMI_TLM_DE_READ + * RW - SCMI_TLM_BATCH_READ (within an @scmi_tlm_batch) + */ +struct scmi_tlm_de_sample { + __u32 id; + __u32 pad; + __u64 tstamp; + __u64 val; +}; + +/** + * struct scmi_tlm_data_read - Bulk read of a number of DataEvents + * + * @grp_id: Optional group ID number, ignored if not marked as a group req= uest + * in @flags and not supported by SCMI_TLM_BATCH_READ - IN + * @flags: Bitmask to represent special characteristics - IN + * @pad: Padding fields to enforce alignment + * @pad2: Padding fields to enforce alignment + * @num_samples: Number of entries in @samples. In input represents the si= ze of + * @samples, while in output carries the effective number of items + * filled-in. + * On input this has an upper-bound determined as: + * - @scmi_tlm_abi_info.num_des if NOT referring a group + * OR + * - @scmi_tlm_grp_info.num_des if referring a group + * When provided as zero it will return the upper-bound itself + * leaving @samples untouched - IN/OUT + * @samples: A reference to an array of struct scmi_tlm_de_sample containi= ng + * an entry for each DE - IN/OUT + * + * Used by: + * RW - SCMI_TLM_SINGLE_READ + * RW - SCMI_TLM_BULK_READ + */ +struct scmi_tlm_data_read { + __u32 grp_id; + __u8 flags; +#define SCMI_TLM_READ_GROUP (1 << 0) +#define SCMI_TLM_READ_FLAGS (SCMI_TLM_READ_GROUP) +#define SCMI_TLM_READ_IS_GROUP(f) ((f) & SCMI_TLM_READ_GROUP) + __u8 pad[3]; + __u32 pad2; + __u32 num_samples; + __u64 samples; +}; + +/** + * struct scmi_tlm_batch - A wrapper structure for BATCH operations + * + * This structure is used as an opaque container for a set of objects of t= he + * same kind: it is used by a number of different IOCTLs and it is up to t= he + * specific IOCTL implementation to enforce a proper upper-bound on the + * maximum amount of items that can be specified in @num_items. + * + * Exceeding the IOCTL-specific upper bound in @num_items leads to an error + * (-EINVAL), while issuing an IOCTL requesting in @num_items less than the + * related maximum upper-bound it will succeed and simply return only the + * required @num_items. + * + * It is possible to discover each specific upper-bound in a couple of + * alternative ways: + * + * - by issuing a ZERO sized query IOCTl at first (as detailed down below) + * for any of the IOCTLs that use the @scmi_tlm_batch parameter. + * + * - by querying in advance, once for all, the platform to retrieve all the + * statically defined resource counters with SCMI_TLM_GET_ABI_INFO, when + * supported by the IOCTL. + * + * The per-IOCTL statically defined upper-bounds are defined as follows: + * + * - SCMI_TLM_BATCH_READ: scmi_tlm_abi_info.num_des + * - SCMI_TLM_GET_DE_CFG: scmi_tlm_abi_info.num_des + * - SCMI_TLM_SET_DE_CFG: scmi_tlm_abi_info.num_des + * - SCMI_TLM_GET_DE_LIST: scmi_tlm_abi_info.num_des + * - SCMI_TLM_GET_GRP_LIST: scmi_tlm_abi_info.num_groups + * - SCMI_TLM_GET_SHMTI_LIST: scmi_tlm_abi_info.num_shmtis + * + * NOTE that the values returned by SCMI_TLM_GET_UUID_LIST vary dynamicall= y at + * runtime depending on the actual configuration: as a consequence its mos= t up + * to date uppper bound can only be discovered dynamically through zero-si= zed + * querying. + * + * The @states field is optional and when it is NULL it is up to the speci= fic + * IOCTL implementation to decide which policy to enforce in case of failu= res + * in the middle of the batch processing: as of now each BATCHED IOCTLs fa= ils + * as a whole as soon as one of the batched requests fails, without any ki= nd + * of roll-back of the previously succeeded requests. + * + * When this container structure is used as parameter for one the _LIST + * IOCTLs listed below, @states is ignored in any case, and the @num_items + * field can be set to zero in input to query the system about the maximum + * number of available resources. + * Note that this idiomatic query-style is made available as an alternative + * usage pattern, but somr of this information is usually statically defin= ed + * within the same session/boot and available by other means (IOCTLs). + * + * The field @item_sz MUST match the expected payload size for the specific + * IOCTL and it is supposed to be meaningfully set ALSO when @num_items is + * zero. + * + * @num_items: Number of items in @items and @states - IN/OUT + * @item_sz: Size of a single item contained in @items - IN + * @reserved: Some room for future expansion. Must be zero. + * @states: An optional reference to an array of s32 items representing t= he + * outcome of the requests for each single item in @items: these are + * ordered in the same order as the @items. - OUT + * @items: A reference to an array of batched requests - IN/OUT + * + * Used by: + * RW - SCMI_TLM_BATCH_READ - container of @scmi_tlm_de_sample + * RW - SCMI_TLM_GET_DE_CFG - container of @scmi_tlm_de_config + * RW - SCMI_TLM_SET_DE_CFG - container of @scmi_tlm_de_config + * RW - SCMI_TLM_GET_DE_LIST - container of @scmi_tlm_de_info + * RW - SCMI_TLM_GET_UUID_LIST - container of @scmi_tlm_uuid + * RW - SCMI_TLM_GET_GRP_LIST - container of @scmi_tlm_grp_info + * RW - SCMI_TLM_GET_SHMTI_LIST - container of @scmi_tlm_shmti_info + */ +struct scmi_tlm_batch { + __u32 num_items; + __u32 item_sz; + __u64 reserved; + __u64 states; + __u64 items; +}; + +/** + * struct scmi_tlm_grp_info - Group info descriptor + * + * @grp_id: Group ID number - IN + * @num_des: Number of DEs part of this group - OUT + * @num_intervals: Number of update intervals supported. Zero if group doe= s not + * support per-group update interval configuration. - OUT + * @pad: Padding fields to enforce alignment + * @reserved: Some room for future expansion. Must be zero. + * + * Used by: + * RW - SCMI_TLM_GET_GRP_INFO + * RW - SCMI_TLM_GET_GRP_LIST (within a BATCH) + */ +struct scmi_tlm_grp_info { + __u32 grp_id; + __u32 num_des; + __u32 num_intervals; + __u32 pad; + __u64 reserved; +}; + +/** + * struct scmi_tlm_grp_desc - Group descriptor + * + * @grp_id: Group ID number - IN + * @num_des: Number of DEs composing this group. In input represents the s= ize + * of @composing_des, while in output carries the effective number + * of items filled-in - IN/OUT + * @composing_des: A reference to an array of __u32 elements containing the + * DataEvent IDs composing this group. - IN/OUT + * @reserved: Some room for future expansion. Must be zero. + * + * Used by: + * RW - SCMI_TLM_GET_GRP_DESC + */ +struct scmi_tlm_grp_desc { + __u32 grp_id; + __u32 num_des; + __u64 composing_des; + __u64 reserved; +}; + +/** + * struct scmi_tlm_shmti_info - SHMTI descriptor + * + * @sid: SHMTI ID - IN + * @fd: Associated opened file descriptor to use for mmap. This file descr= iptor + * can only be used to mmap the SHMTIs to read-only pages and it is + * implicitly marked as O_CLOEXEC. The lifetime of any associated mapping + * obtained from @fd is independent from it and from the file descriptor + * used to issue the related IOCTL - OUT + * @len: Effective size of the accessible area of the SHMTI that will be m= apped + * using @fd. Note that, since mmap allows mapping length of multiple of + * PAGE_SIZE, this @len implicitly sets the upper-bound of the number of + * PAGE_SIZE pages that you are allowed to request - OUT + * @offset: Offset in the obtained mmap where the specified SHMTI start - = OUT + * @reserved: Some room for future expansion. Must be zero. + * + * Used by: + * RW - SCMI_TLM_GET_SHMTI_LIST (within an @scmi_tlm_batch) + */ +struct scmi_tlm_shmti_info { + __u32 sid; + __u32 fd; + __u32 len; + __u32 offset; + __u64 reserved; +}; + +/** + * struct scmi_tlm_uuid - UUID descriptor + * + * @bytes: An array of bytes containing the UUID in BigEndian order as + * per RFC 9562. - OUT + * + * Used by: + * RW - SCMI_TLM_GET_UUID_LIST (within an @scmi_tlm_batch) + */ +struct scmi_tlm_uuid { + __u8 bytes[SCMI_TLM_DE_IMPL_UUID_SZ]; +}; + +/** + * struct scmi_tlm_event - Event descriptor + * + * @type: Type of Telemetry event to subscribe/unsubscribe - IN + * @efd: An integer representing an open eventfd file descriptor, which wi= ll + * be used to signal the subscribed event. The lifecycle of the event + * subscription is tied to the controlling chardev file descriptor NOT to + * the eventfd lifetime, so that if the file descriptor used to issue this + * IOCTL is closed all the events registered for that instance are cleare= d. + * Note that, though, if in the meantime the Telemetry driver is unloaded, + * the backing event subscription will be removed and the eventfd will + * remain orphaned. - IN + * @cookie: An integer used to represent a subscription request: ONLY posi= tive + * values represents valid subscription. + * On a new subscription this must be set to zero in input, to si= gnify + * that a new request is being made: if the subscription was + * successful a positive integer is returned. + * When, instead, @cookie is set to a positive integer in input, = it + * is interpreted as a reference to an existing subscription to be + * cancelled. + * NOTE that the cookies are per-session, so anybody with knowled= ge of + * the @cookie can cancel the subscription as long as it has acce= ss to + * the same session file-descriptor used to subscribe the event r= elated + * to that cookie. + * @pad: Padding fields to enforce alignment + * @reserved: Some room for future expansion. Must be zero. + * + * Used by: + * RW - SCMI_TLM_EVENT_SUBSCRIBE + */ +struct scmi_tlm_event { +#define SCMI_TLM_EVT_GENERATION 0x0U +#define SCMI_TLM_EVT_LAST (SCMI_TLM_EVT_GENERATION) + __u32 type; + __u32 efd; + __u32 cookie; + __u32 pad; + __u64 reserved; +}; + +#define SCMI_TLM_IOCTL_MAGIC 0xF1 + +#define SCMI_TLM_GET_ABI_INFO _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x00, struct scm= i_tlm_abi_info) +#define SCMI_TLM_GET_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x01, struct scmi_tlm= _config) +#define SCMI_TLM_SET_CFG _IOW(SCMI_TLM_IOCTL_MAGIC, 0x02, struct scmi_tlm_= config) +#define SCMI_TLM_GET_INTRVS _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x03, struct scmi_= tlm_intervals) +#define SCMI_TLM_GET_DE_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x04, struct scmi_= tlm_batch) +#define SCMI_TLM_SET_DE_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x05, struct scmi_= tlm_batch) +#define SCMI_TLM_GET_DE_INFO _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x06, struct scmi= _tlm_de_info) +#define SCMI_TLM_GET_DE_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x07, struct scmi= _tlm_batch) +#define SCMI_TLM_DE_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x08, struct scmi_tlm= _de_sample) +#define SCMI_TLM_GET_ALL_CFG _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x09, struct scmi= _tlm_de_config) +#define SCMI_TLM_SET_ALL_CFG _IOW(SCMI_TLM_IOCTL_MAGIC, 0x0A, struct scmi_= tlm_de_config) +#define SCMI_TLM_GET_GRP_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0B, struct scm= i_tlm_batch) +#define SCMI_TLM_GET_GRP_INFO _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0C, struct scm= i_tlm_grp_info) +#define SCMI_TLM_GET_GRP_DESC _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0D, struct scm= i_tlm_grp_desc) +#define SCMI_TLM_SINGLE_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0E, struct scmi= _tlm_data_read) +#define SCMI_TLM_BULK_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x0F, struct scmi_t= lm_data_read) +#define SCMI_TLM_BATCH_READ _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x10, struct scmi_= tlm_batch) +#define SCMI_TLM_GET_SHMTI_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x11, struct s= cmi_tlm_batch) +#define SCMI_TLM_RESET _IO(SCMI_TLM_IOCTL_MAGIC, 0x12) +#define SCMI_TLM_GET_UUID_LIST _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x13, struct sc= mi_tlm_batch) +#define SCMI_TLM_EVENT_SUBSCRIBE \ + _IOWR(SCMI_TLM_IOCTL_MAGIC, 0x14, struct scmi_tlm_event) + +#endif /* _UAPI_LINUX_SCMI_H */ --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304689; b=e3UECLPIMOkyRh/5LoNx6iySjR7ZmeypUKazHtEiAxBeem9IAfqhIeymJu86+VUYRj2Y nCIphQlsI4J5SOYSEiIFugCmSl5RJcEPNvCB1xgcRMI+Cmt7mtMZzCp28pgPF/NBLLgly NUqYcwY5wiCyetjedvlk+h1fm3WA+cmpA56EbeNs0Bndi2CUH+pkPJ9t20fyerX9rNlwO c53EKZN03Ye4FxBermNJ8I+YqfqIoO0Wt9sqy2AssCfkAmvyLSoQtzxj+m0TERIpluNqb J/iVRbFQ/GKcNJize/7UFzCuf9atrRhQ1UL1X8iJUwq9imQUKaqRGQ0N5t8C7KIjeyA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304689; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Uxtowdsgj4iYAJkSsdZEZk9r8DuePf2aExhDSSpjd3c=; b=AJPDsnHl0s5vPqv3xtr6N3LBHIvXXiYzvZ1pdrvI1CwmSK/p/EcYNCTdJtzLpRz1Leb3 8ZWAeNlCI4DOHetFK9ynrlqlecjsqXXY5ksXgsOs2SDSTUy+fu97l6paCATLCtDAdgsbn hRskPPov+bVoyZrIyDM03f6qpms5mJJkJtNm5kdoY9mWOEwiEx2kbMMewP1H0UXaczOjo WULztESaTtDGZBxYvp0TSqGw/bstpRQhJqrxDNijhcL0mNsvVC06LuJNNohSK4L/sW95o ZENJZQzXDuIEA+jgfx7SgvLj30rehLXCvZywv+6lE+qS9FeA0kCaX6VhrDGuIqzYTUA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=cLicWBSl; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 16040606A680; Sat, 21 Nov 2026 23:44:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 16040606A680 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=cLicWBSl 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 848E41575; Sat, 21 Nov 2026 15:44:44 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9B2033F632; Sat, 21 Nov 2026 15:44:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304687; bh=S7lYoviI2g/aQjimbwHkXz3C35F5X1+0vgiWj/J9jDE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cLicWBSlHbJ+UGWAGKUhAKU3pEqJUSkQxLnT8A/tJZkyE51jlPQJNbnQHkJjO4vad x7xCvYtjpSoCy7tDBxbmDh90IvIwhnhR+qRlmnlzDzaprrJYZVISola0326qn82g6i ElL0lOvbU0kmI29/76kl024PMhXTKk6haOnq80hg= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 26/31] firmware: arm_scmi: Add System Telemetry driver Date: Sat, 21 Nov 2026 23:40:00 +0000 Message-ID: <20261121234005.1525419-27-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an SCMI driver to allow enumeration, configuration and data collection of SCMI Telemetry data. The driver exposes one character device for each found SCMI instance and a set of related IOCTLs can be used to enumerate, configure and collect Telemetry resources. Signed-off-by: Cristian Marussi --- v15 --> v16 - removed group related IOCTLs - removed single sample read IOCTLs - removed reset IOCTL suppport - removed events IOCTL - remove unsupported features from ABI features - clear out->flags in uapi_info - cap ABI INFO usize to PAGE_SIZE - sanitize input update interval - add missing pad/reserved checks in input v14 --> v15 - reject config attempts and intervals query for groups that do not support per-group config - harden/simplify batch helpers - fix shmti mmap size check to include offset - drop usage of sdev->dev in device_release - simplify session_unsubscribe cleanup on exit/unbind - added missing kref.h include - expose TLM EVENTS capabilities ONLY IF CONFIG_EVENTFD - mmap: check/clear also VM_EXEC VM_MAYWRITE VM_MAYEXEC - reject non zero vm_pgoff - drop usage of guard(srcu) to avoid races with put_device v13 --> v14 - avoid double free on probe err path - grab ctx and check if dead when mmapping an anon_fd - move away from dead_mtx in favour of dead_srcu and extend srcu'ed section in ioctl handler v12 --> v13 - pass back E2BIG from copy_struct_from_user to userspace - remove a few redundant copy_from_user - fix filedescriptor lifecycle: drop kref in favour of embedded cdev and use cdev_device_add. Rely on devm_protocol_get for proto_ops - refactor EVENT session unsubscribe and check for aliveness - use *alloc_objs v11 --> v12 - added RCU deref access on UUIDs - dropped needless copy_from_user on states_batch - fixed double free on anon ids error path - reworked eventfd handling using per-session tracking and cleanup on chardev last release - properly free also ti->tsp on error path - properly acquire/release sdev->dev via get/put_device v10 --> v11 - make SCMI tlm_ devices 664 by default - add 0-sized query support for all the NON _LIST remaining BATCH ops - add 0-sized query support on GET_INTRVS - add 0-sized query support on BULOK/SINGLE reads v8 --> v9 - Fix some Sashiko reports on v8 - use kvmalloc/kvcalloc for all buffers with user/FW generated size - add a proper upper bounds checks on IOCTL requests - make SET_ALL_CFG carry on after a failure (reporting last error anyway) - properly initialize some possibly unused stack vars - refactor SHMTIs list processing and ANON fds allocation to avoid leaking fds by premature installing them into process. - completely rework cdev and scmi_tlm_instance interactions and lifecycle and drop all devres allocation: reject fops on cdev after driver unbind and delay data structures freeing till the last reference has been dropped. - reworked BATCH helpers to handle ZERO items requests (for IOCTL query) - reworked all the _LIST IOCTLs to use scmi_tlm_batch as a container structure instead of custom structs - add support for 0-items query pattern to all _LIST IOCTLs v7 --> v8 - Fixes for some Sashiko issues on v7 - refactor batch handling into common batch init/fini routines - use array_size to check for overflows - explain cleanup logic of eventfd_ctx (xa_for_each) - add a mutex to protect events_xa - introduce per-IOCTL check on batch upper bounds v6 --> v7 - fixed module_init errors paths - introduced a check on FMODE to filter-out unprivleged writers - added UUID list IOCTL - added UUID/SID/OFFS tp GET/SET config IOCTLs - added EVENT subscribe IOCTL - modified BATCH READ with per-DE status - added BATCHed mode for DE_CFG_SET/GET - fix setting properly discrete interval flag - new ABI feats flags\ - new SCMI feats flags - new reserved checks v5 --> v6 - drop hunam readable .read capabilities on chardev - fill-up the new UAPI de_implementation version u8 array - added compat_ioctl support - fixed initialization of flags in scmi_tlm_to_uapi_base_info - fix grp_id bounds check in scmi_tlm_grp_info_get_ioctl - fix booleans - fix SHMTI get list to loop on input params - reworked bounds checks into: - scmi_tlm_intervals_get_ioctl - scmi_tlm_des_list_get_ioctl - scmi_tlm_grp_list_get_ioctl - scmi_tlm_grp_desc_get_ioctl - renamed some READ ioctl to be consistent - reject oversized mmap requests - support new GET_ABI_INFO IOCTL - support RESET IOCTL - add a missing READ_ONCE() to rinfo get - avoid gaps in the anon fds returned array needed by shmti mmap - adding missing cdev_del on remove --- drivers/firmware/arm_scmi/Kconfig | 10 + drivers/firmware/arm_scmi/Makefile | 1 + .../firmware/arm_scmi/scmi_system_telemetry.c | 1364 +++++++++++++++++ 3 files changed, 1375 insertions(+) create mode 100644 drivers/firmware/arm_scmi/scmi_system_telemetry.c diff --git a/drivers/firmware/arm_scmi/Kconfig b/drivers/firmware/arm_scmi/= Kconfig index f506c4d1d96a..e7d8dd9fa907 100644 --- a/drivers/firmware/arm_scmi/Kconfig +++ b/drivers/firmware/arm_scmi/Kconfig @@ -113,4 +113,14 @@ config ARM_SCMI_POWER_CONTROL called scmi_power_control. Note this may be needed early in boot to catch early shutdown/reboot SCMI requests. =20 +config ARM_SCMI_SYSTEM_TELEMETRY + tristate "SCMI System Telemetry driver" + depends on ARM_SCMI_PROTOCOL || (COMPILE_TEST && OF) + help + This enables SCMI Systemn Telemetry support that allows userspace to + retrieve ARM Telemetry data made available via SCMI. + + This driver can also be built as a module. If so, the module will be + called scmi_system_telemetry. + endmenu diff --git a/drivers/firmware/arm_scmi/Makefile b/drivers/firmware/arm_scmi= /Makefile index fe55b7aa0707..20f8d55840a5 100644 --- a/drivers/firmware/arm_scmi/Makefile +++ b/drivers/firmware/arm_scmi/Makefile @@ -18,3 +18,4 @@ obj-$(CONFIG_ARM_SCMI_PROTOCOL) +=3D scmi-core.o obj-$(CONFIG_ARM_SCMI_PROTOCOL) +=3D scmi-module.o =20 obj-$(CONFIG_ARM_SCMI_POWER_CONTROL) +=3D scmi_power_control.o +obj-$(CONFIG_ARM_SCMI_SYSTEM_TELEMETRY) +=3D scmi_system_telemetry.o diff --git a/drivers/firmware/arm_scmi/scmi_system_telemetry.c b/drivers/fi= rmware/arm_scmi/scmi_system_telemetry.c new file mode 100644 index 000000000000..86594dbd65f0 --- /dev/null +++ b/drivers/firmware/arm_scmi/scmi_system_telemetry.c @@ -0,0 +1,1364 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * SCMI - System Telemetry Driver + * + * Copyright (C) 2026 ARM Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define SCMI_TLM_DRIVER "scmi-telemetry-driver" +#define SCMI_TLM_NUM_DEVS 256 + +#define MAX_BULK_LINE_CHAR_LENGTH 64 + +/* + * Break if inadvertently struct scmi_telemetry_de_sample deviates from the + * UAPI counterpart struct scmi_tlm_de_sample: this allows more performant + * single DE reads. + */ +#define SCMI_DE_SAMPLE_BUILD_SANITY_CHECK \ +({ \ + BUILD_BUG_ON_MSG(sizeof(struct scmi_telemetry_de_sample) !=3D \ + sizeof(struct scmi_tlm_de_sample), \ + "UAPI DE sample mismatch !"); \ + ASSERT_STRUCT_OFFSET(struct scmi_telemetry_de_sample, id, \ + offsetof(struct scmi_tlm_de_sample, id)); \ + ASSERT_STRUCT_OFFSET(struct scmi_telemetry_de_sample, pad, \ + offsetof(struct scmi_tlm_de_sample, pad)); \ + ASSERT_STRUCT_OFFSET(struct scmi_telemetry_de_sample, tstamp, \ + offsetof(struct scmi_tlm_de_sample, tstamp));\ + ASSERT_STRUCT_OFFSET(struct scmi_telemetry_de_sample, val, \ + offsetof(struct scmi_tlm_de_sample, val)); \ +}) + +static dev_t stlm_devt; + +/** + * struct scmi_tlm_setup - Telemetry setup descriptor + * @sdev: A reference to the related SCMI device + * @ph: A reference to the protocol handle to be used with the ops + * @ops: A reference to the protocol ops + * @rinfo: A reference to the resource info descriptor + */ +struct scmi_tlm_setup { + struct scmi_device *sdev; + struct scmi_protocol_handle *ph; + const struct scmi_telemetry_proto_ops *ops; + const struct scmi_telemetry_res_info *rinfo; +}; + +/** + * struct scmi_tlm_instance - Telemetry instance descriptor + * @id: Progressive number identifying this probed instance. + * @devt: The dev_t descriptor attached to this instance. + * @dev: An embedded device structure. + * @cdev: An embedded CDEV structure. + * @node: A node to link this in the list of all instances. + * @tsp: A reference to the telemetry instance setup + * @info: A reference to this instance SCMI Telemetry info data. + * @dead_srcu: A SRCU to protect @dead + * @dead: A boolean to indicate when the device has been unboud + */ +struct scmi_tlm_instance { + unsigned int id; + dev_t devt; + struct device dev; + struct cdev cdev; + struct list_head node; + struct scmi_tlm_setup *tsp; + const struct scmi_telemetry_info *info; + /* Protect dead */ + struct srcu_struct dead_srcu; + bool dead; +}; + +#define cdev_to_instance(c) container_of(c, struct scmi_tlm_instance, cdev) +#define dev_to_instance(c) container_of(c, struct scmi_tlm_instance, dev) + +struct scmi_tlm_shmti_ctx { + struct scmi_tlm_instance *ti; + struct scmi_telemetry_shmti_info *shmti; + struct kref ref; +}; + +#define to_shmti_ctx(c) container_of(c, struct scmi_tlm_shmti_ctx, ref) + +static inline void scmi_tlm_to_uapi_abi_info(struct scmi_tlm_abi_info *out, + const struct scmi_telemetry_info *in) +{ + out->size =3D sizeof(*out); + out->abi_version =3D SCMI_TLM_CURRENT_ABI_VERSION; + out->abi_features =3D SCMI_TLM_ABI_FEAT_UUID_LIST | + SCMI_TLM_ABI_FEAT_BATCH_STATE | SCMI_TLM_ABI_FEAT_BATCHED_CFG | + SCMI_TLM_ABI_FEAT_DE_TRACKING; + export_uuid(out->primary_de_impl_version, &in->base.primary_revision); + out->num_des =3D in->base.num_des; + out->num_groups =3D in->base.num_groups; + out->num_intervals =3D in->base.num_intervals; + out->num_shmtis =3D in->base.num_shmtis; + out->features =3D in->reset_support ? SCMI_TLM_SCMI_SUPPORT_RESET : 0; + out->features |=3D in->single_read_support ? + SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE : 0; + out->features |=3D in->per_group_config_support ? + SCMI_TLM_SCMI_SUPPORT_GROUP_CONFIG : 0; + out->features |=3D in->continuous_update_support ? + SCMI_TLM_SCMI_SUPPORT_UPDATE_NOTIFICATION : 0; +} + +static inline void +scmi_tlm_to_uapi_intervals(struct scmi_tlm_intervals *out, + struct scmi_telemetry_intervals *in, + struct scmi_tlm_update_interval *out_interv) +{ + unsigned int *in_interv =3D in->update_intervals; + + out->flags =3D in->discrete ? SCMI_TLM_INTERV_DISCRETE : 0; + for (int i =3D 0; i < out->num_intervals; i++) { + out_interv[i].secs =3D + SCMI_TLM_GET_UPDATE_INTERVAL_SECS(in_interv[i]); + out_interv[i].exp =3D + SCMI_TLM_GET_UPDATE_INTERVAL_EXP(in_interv[i]); + } +} + +static inline void scmi_tlm_to_uapi_de_info(struct scmi_tlm_de_info *out, + struct scmi_telemetry_de_info *in) +{ + out->id =3D in->id; + out->grp_id =3D in->grp_id; + out->data_sz =3D in->data_sz; + out->type =3D in->type; + out->unit =3D in->unit; + out->unit_exp =3D in->unit_exp; + out->ts_rate =3D in->ts_rate; + out->instance_id =3D in->instance_id; + out->compo_instance_id =3D in->compo_instance_id; + out->compo_type =3D in->compo_type; + out->persistent =3D !!in->persistent; + out->flags =3D in->grp_id !=3D SCMI_TLM_GRP_INVALID ? + SCMI_TLM_DEINFO_GROUP : 0; + out->pad[0] =3D 0; + out->pad[1] =3D 0; + out->pad2 =3D 0; + memcpy(out->name, in->name, 16); +} + +static inline void +scmi_tlm_to_uapi_des_items(unsigned int num_des, + const struct scmi_telemetry_res_info *in, + struct scmi_tlm_de_info *des) +{ + for (int i =3D 0; i < num_des; i++) + scmi_tlm_to_uapi_de_info(&des[i], &in->dei_store[i]); +} + +/** + * scmi_telemetry_res_info_get - Resources info getter + * @tsp: A reference to the telemetry instance setup + * + * On first call this helper takes care to retrieve and cache all the reso= urces + * descriptor from the platform, then, on the following invocations it will + * always return the cached value. + */ +static inline const struct scmi_telemetry_res_info * +scmi_telemetry_res_info_get(struct scmi_tlm_setup *tsp) +{ + const struct scmi_telemetry_res_info *rinfo; + + if (READ_ONCE(tsp->rinfo)) + return tsp->rinfo; + + rinfo =3D tsp->ops->res_get(tsp->ph); + /* Cache the retrieved resource info value */ + smp_store_mb(tsp->rinfo, rinfo); + + return rinfo; +} + +static long +scmi_tlm_abi_info_get_ioctl(const struct scmi_tlm_instance *ti, unsigned l= ong arg) +{ + struct scmi_tlm_abi_info base =3D {}; + void __user *uptr =3D (void __user *)arg; + bool ignored_trailing; + u32 usize; + int err; + + if (get_user(usize, (u32 __user *)arg)) + return -EFAULT; + + if (usize > PAGE_SIZE) + return -E2BIG; + + if (usize < offsetofend(struct scmi_tlm_abi_info, reserved)) + return -EINVAL; + + err =3D copy_struct_from_user(&base, sizeof(base), uptr, usize); + if (err) + return err; + + if (base.reserved) + return -EINVAL; + + scmi_tlm_to_uapi_abi_info(&base, ti->info); + err =3D copy_struct_to_user(uptr, usize, &base, sizeof(base), + &ignored_trailing); + if (err) + return err; + + if (ignored_trailing) + return -EMSGSIZE; + + return 0; +} + +static long +scmi_tlm_config_get_ioctl(const struct scmi_tlm_instance *ti, unsigned lon= g arg) +{ + unsigned int active =3D ti->info->active_update_interval; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_config cfg; + + if (copy_from_user(&cfg, uptr, sizeof(cfg))) + return -EFAULT; + + if (cfg.pad || cfg.reserved || cfg.flags & ~SCMI_TLM_CONFIG_FLAGS) + return -EINVAL; + + if (SCMI_TLM_CONFIG_IS_GROUP(cfg.flags)) + return -EOPNOTSUPP; + + cfg.enable =3D !!ti->info->enabled; + cfg.active.secs =3D SCMI_TLM_GET_UPDATE_INTERVAL_SECS(active); + cfg.active.exp =3D SCMI_TLM_GET_UPDATE_INTERVAL_EXP(active); + + if (copy_to_user(uptr, &cfg, sizeof(cfg))) + return -EFAULT; + + return 0; +} + +static inline int +scmi_tlm_build_active_interval(struct scmi_tlm_update_interval *intrv, + unsigned int *active) +{ + *active =3D SCMI_TLM_BUILD_UPDATE_INTERVAL(intrv->secs, intrv->exp); + if (SCMI_TLM_GET_UPDATE_INTERVAL_SECS(*active) !=3D intrv->secs || + SCMI_TLM_GET_UPDATE_INTERVAL_EXP(*active) !=3D intrv->exp) { + *active =3D 0; + return -EINVAL; + } + + return 0; +} + +static long +scmi_tlm_config_set_ioctl(const struct scmi_tlm_instance *ti, unsigned lon= g arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_config cfg; + unsigned int active; + bool ena; + + if (copy_from_user(&cfg, uptr, sizeof(cfg))) + return -EFAULT; + + if (cfg.pad || cfg.reserved || cfg.flags & ~SCMI_TLM_CONFIG_FLAGS) + return -EINVAL; + + if (SCMI_TLM_CONFIG_IS_GROUP(cfg.flags)) + return -EOPNOTSUPP; + + if (scmi_tlm_build_active_interval(&cfg.active, &active)) + return -EINVAL; + + ena =3D !!cfg.enable; + cfg.grp_id =3D SCMI_TLM_GRP_INVALID; + + return tsp->ops->collection_configure(tsp->ph, cfg.grp_id, &ena, &active,= NULL); +} + +static long +scmi_tlm_intervals_get_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg) +{ + struct scmi_telemetry_intervals *tlm_ivs; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_intervals ivs; + size_t ivs_intrv_sz; + + if (copy_from_user(&ivs, uptr, sizeof(ivs))) + return -EFAULT; + + if (ivs.pad[0] || ivs.pad[1] || ivs.pad[2] || ivs.pad2 || + ivs.reserved || ivs.flags & ~SCMI_TLM_INTERV_FLAGS) + return -EINVAL; + + if (SCMI_TLM_CONFIG_IS_GROUP(ivs.flags)) + return -EOPNOTSUPP; + + tlm_ivs =3D ti->info->intervals; + + /* Return max interval on zero-query */ + if (!ivs.num_intervals) { + ivs.num_intervals =3D tlm_ivs->num_intervals; + + if (copy_to_user(uptr, &ivs, sizeof(ivs))) + return -EFAULT; + + return 0; + } + + /* Reject only oversized output buffers */ + if (ivs.num_intervals > tlm_ivs->num_intervals) + return -ENOSPC; + + ivs_intrv_sz =3D array_size(ivs.num_intervals, + sizeof(struct scmi_tlm_update_interval)); + if (ivs_intrv_sz =3D=3D SIZE_MAX) + return -EOVERFLOW; + + /* A local scratch buffer... */ + struct scmi_tlm_update_interval *ivs_intrv __free(kvfree) =3D + kvzalloc(ivs_intrv_sz, GFP_KERNEL); + if (!ivs_intrv) + return -ENOMEM; + + /* Passing the array as a param avoids dancing with uptr->intervals */ + scmi_tlm_to_uapi_intervals(&ivs, tlm_ivs, ivs_intrv); + if (copy_to_user(u64_to_user_ptr(ivs.intervals), + (void *)ivs_intrv, ivs_intrv_sz)) + return -EFAULT; + + if (copy_to_user(uptr, &ivs, sizeof(ivs))) + return -EFAULT; + + return 0; +} + +static int +scmi_tlm_batch_initialize(void __user *uptr, struct scmi_tlm_batch *batch, + size_t item_sz, unsigned int max_items, + void **out_items, size_t *out_batch_sz, + int **out_states, size_t *out_states_sz) +{ + size_t batch_sz; + + if (copy_from_user(batch, uptr, sizeof(*batch))) + return -EFAULT; + + if (batch->reserved || batch->item_sz !=3D item_sz || + batch->num_items > max_items) + return -EINVAL; + + /* Sanity check on user invocation */ + if (!out_items || !out_batch_sz) + return -EINVAL; + + *out_items =3D NULL; + *out_batch_sz =3D 0; + if (out_states) + *out_states =3D NULL; + if (out_states_sz) + *out_states_sz =3D 0; + + /* Was a size query ? */ + if (batch->num_items =3D=3D 0) + return 0; + + batch_sz =3D array_size(batch->num_items, batch->item_sz); + if (batch_sz =3D=3D SIZE_MAX) + return -EOVERFLOW; + + void *items __free(kvfree) =3D kvzalloc(batch_sz, GFP_KERNEL); + if (!items) + return -ENOMEM; + + /* Read all the requested configs */ + if (copy_from_user(items, u64_to_user_ptr(batch->items), batch_sz)) + return -EFAULT; + + /* Is per-item status required ? */ + if (batch->states && out_states) { + size_t states_sz; + + states_sz =3D array_size(batch->num_items, sizeof(int)); + if (states_sz =3D=3D SIZE_MAX) + return -EOVERFLOW; + + int *states_arr __free(kvfree) =3D kvzalloc(states_sz, GFP_KERNEL); + if (!states_arr) + return -ENOMEM; + + *out_states =3D no_free_ptr(states_arr); + if (out_states_sz) + *out_states_sz =3D states_sz; + } + + *out_items =3D no_free_ptr(items); + *out_batch_sz =3D batch_sz; + + return 0; +} + +static int +scmi_tlm_batch_finalize(void __user *uptr, struct scmi_tlm_batch *batch, + void *items, int *states, size_t batch_sz, + size_t states_sz) +{ + if (batch_sz) { + if (copy_to_user(u64_to_user_ptr(batch->items), items, batch_sz)) + return -EFAULT; + + if (batch->states && states) { + if (copy_to_user(u64_to_user_ptr(batch->states), + states, states_sz)) + return -EFAULT; + } + } + + if (copy_to_user(uptr, batch, sizeof(*batch))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_all_de_config_set_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg) +{ + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_de_config tcfg =3D {}; + bool ena, t_ena; + int ret =3D 0; + + if (copy_from_user(&tcfg, uptr, sizeof(tcfg))) + return -EFAULT; + + if (tcfg.pad || tcfg.reserved) + return -EINVAL; + + ena =3D !!tcfg.enable; + t_ena =3D !!tcfg.t_enable; + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + for (int i =3D 0; i < rinfo->num_des; i++) { + int err; + + err =3D ti->tsp->ops->state_set(ti->tsp->ph, false, + rinfo->des[i]->info->id, + &ena, &t_ena, NULL, NULL, NULL); + if (err) { + /* Carry on BUT report last error */ + ret =3D err; + continue; + } + } + + return ret; +} + +static long +scmi_tlm_all_de_config_get_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_de_config tcfg =3D {}; + bool ena, t_ena; + int ret; + + if (copy_from_user(&tcfg, uptr, sizeof(tcfg))) + return -EFAULT; + + if (tcfg.pad || tcfg.reserved) + return -EINVAL; + + ena =3D !!tcfg.enable; + t_ena =3D !!tcfg.t_enable; + ret =3D ti->tsp->ops->state_get(ti->tsp->ph, NULL, &ena, &t_ena, + NULL, NULL, NULL); + if (ret) + return ret; + + tcfg.enable =3D !!ena; + tcfg.t_enable =3D !!t_ena; + if (copy_to_user(uptr, &tcfg, sizeof(tcfg))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_de_config_set_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg) +{ + struct scmi_tlm_de_config *tcfg __free(kvfree) =3D NULL; + int ret, *states __free(kvfree) =3D NULL; + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_batch batch =3D {}; + size_t batch_sz, states_sz; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + ret =3D scmi_tlm_batch_initialize(uptr, &batch, sizeof(*tcfg), + rinfo->num_des, (void **)&tcfg, &batch_sz, + &states, &states_sz); + if (ret) + return ret; + + for (int i =3D 0; i < batch.num_items; i++) { + unsigned int sid, offset; + bool ena, t_ena; + uuid_t uuid; + int ret; + + if (tcfg[i].pad || tcfg[i].reserved) + return -EINVAL; + + ena =3D !!tcfg[i].enable; + t_ena =3D !!tcfg[i].t_enable; + ret =3D ti->tsp->ops->state_set(ti->tsp->ph, false, + tcfg[i].id, &ena, &t_ena, + &sid, &offset, &uuid); + if (ret) { + if (!states) + return ret; + + states[i] =3D ret; + continue; + } + + tcfg[i].sid =3D sid; + tcfg[i].offset =3D offset; + export_uuid(tcfg[i].uuid, &uuid); + } + + /* Return max batch size on zero query */ + if (!batch.num_items) + batch.num_items =3D rinfo->num_des; + + return scmi_tlm_batch_finalize(uptr, &batch, tcfg, states, batch_sz, + states_sz); +} + +static long +scmi_tlm_de_config_get_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg) +{ + struct scmi_tlm_de_config *tcfg __free(kvfree) =3D NULL; + int ret, *states __free(kvfree) =3D NULL; + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_batch batch =3D {}; + size_t batch_sz, states_sz; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + ret =3D scmi_tlm_batch_initialize(uptr, &batch, sizeof(*tcfg), + rinfo->num_des, (void **)&tcfg, + &batch_sz, &states, &states_sz); + if (ret) + return ret; + + for (int i =3D 0; i < batch.num_items; i++) { + unsigned int sid =3D SCMI_TLM_SHMTI_ID_INVALID, offset =3D 0; + bool ena, t_ena; + uuid_t uuid =3D {}; + + if (tcfg[i].pad || tcfg[i].reserved) + return -EINVAL; + + ena =3D !!tcfg[i].enable; + t_ena =3D !!tcfg[i].t_enable; + ret =3D ti->tsp->ops->state_get(ti->tsp->ph, &tcfg[i].id, + &ena, &t_ena, &sid, &offset, &uuid); + + if (ret) { + if (!states) + return ret; + + states[i] =3D ret; + continue; + } + + tcfg[i].enable =3D ena; + tcfg[i].t_enable =3D t_ena; + tcfg[i].sid =3D sid; + tcfg[i].offset =3D offset; + export_uuid(tcfg[i].uuid, &uuid); + } + + /* Return max batch size on zero query */ + if (!batch.num_items) + batch.num_items =3D rinfo->num_des; + + return scmi_tlm_batch_finalize(uptr, &batch, tcfg, states, batch_sz, + states_sz); +} + +static long +scmi_tlm_de_info_get_ioctl(const struct scmi_tlm_instance *ti, unsigned lo= ng arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + const struct scmi_telemetry_de *de; + struct scmi_tlm_de_info dei; + + if (copy_from_user(&dei, uptr, sizeof(dei))) + return -EFAULT; + + /* Flags does NOT make sense in the input param */ + if (dei.pad[0] || dei.pad[1] || dei.pad2 || dei.flags || dei.reserved) + return -EINVAL; + + de =3D tsp->ops->de_lookup(tsp->ph, dei.id); + if (!de) + return -EINVAL; + + scmi_tlm_to_uapi_de_info(&dei, de->info); + if (copy_to_user(uptr, &dei, sizeof(dei))) + return -EFAULT; + + return 0; +} + +static long +scmi_tlm_des_list_get_ioctl(const struct scmi_tlm_instance *ti, unsigned l= ong arg) +{ + struct scmi_tlm_de_info *dei __free(kvfree) =3D NULL; + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_batch batch =3D {}; + size_t batch_sz; + int ret; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + ret =3D scmi_tlm_batch_initialize(uptr, &batch, sizeof(*dei), + rinfo->num_des, (void **)&dei, &batch_sz, + NULL, NULL); + if (ret) + return ret; + + /* Passing the array as a param avoids dancing with uptr->intervals */ + if (batch.num_items) + scmi_tlm_to_uapi_des_items(batch.num_items, rinfo, dei); + else + batch.num_items =3D rinfo->num_des; + + return scmi_tlm_batch_finalize(uptr, &batch, dei, NULL, batch_sz, 0); +} + +static long +scmi_tlm_de_value_get_ioctl(const struct scmi_tlm_instance *ti, unsigned l= ong arg) +{ + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_de_sample sample; + int ret; + + SCMI_DE_SAMPLE_BUILD_SANITY_CHECK; + + if (copy_from_user(&sample, uptr, sizeof(sample))) + return -EFAULT; + + if (sample.pad) + return -EINVAL; + + ret =3D tsp->ops->de_data_read(tsp->ph, + (struct scmi_telemetry_de_sample *)&sample); + if (ret) + return ret; + + if (copy_to_user(uptr, &sample, sizeof(sample))) + return -EFAULT; + + return 0; +} + +static long scmi_tlm_des_read_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg) +{ + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_setup *tsp =3D ti->tsp; + struct scmi_tlm_data_read bulk; + unsigned int grp_id; + int ret; + + if (copy_from_user(&bulk, uptr, sizeof(bulk))) + return -EFAULT; + + if (bulk.pad[0] || bulk.pad[1] || bulk.pad[2] || bulk.pad2 || + bulk.flags & ~SCMI_TLM_READ_FLAGS) + return -EINVAL; + + if (SCMI_TLM_CONFIG_IS_GROUP(bulk.flags)) + return -EOPNOTSUPP; + + /* Check that a reasonably sized buffer has been requested */ + rinfo =3D scmi_telemetry_res_info_get(tsp); + if (bulk.num_samples > rinfo->num_des) + return -E2BIG; + + grp_id =3D SCMI_TLM_GRP_INVALID; + /* Return max samples on zero-query */ + if (!bulk.num_samples) { + bulk.num_samples =3D rinfo->num_des; + if (copy_to_user(uptr, &bulk, sizeof(bulk))) + return -EFAULT; + + return 0; + } + + struct scmi_telemetry_de_sample *samples __free(kvfree) =3D + kvzalloc_objs(*samples, bulk.num_samples); + if (!samples) + return -ENOMEM; + + ret =3D tsp->ops->des_bulk_read(tsp->ph, grp_id, + &bulk.num_samples, samples); + if (ret) + return ret; + + if (copy_to_user(u64_to_user_ptr(bulk.samples), samples, + bulk.num_samples * sizeof(*samples))) + return -EFAULT; + + if (copy_to_user(uptr, &bulk, sizeof(bulk))) + return -EFAULT; + + return 0; +} + +static long scmi_tlm_des_batch_read_ioctl(const struct scmi_tlm_instance *= ti, + unsigned long arg) +{ + struct scmi_telemetry_de_sample *samples __free(kvfree) =3D NULL; + int ret, *states __free(kvfree) =3D NULL; + const struct scmi_telemetry_res_info *rinfo; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_batch batch =3D {}; + size_t batch_sz, states_sz; + + rinfo =3D scmi_telemetry_res_info_get(ti->tsp); + ret =3D scmi_tlm_batch_initialize(uptr, &batch, sizeof(*samples), + rinfo->num_des, (void **)&samples, + &batch_sz, &states, &states_sz); + if (ret) + return ret; + + for (int i =3D 0; i < batch.num_items; i++) { + int ret; + + if (samples[i].pad) + return -EINVAL; + + ret =3D ti->tsp->ops->de_data_read(ti->tsp->ph, &samples[i]); + if (ret) { + if (!states) + return ret; + + states[i] =3D ret; + continue; + } + } + + /* Return max batch size on zero query */ + if (!batch.num_items) + batch.num_items =3D rinfo->num_des; + + return scmi_tlm_batch_finalize(uptr, &batch, samples, states, batch_sz, + states_sz); +} + +static struct scmi_tlm_shmti_ctx * +scmi_tlm_shmti_ctx_alloc(struct scmi_tlm_instance *ti, + struct scmi_telemetry_shmti_info *shmti) +{ + struct scmi_tlm_shmti_ctx *ctx; + + ctx =3D kzalloc_obj(*ctx); + if (!ctx) + return ERR_PTR(-ENOMEM); + + ctx->shmti =3D shmti; + kref_init(&ctx->ref); + + /* Get hold of the Telemetry instance when returning anon_fds */ + get_device(&ti->dev); + ctx->ti =3D ti; + + return ctx; +} + +static void scmi_tlm_shmti_ctx_release(struct kref *kref) +{ + struct scmi_tlm_shmti_ctx *ctx =3D to_shmti_ctx(kref); + + put_device(&ctx->ti->dev); + + kfree(ctx); +} + +static struct scmi_tlm_shmti_ctx * +scmi_tlm_shmti_ctx_get(struct scmi_tlm_shmti_ctx *ctx) +{ + kref_get(&ctx->ref); + + return ctx; +} + +static void scmi_tlm_shmti_ctx_put(struct scmi_tlm_shmti_ctx *ctx) +{ + kref_put(&ctx->ref, scmi_tlm_shmti_ctx_release); +} + +static void scmi_tlm_shmti_vma_open(struct vm_area_struct *vma) +{ + scmi_tlm_shmti_ctx_get(vma->vm_private_data); +} + +static void scmi_tlm_shmti_vma_close(struct vm_area_struct *vma) +{ + scmi_tlm_shmti_ctx_put(vma->vm_private_data); +} + +static const struct vm_operations_struct scmi_tlm_shmti_vm_ops =3D { + .open =3D scmi_tlm_shmti_vma_open, + .close =3D scmi_tlm_shmti_vma_close, +}; + +static int scmi_tlm_shmti_mmap(struct file *filp, struct vm_area_struct *v= ma) +{ + unsigned long npages, expect, needed, req; + struct scmi_tlm_shmti_ctx *ctx; + phys_addr_t base; + size_t map_sz; + int ret, idx; + + if (vma->vm_pgoff) + return -EINVAL; + + if (vma->vm_flags & (VM_WRITE | VM_EXEC)) + return -EPERM; + + vm_flags_clear(vma, VM_MAYWRITE | VM_MAYEXEC); + + /* Grab a ctx reference early on */ + ctx =3D scmi_tlm_shmti_ctx_get(filp->private_data); + /* + * Note that any ctx_put potentially release the device and + * cleans the SRCU structure...no guards. + */ + idx =3D srcu_read_lock(&ctx->ti->dead_srcu); + if (READ_ONCE(ctx->ti->dead)) { + srcu_read_unlock(&ctx->ti->dead_srcu, idx); + scmi_tlm_shmti_ctx_put(ctx); + return -ENODEV; + } + + needed =3D ctx->shmti->len + ctx->shmti->offset; + req =3D vma->vm_end - vma->vm_start; + if (req < needed) { + srcu_read_unlock(&ctx->ti->dead_srcu, idx); + scmi_tlm_shmti_ctx_put(ctx); + return -EINVAL; + } + + /* Align & reject oversized requests */ + base =3D ctx->shmti->phys & PAGE_MASK; + expect =3D DIV_ROUND_UP(needed, PAGE_SIZE); + npages =3D req >> PAGE_SHIFT; + if (npages > expect) { + srcu_read_unlock(&ctx->ti->dead_srcu, idx); + scmi_tlm_shmti_ctx_put(ctx); + return -EINVAL; + } + + map_sz =3D PAGE_ALIGN(needed); + vma->vm_private_data =3D ctx; + vma->vm_ops =3D &scmi_tlm_shmti_vm_ops; + /* Set appropriate caching attributes */ + vma->vm_page_prot =3D pgprot_noncached(vma->vm_page_prot); + + /* Map physical address range into user space */ + ret =3D vm_iomap_memory(vma, base, map_sz); + if (ret) { + srcu_read_unlock(&ctx->ti->dead_srcu, idx); + scmi_tlm_shmti_ctx_put(ctx); + return ret; + } + + srcu_read_unlock(&ctx->ti->dead_srcu, idx); + + return 0; +} + +static int scmi_tlm_shmti_release(struct inode *ino, struct file *filp) +{ + scmi_tlm_shmti_ctx_put(filp->private_data); + + return 0; +} + +static const struct file_operations scmi_tlm_shmti_fops =3D { + .owner =3D THIS_MODULE, + .mmap =3D scmi_tlm_shmti_mmap, + .release =3D scmi_tlm_shmti_release, +}; + +static void +scmi_tlm_allocate_anon_fds(struct scmi_tlm_instance *ti, unsigned int *num= _shmtis, + struct scmi_tlm_shmti_info *shinfo, + struct file **filps) +{ + const struct scmi_telemetry_info *info =3D ti->info; + struct device *dev =3D &ti->tsp->sdev->dev; + int idx =3D 0; + + for (int i =3D 0; i < *num_shmtis; i++) { + struct scmi_telemetry_shmti_info *shmti =3D info->shmtis[i]; + struct scmi_tlm_shmti_ctx *ctx; + struct file *filp; + int fd; + + fd =3D get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) { + dev_err(dev, "Failed GET_UNUSED_FD for SID: %u\n", + shmti->sid); + continue; + } + + ctx =3D scmi_tlm_shmti_ctx_alloc(ti, shmti); + if (IS_ERR(ctx)) { + dev_err(dev, "Failed CTX_ALLOC for SID: %u\n", shmti->sid); + put_unused_fd(fd); + continue; + } + + filp =3D anon_inode_getfile(SCMI_TLM_DRIVER, &scmi_tlm_shmti_fops, + ctx, O_RDONLY | O_CLOEXEC); + if (IS_ERR(filp)) { + scmi_tlm_shmti_ctx_put(ctx); + put_unused_fd(fd); + dev_err(dev, "Failed ANON GET_FILE for SID: %u\n", + shmti->sid); + continue; + } + + shinfo[idx].sid =3D shmti->sid; + shinfo[idx].fd =3D fd; + shinfo[idx].len =3D shmti->len; + shinfo[idx].offset =3D shmti->offset; + + filps[idx++] =3D filp; + } + + /* Return effective initialized items */ + *num_shmtis =3D idx; +} + +static inline void +scmi_tlm_anon_fds_free(int num_shmtis, struct scmi_tlm_shmti_info *shinfo, + struct file **filps) +{ + for (int i =3D 0; i < num_shmtis; i++) { + put_unused_fd(shinfo[i].fd); + /* Triggering scmi_tlm_shmti_release */ + fput(filps[i]); + } +} + +static inline void +scmi_tlm_anon_fds_install(int num_shmtis, struct scmi_tlm_shmti_info *shin= fo, + struct file **filps) +{ + for (int i =3D 0; i < num_shmtis; i++) + fd_install(shinfo[i].fd, filps[i]); +} + +static long +scmi_tlm_shmtis_list_get_ioctl(struct scmi_tlm_instance *ti, unsigned long= arg) +{ + struct scmi_tlm_shmti_info *shinfo __free(kvfree) =3D NULL; + unsigned int max_shmtis =3D ti->info->base.num_shmtis; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_batch batch =3D {}; + size_t batch_sz; + int ret; + + ret =3D scmi_tlm_batch_initialize(uptr, &batch, sizeof(*shinfo), max_shmt= is, + (void **)&shinfo, &batch_sz, NULL, NULL); + if (ret) + return ret; + + if (batch.num_items =3D=3D 0) { + batch.num_items =3D max_shmtis; + + return scmi_tlm_batch_finalize(uptr, &batch, shinfo, NULL, + batch_sz, 0); + } + + struct file **filps __free(kfree) =3D kzalloc_objs(*filps, batch.num_item= s); + if (!filps) + return -ENOMEM; + + /* Passing the array as a param avoids dancing with uptr->items */ + scmi_tlm_allocate_anon_fds(ti, &batch.num_items, shinfo, filps); + ret =3D scmi_tlm_batch_finalize(uptr, &batch, shinfo, NULL, batch_sz, 0); + if (ret) { + scmi_tlm_anon_fds_free(batch.num_items, shinfo, filps); + return ret; + } + + scmi_tlm_anon_fds_install(batch.num_items, shinfo, filps); + + return 0; +} + +static long +scmi_tlm_uuid_list_get_ioctl(const struct scmi_tlm_instance *ti, + unsigned long arg) +{ + struct scmi_tlm_uuid *uuids __free(kvfree) =3D NULL; + void __user *uptr =3D (void __user *)arg; + struct scmi_tlm_batch batch =3D {}; + unsigned int num_uuids; + uuid_t **cur_uuids; + size_t batch_sz; + int ret; + + /* + * Note that UUID array can ONLY grow and it has NOT a known upper + * bound, so it does not matter if by the time we grab the cur_uuids + * via rcu_dereference() the array has grown in size and we end up + * here reading a stale/incomplete set of UUIDs, we will simply + * read the new UUIDs next-time: this simplifies allocation. + */ + num_uuids =3D smp_load_acquire(&ti->info->num_uuids); + ret =3D scmi_tlm_batch_initialize(uptr, &batch, sizeof(*uuids), + num_uuids, (void **)&uuids, &batch_sz, + NULL, NULL); + if (ret) + return ret; + + rcu_read_lock(); + cur_uuids =3D rcu_dereference(ti->info->uuids); + for (int j =3D 0; j < batch.num_items; j++) + export_uuid(uuids[j].bytes, cur_uuids[j]); + rcu_read_unlock(); + + /* Return max batch size on zero query */ + if (!batch.num_items) + batch.num_items =3D num_uuids; + + return scmi_tlm_batch_finalize(uptr, &batch, uuids, NULL, batch_sz, 0); +} + +static long scmi_tlm_unlocked_ioctl(struct file *filp, unsigned int cmd, + unsigned long arg) +{ + struct scmi_tlm_instance *ti =3D filp->private_data; + bool writable =3D filp->f_mode & FMODE_WRITE; + + guard(srcu)(&ti->dead_srcu); + if (READ_ONCE(ti->dead)) + return -ENODEV; + + switch (cmd) { + case SCMI_TLM_GET_ABI_INFO: + return scmi_tlm_abi_info_get_ioctl(ti, arg); + case SCMI_TLM_GET_CFG: + return scmi_tlm_config_get_ioctl(ti, arg); + case SCMI_TLM_SET_CFG: + if (writable) + return scmi_tlm_config_set_ioctl(ti, arg); + break; + case SCMI_TLM_GET_INTRVS: + return scmi_tlm_intervals_get_ioctl(ti, arg); + case SCMI_TLM_GET_DE_CFG: + return scmi_tlm_de_config_get_ioctl(ti, arg); + case SCMI_TLM_SET_DE_CFG: + if (writable) + return scmi_tlm_de_config_set_ioctl(ti, arg); + break; + case SCMI_TLM_GET_DE_INFO: + return scmi_tlm_de_info_get_ioctl(ti, arg); + case SCMI_TLM_GET_DE_LIST: + return scmi_tlm_des_list_get_ioctl(ti, arg); + case SCMI_TLM_DE_READ: + return scmi_tlm_de_value_get_ioctl(ti, arg); + case SCMI_TLM_GET_ALL_CFG: + return scmi_tlm_all_de_config_get_ioctl(ti, arg); + case SCMI_TLM_SET_ALL_CFG: + if (writable) + return scmi_tlm_all_de_config_set_ioctl(ti, arg); + break; + case SCMI_TLM_BULK_READ: + return scmi_tlm_des_read_ioctl(ti, arg); + case SCMI_TLM_BATCH_READ: + return scmi_tlm_des_batch_read_ioctl(ti, arg); + case SCMI_TLM_GET_SHMTI_LIST: + return scmi_tlm_shmtis_list_get_ioctl(ti, arg); + case SCMI_TLM_GET_UUID_LIST: + return scmi_tlm_uuid_list_get_ioctl(ti, arg); + default: + return -ENOTTY; + } + + return -EPERM; +} + +static int scmi_tlm_open(struct inode *ino, struct file *filp) +{ + struct scmi_tlm_instance *ti =3D cdev_to_instance(ino->i_cdev); + + guard(srcu)(&ti->dead_srcu); + if (READ_ONCE(ti->dead)) + return -ENODEV; + + get_device(&ti->dev); + + filp->private_data =3D ti; + + return nonseekable_open(ino, filp); +} + +static int scmi_tlm_release(struct inode *ino, struct file *filp) +{ + struct scmi_tlm_instance *ti =3D filp->private_data; + int idx; + + idx =3D srcu_read_lock(&ti->dead_srcu); + if (READ_ONCE(ti->dead)) { + srcu_read_unlock(&ti->dead_srcu, idx); + put_device(&ti->dev); + return -ENODEV; + } + srcu_read_unlock(&ti->dead_srcu, idx); + + put_device(&ti->dev); + + return 0; +} + +static const struct file_operations stlm_fops =3D { + .owner =3D THIS_MODULE, + .open =3D scmi_tlm_open, + .release =3D scmi_tlm_release, + .unlocked_ioctl =3D scmi_tlm_unlocked_ioctl, + .compat_ioctl =3D compat_ptr_ioctl, +}; + +static struct scmi_tlm_instance * +scmi_tlm_instance_init(struct scmi_device *sdev, + const struct scmi_telemetry_proto_ops *ops, + struct scmi_protocol_handle *ph, unsigned int instance_id) +{ + int ret; + + struct scmi_tlm_instance *ti __free(kfree) =3D kzalloc_obj(*ti); + if (!ti) + return ERR_PTR(-ENOMEM); + + struct scmi_tlm_setup *tsp __free(kfree) =3D kzalloc_obj(*tsp); + if (!tsp) + return ERR_PTR(-ENOMEM); + + ti->info =3D ops->info_get(ph); + if (!ti->info) + return dev_err_ptr_probe(&sdev->dev, + -EINVAL, "invalid Telemetry info !\n"); + + ret =3D init_srcu_struct(&ti->dead_srcu); + if (ret) + return ERR_PTR(ret); + + tsp->sdev =3D sdev; + tsp->ops =3D ops; + tsp->ph =3D ph; + ti->tsp =3D no_free_ptr(tsp); + + ti->id =3D instance_id; + + return no_free_ptr(ti); +} + +static char *scmi_tlm_devnode(const struct device *dev, umode_t *mode) +{ + if (mode) + *mode =3D 0664; + + return kasprintf(GFP_KERNEL, "scmi/%s", dev_name(dev)); +} + +static struct class stlm_class =3D { + .name =3D "stlm", + .devnode =3D scmi_tlm_devnode, +}; + +static void scmi_tlm_device_release(struct device *dev) +{ + struct scmi_tlm_instance *ti =3D dev_to_instance(dev); + + dev_dbg(&ti->dev, "Finalizing Telemetry instance %u\n", ti->id); + + cleanup_srcu_struct(&ti->dead_srcu); + kfree(ti->tsp); + kfree(ti); +} + +static int scmi_telemetry_probe(struct scmi_device *sdev) +{ + const struct scmi_handle *handle =3D sdev->handle; + struct scmi_protocol_handle *ph; + struct scmi_tlm_instance *ti; + unsigned int instance; + const void *ops; + int ret; + + if (!handle) + return -ENODEV; + + instance =3D handle->id; + if (instance >=3D SCMI_TLM_NUM_DEVS) + return -ENOSPC; + + ops =3D handle->devm_protocol_get(sdev, sdev->protocol_id, &ph); + if (IS_ERR(ops)) + return dev_err_probe(&sdev->dev, PTR_ERR(ops), + "Cannot access protocol:0x%X\n", + sdev->protocol_id); + + ti =3D scmi_tlm_instance_init(sdev, ops, ph, instance); + if (IS_ERR(ti)) + return PTR_ERR(ti); + + /* + * Setting the platform device as parent to enforce proper ordering + * at shutdown and to avoid the need for explicit get/put_device + */ + ti->dev.parent =3D &sdev->dev; + ti->dev.devt =3D MKDEV(MAJOR(stlm_devt), MINOR(stlm_devt) + instance); + ti->dev.class =3D &stlm_class; + ti->dev.release =3D scmi_tlm_device_release; + device_initialize(&ti->dev); + + ret =3D dev_set_name(&ti->dev, "tlm_%d", ti->id); + if (ret) + goto err_dev; + + cdev_init(&ti->cdev, &stlm_fops); + ti->cdev.owner =3D THIS_MODULE; + + ret =3D cdev_device_add(&ti->cdev, &ti->dev); + if (ret) + goto err_dev; + + dev_set_drvdata(&sdev->dev, ti); + + return 0; + +err_dev: + put_device(&ti->dev); + + return ret; +} + +static void scmi_telemetry_remove(struct scmi_device *sdev) +{ + struct scmi_tlm_instance *ti; + + ti =3D dev_get_drvdata(&sdev->dev); + if (!ti) + return; + + /* Stop ongoing opens and wait on in-flight operation to drain */ + WRITE_ONCE(ti->dead, true); + synchronize_srcu(&ti->dead_srcu); + + /* Inhibit future opens */ + cdev_device_del(&ti->cdev, &ti->dev); + + put_device(&ti->dev); +} + +static const struct scmi_device_id scmi_id_table[] =3D { + { SCMI_PROTOCOL_TELEMETRY, "telemetry" }, + { } +}; +MODULE_DEVICE_TABLE(scmi, scmi_id_table); + +static struct scmi_driver scmi_telemetry_driver =3D { + .name =3D SCMI_TLM_DRIVER, + .probe =3D scmi_telemetry_probe, + .remove =3D scmi_telemetry_remove, + .id_table =3D scmi_id_table, +}; + +static int __init scmi_telemetry_init(void) +{ + int ret; + + ret =3D alloc_chrdev_region(&stlm_devt, 0, SCMI_TLM_NUM_DEVS, + SCMI_TLM_DRIVER); + if (ret) + return ret; + + ret =3D class_register(&stlm_class); + if (ret) + goto err_class; + + ret =3D scmi_register(&scmi_telemetry_driver); + if (ret) + goto err_scmi; + + return 0; + +err_scmi: + class_unregister(&stlm_class); + +err_class: + unregister_chrdev_region(stlm_devt, SCMI_TLM_NUM_DEVS); + + return ret; +} +module_init(scmi_telemetry_init); + +static void __exit scmi_telemetry_exit(void) +{ + scmi_unregister(&scmi_telemetry_driver); + unregister_chrdev_region(stlm_devt, SCMI_TLM_NUM_DEVS); + class_unregister(&stlm_class); +} +module_exit(scmi_telemetry_exit); + +MODULE_AUTHOR("Cristian Marussi "); +MODULE_DESCRIPTION("ARM SCMI Telemetry Driver"); +MODULE_LICENSE("GPL"); --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304691; b=Qxf4Si6RIYNGeRIlsVszyPEceUZtLjwtKvkzYoQtIh0nCamROIkntmtTO/0y3NQkod57 TLJ3TcmsUMeV1+oHxxdGvlm+iOZOX7FTX77WZz/9kSzDcd67HuUnRjS2qrcbIOL4x4Mrm XTfZX6bFcF1SShGegOC9g/ZjvipPKTI5pLxVmn/bqATkKeNlDVUBg1Kj2ADYR7wE4ZK/Y w4zGrchzpFE8NtlWJm8nNewI0jpcas4IBjs0zKNCfnPwRrQrMOwBTO+dRYjoudtmc1hlX 0+LApywUB0RYDBAx/ZatWkJafs3JbQJ57XhaE9pHq/T2NZdypX3CaNoo4uTaR3gObSA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304691; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=ybksNdKGZwcUybmuPou+wEJRhGtFCZ9Lfc93sClRt68=; b=s0Bah7JN0TAIj5uH31fFyN6ElMod1TDwp0Mb96rfgYHEMd+0dBVtLT25xlW6TxfPSwHA R+A5UdG+j8q7YmqHewM9lNdgOcQd+eqe7bpqFiNQQfjVu0LP70ukJ/QNJAboXwIALIoi/ 5zRbB+QcjsbPRfDgcL9ilWO3F59D+WuDvmG1XXl7eTnvquPEg+waC9E0R7KLFKZ39Bl98 q4CVd/2v00M9DD2Jd1jkpAQZB15NaThl+rpEzJUJZsBwvg57qnur03NcI1F6rmmZ9diKe HuryD5JWocLHSewGBdsEDChY8Wf0DBEYkmHW+947M5vAVIiFzqnWSNHk61Fu/afWmkw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Qa8m6wdW; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 410C2606A68B; Sat, 21 Nov 2026 23:44:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 410C2606A68B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Qa8m6wdW 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 A016B16F2; Sat, 21 Nov 2026 15:44:47 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B5C133F632; Sat, 21 Nov 2026 15:44:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304690; bh=XLIQNLKLqBN+XK89CZJOMck4z1OnlpSdMf9XcQtFphE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qa8m6wdWbe8jp5e4Nbx42ZUCMtYHu2+k3QJRySv4AuwU5HbKanMM9u5UGnJvFeNEZ 2sFGFObLoJM13G+6cOGjZa/S5Bid8LwwjednVKyvohBvzpHKP0xTVJbbU8d1i1o3Aq XPfb7GAju2cjrwL606WjBQWYVZqYjLyZQJp25aPg= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 27/31] firmware: arm_scmi: Enable autoloading for System Telemetry driver Date: Sat, 21 Nov 2026 23:40:01 +0000 Message-ID: <20261121234005.1525419-28-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the SCMI System Telemetry driver and related protocols to the list of standard protocols whosw devices are created automatically, so enabling module autoloading. Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/bus.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/firmware/arm_scmi/bus.c b/drivers/firmware/arm_scmi/bu= s.c index 25197197db8e..32002fd9d843 100644 --- a/drivers/firmware/arm_scmi/bus.c +++ b/drivers/firmware/arm_scmi/bus.c @@ -546,6 +546,7 @@ static const struct scmi_device_id scmi_std_id_table[] = =3D { { SCMI_PROTOCOL_POWERCAP, "powercap" }, { SCMI_PROTOCOL_PINCTRL, "pinctrl" }, { SCMI_PROTOCOL_PINCTRL, "pinctrl-imx" }, + { SCMI_PROTOCOL_TELEMETRY, "telemetry" }, { }, }; =20 --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304695; b=wTppnz3ySVcZ1STc7UBoNAxfd1LjV3ehM+Eug+QunkZLATXeJHkJFBtT8oHwIYe6QU0P W58Job+530AlJRov8Gj3X23ZD5aFr46TP9n4r2KK6lAzjKpadeYxK+gjNfmCICnr6XZzt DNXSXVu7+o64oIxhz5S8RuHoVRVPQbnwY7glATPdU0qhZzs5EADuj4up1PJPBgOONWwj/ uxaQi/P1Ek7Ld5Q10kSXJZFpPYE7cnAYqUQrxhQlEPQZ6f0GlOogPzeHou2B+E1L4P5Vl AFSt1zIDhfFxLNmxMOxEjYYvcT/iprfjrdSr2RD33U1136X/1cNAmHu/b84ubtuKFGw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304695; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=bqq8tdBHE7PY5lGg3anCtrKLQXzps/O+HDy/opBt6hY=; b=ySuGAu2dygUoK46aWHgHITDVV8/8LsmA/yV/nfmAAYZsuvaE08w8SnRQ2eDrPAEWsTFA qQartI/m4MMr4WSYMbxlEvdmjg03ilw6IF+WsG8mBXl7gm7X8AM1GlwcELQi5MktqjzZ3 Ph78k7guD0XbEUYN+WA+hEilAmMwQQAHla5f6cYtyXVxRa+MlGDw3ol+HjPbEdXRY52+M Ny+cMcdAfLER3OvVG9Z/ySspXyw9SNHI2916GDTbRxa2yk9Qnn64lt6Ip7Hrauub4dEGI 15qw/1+kYCWk8UZIX33h76oFevMYZLC5/9t3O9fCR3WK8k0g+SgxkbxC16V5HURiWZA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b="KzBHSe/X"; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8684B606A682; Sat, 21 Nov 2026 23:44:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8684B606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=KzBHSe/X 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 EF36D1758; Sat, 21 Nov 2026 15:44:50 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D1D5C3F632; Sat, 21 Nov 2026 15:44:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304693; bh=SV9pZMl6M/fxVelvGmHOdqTt7nzTXsoHruNVd0Kxcmo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KzBHSe/XhNtnIfHPS6w345as4YhfsREt0GbOD1D/xXpv7o2ebdmLAvqr1/qblIO8a 5xWbJ57q+vg2M7vjEwXvUtCB++8CFMHC6H5BoyY0D6VQo3fi/vwCov3emWw1ACreRS wnsP1Jwin3KDz0jzko7dwDOnWaeaPQkTLGMR8HPI= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi , Jonathan Corbet , Shuah Khan Subject: [PATCH v16 28/31] docs: ioctl-number: Add SCMI Ioctls Date: Sat, 21 Nov 2026 23:40:02 +0000 Message-ID: <20261121234005.1525419-29-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add SCMI Subsystem IOCTLs numbers Cc: Jonathan Corbet Cc: Shuah Khan Cc: linux-doc@vger.kernel.org Signed-off-by: Cristian Marussi --- Documentation/userspace-api/ioctl/ioctl-number.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documenta= tion/userspace-api/ioctl/ioctl-number.rst index dbe462040999..53e970fc750e 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -403,6 +403,7 @@ Code Seq# Include File = Comments 0xEC 00-01 linux/platform_data/cros_ec_chardev.h Chr= omeOS EC driver 0xEE 00-09 uapi/linux/pfrut.h Pla= tform Firmware Runtime Update and Telemetry 0xEF 00-0F uapi/linux/ntfs.h NTFS +0xF1 00-FF uapi/linux/scmi.h ARM SCMI Subsystem 0xF3 00-3F drivers/usb/misc/sisusbvga/sisusb.h sis= fb (in development) 0xF6 all LTT= ng Linux Trace Toolkit Next Generation --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304698; b=B6gJ4ghSNPOlXtWBwH8Ft6A/cQxQ1yEpN3aPfszvzAQITtBMIrsSuqvMhuYgnwQjy2qN P8m7MkTcol57uSL8JfmmUN9WqbGowaVteRqwawdIuwbvk+jzHSEWlt7ru+XFszphKib3/ DLxzIxY1v+jIh0POSIB8oQHxqsvPfqNNbapQ6Cm5TMieq0c+6fP7ZfYdek/ZPLTO8iekf /mNNGkjJ8dDBSp0kDQfmMvS/m3ukEpbBI589rm8OZDiDr/Dtpr6Beedx3oDcQEb18OoAe SKsG8LzAdVohuIALEJ2xRT8MmTuzsrnThuip+EbE5IXswW85u2lVKyIZNHeM7FSn4fQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304698; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=P7dCxI9rbZC3lO5ZB0+k5t7OMvD4AjaQtjAPEagHsgY=; b=lU6FONJlG6ROBVfg1GH55EyrUEzOsHTr2xoI7qaq0VeIjFtMB7I2l5w9w5e/usiJtZUQ hvT3B00C+lBHBgHljJ6auefJzWjGXdlztP/BP9NjfErGW3NJfN/dwic506fsB1YtL+4JD 0XqZothbVR+MvXH+UHZ3eAUziAMhFXcUk/+W+pqGZrq42kaVSXGnkb5kqam1LOBLS2SdQ GQeC0OgWmHwGpelkuVavRmwrXMhFUos9DAkCY6nGIfXfw9mN8svwWgVcAhrRa80XO87YK RQJgleMusTg66XveJwIWsfPENHs9Sixiut3lDPEeRoP+ptLTjiqMcp5S4zqXOtsbHfQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=mCqtzMZD; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 82AA3606A684; Sat, 21 Nov 2026 23:44:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 82AA3606A684 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=mCqtzMZD 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 1CDC6175D; Sat, 21 Nov 2026 15:44:54 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2EFB63F632; Sat, 21 Nov 2026 15:44:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304697; bh=hv9WEBWlLQR6QR6A3XeIblL4Zv3w4xdS0StYPBYCMkk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mCqtzMZDzDmtfpsKf5gKtIwAmNyRpIZyWitD5KPj8lnTapQBQNo55LxHGSTiQthZR jqEBnxOnTgSarA8b7/HjPyTXhNv4jEWhJ0gRc+Fo5/A7Z12Rpc/s6LXLDAGMUULUwe Xb8l6jaXvF3Z2lACFxwpckIDyrmm9JFI2kcI/u8U= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 29/31] [RFC] Documentation: Add SCMI System Telemetry documentation Date: Sat, 21 Nov 2026 23:40:03 +0000 Message-ID: <20261121234005.1525419-30-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Document ARM SCMI Telemetry IOCTLs interface. Co-developed-by: Philip Radford Signed-off-by: Philip Radford Signed-off-by: Cristian Marussi --- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/stlm.rst | 590 ++++++++++++++++++++++++++ 2 files changed, 591 insertions(+) create mode 100644 Documentation/userspace-api/stlm.rst diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspac= e-api/index.rst index a68b1bea57a8..22daa7ebf78e 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -54,6 +54,7 @@ Devices and I/O dcdbas vduse isapnp + stlm =20 Everything else =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D diff --git a/Documentation/userspace-api/stlm.rst b/Documentation/userspace= -api/stlm.rst new file mode 100644 index 000000000000..17c54accc66a --- /dev/null +++ b/Documentation/userspace-api/stlm.rst @@ -0,0 +1,590 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +STLM - ARM SCMI Telemetry Userspace API +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. contents:: + +Overview +=3D=3D=3D=3D=3D=3D=3D=3D + +ARM SCMI is a System and Configuration Management protocol, based on a +client-server model, that defines a number of messages that allows a +client/agent like Linux to discover, configure and make use of services +provided by the server/platform firmware. + +SCMI v4.0 [1] introduced support for System Telemetry, through which an +agent can dynamically enumerate, configure and collect Telemetry Data +Events (DE) exposed by the platform. + +The SCMI System Telemetry driver exposes one or more dedicated devices, +named as tlm_, grouped under /dev/scmi/, one for each discovered SCMI +instance. + +An IOCTL based interface is defined in order to enumerate, configure and +read telemetry data, as succinctly described in the following. + +For more details on the uAPI, please see include/uapi/linux/scmi.h + +Resources Enumeration +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + + - SCMI_TLM_GET_ABI_INFO: Gather ABI versioning detail and basic SCMI + Telemetry features like number of resources and supported features. + + - SCMI_TLM_GET_INTRVS: Enumerate available update intervals. + + - SCMI_TLM_GET_DE_INFO: Retrieve DataEvent description. + + - SCMI_TLM_GET_DE_LIST: Retrieve the list of all the existent DataEvent + descriptors. + + - SCMI_TLM_GET_GRP_LIST: Gather a list of descriptors for all defined Gro= ups. + + - SCMI_TLM_GET_GRP_INFO: Gather information for a specific Group. + + - SCMI_TLM_GET_GRP_DESC: Gather detailed group composition information for + the specified Group. + + - SCMI_TLM_GET_UUID_LIST: Gather a list of descriptors for all the SCMI + Telemetry UUIDs defined on this system. + +Enumerating DataEvents +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +SCMI_TLM_GET_DE_LIST returns the DataEvent descriptors exposed by the +Telemetery instance. Users can size the request from +SCMI_TLM_GET_ABI_INFO.num_des or by issuing a zero-sized batch query. + +Example +------- + +.. code-block:: c + + int main(int argc, char **argv) + { + struct scmi_tlm_abi_info abi =3D {}; + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_info *des; + int fd, ret, i; + + fd =3D open("/dev/scmi/tlm_0", O_RDONLY | O_CLOEXEC); + if (fd < 0) + return fd; + + abi.size =3D sizeof(abi); + ret =3D ioctl(fd, SCMI_TLM_GET_ABI_INFO, &abi); + if (ret) { + close(fd); + return ret; + } + + if (!abi.num_des) { + close(fd); + return ret; + } + + des =3D calloc(abi.num_des, sizeof(*des)); + if (!des) { + close(fd); + return -ENOMEM; + } + + batch.num_items =3D abi.num_des; + batch.item_sz =3D sizeof(*des); + batch.items =3D (unsigned long)des; + + ret =3D ioctl(fd, SCMI_TLM_GET_DE_LIST, &batch); + if (ret) { + free(des); + close(fd); + return ret; + } + + for (i =3D 0; i < batch.num_items; i++) { + printf("0x%08x: %.*s size:%u type:%u unit:%u", des[i].id, 16, + des[i].name, des[i].data_sz, des[i].type, des[i].unit); + + if (des[i].flags & SCMI_TLM_DEINFO_GROUP) + printf("group:%u", des[i].grp_id); + + printf("\n"); + } + + return 0; + } + +Enumerating Groups +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +SCMI_TLM_GET_GRP_LIST returns the groups exposed by the Telemetry Instance. +Group descriptors report; the group ID, the number of DEs in the group, and +the number of update intervals supported by the group. + +SCMI_TLM_GET_GRP_DESC can then be used to retrieve the DE IDs of a specific +group. + +Example +------- + +.. code-block:: c + + int main(int argc, char **argv) + { + struct scmi_tlm_abi_info abi =3D {}; + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_grp_info *groups; + int fd, ret, i; + + fd =3D open("/dev/scmi/tlm_0", O_RDONLY | O_CLOEXEC); + if (fd < 0) + return fd; + + abi.size =3D sizeof(abi); + ret =3D ioctl(fd, SCMI_TLM_GET_ABI_INFO, &abi); + + if (ret){ + free(groups); + close(fd); + return ret; + } + + if (!abi.num_groups) { + ret =3D 0; + free(groups); + close(fd); + return ret; + } + + groups =3D calloc(abi.num_groups, sizeof(*groups)); + if (!groups) { + ret =3D -ENOMEM; + free(groups); + close(fd); + } + + batch.num_items =3D abi.num_groups; + batch.item_sz =3D sizeof(*groups); + batch.items =3D (unsigned long)groups; + + ret =3D ioctl(fd, SCMI_TLM_GET_GRP_LIST, &batch); + if (ret) { + free(groups); + close(fd); + return ret; + } + + for (i =3D 0; i < batch.num_items; i++) { + struct scmi_tlm_grp_desc desc =3D {}; + __u32 *de_ids; + int j; + + printf("group:%u des:%u intervals:%u\n", groups[i].grp_id, + groups[i].num_des, groups[i].num_intervals); + + if (!groups[i].num_des) + continue; + + de_ids =3D calloc(groups[i].num_des, sizeof(*de_ids)); + if (!de_ids) { + ret =3D -ENOMEM; + free(groups); + close(fd); + return de_ids; + } + + desc.grp_id =3D groups[i].grp_id; + desc.num_des =3D groups[i].num_des; + desc.composing_des =3D (unsigned long)de_ids; + + ret =3D ioctl(fd, SCMI_TLM_GET_GRP_DESC, &desc); + if (ret) { + free(de_ids); + free(groups); + close(fd); + return ret; + } + + printf("DataEvents:"); + for (j =3D 0; j < desc.num_des; j++) + printf(" 0x%08x", de_ids[j]); + printf("\n"); + + free(de_ids); + } + + free(groups); + close(fd); + return 0; + } + +Enumerating Update Intervals +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D + +SCMI_TLM_GET_INTRVS returns the update intervals supported by the Telemetry +instance. Userspace can size the request from SCMI_TLM_GET_ABI_INFO.num_in= tervals +or by sending a zero-sized query. + +The returned intervals may be discrete values, or a range described as +low/high/step when SCMI_TLM_INTERV_DISCRETE is not set. + +Example +------- + +.. code-block:: c + + int main(int argc, char **argv) + { + struct scmi_tlm_abi_info abi =3D {}; + struct scmi_tlm_intervals ivs =3D {}; + struct scmi_tlm_update_interval *intervals; + int fd, ret, i; + + fd =3D open("/dev/scmi/tlm_0", O_RDONLY | O_CLOEXEC); + if (fd < 0) + return fd; + + abi.size =3D sizeof(abi); + ret =3D ioctl(fd, SCMI_TLM_GET_ABI_INFO, &abi); + if (ret) + goto out_fd; + + if (!abi.num_intervals) + goto out_fd; + + intervals =3D calloc(abi.num_intervals, sizeof(*intervals)); + if (!intervals) { + ret =3D -ENOMEM; + goto out_fd; + } + + ivs.num_intervals =3D abi.num_intervals; + ivs.intervals =3D (unsigned long)intervals; + + ret =3D ioctl(fd, SCMI_TLM_GET_INTRVS, &ivs); + if (ret) + goto out_free; + + if (ivs.flags & SCMI_TLM_INTERV_DISCRETE) { + for (i =3D 0; i < ivs.num_intervals; i++) + printf("interval[%d]: %u * 10^%d seconds\n", i, + intervals[i].secs, intervals[i].exp); + } else if (ivs.num_intervals >=3D 3) { + printf("low: %u * 10^%d seconds\n", + intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW].secs, + intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_LOW].exp); + printf("high: %u * 10^%d seconds\n", + intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH].secs, + intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_HIGH].exp); + printf("step: %u * 10^%d seconds\n", + intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP].secs, + intervals[SCMI_TLM_UPDATE_INTVL_SEGMENT_STEP].exp); + } + out_free: + free(intervals); + out_fd: + close(fd); + + return ret ? 1 : 0; + } + +Telemetry Instance Configuration +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D + +SCMI_TLM_GET_CFG returns the current Telemetry configuration. SCMI_TLM_SET= _CFG +updates teh enabled state and active update interval for the whole Telemet= ry +instance. + +The update interval is described as 'secs * 10 ^ exp' seconds. Userspace +should choose an interval reported by SCMI_TLM_GET_INTRVS. + +Example +------- + +.. code-block:: c + int main(int argc, char **argv) + { + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_config new_cfg =3D {}; + int fd, ret; + + fd =3D open("/dev/scmi/tlm_0", O_RDWR | O_CLOEXEC); + if (fd < 0) + return fd; + + ret =3D ioctl(fd, SCMI_TLM_GET_CFG, &old_cfg); + if (ret) { + close(fd); + return ret; + } + + printf("enabled:%u timestamps:%u interval:%u * 10^%d seconds\n", + old_cfg.enable, old_cfg.t_enable, + old_cfg.active.secs, old_cfg.active.exp); + + new_cfg.enable =3D 1; + new_cfg.t_enable =3D old_cfg.t_enable; + + new_cfg.active.secs =3D 400; + new_cfg.active.exp =3D -3; + + ret =3D ioctl(fd, SCMI_TLM_SET_CFG, &new_cfg); + if (ret) { + close(fd); + return ret; + } + + close(fd) + + return 0; + } + +Group Configuration +------------------- + +If group configuration is supported, the same ioctls can operate on a group +by setting SCMI_TLM_CONFIG_GROUP in flags and setting grp_id to a group ID +discovered with SCMI_TLM_GET_GRP_LIST. + +Configuring DataEvents +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +SCMI_TLM_GET_DE_CFG returns the configuration of one or more DataEvents. +SCMI_TLM_SET_DE_CFG updates the enabled state and timestamp state of one or +more DataEvents. + +Both ioctls use struct scmi_tlm_batch with items pointing to an array of +struct scmi_tlm_de_config. Each entry must contain the DataEvent ID in id. +Valid IDs can be discovered with SCMI_TLM_GET_DE_LIST. + +When a DataEvent is enabled, SCMI_TLM_GET_DE_CFG also reports the SHMTI sid +and offset where the DataEvent can be found in the shared memory TDCF data, +together with the UUID associated with that DataEvent. + +Note that in the example below, the cfgs[n].id numbers would normally come +from SCMI_TLM_GET_DE_LIST. + +Example +------- + +.. code-block:: c + + int main(int argc, char **argv) + { + struct scmi_tlm_de_config cfgs[2] =3D {}; + struct scmi_tlm_batch batch =3D {}; + int fd, ret, i; + + fd =3D open("/dev/scmi/tlm_0", O_RDWR | O_CLOEXEC); + if (fd < 0) + return fd; + + cfgs[0].id =3D 0x00000001; + cfgs[1].id =3D 0x00000002; + + batch.num_items =3D 2; + batch.item_sz =3D sizeof(cfgs[0]); + batch.items =3D (unsigned long)cfgs; + + ret =3D ioctl(fd, SCMI_TLM_GET_DE_CFG, &batch); + if (ret) { + close(fd); + return ret; + } + + for (i =3D 0; i < batch.num_items; i++) { + printf("DE 0x%08x: enabled:%u timestamps:%u\n", + cfgs[i].id, cfgs[i].enable, cfgs[i].t_enable); + } + + for (i =3D 0; i < batch.num_items; i++) { + cfgs[i].enable =3D 1; + cfgs[i].e_enable =3D 1; + } + + ret =3D ioctl(fd, SCMI_TLM_SET_DE_CFG, &batch); + if (ret) { + close(fd); + return ret; + } + + ret =3D ioctl(fd, SCMI_TLM_GET_DE_CFG, &batch); + if (ret) { + close(fd); + return ret; + } + + for (i =3D 0; i < batch.num_items; i++) { + printf("DE 0x%08x: enabled:%u timestamps:%u sid:%u offset:%u\n", + cfgs[i].id, cfgs[i].enable, cfgs[i].t_enable, + cfgs[i].sid, cfgs[i].offset); + } + + close(fd); + return 0; + } + +Further Configuration +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + + - SCMI_TLM_RESET: Reset the whole Telemetry configuration server side. + + - SCMI_TLM_EVENT_SUBSCRIBE: Subscribe/unsubscribe to a Telemetry event. + Lets userspace ask the kernel to notify it when a generation event happ= ens + by siganling the eventfd provided. + +Event Notification +------------------ + +Example +------- + + - Subscribe to the Telemetry generation event and wait for a notification + throught an eventfd. Event support is optional so this example checks + SCMI_TLM_ABI_FEAT_EVENT. + +.. code-block:: c + + int main(int argc, char **argv) + { + struct scmi_tlm_abi_info abi =3D {}; + struct scmi_tlm_event evt =3D {}; + eventfd_t count; + int fd, efd, ret; + + fd =3D open("/dev/scmi/tlm_0", O_RDWR | O_CLOEXEC); + if (fd < 0) + return fd; + + abi.size =3D sizeof(abi); + evt.type =3D SCMI_TLM_EVT_GENERATION; + + ret =3D ioctl(fd, SCMI_TLM_GET_ABI_INFO, &abi); + if (ret) + return ret; + + if (!(abi.abi_features & SCMI_TLM_ABI_FEAT_EVENT)) { + close(fd); + return -EOPNOTSUPP; + } + + efd =3D eventfd(0, EFD_CLOEXEC); + if (efd < 0) + return efd; + + evt.efd =3D efd; + ret =3D ioctl(fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt); + if (ret) + return ret; + + struct pollfd pfd =3D { + .fd =3D efd, + .events =3D POLLIN, + }; + + while (1) { + ret =3D poll(&pfd, 1, -1); + if (ret < 0) + break; + + if (pfd.revents & POLLIN) { + ret =3D eventfd_read(efd, &count); + if (ret) + break; + + printf("telelmetry generation changed: %llu\n", + (unsigned long long)count); + } + } + + /* Re-issue returned cookie to remove subscription */ + if (evt.cookie) + ioctl(fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt); + + close(efd); + close(fd); + + return 0; + } + +Data Collection +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + + - SCMI_TLM_DE_READ: Report the last updated sample for the specified + DataEvent. + + - SCMI_TLM_BULK_READ: Report the last samples for all the currently enabl= ed + DataEvents. + + - SCMI_TLM_BATCH_READ: Report the last samples for the DataEvents IDs + specified within the samples input params. + + - SCMI_TLM_SINGLE_READ: Trigger an immediate platform-side DataEvent + update and report the collected samples. + +Memory Mapped Raw Access +------------------------ + + - SCMI_TLM_GET_SHMTI_LIST: Gather a list of open file descriptors, one for + each SHMTI memory area defined for this instance, that can be used to + memory-map such areas in the calling process address space so as to be + able to directly access the SCMI Telemetry SHMTI areas shared by the + platform firmware, and parse the TDCF format. + The SCMI Telemetry TDCF format is defined in the specification at [1]. + + +Example +------- + +.. code-block:: c + + int main(int argc, char **argv) + { + struct scmi_tlm_de_config des_cfg =3D {}; + struct scmi_tlm_config cfg =3D {}; + struct scmi_tlm_de_sample samples[3] =3D {}; + struct scmi_tlm_data_read data =3D {}; + int fd, ret; + + fd =3D open("/dev/scmi/tlm_0", O_RDWR); + if (fd < 0) + return fd; + + /* Enable ALL Data Events with timestamps*/ + des_cfg.enable =3D 1; + des_cfg.t_enable =3D 1; + ret =3D ioctl(fd, SCMI_TLM_SET_ALL_CFG, &des_cfg); + if (ret) + return ret; + + /* Enable Telemetry as a whole, set a 400ms update interval */ + cfg.enable =3D 1; + cfg.active.secs =3D 400; + cfg.active.exp =3D -3; + + ret =3D ioctl(fd, SCMI_TLM_SET_CFG, &cfg); + if (ret) + return ret; + + /* Read a selection of DEs */ + samples[0].id =3D 0xA001; + samples[1].id =3D 0x0002; + samples[2].id =3D 0x1010; + data.num_samples =3D 3; + data.samples =3D (unsigned long)samples; + + ret =3D ioctl(fd, SCMI_TLM_BATCH_READ, &data); + if (ret) + return ret; + + for (int i =3D 0; i < 3; i++) + fprintf(stdout, "%llu: 0x%08X -> %llu\n", + samples[i].tstamp, samples[i].id, samples[i].val); + + return 0; + } + +References +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +[1]: https://developer.arm.com/documentation/den0056/latest/ --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304702; b=N2hPctzGHonD0ZfgX+2xMCUKe2CQRYho3jaaoLR2qcOBjn3MCD2DSdLk4HTI/LLf0Fnh xvxpOSCvY/A48yFToxAKfFgzE6ANjLLMqAz4tJrU+dahmriFu3qu9UzjUsmUse07idqxP QDgY8Yj+JnB2cjIQYDQ6lu1siTpeuK56Uxo3WB9sMhh1QRgT6M+VLQIl65wAkf5VkjMmk Kp37PeoL0uuAkS62u4r22ASipRXKPF/Buem5DfeN4g4JQvv8CeT9pdrjBmmBQRf25aEmH 7EtBzp6dVHcgzWQfsPLqk3AREwLH+26EMAH8t2xUdZOIcQP2Zpq3t7nnsOxrShNuPUw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304702; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=DFCyskWT4gsQNHxstjOYTM+sMA7tJMel7jescmsdbIQ=; b=y30Jqjm76Uva51UD/OI4m7LCK8YteDLY+xzjHUSIO7GYGzDY2RpaWFDwZCUzq7iG4cEG yympa4boJc8Q9k7YnYlxhezlDrCfOizZyBUCRJp+B4QnvrbB4UUdLSmvtInpH8OY7uX5u P8secG7kQLJ6mnHSAsmR7QMKPiWrV4MsRS48L3NfQuecelbE9k60kwgEqYY+Ma473ckm4 Cd15Fr0aK0p4aQMvfqS9CDUXswOk666KTgTs7F5lcpMd1qFw85u21qptTIPhIIV++82BW Zea8uLZOxPD7xeyuZ31Xtj0FYAjuEWRGrse/JaoDoF814+oXKZm3pX/IPH8kQ2MhnmA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=HtTsptXe; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 90130606A6AC; Sat, 21 Nov 2026 23:45:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 90130606A6AC Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=HtTsptXe 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 3F30F1575; Sat, 21 Nov 2026 15:44:57 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 50EA53F632; Sat, 21 Nov 2026 15:44:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304700; bh=GzV2Djeo/knNVwWJbCh0hft47sO14tCdvVJooNm5FkM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HtTsptXej8ixDZg3+PJeOTN5nWpOuxmwgWRD8QB0NAHNJMiegZ4GzXtx0U3UMXpSb XWGq8u9UjpTI2zK8LFLxev3rPnPyF5M0HjNOQxi7OQ+smdpNrE+s2sbXj8Lb0LC+IA 5uL2XRrsM3/YbjVtlPp/8+FICeynKf+V/DSFidJg= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 30/31] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases Date: Sat, 21 Nov 2026 23:40:04 +0000 Message-ID: <20261121234005.1525419-31-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Philip Radford Add support to test SCMI Telemetry UAPI compliance testcases. Co-developed-by: Cristian Marussi Signed-off-by: Philip Radford Signed-off-by: Cristian Marussi --- tools/testing/selftests/arm64/Makefile | 2 +- tools/testing/selftests/arm64/scmi/Makefile | 12 + tools/testing/selftests/arm64/scmi/config | 2 + .../arm64/scmi/test_scmi_telemetry_uapi.c | 2034 +++++++++++++++++ 4 files changed, 2049 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/arm64/scmi/Makefile create mode 100644 tools/testing/selftests/arm64/scmi/config create mode 100644 tools/testing/selftests/arm64/scmi/test_scmi_telemetry_= uapi.c diff --git a/tools/testing/selftests/arm64/Makefile b/tools/testing/selftes= ts/arm64/Makefile index e456f3b62fa1..f3beae2ae164 100644 --- a/tools/testing/selftests/arm64/Makefile +++ b/tools/testing/selftests/arm64/Makefile @@ -4,7 +4,7 @@ ARCH ?=3D $(shell uname -m 2>/dev/null || echo not) =20 ifneq (,$(filter $(ARCH),aarch64 arm64)) -ARM64_SUBTARGETS ?=3D tags signal pauth fp mte bti abi gcs +ARM64_SUBTARGETS ?=3D tags signal pauth fp mte bti abi gcs scmi else ARM64_SUBTARGETS :=3D endif diff --git a/tools/testing/selftests/arm64/scmi/Makefile b/tools/testing/se= lftests/arm64/scmi/Makefile new file mode 100644 index 000000000000..3d9576c7a3e8 --- /dev/null +++ b/tools/testing/selftests/arm64/scmi/Makefile @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 ARM Limited + +# Additional include paths needed by kselftest.h and local headers +CFLAGS +=3D -std=3Dgnu99 -I. +LDFLAGS +=3D -static + +# Generated binaries to be installed by top KSFT script +TEST_GEN_PROGS :=3D test_scmi_telemetry_uapi + +# Include KSFT lib.mk. +include ../../lib.mk diff --git a/tools/testing/selftests/arm64/scmi/config b/tools/testing/self= tests/arm64/scmi/config new file mode 100644 index 000000000000..63cd7e67ab55 --- /dev/null +++ b/tools/testing/selftests/arm64/scmi/config @@ -0,0 +1,2 @@ +CONFIG_ARM_SCMI_SYSTEM_TELEMETRY=3Dy + diff --git a/tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c = b/tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c new file mode 100644 index 000000000000..12dd44a5c72d --- /dev/null +++ b/tools/testing/selftests/arm64/scmi/test_scmi_telemetry_uapi.c @@ -0,0 +1,2034 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 ARM Limited + +#include +#include +#include +#include +#include +#include + +#include "kselftest_harness.h" + +#include <../../../../../include/uapi/linux/scmi.h> + +#define TLM_0 "tlm_0" + +struct scmi_ksft_session { + bool initialized; + struct scmi_tlm_abi_info info; + struct scmi_tlm_config *original_cfg; + struct scmi_tlm_intervals available; + struct scmi_tlm_update_interval *intervals; + struct scmi_tlm_de_info *de_list; + unsigned int num_des; + +} ksft_session; + +static int scmi_tlm_batch_init(struct scmi_tlm_batch *batch, + unsigned int num_items, + size_t item_sz, + bool with_states) +{ + void *items =3D NULL; + int *states =3D NULL; + + bzero(batch, sizeof(*batch)); + + if (num_items) { + items =3D malloc(item_sz * num_items); + if (!items) + return -1; + + bzero(items, item_sz * num_items); + } + + if (with_states && num_items) { + states =3D malloc(sizeof(*states) * num_items); + if (!states) { + free(items); + return -1; + } + + bzero(states, sizeof(*states) * num_items); + } + + batch->num_items =3D num_items; + batch->item_sz =3D item_sz; + batch->items =3D (__u64)items; + batch->states =3D (__u64)states; + + return 0; +} + +static void scmi_tlm_batch_free(struct scmi_tlm_batch *batch) +{ + free((void *)batch->items); + free((void *)batch->states); + bzero(batch, sizeof(*batch)); +} + +static __attribute__((constructor)) void scmi_tlm_initialize(void) +{ + struct scmi_tlm_batch batch =3D {}; + int fd, ret; + + fd =3D open("/dev/scmi/" TLM_0, O_RDWR); + if (fd < 0) { + fprintf(stderr, "INIT: device not available\n"); + return; + } + + ksft_session.info.size =3D sizeof(struct scmi_tlm_abi_info); + ret =3D ioctl(fd, SCMI_TLM_GET_ABI_INFO, &ksft_session.info); + if (ret) { + fprintf(stderr, "INIT: failed to get ABI info\n"); + goto out; + } + + if (ksft_session.info.abi_version !=3D SCMI_TLM_ABI_VERSION_V1) { + fprintf(stderr, "INIT: ABI Version %u NOT supported", + ksft_session.info.abi_version); + goto out; + } + + if (ksft_session.info.num_des) { + ret =3D scmi_tlm_batch_init(&batch, ksft_session.info.num_des, + sizeof(*ksft_session.de_list), false); + if (!ret) { + ret =3D ioctl(fd, SCMI_TLM_GET_DE_LIST, &batch); + if (!ret) { + ksft_session.de_list =3D (struct scmi_tlm_de_info *)batch.items; + ksft_session.num_des =3D batch.num_items; + } else { + scmi_tlm_batch_free(&batch); + } + } + } + + if (ksft_session.info.num_intervals) { + int num_intervals =3D ksft_session.info.num_intervals; + struct scmi_tlm_update_interval *intervals; + + intervals =3D malloc(sizeof(*intervals) * num_intervals); + if (!intervals) { + fprintf(stderr, "INIT: Failed to allocate intervals"); + goto out; + } + + bzero(intervals, sizeof(*intervals) * num_intervals); + + ksft_session.intervals =3D intervals; + ksft_session.available.intervals =3D (__u64)intervals; + ksft_session.available.num_intervals =3D num_intervals; + + ret =3D ioctl(fd, SCMI_TLM_GET_INTRVS, &ksft_session.available); + if (ret) { + fprintf(stderr, "INIT: Failed to retrieve intervals"); + free(intervals); + goto out; + } + } + + fprintf(stdout, "INIT: Found ABI version %u\n", + ksft_session.info.abi_version); + fprintf(stdout, "INIT: Discovered %u global update intervals\n", + ksft_session.available.num_intervals); + + ksft_session.original_cfg =3D malloc(sizeof(*ksft_session.original_cfg)); + if (ksft_session.original_cfg) { + bzero(ksft_session.original_cfg, sizeof(*ksft_session.original_cfg)); + + ret =3D ioctl(fd, SCMI_TLM_GET_CFG, ksft_session.original_cfg); + if (ret) { + free(ksft_session.original_cfg); + ksft_session.original_cfg =3D NULL; + } + } + + ksft_session.initialized =3D true; + +out: + close(fd); +} + +TEST(scmi_tlm_config_struct_layout) { + ASSERT_EQ(24, sizeof(struct scmi_tlm_config)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_config, enable)); + ASSERT_EQ(1, offsetof(struct scmi_tlm_config, t_enable)); + ASSERT_EQ(2, offsetof(struct scmi_tlm_config, flags)); + ASSERT_EQ(3, offsetof(struct scmi_tlm_config, pad)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_config, grp_id)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_config, active)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_config, reserved)); +} + +TEST(scmi_tlm_abi_info_struct_layout) { + ASSERT_EQ(56, sizeof(struct scmi_tlm_abi_info)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_abi_info, size)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_abi_info, abi_version)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_abi_info, abi_features)); + ASSERT_EQ(12, offsetof(struct scmi_tlm_abi_info, primary_de_impl_version)= ); + ASSERT_EQ(28, offsetof(struct scmi_tlm_abi_info, num_des)); + ASSERT_EQ(32, offsetof(struct scmi_tlm_abi_info, num_groups)); + ASSERT_EQ(36, offsetof(struct scmi_tlm_abi_info, num_intervals)); + ASSERT_EQ(40, offsetof(struct scmi_tlm_abi_info, num_shmtis)); + ASSERT_EQ(44, offsetof(struct scmi_tlm_abi_info, features)); + ASSERT_EQ(48, offsetof(struct scmi_tlm_abi_info, reserved)); +} + +TEST(scmi_tlm_update_interval_struct_layout) { + ASSERT_EQ(8, sizeof(struct scmi_tlm_update_interval)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_update_interval, secs)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_update_interval, exp)); +} + +TEST(scmi_tlm_intervals_struct_layout) { + ASSERT_EQ(32, sizeof(struct scmi_tlm_intervals)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_intervals, grp_id)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_intervals, flags)); + ASSERT_EQ(5, offsetof(struct scmi_tlm_intervals, pad)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_intervals, num_intervals)); + ASSERT_EQ(12, offsetof(struct scmi_tlm_intervals, pad2)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_intervals, reserved)); + ASSERT_EQ(24, offsetof(struct scmi_tlm_intervals, intervals)); +} + +TEST(scmi_tlm_de_config_struct_layout) { + ASSERT_EQ(48, sizeof(struct scmi_tlm_de_config)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_de_config, id)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_de_config, enable)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_de_config, t_enable)); + ASSERT_EQ(12, offsetof(struct scmi_tlm_de_config, sid)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_de_config, offset)); + ASSERT_EQ(20, offsetof(struct scmi_tlm_de_config, pad)); + ASSERT_EQ(24, offsetof(struct scmi_tlm_de_config, uuid)); + ASSERT_EQ(40, offsetof(struct scmi_tlm_de_config, reserved)); +} + +TEST(scmi_tlm_de_info_struct_layout) { + ASSERT_EQ(72, sizeof(struct scmi_tlm_de_info)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_de_info, id)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_de_info, grp_id)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_de_info, data_sz)); + ASSERT_EQ(12, offsetof(struct scmi_tlm_de_info, type)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_de_info, unit)); + ASSERT_EQ(20, offsetof(struct scmi_tlm_de_info, unit_exp)); + ASSERT_EQ(24, offsetof(struct scmi_tlm_de_info, ts_rate)); + ASSERT_EQ(28, offsetof(struct scmi_tlm_de_info, instance_id)); + ASSERT_EQ(32, offsetof(struct scmi_tlm_de_info, compo_instance_id)); + ASSERT_EQ(36, offsetof(struct scmi_tlm_de_info, compo_type)); + ASSERT_EQ(40, offsetof(struct scmi_tlm_de_info, persistent)); + ASSERT_EQ(41, offsetof(struct scmi_tlm_de_info, flags)); + ASSERT_EQ(42, offsetof(struct scmi_tlm_de_info, pad)); + ASSERT_EQ(44, offsetof(struct scmi_tlm_de_info, pad2)); + ASSERT_EQ(48, offsetof(struct scmi_tlm_de_info, name)); + ASSERT_EQ(64, offsetof(struct scmi_tlm_de_info, reserved)); +} + +TEST(scmi_tlm_de_sample_struct_layout) { + ASSERT_EQ(24, sizeof(struct scmi_tlm_de_sample)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_de_sample, id)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_de_sample, pad)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_de_sample, tstamp)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_de_sample, val)); +} + +TEST(scmi_tlm_data_read_struct_layout) { + ASSERT_EQ(24, sizeof(struct scmi_tlm_data_read)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_data_read, grp_id)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_data_read, flags)); + ASSERT_EQ(5, offsetof(struct scmi_tlm_data_read, pad)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_data_read, pad2)); + ASSERT_EQ(12, offsetof(struct scmi_tlm_data_read, num_samples)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_data_read, samples)); +} + +TEST(scmi_tlm_batch_struct_layout) { + ASSERT_EQ(32, sizeof(struct scmi_tlm_batch)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_batch, num_items)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_batch, item_sz)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_batch, reserved)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_batch, states)); + ASSERT_EQ(24, offsetof(struct scmi_tlm_batch, items)); +} + +TEST(scmi_tlm_grp_info_struct_layout) { + ASSERT_EQ(24, sizeof(struct scmi_tlm_grp_info)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_grp_info, grp_id)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_grp_info, num_des)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_grp_info, num_intervals)); + ASSERT_EQ(12, offsetof(struct scmi_tlm_grp_info, pad)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_grp_info, reserved)); +} + +TEST(scmi_tlm_grp_desc_struct_layout) { + ASSERT_EQ(24, sizeof(struct scmi_tlm_grp_desc)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_grp_desc, grp_id)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_grp_desc, num_des)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_grp_desc, composing_des)); +} + +TEST(scmi_tlm_shmti_info_struct_layout) { + ASSERT_EQ(24, sizeof(struct scmi_tlm_shmti_info)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_shmti_info, sid)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_shmti_info, fd)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_shmti_info, len)); + ASSERT_EQ(12, offsetof(struct scmi_tlm_shmti_info, offset)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_shmti_info, reserved)); +} + +TEST(scmi_tlm_uuid_struct_layout) { + ASSERT_EQ(16, sizeof(struct scmi_tlm_uuid)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_uuid, bytes)); +} + +TEST(scmi_tlm_event_struct_layout) { + ASSERT_EQ(24, sizeof(struct scmi_tlm_event)); + ASSERT_EQ(0, offsetof(struct scmi_tlm_event, type)); + ASSERT_EQ(4, offsetof(struct scmi_tlm_event, efd)); + ASSERT_EQ(8, offsetof(struct scmi_tlm_event, cookie)); + ASSERT_EQ(12, offsetof(struct scmi_tlm_event, pad)); + ASSERT_EQ(16, offsetof(struct scmi_tlm_event, reserved)); +} + +FIXTURE(stlm) { + int fd; +}; + +FIXTURE_SETUP(stlm) { + if (!ksft_session.initialized) + SKIP(return, "initialization failed"); + + self->fd =3D open("/dev/scmi/" TLM_0, O_RDWR); + if (self->fd < 0) + SKIP(return, "device not available"); + + TH_LOG("Testing ABI Version %u on device %s", + ksft_session.info.abi_version, TLM_0); +} + +FIXTURE_TEARDOWN(stlm) { + close(self->fd); +} + +TEST_F(stlm, tlm_get_cfg) { + struct scmi_tlm_config cfg =3D {}; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg)); + ASSERT_EQ(0, cfg.pad); + ASSERT_EQ(0, cfg.reserved); + ASSERT_EQ(0, cfg.flags & ~SCMI_TLM_CONFIG_FLAGS); + + TH_LOG("SCMI_TLM_GET_CFG - ena:%u t_ena:%u secs:%u exp:%d", + cfg.enable, cfg.t_enable, cfg.active.secs, cfg.active.exp); + + for (int i =3D 0; i < ksft_session.info.num_groups; i++) { + cfg.flags =3D SCMI_TLM_CONFIG_GROUP; + cfg.grp_id =3D i; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg)); + ASSERT_EQ(0, cfg.pad); + ASSERT_EQ(0, cfg.reserved); + ASSERT_EQ(SCMI_TLM_CONFIG_GROUP, cfg.flags & SCMI_TLM_CONFIG_FLAGS); + ASSERT_EQ(i, cfg.grp_id); + + TH_LOG("SCMI_TLM_GET_CFG - ena:%u t_ena:%u grp_id:%u secs:%u exp:%d", + cfg.enable, cfg.t_enable, cfg.grp_id, cfg.active.secs, cfg.active= .exp); + } + + /* Invalid GRP ID should be ignored when proper flags missing */ + cfg.grp_id =3D ksft_session.info.num_groups; + cfg.flags =3D 0x0; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg)); + ASSERT_EQ(0, cfg.pad); + ASSERT_EQ(0, cfg.reserved); +} + +TEST_F(stlm, tlm_get_cfg_bad_group) { + struct scmi_tlm_config cfg =3D {}; + + cfg.flags =3D SCMI_TLM_CONFIG_GROUP; + cfg.grp_id =3D ksft_session.info.num_groups; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg)); + ASSERT_EQ(0, cfg.pad); + ASSERT_EQ(0, cfg.reserved); + ASSERT_EQ(0, cfg.flags & ~SCMI_TLM_CONFIG_FLAGS); +} + +TEST_F(stlm, tlm_get_cfg_null_ptr_rejected) { + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, NULL)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_get_cfg_invalid_request) { + struct scmi_tlm_config cfg =3D { }; + + cfg.pad =3D 0x1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg)); + EXPECT_EQ(EINVAL, errno); + + cfg.pad =3D 0x0; + cfg.reserved =3D 0x1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg)); + EXPECT_EQ(EINVAL, errno); + + cfg.reserved =3D 0x0; + cfg.flags =3D ~SCMI_TLM_CONFIG_FLAGS; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_CFG, &cfg)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_set_cfg) { + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_config new_cfg =3D {}; + struct scmi_tlm_config check_cfg =3D {}; + + if (!ksft_session.original_cfg) + SKIP(return, "original config not available"); + + old_cfg =3D *ksft_session.original_cfg; + + new_cfg =3D old_cfg; + new_cfg.enable =3D !old_cfg.enable; + + if (ksft_session.info.num_intervals > 0) { + int num_intervals =3D ksft_session.info.num_intervals; + struct scmi_tlm_update_interval *intervals; + bool need_valid_interval; + + intervals =3D (struct scmi_tlm_update_interval *) + ksft_session.available.intervals; + + need_valid_interval =3D new_cfg.enable && old_cfg.active.secs =3D=3D 0 && + old_cfg.active.exp =3D=3D 0; + + for (int i =3D 0; i < num_intervals; i++) { + if (!need_valid_interval && + intervals[i].secs =3D=3D old_cfg.active.secs && + intervals[i].exp =3D=3D old_cfg.active.exp) + continue; + + /* pick another valid interval */ + new_cfg.active.secs =3D intervals[i].secs; + new_cfg.active.exp =3D intervals[i].exp; + break; + } + } + + TH_LOG("SCMI_TLM_SET_CFG - ena:%u t_ena:%u secs:%u exp:%d", + new_cfg.enable, new_cfg.t_enable, new_cfg.active.secs, new_cfg.act= ive.exp); + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_CFG, &new_cfg)); + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_CFG, &check_cfg)); + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_CFG, &old_cfg)); + + ASSERT_EQ(new_cfg.enable, check_cfg.enable); + ASSERT_EQ(new_cfg.active.secs, check_cfg.active.secs); + ASSERT_EQ(new_cfg.active.exp, check_cfg.active.exp); + ASSERT_EQ(0, check_cfg.pad); + ASSERT_EQ(0, check_cfg.reserved); +} + +TEST_F(stlm, tlm_get_intrvs) { + struct scmi_tlm_update_interval *intervals; + struct scmi_tlm_intervals ivs =3D {}; + int num_intervals =3D ksft_session.info.num_intervals; + + if (!num_intervals) + SKIP(return, "No update intervals available"); + + intervals =3D malloc(sizeof(*intervals) * num_intervals); + ASSERT_NE(NULL, intervals); + bzero(intervals, sizeof(*intervals) * num_intervals); + + ivs.num_intervals =3D num_intervals; + ivs.intervals =3D (__u64)intervals; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs)); + + ASSERT_EQ(num_intervals, ivs.num_intervals); + ASSERT_EQ(0, ivs.pad[0]); + ASSERT_EQ(0, ivs.pad[1]); + ASSERT_EQ(0, ivs.pad[2]); + ASSERT_EQ(0, ivs.pad2); + ASSERT_EQ(0, ivs.reserved); + ASSERT_EQ(0, ivs.flags & ~SCMI_TLM_INTERV_FLAGS); + + for (int i =3D 0; i < num_intervals; i++) { + TH_LOG("SCMI_TLM_GET_INTRVS - [%d] secs:%u exp:%d", i, + intervals[i].secs, intervals[i].exp); + } + + free(intervals); +} + +TEST_F(stlm, tlm_get_intrvs_invalid_request) { + struct scmi_tlm_update_interval interval =3D {}; + struct scmi_tlm_intervals ivs =3D {}; + + ivs.num_intervals =3D 1; + ivs.intervals =3D (__u64)&interval; + ivs.pad[0] =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs)); + EXPECT_EQ(EINVAL, errno); + + bzero(&ivs, sizeof(ivs)); + ivs.num_intervals =3D 1; + ivs.intervals =3D (__u64)&interval; + ivs.pad2 =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs)); + EXPECT_EQ(EINVAL, errno); + + bzero(&ivs, sizeof(ivs)); + ivs.num_intervals =3D 1; + ivs.intervals =3D (__u64)&interval; + ivs.reserved =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs)); + EXPECT_EQ(EINVAL, errno); + + bzero(&ivs, sizeof(ivs)); + ivs.num_intervals =3D 1; + ivs.intervals =3D (__u64)&interval; + ivs.flags =3D ~SCMI_TLM_INTERV_FLAGS; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs)); +} + +TEST_F(stlm, tlm_get_intrvs_oversized) { + struct scmi_tlm_update_interval interval =3D {}; + struct scmi_tlm_intervals ivs =3D {}; + + ivs.num_intervals =3D ksft_session.info.num_intervals + 1; + ivs.intervals =3D (__u64)&interval; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_INTRVS, &ivs)); + EXPECT_EQ(ENOSPC, errno); +} + +TEST_F(stlm, tlm_get_de_list) +{ + struct scmi_tlm_de_info *des; + struct scmi_tlm_batch batch =3D {}; + + if (!ksft_session.info.num_des) + SKIP(return, "no DEs available"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, ksft_session.info.num_des, + sizeof(*des), false)); + + des =3D (struct scmi_tlm_de_info *)batch.items; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch)); + + ASSERT_LE(batch.num_items, ksft_session.info.num_des); + + for (int i =3D 0; i < batch.num_items; i++) { + ASSERT_EQ(0, des[i].reserved); + ASSERT_EQ(0, des[i].pad[0]); + ASSERT_EQ(0, des[i].pad[1]); + ASSERT_EQ(0, des[i].pad2); + ASSERT_EQ(0, des[i].flags & ~SCMI_TLM_DEINFO_FLAGS); + + TH_LOG("SCMI_TLM_GET_DE_LIST - [%d] id:0x%x name:%s", i, des[i].id, + des[i].name); + } + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_get_de_list_zero_size_query) +{ + struct scmi_tlm_batch batch =3D {}; + + batch.num_items =3D 0; + batch.item_sz =3D sizeof(struct scmi_tlm_de_info); + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch)); + ASSERT_EQ(ksft_session.info.num_des, batch.num_items); + ASSERT_EQ(sizeof(struct scmi_tlm_de_info), batch.item_sz); + ASSERT_EQ(0, batch.reserved); + ASSERT_EQ(0, batch.items); +} + +TEST_F(stlm, tlm_get_de_list_zero_size_query_bad_item_sz) +{ + struct scmi_tlm_batch batch =3D {}; + + batch.num_items =3D 0; + batch.item_sz =3D sizeof(struct scmi_tlm_de_info) + 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_de_list_invalid_request) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_info de =3D {}; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(de) + 1; + batch.items =3D (__u64)&de; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D 1; + batch.item_sz =3D sizeof(de); + batch.reserved =3D 1; + batch.items =3D (__u64)&de; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D ksft_session.info.num_des + 1; + batch.item_sz =3D sizeof(de); + batch.items =3D (__u64)&de; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_de_info) +{ + struct scmi_tlm_de_info info =3D {}; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + info.id =3D ksft_session.de_list[0].id; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info)); + ASSERT_EQ(0, info.reserved); + ASSERT_EQ(0, info.pad[0]); + ASSERT_EQ(0, info.pad[1]); + ASSERT_EQ(0, info.pad2); + ASSERT_EQ(0, info.flags & ~SCMI_TLM_DEINFO_FLAGS); + + TH_LOG("SCMI_TLM_GET_DE_INFO - id:0x%x name:%s data_sz:%u", + info.id, info.name, info.data_sz); +} + +TEST_F(stlm, tlm_get_de_info_invalid_request) +{ + struct scmi_tlm_de_info info =3D {}; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + info.id =3D ksft_session.de_list[0].id; + info.flags =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info)); + EXPECT_EQ(EINVAL, errno); + + bzero(&info, sizeof(info)); + info.id =3D ksft_session.de_list[0].id; + info.pad[0] =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info)); + EXPECT_EQ(EINVAL, errno); + + bzero(&info, sizeof(info)); + info.id =3D ksft_session.de_list[0].id; + info.reserved =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_de_info_bad_id) +{ + struct scmi_tlm_de_info info =3D {}; + + info.id =3D 0xffffffff; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_INFO, &info)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_de_cfg) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config *cfg; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), false)); + + cfg =3D (struct scmi_tlm_de_config *)batch.items; + cfg[0].id =3D ksft_session.de_list[0].id; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch)); + ASSERT_EQ(1, batch.num_items); + ASSERT_EQ(ksft_session.de_list[0].id, cfg[0].id); + ASSERT_EQ(0, cfg[0].reserved); + + TH_LOG("SCMI_TLM_GET_DE_CFG - id:0x%x ena:%u sid:%u offset:%u", + cfg[0].id, cfg[0].enable, cfg[0].sid, + cfg[0].offset); + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_get_de_cfg_invalid_request) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config cfg =3D {}; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(cfg) + 1; + batch.items =3D (__u64)&cfg; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D ksft_session.info.num_des + 1; + batch.item_sz =3D sizeof(cfg); + batch.items =3D (__u64)&cfg; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D 1; + batch.item_sz =3D sizeof(cfg); + batch.items =3D 0; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_get_de_cfg_bad_id) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config *cfg; + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), false)); + + cfg =3D (struct scmi_tlm_de_config *)batch.items; + cfg[0].id =3D 0xffffffff; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch)); + EXPECT_EQ(ENODEV, errno); + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_get_de_cfg_bad_id_with_states) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config *cfg; + int *states; + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), true)); + + cfg =3D (struct scmi_tlm_de_config *)batch.items; + states =3D (int *)batch.states; + + cfg[0].id =3D 0xffffffff; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch)); + ASSERT_LT(states[0], 0); + + scmi_tlm_batch_free(&batch); +} + +static int scmi_tlm_get_one_de_cfg(int fd, __u32 id, + struct scmi_tlm_de_config *cfg) +{ + struct scmi_tlm_batch batch =3D {}; + + bzero(cfg, sizeof(*cfg)); + cfg->id =3D id; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(*cfg); + batch.items =3D (__u64)cfg; + + return ioctl(fd, SCMI_TLM_GET_DE_CFG, &batch); +} + +static int scmi_tlm_set_one_de_cfg(int fd, struct scmi_tlm_de_config *cfg) +{ + struct scmi_tlm_batch batch =3D {}; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(*cfg); + batch.items =3D (__u64)cfg; + + return ioctl(fd, SCMI_TLM_SET_DE_CFG, &batch); +} + +static int scmi_tlm_enable_for_read(int fd, __u32 de_id, + struct scmi_tlm_config *old_cfg, + struct scmi_tlm_de_config *old_de_cfg) +{ + struct scmi_tlm_config new_cfg; + struct scmi_tlm_de_config new_de_cfg; + + if (ioctl(fd, SCMI_TLM_GET_CFG, old_cfg)) + return -1; + + if (scmi_tlm_get_one_de_cfg(fd, de_id, old_de_cfg)) + return -1; + + new_cfg =3D *old_cfg; + new_cfg.enable =3D 1; + + if (new_cfg.active.secs =3D=3D 0 && new_cfg.active.exp =3D=3D 0) { + struct scmi_tlm_update_interval *intervals; + + if (!ksft_session.info.num_intervals) + return -1; + + intervals =3D (struct scmi_tlm_update_interval *) + ksft_session.available.intervals; + + new_cfg.active =3D intervals[0]; + } + + if (ioctl(fd, SCMI_TLM_SET_CFG, &new_cfg)) { + ioctl(fd, SCMI_TLM_SET_CFG, old_cfg); + return -1; + } + + new_de_cfg =3D *old_de_cfg; + new_de_cfg.enable =3D 1; + + if (scmi_tlm_set_one_de_cfg(fd, &new_de_cfg)) { + ioctl(fd, SCMI_TLM_SET_CFG, old_cfg); + return -1; + } + + return 0; +} + +static void scmi_tlm_restore_after_read(int fd, + struct scmi_tlm_config *old_cfg, + struct scmi_tlm_de_config *old_de_cfg) +{ + scmi_tlm_set_one_de_cfg(fd, old_de_cfg); + ioctl(fd, SCMI_TLM_SET_CFG, old_cfg); +} + +TEST_F(stlm, tlm_de_read) +{ + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_de_config old_de_cfg =3D {}; + struct scmi_tlm_de_sample sample =3D {}; + __u32 de_id; + int ret; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list availalbe"); + + de_id =3D ksft_session.de_list[0].id; + + if (scmi_tlm_enable_for_read(self->fd, de_id, &old_cfg, &old_de_cfg)) + SKIP(return, "failed to enable telemetry for read"); + + sample.id =3D de_id; + ret =3D ioctl(self->fd, SCMI_TLM_DE_READ, &sample); + + scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg); + + ASSERT_EQ(0, ret); + ASSERT_EQ(de_id, sample.id); + ASSERT_EQ(0, sample.pad); + + TH_LOG("SCMI_TLM_DE_READ - id:0x%x tstamp:%llu val:%llu", sample.id, + sample.tstamp, sample.val); +} + +TEST_F(stlm, tlm_de_read_invalid_request) +{ + struct scmi_tlm_de_sample sample =3D {}; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + sample.id =3D ksft_session.de_list[0].id; + sample.pad =3D 1; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_DE_READ, &sample)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_de_read_null_ptr_rejected) +{ + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_DE_READ, NULL)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_de_read_bad_id) +{ + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_de_config old_de_cfg =3D {}; + struct scmi_tlm_de_sample sample =3D {}; + int ret, saved_errno; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + if (scmi_tlm_enable_for_read(self->fd, ksft_session.de_list[0].id, + &old_cfg, &old_de_cfg)) + SKIP(return, "failed to enable telemetry for read"); + + sample.id =3D 0xffffffff; + + ret =3D ioctl(self->fd, SCMI_TLM_DE_READ, &sample); + saved_errno =3D errno; + + scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg); + + ASSERT_EQ(-1, ret); + EXPECT_EQ(ENOENT, saved_errno); +} + +TEST_F(stlm, tlm_single_read) +{ + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_de_config old_de_cfg =3D {}; + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read read =3D {}; + int num_samples =3D ksft_session.num_des; + int ret; + __u32 de_id; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + de_id =3D ksft_session.de_list[0].id; + + if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE) || + !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE)) + SKIP(return, "single sample read not supported"); + + if (scmi_tlm_enable_for_read(self->fd, de_id, &old_cfg, &old_de_cfg)) + SKIP(return, "failed to enable telemetry for read"); + + samples =3D malloc(sizeof(*samples) * num_samples); + ASSERT_NE(NULL, samples); + bzero(samples, sizeof(*samples) * num_samples); + + read.num_samples =3D num_samples; + read.samples =3D (__u64)samples; + + ret =3D ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read); + scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg); + + ASSERT_EQ(0, ret); + ASSERT_LE(read.num_samples, num_samples); + ASSERT_EQ(0, read.pad[0]); + ASSERT_EQ(0, read.pad[1]); + ASSERT_EQ(0, read.pad[2]); + ASSERT_EQ(0, read.pad2); + ASSERT_EQ(0, read.flags & ~SCMI_TLM_READ_FLAGS); + + for (int i =3D 0; i < read.num_samples; i++) { + ASSERT_EQ(0, samples[i].pad); + + TH_LOG("SCMI_TLM_SINGE_READ =3D [%d] id:0x%x tstamp:%llu val:%llu", i, + samples[i].id, samples[i].tstamp, samples[i].val); + } + + free(samples); +} + +TEST_F(stlm, tlm_single_read_invalid_request) +{ + struct scmi_tlm_de_sample sample =3D {}; + struct scmi_tlm_data_read read =3D {}; + + if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE) || + !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE)) + SKIP(return, "single sample read not supported"); + + read.num_samples =3D 1; + read.samples =3D (__u64)&sample; + read.pad[0] =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read)); + EXPECT_EQ(EINVAL, errno); + + bzero(&read, sizeof(read)); + read.num_samples =3D 1; + read.samples =3D (__u64)&sample; + read.pad2 =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read)); + EXPECT_EQ(EINVAL, errno); + + bzero(&read, sizeof(read)); + read.num_samples =3D 1; + read.samples =3D (__u64)&sample; + read.flags =3D ~SCMI_TLM_READ_FLAGS; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_single_read_oversized) +{ + struct scmi_tlm_de_sample sample =3D {}; + struct scmi_tlm_data_read read =3D {}; + + if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE) || + !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE)) + SKIP(return, "single sample read not supported"); + + read.num_samples =3D ksft_session.info.num_des + 1; + read.samples =3D (__u64)&sample; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, &read)); + EXPECT_EQ(E2BIG, errno); +} + +TEST_F(stlm, tlm_single_read_null_ptr_rejected) +{ + if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE) || + !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE)) + SKIP(return, "single sample read not supported"); + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SINGLE_READ, NULL)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_bulk_read) +{ + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_de_config old_de_cfg =3D {}; + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read read =3D {}; + int num_samples =3D ksft_session.num_des; + int i, ret; + __u32 de_id; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + de_id =3D ksft_session.de_list[0].id; + + if (scmi_tlm_enable_for_read(self->fd, de_id, &old_cfg, &old_de_cfg)) + SKIP(return, "failed to enable telemetry for read"); + + samples =3D malloc(sizeof(*samples) * num_samples); + ASSERT_NE(NULL, samples); + bzero(samples, sizeof(*samples) * num_samples); + + read.num_samples =3D num_samples; + read.samples =3D (__u64)samples; + + ret =3D ioctl(self->fd, SCMI_TLM_BULK_READ, &read); + scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg); + + ASSERT_EQ(0, ret); + ASSERT_LE(read.num_samples, num_samples); + ASSERT_EQ(0, read.pad[0]); + ASSERT_EQ(0, read.pad[1]); + ASSERT_EQ(0, read.pad[2]); + ASSERT_EQ(0, read.pad2); + ASSERT_EQ(0, read.flags & ~SCMI_TLM_READ_FLAGS); + + for (i =3D 0; i < read.num_samples; i++) { + ASSERT_EQ(0, samples[i].pad); + + TH_LOG("SCMI_TLM_BULK_READ - [%d] id:0x%x tstamp:%llu val:%llu", i, + samples[i].id, samples[i].tstamp, samples[i].val); + } + + free(samples); +} + +TEST_F(stlm, tlm_bulk_read_invalid_request) +{ + struct scmi_tlm_de_sample sample =3D {}; + struct scmi_tlm_data_read read =3D {}; + + read.num_samples =3D 1; + read.samples =3D (__u64)&sample; + read.pad[0] =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, &read)); + EXPECT_EQ(EINVAL, errno); + + bzero(&read, sizeof(read)); + read.num_samples =3D 1; + read.samples =3D (__u64)&sample; + read.pad2 =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, &read)); + EXPECT_EQ(EINVAL, errno); + + bzero(&read, sizeof(read)); + read.num_samples =3D 1; + read.samples =3D (__u64)&sample; + read.flags =3D ~SCMI_TLM_READ_FLAGS; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, &read)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_bulk_read_oversized) +{ + struct scmi_tlm_de_sample sample =3D {}; + struct scmi_tlm_data_read read =3D {}; + + read.num_samples =3D ksft_session.info.num_des + 1; + read.samples =3D (__u64)&sample; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, &read)); + EXPECT_EQ(E2BIG, errno); +} + +TEST_F(stlm, tlm_bulk_read_null_ptr_rejected) +{ + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BULK_READ, NULL)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_batch_read) +{ + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_de_config old_de_cfg =3D {}; + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_sample *samples; + int ret; + __u32 de_id; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + de_id =3D ksft_session.de_list[0].id; + + if (scmi_tlm_enable_for_read(self->fd, de_id, &old_cfg, &old_de_cfg)) + SKIP(return, "failed to enable telemetry for read"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*samples), + false)); + + samples =3D (struct scmi_tlm_de_sample *)batch.items; + samples[0].id =3D de_id; + + ret =3D ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch); + + scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg); + ASSERT_EQ(0, ret); + + ASSERT_EQ(1, batch.num_items); + ASSERT_EQ(ksft_session.de_list[0].id, samples[0].id); + ASSERT_EQ(0, samples[0].pad); + + TH_LOG("SCMI_TLM_BATCH_READ - id:0x%x tstamp:%llu val:%llu", samples[0].i= d, + samples[0].tstamp, samples[0].val); + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_batch_read_invalid_request) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_sample sample =3D {}; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(sample) + 1; + batch.items =3D (__u64)&sample; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D 1; + batch.item_sz =3D sizeof(sample); + batch.reserved =3D 1; + batch.items =3D (__u64)&sample; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D ksft_session.info.num_des + 1; + batch.item_sz =3D sizeof(sample); + batch.items =3D (__u64)&sample; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_batch_read_bad_items_ptr) +{ + struct scmi_tlm_batch batch =3D {}; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(struct scmi_tlm_de_sample); + batch.items =3D 0; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_batch_read_bad_id) +{ + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_de_config old_de_cfg =3D {}; + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_sample *samples; + int ret, saved_errno; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + if (scmi_tlm_enable_for_read(self->fd, ksft_session.de_list[0].id, + &old_cfg, &old_de_cfg)) + SKIP(return, "failed to enable telemetry for read"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*samples), false)); + + samples =3D (struct scmi_tlm_de_sample *)batch.items; + samples[0].id =3D 0xffffffff; + + ret =3D ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch); + saved_errno =3D errno; + + scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg); + + ASSERT_EQ(-1, ret); + EXPECT_EQ(ENOENT, saved_errno); + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_batch_read_bad_id_with_states) +{ + struct scmi_tlm_config old_cfg =3D {}; + struct scmi_tlm_de_config old_de_cfg =3D {}; + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_sample *samples; + int *states; + int ret; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + if (scmi_tlm_enable_for_read(self->fd, ksft_session.de_list[0].id, + &old_cfg, &old_de_cfg)) + SKIP(return, "failed to enable telemetry for read"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*samples), true)); + + samples =3D (struct scmi_tlm_de_sample *)batch.items; + states =3D (int *)batch.states; + + samples[0].id =3D 0xffffffff; + + ret =3D ioctl(self->fd, SCMI_TLM_BATCH_READ, &batch); + + scmi_tlm_restore_after_read(self->fd, &old_cfg, &old_de_cfg); + + ASSERT_EQ(0, ret); + ASSERT_EQ(-ENOENT, states[0]); + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_get_grp_list_zero_size_query) +{ + struct scmi_tlm_batch batch =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + batch.num_items =3D 0; + batch.item_sz =3D sizeof(struct scmi_tlm_grp_info); + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch)); + ASSERT_EQ(ksft_session.info.num_groups, batch.num_items); + ASSERT_EQ(sizeof(struct scmi_tlm_grp_info), batch.item_sz); + ASSERT_EQ(0, batch.reserved); +} + +TEST_F(stlm, tlm_get_grp_list) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_grp_info *groups; + int i; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + if (!ksft_session.info.num_groups) + SKIP(return, "no groups available"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, ksft_session.info.num_groups, + sizeof(*groups), false)); + + groups =3D (struct scmi_tlm_grp_info *)batch.items; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch)); + ASSERT_LE(batch.num_items, ksft_session.info.num_groups); + + for (i =3D 0; i < batch.num_items; i++) { + ASSERT_EQ(i, groups[i].grp_id); + ASSERT_EQ(0, groups[i].pad); + ASSERT_EQ(0, groups[i].reserved); + + TH_LOG("SCMI_TLM_GRP_LIST - [%d] grp:%u num_des:%u num_intervals:%u", + i, groups[i].grp_id, groups[i].num_des, groups[i].num_intervals); + } + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_get_grp_list_invalid_request) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_grp_info group =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(group) + 1; + batch.items =3D (__u64)&group; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D 1; + batch.item_sz =3D sizeof(group); + batch.reserved =3D 1; + batch.items =3D (__u64)&group; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D ksft_session.info.num_groups + 1; + batch.item_sz =3D sizeof(group); + batch.items =3D (__u64)&group; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_grp_info) +{ + struct scmi_tlm_grp_info group =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + if (!ksft_session.info.num_groups) + SKIP(return, "no groups available"); + + group.grp_id =3D 0; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group)); + ASSERT_EQ(0, group.grp_id); + ASSERT_EQ(0, group.pad); + ASSERT_EQ(0, group.reserved); + + TH_LOG("SCMI_TLM_GET_GRP_INFO - grp:%u num_des:%u num_intervals:%u", + group.grp_id, group.num_des, group.num_intervals); +} + +TEST_F(stlm, tlm_get_grp_info_invalid_request) +{ + struct scmi_tlm_grp_info group =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + group.grp_id =3D 0; + group.pad =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group)); + EXPECT_EQ(EINVAL, errno); + + bzero(&group, sizeof(group)); + group.grp_id =3D 0; + group.reserved =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_grp_info_bad_group) +{ + struct scmi_tlm_grp_info group =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + group.grp_id =3D ksft_session.info.num_groups; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_grp_desc) +{ + struct scmi_tlm_grp_info group =3D {}; + struct scmi_tlm_grp_desc desc =3D {}; + int i; + __u32 *de_ids; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + if (!ksft_session.info.num_groups) + SKIP(return, "no groups available"); + + group.grp_id =3D 0; + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group)); + + if (!group.num_des) + SKIP(return, "group had no DEs"); + + de_ids =3D malloc(sizeof(*de_ids) * group.num_des); + ASSERT_NE(NULL, de_ids); + bzero(de_ids, sizeof(*de_ids) * group.num_des); + + desc.grp_id =3D group.grp_id; + desc.num_des =3D group.num_des; + desc.composing_des =3D (__u64)de_ids; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc)); + ASSERT_EQ(group.grp_id, desc.grp_id); + ASSERT_LE(desc.num_des, group.num_des); + ASSERT_EQ(0, desc.reserved); + + for (i =3D 0; i < desc.num_des; i++) + TH_LOG("SCMI_TLM_GET_GRP_DESC - grp:%u de[%d]:0x%x", desc.grp_id, + i, de_ids[i]); + + free(de_ids); +} + +TEST_F(stlm, tlm_get_grp_desc_invalid_request) +{ + struct scmi_tlm_grp_desc desc =3D {}; + __u32 de_id =3D 0; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + if (!ksft_session.info.num_groups) + SKIP(return, "no groups available"); + + desc.grp_id =3D 0; + desc.num_des =3D 1; + desc.composing_des =3D (__u64)&de_id; + desc.reserved =3D 1; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_grp_desc_bad_group) +{ + struct scmi_tlm_grp_desc desc =3D {}; + __u32 de_id =3D 0; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + desc.grp_id =3D ksft_session.info.num_groups; + desc.num_des =3D 1; + desc.composing_des =3D (__u64)&de_id; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_grp_desc_oversized) +{ + struct scmi_tlm_grp_info group =3D {}; + struct scmi_tlm_grp_desc desc =3D {}; + __u32 de_id =3D 0; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + if (!ksft_session.info.num_groups) + SKIP(return, "no groups available"); + + group.grp_id =3D 0; + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group)); + desc.grp_id =3D 0; + desc.num_des =3D group.num_des + 1; + desc.composing_des =3D (__u64)&de_id; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc)); + EXPECT_EQ(ENOSPC, errno); +} + +TEST_F(stlm, tlm_get_grp_desc_bad_pointer) +{ + struct scmi_tlm_grp_info group =3D {}; + struct scmi_tlm_grp_desc desc =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)) + SKIP(return, "GROUPS not supported"); + + if (!ksft_session.info.num_groups) + SKIP(return, "no groups available"); + + group.grp_id =3D 0; + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_GRP_INFO, &group)); + + if (!group.num_des) + SKIP(return, "group has no DEs"); + + desc.grp_id =3D 0; + desc.num_des =3D group.num_des; + desc.composing_des =3D 0; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_GRP_DESC, &desc)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_get_all_cfg) +{ + struct scmi_tlm_de_config cfg =3D {}; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, &cfg)); + ASSERT_EQ(0, cfg.reserved); + ASSERT_LE(cfg.enable, 1); + ASSERT_LE(cfg.t_enable, 1); + + TH_LOG("SCMI_TLM_GET_ALL_CFG - ena:%u t_ena:%u", cfg.enable, cfg.t_enable= ); +} + +TEST_F(stlm, tlm_get_all_cfg_invalid_request) +{ + struct scmi_tlm_de_config cfg =3D {}; + + cfg.reserved =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, &cfg)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_all_cfg_null_ptr_rejected) +{ + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, NULL)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_set_de_cfg) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config old_cfg =3D {}; + struct scmi_tlm_de_config new_cfg =3D {}; + struct scmi_tlm_de_config check_cfg =3D {}; + struct scmi_tlm_de_config *cfg; + int set_ret; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), false)); + + cfg =3D (struct scmi_tlm_de_config *)batch.items; + cfg[0].id =3D ksft_session.de_list[0].id; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch)); + old_cfg =3D cfg[0]; + + new_cfg =3D old_cfg; + new_cfg.enable =3D !old_cfg.enable; + + cfg[0] =3D new_cfg; + set_ret =3D ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch); + ASSERT_EQ(0, set_ret); + + cfg[0].id =3D old_cfg.id; + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &batch)); + check_cfg =3D cfg[0]; + + cfg[0] =3D old_cfg; + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch)); + + ASSERT_EQ(new_cfg.id, check_cfg.id); + ASSERT_EQ(!!new_cfg.enable, !!check_cfg.enable); + ASSERT_EQ(!!new_cfg.t_enable, !!check_cfg.t_enable); + ASSERT_EQ(0, check_cfg.reserved); + + TH_LOG("SCMI_TLM_SET_DE_CFG =3D id:0x%x ena:%u t_ena:%u", check_cfg.id, + check_cfg.enable, check_cfg.t_enable); + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_set_de_cfg_invalid_request) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config cfg =3D {}; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(cfg) + 1; + batch.items =3D (__u64)&cfg; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D ksft_session.info.num_des + 1; + batch.item_sz =3D sizeof(cfg); + batch.items =3D (__u64)&cfg; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D 1; + batch.item_sz =3D sizeof(cfg); + batch.items =3D 0; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_set_de_cfg_read_only_rejected) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config cfg =3D {}; + int fd; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + fd =3D open("/dev/scmi/" TLM_0, O_RDONLY); + ASSERT_GE(fd, 0); + + cfg.id =3D ksft_session.de_list[0].id; + batch.num_items =3D 1; + batch.item_sz =3D sizeof(cfg); + batch.items =3D (__u64)&cfg; + + ASSERT_EQ(-1, ioctl(fd, SCMI_TLM_SET_DE_CFG, &batch)); + EXPECT_EQ(EPERM, errno); + + close(fd); +} + +TEST_F(stlm, tlm_set_de_cfg_bad_id) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config *cfg; + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), false)); + + cfg =3D (struct scmi_tlm_de_config *)batch.items; + cfg[0].id =3D 0xffffffff; + cfg[0].enable =3D 1; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch)); + EXPECT_EQ(ENODEV, errno); + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_set_de_cfg_bad_id_with_states) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_de_config *cfg; + int *states; + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, 1, sizeof(*cfg), true)); + + cfg =3D (struct scmi_tlm_de_config *)batch.items; + states =3D (int *)batch.states; + + cfg[0].id =3D 0xffffffff; + cfg[0].enable =3D 1; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &batch)); + ASSERT_LT(states[0], 0); + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_set_all_cfg) +{ + struct scmi_tlm_batch saved_batch =3D {}; + struct scmi_tlm_de_config *saved; + struct scmi_tlm_de_config old_all =3D {}; + struct scmi_tlm_de_config new_all =3D {}; + struct scmi_tlm_de_config check_all =3D {}; + int i, ret; + + if (!ksft_session.de_list || !ksft_session.num_des) + SKIP(return, "no DE list available"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&saved_batch, ksft_session.num_des, + sizeof(*saved), false)); + + saved =3D (struct scmi_tlm_de_config *)saved_batch.items; + + for (i =3D 0; i < ksft_session.num_des; i++) + saved[i].id =3D ksft_session.de_list[i].id; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_DE_CFG, &saved_batch)); + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, &old_all)); + + new_all.enable =3D !old_all.enable; + new_all.t_enable =3D 0; + + ret =3D ioctl(self->fd, SCMI_TLM_SET_ALL_CFG, &new_all); + if (ret) { + ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &saved_batch); + scmi_tlm_batch_free(&saved_batch); + ASSERT_EQ(0, ret); + } + + ret =3D ioctl(self->fd, SCMI_TLM_GET_ALL_CFG, &check_all); + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_SET_DE_CFG, &saved_batch)); + + ASSERT_EQ(0, ret); + ASSERT_EQ(!!new_all.enable, !!check_all.enable); + ASSERT_EQ(0, check_all.reserved); + + TH_LOG("SCMI_TLM_SET_ALL_CFG - ena:%u t_ena:%u", check_all.enable, + check_all.t_enable); + + scmi_tlm_batch_free(&saved_batch); +} + +TEST_F(stlm, tlm_set_all_cfg_invalid_request) +{ + struct scmi_tlm_de_config cfg =3D {}; + + cfg.reserved =3D 1; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_ALL_CFG, &cfg)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_set_all_cfg_null_ptr_rejected) +{ + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_SET_ALL_CFG, NULL)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_set_all_cfg_read_only_rejected) +{ + struct scmi_tlm_de_config cfg =3D {}; + int fd; + + fd =3D open("/dev/scmi/" TLM_0, O_RDONLY); + ASSERT_GE(fd, 0); + ASSERT_EQ(-1, ioctl(fd, SCMI_TLM_SET_ALL_CFG, &cfg)); + EXPECT_EQ(EPERM, errno); + + close(fd); +} + +TEST_F(stlm, tlm_get_shmti_list_zero_size_query) +{ + struct scmi_tlm_batch batch =3D {}; + + batch.num_items =3D 0; + batch.item_sz =3D sizeof(struct scmi_tlm_shmti_info); + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch)); + ASSERT_EQ(ksft_session.info.num_shmtis, batch.num_items); + ASSERT_EQ(sizeof(struct scmi_tlm_shmti_info), batch.item_sz); + ASSERT_EQ(0, batch.reserved); + ASSERT_EQ(0, batch.items); +} + +TEST_F(stlm, tlm_get_shmti_list) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_shmti_info *shmtis; + int i; + + if (!ksft_session.info.num_shmtis) + SKIP(return, "no SHMTIs available"); + + ASSERT_EQ(0, scmi_tlm_batch_init(&batch, ksft_session.info.num_shmtis, + sizeof(*shmtis), false)); + + shmtis =3D (struct scmi_tlm_shmti_info *)batch.items; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch)); + ASSERT_LE(batch.num_items, ksft_session.info.num_shmtis); + + for (i =3D 0; i < batch.num_items; i++) { + EXPECT_EQ(0, shmtis[i].reserved); + EXPECT_GT(shmtis[i].len, 0); + EXPECT_NE(SCMI_TLM_SHMTI_ID_INVALID, shmtis[i].sid); + + EXPECT_NE(-1, fcntl(shmtis[i].fd, F_GETFD)); + EXPECT_EQ(FD_CLOEXEC, fcntl(shmtis[i].fd, F_GETFD) & FD_CLOEXEC); + + TH_LOG("SCMI_TLM_GET_SHMTI_LIST - [%d] sid:%u fd:%u len:%u offset:%u", + i, shmtis[i].sid, shmtis[i].fd, shmtis[i].len, shmtis[i].offset); + + close(shmtis[i].fd); + } + + scmi_tlm_batch_free(&batch); +} + +TEST_F(stlm, tlm_get_shmti_list_invalid_request) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_shmti_info shmti =3D {}; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(shmti) + 1; + batch.items =3D (__u64)&shmti; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D 1; + batch.item_sz =3D sizeof(shmti); + batch.reserved =3D 1; + batch.items =3D (__u64)&shmti; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_shmti_list_bad_items_ptr) +{ + struct scmi_tlm_batch batch =3D {}; + + if (!ksft_session.info.num_shmtis) + SKIP(return, "no SHMTIs available"); + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(struct scmi_tlm_shmti_info); + batch.items =3D 0; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_SHMTI_LIST, &batch)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_get_uuid_list_zero_size_query) +{ + struct scmi_tlm_batch batch =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST)) + SKIP(return, "UUID list not supported"); + + batch.num_items =3D 0; + batch.item_sz =3D sizeof(struct scmi_tlm_uuid); + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch)); + ASSERT_EQ(sizeof(struct scmi_tlm_uuid), batch.item_sz); + ASSERT_EQ(0, batch.reserved); + ASSERT_EQ(0, batch.items); + + TH_LOG("SCMI_TLM_GET_UUID_LIST - num_uuids:%u", batch.num_items); +} + +TEST_F(stlm, tlm_get_uuid_list) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_uuid *uuids; + int i, j; + bool nonzero; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST)) + SKIP(return, "UUID list not supported"); + + batch.num_items =3D 0; + batch.item_sz =3D sizeof(*uuids); + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch)); + + if (!batch.num_items) + SKIP(return, "no UUIDs available"); + + uuids =3D malloc(sizeof(*uuids) * batch.num_items); + ASSERT_NE(NULL, uuids); + bzero(uuids, sizeof(*uuids) * batch.num_items); + + batch.items =3D (__u64)uuids; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch)); + ASSERT_GT(batch.num_items, 0); + ASSERT_EQ(sizeof(*uuids), batch.item_sz); + ASSERT_EQ(0, batch.reserved); + + for (i =3D 0; i < batch.num_items; i++) { + nonzero =3D false; + + for (j =3D 0; j < SCMI_TLM_DE_IMPL_UUID_SZ; j++) { + if (uuids[i].bytes[j]) { + nonzero =3D true; + break; + } + } + + EXPECT_TRUE(nonzero); + + TH_LOG("SCMI_TLM_GET_UUID_LIST - [%d] %02x%02x%02x%02x-%02x%02x-%02x%02x= -%02x%02x-%02x%02x%02x%02x%02x%02x", + i, uuids[i].bytes[0], uuids[i].bytes[1], + uuids[i].bytes[2], uuids[i].bytes[3], + uuids[i].bytes[4], uuids[i].bytes[5], + uuids[i].bytes[6], uuids[i].bytes[7], + uuids[i].bytes[8], uuids[i].bytes[9], + uuids[i].bytes[10], uuids[i].bytes[11], + uuids[i].bytes[12], uuids[i].bytes[13], + uuids[i].bytes[14], uuids[i].bytes[15]); + } + + free(uuids); +} + +TEST_F(stlm, tlm_get_uuid_list_invalid_request) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_uuid uuid =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST)) + SKIP(return, "UUID list not support"); + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(uuid) + 1; + batch.items =3D (__u64)&uuid; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); + + bzero(&batch, sizeof(batch)); + batch.num_items =3D 1; + batch.item_sz =3D sizeof(uuid); + batch.reserved =3D 1; + batch.items =3D (__u64)&uuid; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch)); + EXPECT_EQ(EINVAL, errno); +} + +TEST_F(stlm, tlm_get_uuid_list_bad_items_ptr) +{ + struct scmi_tlm_batch batch =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST)) + SKIP(return, "UUID list not supported"); + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(struct scmi_tlm_uuid); + batch.items =3D 0; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_GET_UUID_LIST, &batch)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_F(stlm, tlm_reset) +{ + if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_RESET) || + !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_RESET)) + SKIP(return, "reset not supported"); + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_RESET)); + + TH_LOG("SCMI_TLM_RESET"); +} + +TEST_F(stlm, tlm_reset_read_only_rejected) +{ + int fd; + + if (!(ksft_session.info.features & SCMI_TLM_SCMI_SUPPORT_RESET) || + !(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_RESET)) + SKIP(return, "reset not supported"); + + fd =3D open("/dev/scmi/" TLM_0, O_RDONLY); + ASSERT_GE(fd, 0); + ASSERT_EQ(-1, ioctl(fd, SCMI_TLM_RESET)); + EXPECT_EQ(EPERM, errno); + + close(fd); +} + +TEST_F(stlm, tlm_event_subscribe) +{ + struct scmi_tlm_event evt =3D {}; + int efd; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT)) + SKIP(return, "events not supported"); + + efd =3D eventfd(0, EFD_CLOEXEC); + ASSERT_GE(efd, 0); + + evt.type =3D SCMI_TLM_EVT_GENERATION; + evt.efd =3D efd; + evt.cookie =3D 0; + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt)); + ASSERT_GT(evt.cookie, 0); + ASSERT_EQ(0, evt.pad); + ASSERT_EQ(0, evt.reserved); + + TH_LOG("SCMI_TLM_EVENT_SUBSCRIBE - type:%u cookie:%u", evt.type, + evt.cookie); + + ASSERT_EQ(0, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt)); + + close(efd); +} + +TEST_F(stlm, tlm_event_subscribe_invalid_request) +{ + struct scmi_tlm_event evt =3D {}; + int efd; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT)) + SKIP(return, "events not supported"); + + efd =3D eventfd(0, EFD_CLOEXEC); + ASSERT_GE(efd, 0); + + evt.type =3D SCMI_TLM_EVT_GENERATION; + evt.efd =3D efd; + evt.pad =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt)); + EXPECT_EQ(EINVAL, errno); + + bzero(&evt, sizeof(evt)); + evt.type =3D SCMI_TLM_EVT_GENERATION; + evt.efd =3D efd; + evt.reserved =3D 1; + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt)); + EXPECT_EQ(EINVAL, errno); + + close(efd); +} + +TEST_F(stlm, tlm_event_subscribe_bad_type) +{ + struct scmi_tlm_event evt =3D {}; + int efd; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT)) + SKIP(return, "events not supported"); + + efd =3D eventfd(0, EFD_CLOEXEC); + ASSERT_GE(efd, 0); + + evt.type =3D SCMI_TLM_EVT_LAST + 1; + evt.efd =3D efd; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt)); + EXPECT_EQ(ENODEV, errno); + + close(efd); +} + +TEST_F(stlm, tlm_event_subscribe_bad_eventfd) +{ + struct scmi_tlm_event evt =3D {}; + + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT)) + SKIP(return, "events not supported"); + + evt.type =3D SCMI_TLM_EVT_GENERATION; + evt.efd =3D -1; + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt)); + EXPECT_EQ(EBADF, errno); +} + +TEST_F(stlm, tlm_event_subscribe_null_ptr_rejected) +{ + if (!(ksft_session.info.abi_features & SCMI_TLM_ABI_FEAT_EVENT)) + SKIP(return, "events not supported"); + + ASSERT_EQ(-1, ioctl(self->fd, SCMI_TLM_EVENT_SUBSCRIBE, NULL)); + EXPECT_EQ(EFAULT, errno); +} + +TEST_HARNESS_MAIN /* generates main() */ --=20 2.54.0 From nobody Tue Nov 24 12:36:09 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795304706; b=Seck6V78xjS6zGq0a217PfP+wk2L5T98hsUXNc7s3X4S+iHYFpY2tqXDCBjN9A08bD+F +/2Lakqp7+44qzkaded4on4BqCBzOmvwf3T05b90tAnIhVyYg2Ba54KIycfZVNLS/K8hK N4oqXey62uIxnj5rBOmsYxWxvfVM9yMi3woj3xETLhJt6GpIxLepzmr6RpVXv+ZMcAG5V yxmJSQ+cZyQx/d8vQsBFSKO8n0YWgABTddKSwcPgkEVA7z7gKIr0CS9bz9QerKKMjg5Ks pMUQJ2bY08OvhqQspJuvM1Jb407OmZIgO5tzm2EBOTTiaUlY6ck39hO4GQmkoiI27pA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795304706; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=7wMp/U+IABgNP5tNO2jAgdOQP3Id1y/R840XtwUDTF0=; b=iAoumIjXC+fzUONrWEnfO6OLaahfryyFD4nTbD/6QmMIpXhZ5yLsJg716wvQ6G3NJx18 SoUyNRWer08/+oDkAB2ndjp3KY1KrfMCtFJwYy0u+1HXkIfqBIE7vLAY4pg55u/W+G26Q RdlLYzmQOLZzSuRu0L3Mvxp0/ibZEVpQjE7NQCQBcCFJCcKwJgt1zEbbCBLUMpg8Em3a6 ZNjs2auPRBMo+G/N+zoatlsCrdM+O7YN+dxxDwCypB8Swvo9t72Af2vL1//bM7Aq8Wqd1 nF2DNZFSFPk0O7I09M06KBi7VBHeToJkLOweiS8hUPD0TX69KiE3ygfhSeB7JbbsFmA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Xv7JZcgm; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E8E6C606A697; Sat, 21 Nov 2026 23:45:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E8E6C606A697 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Xv7JZcgm 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 5E5C316F2; Sat, 21 Nov 2026 15:45:00 -0800 (PST) Received: from pluto (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7323B3F632; Sat, 21 Nov 2026 15:45:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1795304703; bh=beHXZnMYRCZZFeHfoMa26h1eZzfmVjjhHcDHqnrm9DE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xv7JZcgmSstOmp0xZoSNmJcfxZGU/sGgAFuXrVfj6liKQH/MTJHKQh1zcogW3J6N9 iuMVLt7TCWCn8LnMHoC4I2boMYcxMQnd30vxMThYAKsTx8DDz8x2UelyNhk4REnGNO pZvYFUimBylqOEq3vNe9vsq9VTqq3AbpYfYRn3As= From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, arm-scmi@vger.kernel.org, linux-doc@vger.kernel.org Cc: sudeep.holla@kernel.org, james.quinlan@broadcom.com, f.fainelli@gmail.com, vincent.guittot@linaro.org, etienne.carriere@st.com, peng.fan@oss.nxp.com, michal.simek@amd.com, d-gole@ti.com, jonathan.cameron@oss.qualcomm.com, elif.topuz@arm.com, lukasz.luba@arm.com, philip.radford@arm.com, david@kernel.org, souvik.chakravarty@arm.com, leitao@kernel.org, kas@kernel.org, puranjay@kernel.org, usama.arif@linux.dev, kernel-team@meta.com, Cristian Marussi Subject: [PATCH v16 31/31] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool Date: Sat, 21 Nov 2026 23:40:05 +0000 Message-ID: <20261121234005.1525419-32-cristian.marussi@arm.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261121234005.1525419-1-cristian.marussi@arm.com> References: <20261121234005.1525419-1-cristian.marussi@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a testing tool that exercises the SCMI ioctls UAPI interface: as of now the tool simply queries the initial state of the SCMI Telemetry subsystem, tries to enable all the existent Data Events and dumps all the Telemetry data. Signed-off-by: Cristian Marussi --- v15v --> v16 - handle new abi_features GROUPS/SINGLE_SAMPLE Signed-off-by: Cristian Marussi --- MAINTAINERS | 1 + tools/testing/scmi/Makefile | 25 + tools/testing/scmi/stlm.c | 1698 ++++++++++++++++++++++++++++++ tools/testing/scmi/stlm_decode.c | 252 +++++ tools/testing/scmi/stlm_decode.h | 7 + 5 files changed, 1983 insertions(+) create mode 100644 tools/testing/scmi/Makefile create mode 100644 tools/testing/scmi/stlm.c create mode 100644 tools/testing/scmi/stlm_decode.c create mode 100644 tools/testing/scmi/stlm_decode.h diff --git a/MAINTAINERS b/MAINTAINERS index 8c203b2be066..bbd68e61b3cf 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -27347,6 +27347,7 @@ F: include/linux/sc[mp]i_protocol.h F: include/trace/events/scmi.h F: include/uapi/linux/scmi.h F: include/uapi/linux/virtio_scmi.h +F: tools/testing/scmi/ =20 SYSTEM CONTROL MANAGEMENT INTERFACE (SCMI) i.MX Extension Message Protocol= drivers M: Peng Fan diff --git a/tools/testing/scmi/Makefile b/tools/testing/scmi/Makefile new file mode 100644 index 000000000000..790ff8086489 --- /dev/null +++ b/tools/testing/scmi/Makefile @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +CC=3D$(CROSS_COMPILE)gcc +OBJS =3D stlm.o stlm_decode.o + +CFLAGS=3D-Wall -static -std=3Dgnu11 -I ../../../include/uapi/ +ifneq ($(DEBUG), ) + CFLAGS+=3D-O0 -g -ggdb +else + CFLAGS+=3D-static +endif + +all: stlm + +stlm: $(OBJS) + $(CC) $(CFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +clean: + rm -f *.o + rm -f stlm + +.PHONY: clean diff --git a/tools/testing/scmi/stlm.c b/tools/testing/scmi/stlm.c new file mode 100644 index 000000000000..76f41cb170ec --- /dev/null +++ b/tools/testing/scmi/stlm.c @@ -0,0 +1,1698 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * SCMI Telemetry Test Tool + * + * Copyright (C) 2026 ARM Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include "stlm_decode.h" + +#define IOCTL_ERR_STR(_ioctl) "IOCTL:" #_ioctl +#define MIN_TDCF_SZ_V1 24 + +#define USAGE_STRING \ + "\t[OPTIONS]\n" \ + "\t -i \t\tUse this device instance path. Default: /dev/sc= mi/tlm_0\n" \ + "\t[COMMANDS]\n" \ + "\t info [full] - Display summarized (or full) configuration and status d= ata\n" \ + "\t query - Query ALL possible IOCTLs for resources (item=3D0)\n" \ + "\t uuids - UUIDs dump\n" \ + "\t monitor [detach] - Monitor Generation counter\n" \ + "\t shmti [check|dump []|decode []] - SHMTI operations\n" \ + "\t\t check - Sanity check of all SHMTis\n" \ + "\t\t dump [] - Binary dump of or all SHMTI areas\n" \ + "\t\t decode [] - Line-by-line decode of or all SHMTI areas\n"= \ + "\t des [info|data] [] - Display detailed DEs info, config or data= \n" \ + "\t groups - Display detailed GROUPS info and config data\n" \ + "\t cfg [SUB-COMMANDS] - Configure Telemetry\n" \ + "\t\t tlm on|off - Enable or disable Telemetry as a whole\n" \ + "\t\t update - Set global update interval\n" \ + "\t\t all_des [[on|off]:[on|off]] - Enable or disable ALL DEs with or wou= t TS\n" \ + "\t\t de [ [[on|off]:[on|off]]] - Get/Set configuration of one or = ALL de\n" \ + "\t\t group [] [[on|off]:[on|off]:[]] - Get/Set GRO= UP configuration\n" \ + "\t single - Perform a single sample read\n" \ + "\t bulk - Perform a bulk read\n" \ + "\t batch [de_id [de_id]... ] - Perform a batch read of the DE ID= s listed in input\n" \ + "\t reset - Perform a Telemetry subsytem reset platform-side\n" + +struct tlm_de { + struct scmi_tlm_de_info *info; + struct scmi_tlm_de_config cfg; + struct scmi_tlm_de_sample sample; +}; + +struct tlm_group { + int fd; + struct scmi_tlm_grp_info *info; + struct scmi_tlm_grp_desc *desc; + struct scmi_tlm_intervals *ivs; +}; + +struct tlm_state { + int fd; + const char *path; + struct scmi_tlm_abi_info info; + struct scmi_tlm_config cfg; + struct scmi_tlm_intervals *ivs; + struct scmi_tlm_batch *shmtis; + unsigned int num_des; + struct tlm_de *des; + unsigned int num_groups; + struct tlm_group *grps; +}; + +struct parsed_args { + const char *instance; + const char *cmd; + unsigned int cnt; + char **opts; +}; + +static const char *argv0; + +static void gather_tlm_res_state(struct tlm_state *st); + +static inline void dump_des_info(struct tlm_state *st, uint32_t *de_id) +{ + gather_tlm_res_state(st); + + fprintf(stdout, "\n+ %s #%d DEs:\n", + !de_id ? "Found" : "Lookup", + !de_id ? st->num_des : 1); + + for (int i =3D 0; i < st->num_des; i++) { + if (de_id && st->des[i].info->id !=3D *de_id) + continue; + + fprintf(stdout, "\t[0x%08X][%s]\n", + st->des[i].info->id, st->des[i].info->name); + fprintf(stdout, "\t\tGRP_ID:0x%08X SZ:%u TYPE:%u UNIT:%u UNIT_EXP:%d= TS_RATE:%d\n", + st->des[i].info->grp_id, st->des[i].info->data_sz, + st->des[i].info->type, st->des[i].info->unit, + st->des[i].info->unit_exp, st->des[i].info->ts_rate); + fprintf(stdout, "\t\tINST_ID:%u COMPO_ID:%u COMPO_TYPE:%u PERSIST:%u\= n\n", + st->des[i].info->instance_id, st->des[i].info->compo_instance_id, + st->des[i].info->compo_type, st->des[i].info->persistent); + } + + fprintf(stdout, "\n"); +} + +static void dump_single_de_data(struct tlm_state *st, uint32_t *de_id) +{ + struct tlm_de de =3D {}; + + de.sample.id =3D *de_id; + if (ioctl(st->fd, SCMI_TLM_DE_READ, &de.sample)) { + perror(IOCTL_ERR_STR(SCMI_TLM_DE_READ)); + return; + } + + fprintf(stdout, "\t[0x%08X]\t\t -->> TS:%16llu %016llX\n", + de.sample.id, de.sample.tstamp, de.sample.val); +} + +static void dump_des_data(struct tlm_state *st, uint32_t *de_id) +{ + if (de_id) + return dump_single_de_data(st, de_id); + + gather_tlm_res_state(st); + + for (int i =3D 0; i < st->num_des; i++) { + struct tlm_de *de =3D &st->des[i]; + int ret; + + de->sample.id =3D de->info->id; + ret =3D ioctl(st->fd, SCMI_TLM_DE_READ, &de->sample); + if (ret) { + if (errno !=3D ENODATA) { + perror(IOCTL_ERR_STR(SCMI_TLM_DE_READ)); + continue; + } + } + + fprintf(stdout, "\t[0x%08X][%16s] --- %s/%s\n\t\t-->> TS:%16llu %016llX\= n", + de->info->id, de->info->name, + de->cfg.enable ? "ON" : "OFF", + de->cfg.t_enable ? "TS_ON " : "TS_OFF", + de->sample.tstamp, de->sample.val); + } + + fprintf(stdout, "\n"); +} + +static void dump_des(struct tlm_state *st, const char *action, uint32_t *d= e_id) +{ + if (!action) { + dump_des_info(st, de_id); + dump_des_data(st, de_id); + } else if (!strncmp(action, "info", 4)) { + dump_des_info(st, de_id); + } else if (!strncmp(action, "data", 4)) { + dump_des_data(st, de_id); + } +} + +static inline void usage(const char *program_name) +{ + printf("Usage: %s [OPTIONS] [CMD [CMD_OPTS]...]\n", program_name); + printf("%s", USAGE_STRING); +} + +static int cmd_unknown(const char *cmd) +{ + fprintf(stderr, "Unknown command '%s'\n", cmd); + usage(argv0); + + return EXIT_FAILURE; +} + +static inline int cmd_dump_des(struct tlm_state *st, struct parsed_args *a= rgs) +{ + uint32_t id, *de =3D NULL; + const char *action =3D NULL; + + if (args && args->cnt) { + const char *idstr =3D NULL; + + if (!strncmp(args->opts[0], "info", 4) || + !strncmp(args->opts[0], "data", 4)) + action =3D args->opts[0]; + + idstr =3D !action ? args->opts[0] : ((args->cnt > 1) ? args->opts[1] : N= ULL); + if (idstr) { + id =3D strtoul(idstr, NULL, 0); + if (id =3D=3D ULONG_MAX || errno =3D=3D EINVAL) { + fprintf(stderr, "\n+ BAD DE_ID: %s\n", idstr); + return 0; + } + de =3D &id; + } + } + + dump_des(st, action, de); + + return 0; +} + +static inline int cmd_dump_groups(struct tlm_state *st) +{ + gather_tlm_res_state(st); + + fprintf(stdout, "\n+ Found %d GRPs: ", st->num_groups); + for (int i =3D 0; i < st->num_groups; i++) { + struct scmi_tlm_update_interval *grp_intrvs; + struct tlm_group *grp =3D &st->grps[i]; + uint32_t *composing_des; + + fprintf(stdout, "\n\tGRP_ID:%d DES#:%d INTRVS#:%d %s\n", + grp->info->grp_id, grp->info->num_des, + grp->info->num_intervals, + !grp->ivs ? "(---)" : + ((grp->ivs->flags & SCMI_TLM_INTERV_DISCRETE) ? "(DISCRETE)" : "")); + + fprintf(stdout, "\tGRP Intervals:\n"); + if (grp->ivs) { + grp_intrvs =3D (struct scmi_tlm_update_interval *)grp->ivs->intervals; + for (int j =3D 0; j < grp->ivs->num_intervals; j++) + fprintf(stdout, "\t[%d]::%u,%d\n", j, + grp_intrvs[j].secs, grp_intrvs[j].exp); + } + + composing_des =3D (uint32_t *)grp->desc->composing_des; + fprintf(stdout, "\tCOMPOSING_DES:"); + for (int j =3D 0; j < grp->desc->num_des; j++) + fprintf(stdout, "0x%08X ", composing_des[j]); + } + fprintf(stdout, "\n"); + + return 0; +} + +static void print_uuid(const char *prefix, void *arg) +{ + unsigned char *uuid =3D arg; + + fprintf(stdout, "%s", prefix); + for (int i =3D 0; i < SCMI_TLM_DE_IMPL_UUID_SZ; i++) { + fprintf(stdout, "%02X", uuid[i]); + switch (i) { + case 3: + case 5: + case 7: + case 9: + fprintf(stdout, "%c", '-'); + break; + case 15: + fprintf(stdout, "%c", '\n'); + break; + } + } +} + +static inline int cmd_dump_state(struct tlm_state *st, struct parsed_args = *args) +{ + struct scmi_tlm_update_interval *intervals; + struct scmi_tlm_shmti_info *shinfo; + + fprintf(stdout, "- SYSTEM TELEMETRY @instance: %s\n\n", st->path); + fprintf(stdout, "+ ABI Version: %u\n", st->info.abi_version); + fprintf(stdout, "+ ABI Features: 0x%04X\n", st->info.abi_features); + fprintf(stdout, "\t (RESET:%u", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_RESET)); + fprintf(stdout, " EVENT:%u", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_EVENT)); + fprintf(stdout, " UUID_LIST:%u", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_UUID_LIST)); + fprintf(stdout, " BATCH_STATE:%u", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_BATCH_STATE)); + fprintf(stdout, " BATCHED_CFG:%u", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_BATCHED_CFG)); + fprintf(stdout, " DE_TRACKING:%u", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_DE_TRACKING)); + fprintf(stdout, " GROUPS:%u", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS)); + fprintf(stdout, " SINGLE_SAMPLE:%u)\n", + !!(st->info.abi_features & SCMI_TLM_ABI_FEAT_SINGLE_SAMPLE)); + fprintf(stdout, "+ SCMI Features: 0x%04X\n", st->info.features); + fprintf(stdout, "\t (RESET:%u", + !!(st->info.features & SCMI_TLM_SCMI_SUPPORT_RESET)); + fprintf(stdout, " SINGLE_SAMPLE:%u", + !!(st->info.features & SCMI_TLM_SCMI_SUPPORT_SINGLE_SAMPLE)); + fprintf(stdout, " GROUP_CONFIG:%u", + !!(st->info.features & SCMI_TLM_SCMI_SUPPORT_GROUP_CONFIG)); + fprintf(stdout, " UPDATE_NOTIFICATIONS:%u)\n", + !!(st->info.features & SCMI_TLM_SCMI_SUPPORT_UPDATE_NOTIFICATION)); + fprintf(stdout, "+ DEs#: %d\n", st->info.num_des); + fprintf(stdout, "+ GRPS#: %d\n", st->info.num_groups); + fprintf(stdout, "+ INTRV#: %d %s\n", st->info.num_intervals, + (st->ivs->flags & SCMI_TLM_INTERV_DISCRETE) ? "(DISCRETE)" : ""); + fprintf(stdout, "+ SHMTIS#: %d\n", st->info.num_shmtis); + + print_uuid("+ Primary UUID: ", &st->info.primary_de_impl_version[0]); + fprintf(stdout, "\n+ TLM_ENABLED: %d\n", st->cfg.enable); + fprintf(stdout, "+ CURRENT_UPDATE_INTERVAL: %u,%d\n", + st->cfg.active.secs, st->cfg.active.exp); + + fprintf(stdout, "\n+ Found #%u Global Update Intervals\n", + st->ivs->num_intervals); + intervals =3D (struct scmi_tlm_update_interval *)st->ivs->intervals; + for (int i =3D 0; i < st->ivs->num_intervals; i++) + fprintf(stdout, "\t[%d]::%u,%d\n", i, + intervals[i].secs, intervals[i].exp); + + fprintf(stdout, "\n+ Found #%u SHMTI areas\n", + st->shmtis ? st->shmtis->num_items : 0); + if (st->shmtis) { + shinfo =3D (struct scmi_tlm_shmti_info *)st->shmtis->items; + for (int i =3D 0; i < st->shmtis->num_items; i++) + fprintf(stdout, "\t[%d]:: FD:%u LEN:%u\n", + shinfo[i].sid, shinfo[i].fd, shinfo[i].len); + } + + if (args && args->cnt && args->opts && + !strncmp(args->opts[0], "full", strlen(args->opts[0]))) { + cmd_dump_des(st, NULL); + cmd_dump_groups(st); + + if (st->info.num_des !=3D st->num_des) { + fprintf(stdout, "\n++++++ DES NOT FULLY_ENUMERATED ++++++\n"); + fprintf(stdout, "+++ DECLARED:%u ENUMERATED:%u +++\n", + st->info.num_des, st->num_des); + } + } + + return 0; +} + +static int discover_base_info(int fd, struct scmi_tlm_abi_info *info) +{ + int ret; + + info->size =3D sizeof(*info); + ret =3D ioctl(fd, SCMI_TLM_GET_ABI_INFO, info); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_ABI_INFO)); + return -errno; + } + + return 0; +} + +static int +query_items(int fd, unsigned long ioctl_op, size_t item_sz, const char *ta= g) +{ + struct scmi_tlm_batch query =3D {}; + int ret; + + query.item_sz =3D item_sz; + ret =3D ioctl(fd, ioctl_op, &query); + if (ret < 0) { + perror(tag); + return -errno; + } + + fprintf(stdout, "--- [QUERY]:[%s] -> NUM_ITEMS:%u\n", + tag, query.num_items); + + return 0; +} + +static inline struct scmi_tlm_batch * +batch_buffer_alloc(int num_items, size_t item_sz, bool need_status) +{ + struct scmi_tlm_batch *batch; + size_t batch_sz, all_items_sz; + void *items; + + batch_sz =3D sizeof(*batch); + batch =3D malloc(batch_sz); + if (!batch) + return NULL; + + bzero(batch, batch_sz); + batch->num_items =3D num_items; + + batch->item_sz =3D item_sz; + all_items_sz =3D batch->num_items * batch->item_sz; + items =3D malloc(all_items_sz); + if (!items) { + free(batch); + return NULL; + } + + bzero(items, all_items_sz); + batch->items =3D (uint64_t)items; + + if (need_status) { + int *states; + size_t states_sz =3D sizeof(*states) * batch->num_items; + + states =3D malloc(states_sz); + if (!states) { + free(items); + free(batch); + return NULL; + } + + bzero(states, states_sz); + batch->states =3D (uint64_t)states; + } + + return batch; +} + +static struct scmi_tlm_batch *scmi_get_des_list(int fd, int num_des) +{ + struct scmi_tlm_de_info *dei; + struct scmi_tlm_batch *batch; + int ret; + + batch =3D batch_buffer_alloc(num_des, sizeof(*dei), false); + if (!batch) + return NULL; + + ret =3D ioctl(fd, SCMI_TLM_GET_DE_LIST, batch); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_LIST)); + free(batch); + return NULL; + } + + return batch; +} + +static struct tlm_de *enumerate_des(struct tlm_state *st) +{ + struct scmi_tlm_batch *des_batch; + struct tlm_de *des; + int num_items; + + des_batch =3D scmi_get_des_list(st->fd, st->info.num_des); + if (!des_batch) + return NULL; + + num_items =3D des_batch->num_items; + des =3D malloc(sizeof(*des) * num_items); + if (!des) { + free(des_batch); + return NULL; + } + + bzero(des, sizeof(*des) * num_items); + + for (int i =3D 0; i < num_items; i++) { + struct tlm_de *de =3D &des[i]; + struct scmi_tlm_batch batch =3D {}; + int ret; + + de->info =3D &(((struct scmi_tlm_de_info *)des_batch->items)[i]); + de->cfg.id =3D de->info->id; + + batch.num_items =3D 1; + batch.item_sz =3D sizeof(de->cfg); + batch.items =3D (uint64_t)&de->cfg; + + ret =3D ioctl(st->fd, SCMI_TLM_GET_DE_CFG, &batch); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_CFG)); + free(des); + free(des_batch); + return NULL; + } + } + + /* On failure st->num_des remains zero */ + st->num_des =3D num_items; + + return des; +} + +static int get_current_config(int fd, struct scmi_tlm_config *cfg) +{ + int ret; + + ret =3D ioctl(fd, SCMI_TLM_GET_CFG, cfg); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_CFG)); + return -errno; + } + + return 0; +} + +static struct scmi_tlm_batch *scmi_get_grps_list(int fd, int num_groups) +{ + struct scmi_tlm_batch *batch; + int ret; + + batch =3D batch_buffer_alloc(num_groups, sizeof(struct scmi_tlm_grp_info), + false); + if (!batch) + return NULL; + + ret =3D ioctl(fd, SCMI_TLM_GET_GRP_LIST, batch); + if (ret) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_GRP_LIST)); + free(batch); + return NULL; + } + + return batch; +} + +static struct scmi_tlm_intervals * +enumerate_intervals(int fd, int num_intervals, unsigned int *grp_id) +{ + struct scmi_tlm_update_interval *intervs; + struct scmi_tlm_intervals *ivs; + size_t sz, i_sz; + int ret; + + sz =3D sizeof(*ivs); + ivs =3D malloc(sz); + if (!ivs) + return NULL; + + bzero(ivs, sz); + + i_sz =3D sizeof(*intervs) * num_intervals; + intervs =3D malloc(i_sz); + if (!intervs) { + free(ivs); + return NULL; + } + + bzero(intervs, i_sz); + + ivs->num_intervals =3D num_intervals; + ivs->intervals =3D (uint64_t)intervs; + if (grp_id) { + ivs->grp_id =3D *grp_id; + ivs->flags =3D SCMI_TLM_INTERV_GROUP; + } + + ret =3D ioctl(fd, SCMI_TLM_GET_INTRVS, ivs); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_INTRVS)); + free(ivs); + free(intervs); + return NULL; + } + + return ivs; +} + +static struct scmi_tlm_batch *enumerate_shmtis(struct tlm_state *st) +{ + struct scmi_tlm_batch *batch; + int ret; + + batch =3D batch_buffer_alloc(st->info.num_shmtis, + sizeof(struct scmi_tlm_shmti_info), false); + if (!batch) + return NULL; + + ret =3D ioctl(st->fd, SCMI_TLM_GET_SHMTI_LIST, batch); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_SHMTI_LIST)); + free(batch); + return NULL; + } + + return batch; +} + +static struct tlm_group *enumerate_groups(struct tlm_state *st) +{ + struct scmi_tlm_batch *grps_batch; + struct tlm_group *grps; + int num_items; + + grps_batch =3D scmi_get_grps_list(st->fd, st->info.num_groups); + if (!grps_batch) + return NULL; + + num_items =3D grps_batch->num_items; + grps =3D malloc(sizeof(*grps) * num_items); + if (!grps) { + free(grps_batch); + return NULL; + } + + bzero(grps, sizeof(*grps) * num_items); + for (int i =3D 0; i < num_items; i++) { + struct tlm_group *grp =3D &grps[i]; + size_t size, composing_sz; + uint32_t *composing_des; + int ret; + + grp->info =3D &(((struct scmi_tlm_grp_info *)grps_batch->items)[i]); + size =3D sizeof(*grp->desc); + grp->desc =3D malloc(size); + if (!grp->desc) { + free(grps); + free(grps_batch); + return NULL; + } + + bzero(grp->desc, size); + composing_sz =3D sizeof(uint32_t) * grp->info->num_des; + composing_des =3D malloc(composing_sz); + if (!composing_des) { + free(grp->desc); + free(grps); + free(grps_batch); + return NULL; + } + + grp->desc->grp_id =3D grp->info->grp_id; + grp->desc->num_des =3D grp->info->num_des; + grp->desc->composing_des =3D (uint64_t)composing_des; + ret =3D ioctl(st->fd, SCMI_TLM_GET_GRP_DESC, grp->desc); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_GRP_DESC)); + free(composing_des); + free(grp->desc); + free(grps); + free(grps_batch); + return NULL; + } + + grp->ivs =3D enumerate_intervals(st->fd, grp->info->num_intervals, + &grp->info->grp_id); + } + + /* On failure st->num_groups remains zero */ + st->num_groups =3D num_items; + + return grps; +} + +static void gather_tlm_res_state(struct tlm_state *st) +{ + if (st->info.num_des && !st->des) { + st->des =3D enumerate_des(st); + if (!st->des) + fprintf(stderr, "Failed to enumerate DES\n"); + } + + /* Enumerate even though GROUPS NOT supported */ + if (st->info.num_groups && !st->grps) { + st->grps =3D enumerate_groups(st); + if (!st->grps) + fprintf(stderr, "Failed to enumerate GROUPS\n"); + } +} + +static int gather_tlm_state(struct tlm_state *st) +{ + int ret; + + ret =3D discover_base_info(st->fd, &st->info); + if (ret) + return ret; + + st->ivs =3D enumerate_intervals(st->fd, st->info.num_intervals, NULL); + if (!st->ivs) { + fprintf(stderr, "Failed to enumerate Global Intervals. Abort.\n"); + return -1; + } + + ret =3D get_current_config(st->fd, &st->cfg); + if (ret) + return ret; + + if (st->info.num_shmtis) { + st->shmtis =3D enumerate_shmtis(st); + if (!st->shmtis) + fprintf(stderr, "Failed to enumerate SHMTIS\n"); + } + + return 0; +} + +static struct tlm_state *open_session(const char *path) +{ + struct tlm_state *st; + + st =3D malloc(sizeof(*st)); + if (!st) + return NULL; + + bzero(st, sizeof(*st)); + st->fd =3D open(path, O_RDWR); + if (st->fd < 0) { + perror("open"); + free(st); + return NULL; + } + + st->path =3D path; + + return st; +} + +static ssize_t tdcf_dump(const void *tdcf, size_t len) +{ + ssize_t read_bytes =3D 0; + + do { + ssize_t bytes; + + bytes =3D write(1, tdcf + read_bytes, len - read_bytes); + if (bytes < 0) { + read_bytes =3D -1; + break; + } + read_bytes +=3D bytes; + } while (read_bytes < len); + + return read_bytes; +} + +static int dump_shmti(struct scmi_tlm_shmti_info *shmti, bool decode) +{ + ssize_t read_bytes =3D 0; + void *tdcf; + unsigned int len; + + if (shmti->len < MIN_TDCF_SZ_V1) { + fprintf(stderr, + "Skipping malformed SHMTI - SID:%u LEN:%u OFFSET:0x%X\n", + shmti->sid, shmti->len, shmti->offset); + return -1; + } + + len =3D shmti->len + shmti->offset; + tdcf =3D mmap(NULL, len, PROT_READ, MAP_SHARED, shmti->fd, 0); + if (tdcf =3D=3D MAP_FAILED) { + fprintf(stderr, "%s\n", strerror(errno)); + return -1; + } + + if (decode) + read_bytes =3D tdcf_decode(tdcf + shmti->offset, shmti->len); + else + read_bytes =3D tdcf_dump(tdcf + shmti->offset, shmti->len); + + munmap(tdcf, len); + + return read_bytes; +} + +static int shmti_dump(struct tlm_state *st, struct parsed_args *args, bool= decode) +{ + struct scmi_tlm_shmti_info *shmti; + unsigned long first, last; + + if (!st->shmtis || !st->shmtis->num_items) + return 0; + + if (args->cnt > 1) { + first =3D last =3D strtoul(args->opts[1], NULL, 0); + if (first =3D=3D ULONG_MAX || errno =3D=3D EINVAL || + first >=3D st->shmtis->num_items) { + fprintf(stderr, "Invalid SHMTI ID: %lu\n", first); + return -1; + } + } else { + first =3D 0; + last =3D st->shmtis->num_items - 1; + } + + for (int sid =3D first; sid <=3D last; sid++) { + int ret; + + shmti =3D &(((struct scmi_tlm_shmti_info *)(st->shmtis->items))[sid]); + fprintf(stderr, "\nBinary %s of SHMTI[%u] LEN:%u\n", + decode ? "DECODE" : "DUMP", shmti->sid, shmti->len); + + ret =3D dump_shmti(shmti, decode); + if (!ret) + fprintf(stderr, "...FAILED DUMP of SHMTI[%u] !!!\n", sid); + } + + return 0; +} + +static int shmti_check(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_shmti_info *shmtis; + + if (!st->shmtis) + return 0; + + shmtis =3D (struct scmi_tlm_shmti_info *)st->shmtis->items; + + fprintf(stdout, "\n- SHMTI mmap SANITY CHECK -\n-------------------\n"); + + for (int i =3D 0; i < st->shmtis->num_items; i++) { + unsigned long *start, *end; + unsigned int len; + void *tdcf; + char *buf; + + if (shmtis[i].len < MIN_TDCF_SZ_V1) { + fprintf(stdout, + "Skipping malformed SHMTI[%u] - SID:%u LEN:%u OFFSET:0x%X\n", + i, shmtis[i].sid, shmtis[i].len, shmtis[i].offset); + continue; + } + + fprintf(stdout, "Mapping SHMTI[%u] - SID:%u LEN:%u FD:%u OFFS:0x%X\n", + i, shmtis[i].sid, shmtis[i].len, shmtis[i].fd, shmtis[i].offset); + + len =3D shmtis[i].len + shmtis[i].offset; + tdcf =3D mmap(NULL, len, PROT_READ, MAP_SHARED, shmtis[i].fd, 0); + if (tdcf =3D=3D MAP_FAILED) { + fprintf(stderr, "%s\n", strerror(errno)); + return -1; + } + + start =3D tdcf + shmtis[i].offset; + end =3D (void *)start + shmtis[i].len; + + fprintf(stdout, "-----------------------------------------------------\n= "); + fprintf(stdout, "Mapped SHMTI - SID:%u %u@%p\n", + shmtis[i].sid, shmtis[i].offset, tdcf); + + buf =3D (char *)start; + for (int j =3D 0; j < 4; j++) + fprintf(stdout, "%c ", buf[j]); + + fprintf(stdout, " =3D=3D=3D> "); + + buf =3D ((char *)end) - 4; + for (int j =3D 0 ; j < 4; j++) + fprintf(stdout, "%c ", buf[j]); + fprintf(stdout, "\n-----------------------------------------------------= \n"); + + munmap(tdcf, len); + } + + return 0; +} + +static int cmd_shmti_ops(struct tlm_state *st, struct parsed_args *args) +{ + if (!args->cnt || !args->opts) + return cmd_unknown("_"); + + if (!strncmp(args->opts[0], "dump", strlen(args->opts[0]))) + return shmti_dump(st, args, false); + if (!strncmp(args->opts[0], "check", strlen(args->opts[0]))) + return shmti_check(st, args); + if (!strncmp(args->opts[0], "decode", strlen(args->opts[0]))) + return shmti_dump(st, args, true); + + return cmd_unknown(args->opts[0]); +} + +static int parse_options(int argc, char **argv, struct parsed_args *args) +{ + int opt =3D 0; + + /* Set defaults */ + args->instance =3D "/dev/scmi/tlm_0"; + while ((opt =3D getopt(argc, argv, "i:h")) !=3D -1) { + switch (opt) { + case 'i': + args->instance =3D optarg; + break; + case 'h': + default: + return -EINVAL; + } + } + + if (optind <=3D argc) { + if (optind < argc - 1) { + args->cnt =3D argc - 1 - optind; + args->opts =3D malloc(sizeof(*args->opts) * args->cnt); + if (!args->opts) + return -1; + } + + args->cmd =3D argv[optind]; + for (int i =3D 0, j =3D optind + 1; j < argc; i++, j++) + args->opts[i] =3D argv[j]; + } + + return 0; +} + +static inline struct scmi_tlm_data_read *bulk_buffer_alloc(struct tlm_stat= e *st) +{ + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read *bulk; + size_t bulk_sz, samples_sz; + + bulk_sz =3D sizeof(*bulk); + bulk =3D malloc(bulk_sz); + if (!bulk) + return NULL; + + bzero(bulk, bulk_sz); + samples_sz =3D st->info.num_des * sizeof(*samples); + samples =3D malloc(samples_sz); + if (!samples) { + free(bulk); + return NULL; + } + + bzero(samples, samples_sz); + bulk->samples =3D (uint64_t)samples; + bulk->num_samples =3D st->info.num_des; + + return bulk; +} + +static int cmd_single_read(struct tlm_state *st) +{ + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read *bulk; + int ret; + + bulk =3D bulk_buffer_alloc(st); + if (!bulk) + return -1; + + ret =3D ioctl(st->fd, SCMI_TLM_SINGLE_READ, bulk); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_SINGLE_READ)); + return -errno; + } + + fprintf(stdout, "\n- Single ASYNC read -\n-------------------\n"); + samples =3D (struct scmi_tlm_de_sample *)bulk->samples; + for (int i =3D 0; i < bulk->num_samples; i++) + fprintf(stdout, "0x%08X %016llu %016llX\n", + samples[i].id, samples[i].tstamp, samples[i].val); + + return 0; +} + +static int cmd_bulk_read(struct tlm_state *st) +{ + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_data_read *bulk; + int ret; + + bulk =3D bulk_buffer_alloc(st); + if (!bulk) + return -1; + + ret =3D ioctl(st->fd, SCMI_TLM_BULK_READ, bulk); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_BULK_READ)); + return -errno; + } + + fprintf(stdout, "\n- BULK read -\n-------------------\n"); + samples =3D (struct scmi_tlm_de_sample *)bulk->samples; + for (int i =3D 0; i < bulk->num_samples; i++) + fprintf(stdout, "0x%08X %016llu %016llX\n", + samples[i].id, samples[i].tstamp, samples[i].val); + + return 0; +} + +static int cmd_tlm_reset(struct tlm_state *st) +{ + int ret; + + ret =3D ioctl(st->fd, SCMI_TLM_RESET); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_RESET)); + return -errno; + } + + fprintf(stdout, "\n--- TLM RESET ISSUED\n"); + + return 0; +} + +static int cmd_batch_read(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_de_sample *samples; + struct scmi_tlm_batch *batch; + int ret; + + if (!args->opts || !args->cnt) + return -EINVAL; + + if (args->cnt > st->info.num_des) + return -ENOSPC; + + /* Allocate for full size with status */ + batch =3D batch_buffer_alloc(st->info.num_des, sizeof(*samples), true); + if (!batch) + return -1; + + samples =3D (struct scmi_tlm_de_sample *)batch->items; + batch->num_items =3D args->cnt; + for (int i =3D 0; i < batch->num_items; i++) { + unsigned long val; + + val =3D strtoul(args->opts[i], NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D EINVAL) + continue; + + samples[i].id =3D (unsigned int)val; + } + + ret =3D ioctl(st->fd, SCMI_TLM_BATCH_READ, batch); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_BATCH_READ)); + return -errno; + } + + fprintf(stdout, "\n- BATCH read -\n-------------------\n"); + for (int i =3D 0; i < batch->num_items; i++) + fprintf(stdout, "STATUS[%d] =3D> 0x%08X %016llu %016llX\n", + ((uint32_t *)batch->states)[i], + samples[i].id, samples[i].tstamp, samples[i].val); + + return 0; +} + +/* + * :[:] + */ +static void +parse_item_cfg(char *str, bool *ena, bool *t_ena, unsigned int *update) +{ + char *tok; + + if (!ena) + return; + + *ena =3D false; + tok =3D strtok(str, ":"); + if (tok) + *ena =3D strncmp(tok, "on", strlen(tok)) =3D=3D 0; + + if (!t_ena) + return; + + *t_ena =3D false; + tok =3D strtok(NULL, ":"); + if (tok) + *t_ena =3D strncmp(tok, "on", strlen(tok)) =3D=3D 0; + + if (!update) + return; + + tok =3D strtok(NULL, ":"); + if (tok) { + unsigned long val; + + val =3D strtoul(tok, NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D EINVAL) + return; + + *update =3D (unsigned int)val; + } +} + +static int configure_all_des(struct tlm_state *st, struct parsed_args *arg= s) +{ + struct scmi_tlm_de_config de_cfg =3D {}; + bool ena, t_ena; + int ret; + + if (!args->opts) + return -1; + + ret =3D ioctl(st->fd, SCMI_TLM_GET_ALL_CFG, &de_cfg); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_ALL_CFG)); + return -errno; + } + + fprintf(stdout, "--- [GET] ALL DEs ON:%d T_ON:%d\n", + de_cfg.enable, de_cfg.t_enable); + + if (args->cnt < 2) + return 0; + + parse_item_cfg(args->opts[1], &ena, &t_ena, NULL); + de_cfg.enable =3D !!ena; + de_cfg.t_enable =3D !!t_ena; + + fprintf(stdout, "--- [SET] ALL DEs ON:%d T_ON:%d\n", + de_cfg.enable, de_cfg.t_enable); + + ret =3D ioctl(st->fd, SCMI_TLM_SET_ALL_CFG, &de_cfg); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_ALL_CFG)); + return -errno; + } + + return 0; +} + +static int de_config_batch_get(struct tlm_state *st, struct scmi_tlm_batch= *batch) +{ + int ret; + + /* Get current config */ + ret =3D ioctl(st->fd, SCMI_TLM_GET_DE_CFG, batch); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_DE_CFG)); + return -errno; + } + + return 0; +} + +static int configure_de(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_de_config *de_cfg; + struct scmi_tlm_batch *batch; + bool ena, t_ena; + unsigned long val; + int ret, *states; + + if (!args->opts) + return -1; + + gather_tlm_res_state(st); + + if (args->cnt < 2) { + /* + * Bail-out to avoid issuing a query-0 which is not what + * intended here + */ + if (!st->num_des) + return 0; + + batch =3D batch_buffer_alloc(st->num_des, sizeof(*de_cfg), true); + if (!batch) + return -1; + + /* Dump all DE configs */ + de_cfg =3D (struct scmi_tlm_de_config *)batch->items; + for (int i =3D 0; i < st->num_des; i++) + de_cfg[i].id =3D st->des[i].info->id; + + ret =3D de_config_batch_get(st, batch); + if (ret) + return ret; + + states =3D (int *)batch->states; + for (int i =3D 0; i < batch->num_items; i++) { + fprintf(stdout, + "--- [GET] STATUS[%d] -> DE:0x%08X ON:%d T_ON:%d SID:%u OFFS:0x%X", + states[i], de_cfg[i].id, de_cfg[i].enable, + de_cfg[i].t_enable, de_cfg[i].sid, de_cfg[i].offset); + print_uuid(" UUID: ", &de_cfg[i].uuid[0]); + fprintf(stdout, "\n"); + } + + return 0; + } + + /*Dump one DE config */ + batch =3D batch_buffer_alloc(1, sizeof(*de_cfg), true); + if (!batch) + return -1; + + states =3D (int *)batch->states; + de_cfg =3D (struct scmi_tlm_de_config *)batch->items; + val =3D strtoul(args->opts[1], NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D EINVAL) + return -1; + + de_cfg[0].id =3D (unsigned int)val; + ret =3D de_config_batch_get(st, batch); + if (ret) + return ret; + + fprintf(stdout, "--- [GET] STATUS[%d] -> DE:0x%08X ON:%d T_ON:%d SID:%= u OFFS:0x%X", + states[0], de_cfg[0].id, de_cfg[0].enable, + de_cfg[0].t_enable, de_cfg[0].sid, de_cfg[0].offset); + print_uuid(" UUID: ", &de_cfg[0].uuid[0]); + fprintf(stdout, "\n"); + + if (args->cnt < 3) + return 0; + + /* Configure one DE */ + parse_item_cfg(args->opts[2], &ena, &t_ena, NULL); + de_cfg[0].enable =3D !!ena; + de_cfg[0].t_enable =3D !!t_ena; + + ret =3D ioctl(st->fd, SCMI_TLM_SET_DE_CFG, batch); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_DE_CFG)); + return -errno; + } + + fprintf(stdout, "--- [SET] STATUS[%d] -> DE:0x%08X ON:%d T_ON:%d SID:%= u OFFS:0x%X", + states[0], de_cfg[0].id, de_cfg[0].enable, + de_cfg[0].t_enable, de_cfg[0].sid, de_cfg[0].offset); + print_uuid(" UUID: ", &de_cfg[0].uuid[0]); + fprintf(stdout, "\n"); + + return 0; +} + +static int grp_config_get(struct tlm_state *st, struct scmi_tlm_config *cf= g) +{ + int ret; + + /* Get current config */ + ret =3D ioctl(st->fd, SCMI_TLM_GET_CFG, cfg); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_CFG)); + return -errno; + } + + return 0; +} + +static int configure_group(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_config cfg =3D {}; + bool ena, t_ena; + unsigned long val; + int ret; + + if (!args->opts) + return -1; + + gather_tlm_res_state(st); + + if (args->cnt < 2) { + /* Dump all GRPS configs */ + for (int i =3D 0; i < st->num_groups; i++) { + cfg.grp_id =3D st->grps[i].info->grp_id; + cfg.active.exp =3D -3; + cfg.flags =3D SCMI_TLM_CONFIG_GROUP; + ret =3D grp_config_get(st, &cfg); + if (!ret) + fprintf(stdout, + "--- [GET] -> GROUP:0x%08X ON:%d T_ON:%d INTRV:%u,%d\n", + cfg.grp_id, cfg.enable, cfg.t_enable, + cfg.active.secs, cfg.active.exp); + } + + return 0; + } + + /*Dump one GROUP config */ + val =3D strtoul(args->opts[1], NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D EINVAL) + return -1; + + cfg.grp_id =3D (unsigned int)val; + cfg.flags =3D SCMI_TLM_CONFIG_GROUP; + cfg.active.exp =3D -3; + ret =3D grp_config_get(st, &cfg); + if (ret) + return ret; + + fprintf(stdout, "--- [GET] -> GROUP:0x%08X ON:%d T_ON:%d INTRV:%u,%d\n= ", + cfg.grp_id, cfg.enable, cfg.t_enable, cfg.active.secs, cfg.active.exp); + + if (args->cnt < 3) + return 0; + + /* Configure one GROUP */ + parse_item_cfg(args->opts[2], &ena, &t_ena, &cfg.active.secs); + cfg.enable =3D !!ena; + cfg.t_enable =3D !!t_ena; + + fprintf(stdout, "--- [SET] -> GROUP:0x%08X ON:%d T_ON:%d INTRV:%u,%d\n= ", + cfg.grp_id, cfg.enable, cfg.t_enable, + cfg.active.secs, cfg.active.exp); + + ret =3D ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG)); + return -errno; + } + + return 0; +} + +static int configure_telemetry(struct tlm_state *st, struct parsed_args *a= rgs) +{ + struct scmi_tlm_config cfg =3D st->cfg; + bool val; + int ret; + + if (args->cnt < 2 || !args->opts) + return -1; + + val =3D strncmp(args->opts[1], "on", strlen(args->opts[1])) =3D=3D 0; + + cfg.enable =3D !!val; + cfg.flags =3D 0; + + fprintf(stdout, "\n---%sabling Telemetry\n", val ? "En" : "Dis"); + ret =3D ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG)); + return -errno; + } + + return 0; +} + +static int configure_update(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_config cfg =3D st->cfg; + unsigned long val; + int ret; + + if (args->cnt < 2 || !args->opts) + return -1; + + val =3D strtoul(args->opts[1], NULL, 0); + if (val =3D=3D ULONG_MAX || errno =3D=3D EINVAL) + return -1; + + cfg.flags =3D 0; + if (!val) { + cfg.enable =3D val; + fprintf(stdout, "\n---Disabling Telemetry\n"); + } else { + cfg.enable =3D !!val; + cfg.active.secs =3D val; + cfg.active.exp =3D -3; + fprintf(stdout, "\n--- Setting update interval to %lu\n", val); + } + + ret =3D ioctl(st->fd, SCMI_TLM_SET_CFG, &cfg); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_SET_CFG)); + return -errno; + } + + return 0; +} + +static int cmd_configure(struct tlm_state *st, struct parsed_args *args) +{ + if (!args->cnt || !args->opts) + return cmd_unknown("_"); + + if (!strncmp(args->opts[0], "all_des", strlen(args->opts[0]))) + return configure_all_des(st, args); + if (!strncmp(args->opts[0], "tlm", strlen(args->opts[0]))) + return configure_telemetry(st, args); + if (!strncmp(args->opts[0], "update", strlen(args->opts[0]))) + return configure_update(st, args); + if (!strncmp(args->opts[0], "de", strlen(args->opts[0]))) + return configure_de(st, args); + if (!strncmp(args->opts[0], "group", strlen(args->opts[0]))) + return configure_group(st, args); + + return cmd_unknown(args->opts[0]); +} + +static inline int cmd_full_scan_and_dump(struct tlm_state *st) +{ + cmd_dump_state(st, NULL); + cmd_dump_des(st, NULL); + cmd_dump_groups(st); + + return 0; +} + +static int cmd_uuids(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_batch batch =3D {}; + struct scmi_tlm_uuid *uuids; + size_t u_sz; + int ret; + + batch.item_sz =3D sizeof(*uuids); + /* Get size at first */ + ret =3D ioctl(st->fd, SCMI_TLM_GET_UUID_LIST, &batch); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_UUID_LIST)); + return -errno; + } + + u_sz =3D sizeof(*uuids) * batch.num_items; + uuids =3D malloc(u_sz); + if (!uuids) + return -ENOMEM; + + bzero(uuids, u_sz); + batch.items =3D (uint64_t)uuids; + ret =3D ioctl(st->fd, SCMI_TLM_GET_UUID_LIST, &batch); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_GET_UUID_LIST)); + return -errno; + } + + fprintf(stdout, "--- [GET] -> NUM_UUIDS: %u\n", batch.num_items); + for (int i =3D 0; i < batch.num_items; i++) { + print_uuid(" UUID: ", + &(((struct scmi_tlm_uuid *)(batch.items))[i])); + fprintf(stdout, "\n"); + } + + return 0; +} + +static int cmd_monitor(struct tlm_state *st, struct parsed_args *args) +{ + struct scmi_tlm_event evt =3D {}; + struct pollfd fds[2]; + sigset_t mask; + int ret, efd, sfd; + + efd =3D eventfd(0, EFD_CLOEXEC); + if (efd < 0) + return -errno; + + sigemptyset(&mask); + sigaddset(&mask, SIGTERM); + sigaddset(&mask, SIGINT); + sigaddset(&mask, SIGQUIT); + ret =3D sigprocmask(SIG_BLOCK, &mask, NULL); + if (ret < 0) { + close(efd); + return -errno; + } + + sfd =3D signalfd(-1, &mask, 0); + if (sfd < 0) { + close(efd); + return -errno; + } + + fds[0].fd =3D sfd; + fds[0].events =3D POLLIN; + + evt.type =3D SCMI_TLM_EVT_GENERATION; + evt.efd =3D efd; + fprintf(stdout, "=3D=3D>> Registering for events of type %u\n", evt.type); + ret =3D ioctl(st->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_EVENT_SUBSCRIBE)); + return -errno; + } + + fds[1].fd =3D evt.efd; + fds[1].events =3D POLLIN; + + if (args->cnt) { + fprintf(stdout, "=3D=3D>> Closing FD on TLM device...\n"); + close(st->fd); + } + + fprintf(stdout, "=3D=3D>> Waiting for events of type %u on cookie:%u\n", + evt.type, evt.cookie); + while (1) { + ret =3D poll(fds, 2, -1); + if (ret < 0) + return ret; + + /* Break on SIGINT */ + if (fds[0].revents & POLLIN) { + struct signalfd_siginfo fdsi; + + read(fds[0].fd, &fdsi, sizeof(fdsi)); + if (fdsi.ssi_signo) { + fprintf(stderr, "Got signal :%u\n", fdsi.ssi_signo); + break; + } + } + + if (fds[1].revents & POLLIN) { + uint64_t val =3D 0; + + read(fds[1].fd, &val, sizeof(val)); + fprintf(stdout, "=3D=3D>> EVENT READ on EFD:%d - VAL:%lu!\n", + evt.efd, val); + } + } + + if (!args->cnt) { + fprintf(stdout, "=3D=3D>> DE-Registering for events of type %u on cookie= :%u\n", + evt.type, evt.cookie); + ret =3D ioctl(st->fd, SCMI_TLM_EVENT_SUBSCRIBE, &evt); + if (ret < 0) { + perror(IOCTL_ERR_STR(SCMI_TLM_EVENT_SUBSCRIBE)); + return -errno; + } + } + + close(sfd); + close(efd); + + return 0; +} + +static int query_intervals(struct tlm_state *st) +{ + struct scmi_tlm_intervals tlm_ivs =3D {}; + int ret; + + /* Global interval */ + ret =3D ioctl(st->fd, SCMI_TLM_GET_INTRVS, &tlm_ivs); + if (ret < 0) { + perror("SCMI_TLM_GET_INTRVS"); + return -errno; + } + + fprintf(stdout, "--- [QUERY]:[SCMI_TLM_GET_INTRVS] -> NUM_ITEMS:%u\n", + tlm_ivs.num_intervals); + + for (int i =3D 0; i < st->num_groups; i++) { + struct scmi_tlm_intervals tlm_ivs =3D {}; + + tlm_ivs.grp_id =3D st->grps[i].info->grp_id; + tlm_ivs.flags =3D SCMI_TLM_INTERV_GROUP; + ret =3D ioctl(st->fd, SCMI_TLM_GET_INTRVS, &tlm_ivs); + if (ret < 0) { + perror("SCMI_TLM_GET_INTRVS"); + return -errno; + } + + fprintf(stdout, + "--- [QUERY][GRP%u]:[SCMI_TLM_GET_INTRVS] -> NUM_ITEMS:%u\n", + tlm_ivs.grp_id, tlm_ivs.num_intervals); + } + + return 0; +} + +static int query_samples(struct tlm_state *st, unsigned long ioctl_op, + const char *tag) +{ + struct scmi_tlm_data_read data =3D {}; + int ret; + + /* Global interval */ + ret =3D ioctl(st->fd, ioctl_op, &data); + if (ret < 0) { + perror(tag); + return -errno; + } + + fprintf(stdout, "--- [QUERY]:[%s] -> NUM_ITEMS:%u\n", + tag, data.num_samples); + + for (int i =3D 0; i < st->num_groups; i++) { + struct scmi_tlm_data_read data =3D {}; + + data.grp_id =3D st->grps[i].info->grp_id; + data.flags =3D SCMI_TLM_READ_GROUP; + ret =3D ioctl(st->fd, ioctl_op, &data); + if (ret < 0) { + perror(tag); + return -errno; + } + + fprintf(stdout, + "--- [QUERY][GRP%u]:[%s] -> NUM_ITEMS:%u\n", + data.grp_id, tag, data.num_samples); + } + + return 0; +} + +static int cmd_query_ioctls(struct tlm_state *st) +{ + int ret; + + gather_tlm_res_state(st); + + ret =3D query_items(st->fd, SCMI_TLM_GET_DE_LIST, + sizeof(struct scmi_tlm_de_info), + "SCMI_TLM_GET_DE_LIST"); + if (ret) + return ret; + + /*Bailoout ONLY whenm GROUPS not supported */ + ret =3D query_items(st->fd, SCMI_TLM_GET_GRP_LIST, + sizeof(struct scmi_tlm_grp_info), + "SCMI_TLM_GET_GRP_LIST"); + if (ret && st->info.abi_features & SCMI_TLM_ABI_FEAT_GROUPS) + return ret; + + ret =3D query_items(st->fd, SCMI_TLM_GET_SHMTI_LIST, + sizeof(struct scmi_tlm_shmti_info), + "SCMI_TLM_GET_SHMTI_LIST"); + if (ret) + return ret; + + ret =3D query_items(st->fd, SCMI_TLM_GET_UUID_LIST, + sizeof(struct scmi_tlm_uuid), + "SCMI_TLM_GET_UUID_LIST"); + if (ret) + return ret; + + ret =3D query_items(st->fd, SCMI_TLM_GET_DE_CFG, + sizeof(struct scmi_tlm_de_config), + "SCMI_TLM_GET_DE_CFG"); + if (ret) + return ret; + + ret =3D query_items(st->fd, SCMI_TLM_SET_DE_CFG, + sizeof(struct scmi_tlm_de_config), + "SCMI_TLM_SET_DE_CFG"); + if (ret) + return ret; + + ret =3D query_items(st->fd, SCMI_TLM_BATCH_READ, + sizeof(struct scmi_tlm_de_sample), + "SCMI_TLM_BATCH_READ"); + if (ret) + return ret; + + ret =3D query_intervals(st); + if (ret) + return ret; + + ret =3D query_samples(st, SCMI_TLM_BULK_READ, "SCMI_TLM_BULK_READ"); + if (ret) + return ret; + + ret =3D query_samples(st, SCMI_TLM_SINGLE_READ, "SCMI_TLM_SINGLE_READ"); + if (ret) + return ret; + + return 0; +} + +int main(int argc, char **argv) +{ + struct parsed_args args =3D {}; + struct tlm_state *st; + int ret; + + argv0 =3D argv[0]; + + ret =3D parse_options(argc, argv, &args); + if (ret < 0) { + usage(argv0); + return EXIT_FAILURE; + } + + st =3D open_session(args.instance); + if (!st) + return EXIT_FAILURE; + + ret =3D gather_tlm_state(st); + if (ret) + return EXIT_FAILURE; + + if (!args.cmd) + return cmd_full_scan_and_dump(st); + else if (!strncmp(args.cmd, "info", strlen(args.cmd))) + return cmd_dump_state(st, &args); + else if (!strncmp(args.cmd, "uuids", strlen(args.cmd))) + return cmd_uuids(st, &args); + else if (!strncmp(args.cmd, "monitor", strlen(args.cmd))) + return cmd_monitor(st, &args); + else if (!strncmp(args.cmd, "shmti", strlen(args.cmd))) + return cmd_shmti_ops(st, &args); + else if (!strncmp(args.cmd, "des", strlen(args.cmd))) + return cmd_dump_des(st, &args); + else if (!strncmp(args.cmd, "groups", strlen(args.cmd))) + return cmd_dump_groups(st); + else if (!strncmp(args.cmd, "cfg", strlen(args.cmd))) + return cmd_configure(st, &args); + else if (!strncmp(args.cmd, "single", strlen(args.cmd))) + return cmd_single_read(st); + else if (!strncmp(args.cmd, "bulk", strlen(args.cmd))) + return cmd_bulk_read(st); + else if (!strncmp(args.cmd, "batch", strlen(args.cmd))) + return cmd_batch_read(st, &args); + else if (!strncmp(args.cmd, "reset", strlen(args.cmd))) + return cmd_tlm_reset(st); + else if (!strncmp(args.cmd, "query", strlen(args.cmd))) + return cmd_query_ioctls(st); + + return cmd_unknown(args.cmd); +} diff --git a/tools/testing/scmi/stlm_decode.c b/tools/testing/scmi/stlm_dec= ode.c new file mode 100644 index 000000000000..186964710184 --- /dev/null +++ b/tools/testing/scmi/stlm_decode.c @@ -0,0 +1,252 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * SCMI Telemetry Test Tool Decoder + * + * Copyright (C) 2026 ARM Ltd. + */ + +/* + * Userspace decoder for a snapshot of an SCMI v4.0 Telemetry SHMTI area + * (TDCF format). libc only, no alignment or endianness assumptions on the + * host or on the snapshot buffer. + * + * Layout (all fields little-endian, except the UUID line payload): + * + * Prologue (16 bytes) + * +0 u32 signature 0x5442474E ("TBGN" as a LE-decoded u32) + * +4 u32 match seq LSB set =3D> platform update in progress + * +8 u32 num_qwords payload size in 64-bit units + * +12 u32 hdr meta [7:0] TDCF revision + * Payload (num_qwords * 8 bytes of lines, each 2 or 3 qwords) + * +0 u32 meta [7:4] line type: 0 DATA, 1 BLK_TS, 2 UUID + * [3] USE_BLK_TS [2] HAS_LINE_EXT + * [1] TS_VALID [0] DATA_INVALID + * +4 u32 id DATA: DE id BLK_TS: clock rate + * +8 DATA : u64 value BLK_TS: u64 timestamp UUID: 16 bytes + * +16 u64 line timestamp (only when HAS_LINE_EXT; UUID lines: part + * of the 16 bytes above, HAS_LINE_EXT is mandatory for them) + * Epilogue (last 8 bytes of the SHMTI) + * +0 u32 match seq must equal the prologue one + * +4 u32 signature 0x54454E44 ("TEND" as a LE-decoded u32) + * + * Line length: 16 bytes, or 24 bytes when HAS_LINE_EXT is set. + * + * Returns 0 on success, -EINVAL on bad size/signature, -EOVERFLOW/-EPROTO + * on malformed payload, -EAGAIN if the snapshot looks torn (odd start + * sequence or start/end sequence mismatch): the content is still decoded. + */ +#include +#include +#include +#include +#include + +#define TDCF_SIG_BEGIN 0x5442474Eu +#define TDCF_SIG_END 0x54454E44u +#define TDCF_PRLG_SZ 16u +#define TDCF_EPLG_SZ 8u +#define TDCF_MIN_SZ (TDCF_PRLG_SZ + TDCF_EPLG_SZ) + +#define TDCF_LINE_DATA 0u +#define TDCF_LINE_BLK_TS 1u +#define TDCF_LINE_UUID 2u + +#define TDCF_META_TYPE(m) (((m) >> 4) & 0xfu) +#define TDCF_META_USE_BLK_TS (1u << 3) +#define TDCF_META_HAS_EXT (1u << 2) +#define TDCF_META_TS_VALID (1u << 1) +#define TDCF_META_INVALID (1u << 0) + +#define TDCF_LINE_SZ 16u +#define TDCF_LINE_EXT_SZ 24u + +static uint32_t tdcf_le32(const uint8_t *p) +{ + return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | + ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); +} + +static uint64_t tdcf_le64(const uint8_t *p) +{ + return ((uint64_t)tdcf_le32(p + 4) << 32) | tdcf_le32(p); +} + +static void tdcf_flags(uint32_t m, char out[16]) +{ + snprintf(out, 16, "%c%c%c%c", + (m & TDCF_META_USE_BLK_TS) ? 'B' : '-', + (m & TDCF_META_HAS_EXT) ? 'E' : '-', + (m & TDCF_META_TS_VALID) ? 'T' : '-', + (m & TDCF_META_INVALID) ? 'I' : '-'); +} + +/* UUID bytes are in memory order, i.e. 4 x BE dwords: no swapping needed = */ +static void tdcf_uuid_str(const uint8_t *u, char out[40]) +{ + snprintf(out, 40, + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" + "%02x%02x%02x%02x%02x%02x", + u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7], + u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15]); +} + +int tdcf_decode(const void *buf, size_t len) +{ + const uint8_t *b =3D buf; + uint32_t sig_s, mseq_s, nq, hdr, mseq_e, sig_e; + size_t off =3D TDCF_PRLG_SZ, end, left; + /* Payload starts at 16, so offset 0 can safely mean "none" */ + size_t bts_off =3D 0, uuid_off =3D 0; + uint64_t bts_ts =3D 0; + uint32_t bts_rate =3D 0; + int torn =3D 0; + + if (!b || len < TDCF_MIN_SZ) { + fprintf(stderr, "TDCF: buffer too small (%zu bytes)\n", len); + return -EINVAL; + } + + end =3D len - TDCF_EPLG_SZ; /* payload lives in [off, end) */ + + sig_s =3D tdcf_le32(b); + mseq_s =3D tdcf_le32(b + 4); + nq =3D tdcf_le32(b + 8); + hdr =3D tdcf_le32(b + 12); + mseq_e =3D tdcf_le32(b + end); + sig_e =3D tdcf_le32(b + end + 4); + + if (sig_s !=3D TDCF_SIG_BEGIN || sig_e !=3D TDCF_SIG_END) { + fprintf(stderr, + "TDCF: bad signature START:0x%08" PRIx32 + " (exp 0x%08x) END:0x%08" PRIx32 " (exp 0x%08x)\n", + sig_s, TDCF_SIG_BEGIN, sig_e, TDCF_SIG_END); + return -EINVAL; + } + + printf("PRLG @0x0000 sig=3DTBGN mseq=3D0x%08" PRIx32 " qwords=3D%" PRIu32 + " rev=3D%" PRIu32 " hdr_meta=3D0x%08" PRIx32 "\n", + mseq_s, nq, hdr & 0xffu, hdr); + + if (mseq_s & 1u) { + fprintf(stderr, "TDCF: odd start sequence: update in progress\n"); + torn =3D 1; + } + if (mseq_s !=3D mseq_e) { + fprintf(stderr, + "TDCF: sequence mismatch start:0x%08" PRIx32 + " end:0x%08" PRIx32 "\n", mseq_s, mseq_e); + torn =3D 1; + } + + left =3D nq; + while (left) { + const uint8_t *p =3D b + off; + char fl[16], us[40]; + uint32_t meta, id; + size_t lb; + + if (end - off < 4) { + fprintf(stderr, "TDCF: line @0x%zx overflows SHMTI\n", off); + return -EOVERFLOW; + } + + meta =3D tdcf_le32(p); + lb =3D (meta & TDCF_META_HAS_EXT) ? TDCF_LINE_EXT_SZ : TDCF_LINE_SZ; + if (end - off < lb) { + fprintf(stderr, "TDCF: line @0x%zx overflows SHMTI\n", off); + return -EOVERFLOW; + } + if (left < lb / 8) { + fprintf(stderr, + "TDCF: line @0x%zx exceeds prologue num_qwords\n", + off); + return -EPROTO; + } + + id =3D tdcf_le32(p + 4); + tdcf_flags(meta, fl); + printf("LINE @0x%04zx %-6s [%s] ", off, + TDCF_META_TYPE(meta) =3D=3D TDCF_LINE_DATA ? "DATA" : + TDCF_META_TYPE(meta) =3D=3D TDCF_LINE_BLK_TS ? "BLK_TS" : + TDCF_META_TYPE(meta) =3D=3D TDCF_LINE_UUID ? "UUID" : "???", + fl); + + if (meta & TDCF_META_INVALID) { + /* Disabled/unused slot: not an error */ + printf("id=3D0x%08" PRIx32 " \n", id); + goto next; + } + + switch (TDCF_META_TYPE(meta)) { + case TDCF_LINE_DATA: { + uint64_t val =3D tdcf_le64(p + 8); + + printf("id=3D0x%08" PRIx32 " val=3D0x%016" PRIx64 + " (%" PRIu64 ") ", id, val, val); + + if (meta & TDCF_META_USE_BLK_TS) { + if (bts_off) + printf("ts=3D0x%016" PRIx64 + " (blk@0x%zx rate=3D%" PRIu32 ")", + bts_ts, bts_off, bts_rate); + else + printf("ts=3D"); + } else if (meta & TDCF_META_TS_VALID) { + if (meta & TDCF_META_HAS_EXT) + printf("ts=3D0x%016" PRIx64 " (line)", + tdcf_le64(p + 16)); + else + printf("ts=3D"); + } else if (meta & TDCF_META_HAS_EXT) { + printf("ts=3Dinvalid"); + } else { + printf("ts=3Dnone"); + } + + if (uuid_off) + printf(" uuid=3Dline@0x%zx", uuid_off); + else + printf(" uuid=3Dprimary"); + printf("\n"); + break; + } + case TDCF_LINE_BLK_TS: + if (meta & (TDCF_META_USE_BLK_TS | TDCF_META_HAS_EXT | + TDCF_META_TS_VALID)) { + printf("rate=3D%" PRIu32 " \n", id); + break; + } + bts_off =3D off; + bts_rate =3D id; + bts_ts =3D tdcf_le64(p + 8); + printf("rate=3D%" PRIu32 "%s ts=3D0x%016" PRIx64 + " (%" PRIu64 ")\n", bts_rate, + bts_rate ? "" : " (0: protocol default)", + bts_ts, bts_ts); + break; + case TDCF_LINE_UUID: + if ((meta & (TDCF_META_TS_VALID | TDCF_META_USE_BLK_TS)) || + !(meta & TDCF_META_HAS_EXT)) { + printf("\n"); + break; + } + uuid_off =3D off; + tdcf_uuid_str(p + 8, us); + printf("id=3D0x%08" PRIx32 " %s\n", id, us); + break; + default: + printf("id=3D0x%08" PRIx32 " \n", + id, TDCF_META_TYPE(meta)); + break; + } +next: + off +=3D lb; + left -=3D lb / 8; + } + + if (end > off) + printf("PAD @0x%04zx %zu unused payload bytes\n", off, end - off); + printf("EPLG @0x%04zx mseq=3D0x%08" PRIx32 " sig=3DTEND\n", end, mseq_e); + + return torn ? -EAGAIN : len; +} diff --git a/tools/testing/scmi/stlm_decode.h b/tools/testing/scmi/stlm_dec= ode.h new file mode 100644 index 000000000000..740d1528d1f4 --- /dev/null +++ b/tools/testing/scmi/stlm_decode.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __STLM_DECODE_H +#define __STLM_DECODE_H + +int tdcf_decode(const void *buf, size_t len); + +#endif --=20 2.54.0