mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cristian Marussi <cristian.marussi@arm.com>
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 <cristian.marussi@arm.com>
Subject: [PATCH v17 12/34] firmware: arm_scmi: Add Telemetry resources enumeration
Date: Tue, 24 Nov 2026 19:48:18 +0000	[thread overview]
Message-ID: <20261124194841.1147072-13-cristian.marussi@arm.com> (raw)
In-Reply-To: <20261124194841.1147072-1-cristian.marussi@arm.com>

Add basic Telemetry allocation and initialization routines, introducing and
wiring-up the logic needed to enumerate all the SCMI Telemetry resources
optionally using a lazy on-demand scheme: this approach factors in the
flexibility needed to minimize the amount of initialization work based on
the effective access pattern used by the upper layer ABI.

Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
v16 --> v17
 - remove unused de.ts_rate
 - renaming discovered to enumerated
 - list_add to fcs_des only after successfull TDE registration
 - use list_del_init for free_des/fcs_des list removals
 - call tdes_cleanup on free_resources
v15 --> v16
 - add tdes_cleanup as devm_action after enumeration
 - use phys_addr_t and proper format string
 - drop TDE_FC_SIZE and introduce better checks
 - unlink FC TDEs from fcs_des list on free
 - bailout on unknown TDE types
 - reject FCs smaller than 8 bytes
v14 --> v15
 - drop usage of devres for FC iomapping
 - trigger full enumeration of DE lookups
 - new IS_TDE_TS_ macro helpers
 - add a dedicated scmi_telemetry_free_tdes_add helper
 - better commenting
 - dropping needless __must_check
 - flag as fully enumerated only when ALL declared des were found
 - drop redundant iounmap
 - check FC size on enumeration
v13 --> v14
 - splitted out of the old Basic Telemetry patch, contains the basic
   instance allocations and the DEs enumeration process
 - renamed some fields to more strictly adhere to spec nomenclature
---
 drivers/firmware/arm_scmi/telemetry.c | 753 ++++++++++++++++++++++++++
 include/linux/scmi_protocol.h         |  37 ++
 2 files changed, 790 insertions(+)

diff --git a/drivers/firmware/arm_scmi/telemetry.c b/drivers/firmware/arm_scmi/telemetry.c
index 402d9587046c..94311cdc9398 100644
--- a/drivers/firmware/arm_scmi/telemetry.c
+++ b/drivers/firmware/arm_scmi/telemetry.c
@@ -8,15 +8,19 @@
 #include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/compiler_types.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/io.h>
 #include <linux/limits.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/sprintf.h>
 #include <linux/string.h>
 #include <linux/uuid.h>
+#include <linux/xarray.h>
 
 #include "protocols.h"
 #include "notify.h"
@@ -52,13 +56,216 @@ struct scmi_msg_resp_telemetry_protocol_attributes {
 	__le32 default_blk_ts_rate;
 };
 
+struct de_desc_fastchannel {
+	__le32 addr_low;
+	__le32 addr_high;
+	__le32 size;
+};
+
+struct scmi_de_desc {
+	__le32 de_id;
+	__le32 group_id;
+	__le32 de_data_sz;
+	__le32 de_attributes_1;
+#define	IS_NAME_SUPPORTED(d)	(le32_get_bits((d)->de_attributes_1, BIT(31)))
+#define	IS_FC_SUPPORTED(d)	(le32_get_bits((d)->de_attributes_1, BIT(30)))
+#define	GET_DE_TYPE(d)		(le32_get_bits((d)->de_attributes_1, GENMASK(29, 22)))
+#define	IS_PERSISTENT(d)	(le32_get_bits((d)->de_attributes_1, BIT(21)))
+#define GET_DE_UNIT_EXP(d)							\
+	({									\
+		__u32 __signed_exp =						\
+			le32_get_bits((d)->de_attributes_1, GENMASK(20, 13));	\
+										\
+		sign_extend32(__signed_exp, 7);					\
+	})
+#define	GET_DE_UNIT(d)		(le32_get_bits((d)->de_attributes_1, GENMASK(12, 5)))
+#define	TSTAMP_SUPPORT(d)	(le32_get_bits((d)->de_attributes_1, GENMASK(1, 0)))
+	__le32 de_attributes_2;
+#define	GET_DE_INSTA_ID(d)	(le32_get_bits((d)->de_attributes_2, GENMASK(31, 24)))
+#define	GET_COMPO_INSTA_ID(d)	(le32_get_bits((d)->de_attributes_2, GENMASK(23, 8)))
+#define	GET_COMPO_TYPE(d)	(le32_get_bits((d)->de_attributes_2, GENMASK(7, 0)))
+	__le32 de_attributes_3;
+};
+
+struct scmi_msg_resp_telemetry_de_description {
+	__le32 num_desc;	/* This does NOT count desc[] by the spec */
+	struct scmi_de_desc desc[];
+};
+
+#define IS_TDE_TSTAMP_TYPE(__t, __ttype)	((__t)->ts_type == (__ttype))
+#define IS_TDE_TS_NONE(_t)	(IS_TDE_TSTAMP_TYPE((_t), TSTAMP_NONE))
+#define IS_TDE_TS_LINE(_t)	(IS_TDE_TSTAMP_TYPE((_t), TSTAMP_LINE))
+#define IS_TDE_TS_BLK(_t)	(IS_TDE_TSTAMP_TYPE((_t), TSTAMP_BLK))
+
+enum timestamps {
+	TSTAMP_NONE = 0,
+	TSTAMP_LINE = 1,
+	TSTAMP_BLK = 2,
+};
+
+#define SHMTI_ID_INVALID	GENMASK(31, 0)
+
+struct telemetry_de {
+	enum timestamps ts_type;
+	bool enumerated;
+	void __iomem *base;
+	u32 sid;
+	u32 offset;
+	/* NOTE THAT DE data_sz is registered in scmi_telemetry_de */
+	u32 fc_size;
+	struct list_head item;
+	struct scmi_telemetry_de de;
+	/* Protect last_val/ts/magic accesses - MUST BE KEPT LAST */
+	struct mutex mtx;
+};
+
+#define to_tde(d)	container_of(d, struct telemetry_de, de)
+
 struct telemetry_info {
 	bool streaming_mode;
 	unsigned int num_shmti;
 	unsigned int num_des_tstamp;
 	unsigned int default_blk_ts_rate;
 	const struct scmi_protocol_handle *ph;
+	struct telemetry_de *tdes;
+	struct xarray xa_des;
+	/* Mutex to protect access to @free_des */
+	struct mutex free_mtx;
+	struct list_head free_des;
+	struct list_head fcs_des;
 	struct scmi_telemetry_info info;
+	atomic_t rinfo_initializing;
+	struct completion rinfo_initdone;
+	struct scmi_telemetry_res_info *rinfo;
+	struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti);
+};
+
+static struct scmi_telemetry_res_info *
+__scmi_telemetry_resources_get(struct telemetry_info *ti);
+
+static struct telemetry_de *
+scmi_telemetry_free_tde_get(struct telemetry_info *ti)
+{
+	struct telemetry_de *tde;
+
+	guard(mutex)(&ti->free_mtx);
+
+	tde = list_first_entry_or_null(&ti->free_des, struct telemetry_de, item);
+	if (!tde)
+		return tde;
+
+	list_del_init(&tde->item);
+
+	return tde;
+}
+
+static inline void scmi_telemetry_free_tdes_add(struct telemetry_info *ti,
+						struct telemetry_de *tde)
+{
+	guard(mutex)(&ti->free_mtx);
+	list_add_tail(&tde->item, &ti->free_des);
+}
+
+static void scmi_telemetry_free_tde_put(struct telemetry_info *ti,
+					struct telemetry_de *tde)
+{
+	struct scmi_telemetry_de_info *info;
+
+	lockdep_assert_held(&tde->mtx);
+
+	/* Unlink and unmap any valid DE/FC */
+	if (tde->de.fc_support) {
+		/*
+		 * During normal system operation this can be triggered only
+		 * once by the DE description iteraror while enumerating
+		 * resources, so the fcs_des list is not manipulated elsewhere
+		 * when we remove this TDE from that list.
+		 */
+		list_del_init(&tde->item);
+		if (tde->base)
+			iounmap(tde->base);
+	}
+
+	/* Save clear and restore */
+	info = tde->de.info;
+	memset(info, 0, sizeof(*info));
+	memset(tde, 0, offsetof(struct telemetry_de, mtx));
+	tde->de.info = info;
+
+	scmi_telemetry_free_tdes_add(ti, tde);
+}
+
+static struct telemetry_de *scmi_telemetry_tde_lookup(struct telemetry_info *ti,
+						      unsigned int de_id)
+{
+	struct scmi_telemetry_de *de;
+
+	de = xa_load(&ti->xa_des, de_id);
+	if (!de)
+		return NULL;
+
+	return to_tde(de);
+}
+
+static struct telemetry_de *scmi_telemetry_tde_get(struct telemetry_info *ti,
+						   unsigned int de_id)
+{
+	struct telemetry_de *tde;
+
+	tde = scmi_telemetry_free_tde_get(ti);
+	if (!tde) {
+		dev_err(ti->ph->dev, "Cannot get TDE for ID:0x%08X\n", de_id);
+		return ERR_PTR(-ENOSPC);
+	}
+
+	tde->sid = SHMTI_ID_INVALID;
+
+	return tde;
+}
+
+static int scmi_telemetry_tde_register(struct telemetry_info *ti,
+				       struct telemetry_de *tde)
+{
+	struct scmi_telemetry_res_info *rinfo;
+	int ret;
+
+	lockdep_assert_held(&tde->mtx);
+
+	/* Get rinfo without triggering a recursive enumeration */
+	rinfo = __scmi_telemetry_resources_get(ti);
+
+	if (rinfo->num_des >= ti->info.base.num_des) {
+		ret = -ENOSPC;
+		goto err;
+	}
+
+	/* Store DE pointer by de_id ... */
+	ret = xa_insert(&ti->xa_des, tde->de.info->id, &tde->de, GFP_KERNEL);
+	if (ret)
+		goto err;
+
+	/* Add to FastChannels list */
+	if (tde->de.fc_support)
+		list_add(&tde->item, &ti->fcs_des);
+
+	/* ... and in the general array */
+	rinfo->des[rinfo->num_des] = &tde->de;
+	/* Make sure the freshly registered DE is visible before the index update */
+	smp_store_release(&rinfo->num_des, rinfo->num_des + 1);
+
+	return 0;
+
+err:
+	dev_err(ti->ph->dev, "Cannot register TDE for ID:0x%08X - ret:%d\n",
+		tde->de.info->id, ret);
+
+	return ret;
+}
+
+struct scmi_tlm_de_priv {
+	struct telemetry_info *ti;
+	void *next;
+	size_t rx_len;
 };
 
 static int
@@ -100,6 +307,324 @@ scmi_telemetry_protocol_attributes_get(struct telemetry_info *ti)
 	return ret;
 }
 
+static void iter_tlm_prepare_message(void *message,
+				     unsigned int desc_index, const void *priv)
+{
+	put_unaligned_le32(desc_index, message);
+}
+
+static int iter_de_descr_update_state(struct scmi_iterator_state *st,
+				      const void *response, void *priv)
+{
+	const struct scmi_msg_resp_telemetry_de_description *r = response;
+	struct scmi_tlm_de_priv *p = priv;
+
+	st->num_returned = le32_get_bits(r->num_desc, GENMASK(15, 0));
+	st->num_remaining = le32_get_bits(r->num_desc, GENMASK(31, 16));
+
+	if (st->rx_len < (sizeof(*r) + sizeof(r->desc[0]) * st->num_returned))
+		return -EINVAL;
+
+	/* Initialized to first descriptor and initial payload size */
+	p->next = (void *)r->desc;
+	p->rx_len = st->rx_len - sizeof(*r);
+
+	return 0;
+}
+
+static inline size_t scmi_telemetry_tde_var_size_calc(struct telemetry_de *tde)
+{
+	size_t sz = 0;
+
+	sz += IS_TDE_TS_LINE(tde) ? sizeof(__le32) : 0;
+	sz += tde->de.fc_support ? sizeof(struct de_desc_fastchannel) : 0;
+	sz += tde->de.name_support ? SCMI_SHORT_NAME_MAX_SIZE : 0;
+
+	return sz;
+}
+
+static inline int
+scmi_telemetry_area_check(struct device *dev, const char *tag,
+			  u32 low, u32 high, u32 size,
+			  phys_addr_t *phys_addr, size_t *len)
+{
+	u64 addr = ((u64)high << 32) | low;
+	phys_addr_t pa = (phys_addr_t)addr;
+	phys_addr_t end_phys;
+
+	/* Does the returned pointer fits architetural size ? */
+	if ((u64)pa != addr) {
+		dev_err(dev, FW_BUG "%s phys_addr (%pa) pointer too big.\n",
+			tag, &pa);
+		return -EOVERFLOW;
+	}
+
+	if (!IS_ALIGNED(pa, 4)) {
+		dev_err(dev, FW_BUG "%s phys_addr (%pa) misaligned.\n",
+			tag, &pa);
+		return -EINVAL;
+	}
+
+	if (!IS_ALIGNED(size, 4)) {
+		dev_err(dev, FW_BUG "%s area length (%u) misaligned\n",
+			tag, size);
+		return -EINVAL;
+	}
+
+	if (check_add_overflow(pa, size, &end_phys)) {
+		dev_err(dev, FW_BUG "%s area (%pa/%u) wrap-around.\n",
+			tag, &pa, size);
+		return -EOVERFLOW;
+	}
+
+	*phys_addr = pa;
+	*len = size;
+
+	return 0;
+}
+
+static inline int
+scmi_telemetry_fc_size_check(struct telemetry_de *tde, size_t size)
+{
+	size_t fc_size, fc_base_sz = tde->de.info->data_sz;
+
+	if (fc_base_sz < 8 || !size)
+		return -EINVAL;
+
+	if (check_add_overflow(fc_base_sz,
+			       tde->de.tstamp_support ? sizeof(u32) * 2 : 0,
+			       &fc_size))
+		return -EOVERFLOW;
+
+	if (size < fc_size)
+		return -ENOSPC;
+
+	return 0;
+}
+
+static int scmi_telemetry_tde_fastchannel_setup(struct device *dev,
+						struct telemetry_de *tde, void **next)
+{
+	struct de_desc_fastchannel *dfc;
+	phys_addr_t phys_addr;
+	void __iomem *addr;
+	size_t size;
+	int ret;
+
+	dfc = *next;
+	ret = scmi_telemetry_area_check(dev, "FC",
+					le32_to_cpu(dfc->addr_low),
+					le32_to_cpu(dfc->addr_high),
+					le32_to_cpu(dfc->size),
+					&phys_addr, &size);
+	if (ret)
+		return ret;
+
+	/* Does it fit ? */
+	if (scmi_telemetry_fc_size_check(tde, size)) {
+		dev_err(dev, FW_BUG "TDE FC size inconsistent (%zu)\n", size);
+		return -EPROTO;
+	}
+
+	addr = ioremap(phys_addr, size);
+	if (!addr)
+		return -EADDRNOTAVAIL;
+
+	tde->base = addr;
+	tde->offset = 0;
+	tde->fc_size = size;
+
+	/* Variably sized depending on FC support */
+	*next += sizeof(*dfc);
+
+	return 0;
+}
+
+static int scmi_telemetry_de_descriptor_parse(struct telemetry_info *ti,
+					      struct telemetry_de *tde,
+					      void **next, size_t *rx_len)
+{
+	const struct scmi_de_desc *desc = *next;
+	struct device *dev = ti->ph->dev;
+	size_t payld_sz = sizeof(*desc);
+	int ret;
+
+	lockdep_assert_held(&tde->mtx);
+
+	if (*rx_len < payld_sz)
+		return -ENOSPC;
+
+	tde->de.info->id = le32_to_cpu(desc->de_id);
+	tde->de.info->grp_id = le32_to_cpu(desc->group_id);
+	tde->de.info->data_sz = le32_to_cpu(desc->de_data_sz);
+	tde->de.info->type = GET_DE_TYPE(desc);
+	tde->de.info->unit = GET_DE_UNIT(desc);
+	tde->de.info->unit_exp = GET_DE_UNIT_EXP(desc);
+	tde->de.info->instance_id = GET_DE_INSTA_ID(desc);
+	tde->de.info->compo_instance_id = GET_COMPO_INSTA_ID(desc);
+	tde->de.info->compo_type = GET_COMPO_TYPE(desc);
+	tde->de.info->persistent = IS_PERSISTENT(desc);
+	tde->ts_type = TSTAMP_SUPPORT(desc);
+	tde->de.tstamp_support = !!tde->ts_type;
+	tde->de.fc_support = IS_FC_SUPPORTED(desc);
+	tde->de.name_support = IS_NAME_SUPPORTED(desc);
+	/* Update DE_DESCRIPTOR size for the next iteration */
+	*next += sizeof(*desc);
+
+	payld_sz += scmi_telemetry_tde_var_size_calc(tde);
+	if (*rx_len < payld_sz)
+		return -ENOSPC;
+
+	if (IS_TDE_TS_LINE(tde)) {
+		__le32 *line_ts_rate = *next;
+
+		tde->de.info->ts_rate = le32_to_cpu((*line_ts_rate));
+
+		/* Variably sized depending on TS support */
+		*next += sizeof(*line_ts_rate);
+	} else if (IS_TDE_TS_BLK(tde)) {
+		/* Setup default BLK TS value at first */
+		tde->de.info->ts_rate = ti->default_blk_ts_rate;
+	} else if (!IS_TDE_TS_NONE(tde)) {
+		dev_warn(dev, "Unknown TDE timestamp type %u\n", tde->ts_type);
+		return -EPROTO;
+	}
+
+	if (tde->de.fc_support) {
+		ret = scmi_telemetry_tde_fastchannel_setup(dev, tde, next);
+		if (ret)
+			return ret;
+	}
+
+	if (tde->de.name_support) {
+		const char *de_name = *next;
+
+		strscpy(tde->de.info->name, de_name, SCMI_SHORT_NAME_MAX_SIZE);
+		/* Variably sized depending on name support */
+		*next += SCMI_SHORT_NAME_MAX_SIZE;
+	}
+
+	*rx_len -= payld_sz;
+
+	return 0;
+}
+
+static int iter_de_descr_process_response(const struct scmi_protocol_handle *ph,
+					  const void *response,
+					  struct scmi_iterator_state *st,
+					  void *priv)
+{
+	struct scmi_tlm_de_priv *p = priv;
+	struct telemetry_info *ti = p->ti;
+	const struct scmi_de_desc *desc = p->next;
+	struct telemetry_de *tde;
+	bool enumerated = false;
+	unsigned int de_id;
+	int ret;
+
+	/* Is at least reading de_id safe */
+	if (p->rx_len < sizeof(desc->de_id))
+		return -ENOSPC;
+
+	de_id = le32_to_cpu(desc->de_id);
+	/*
+	 * Check if this DE was already been discovered by other means,
+	 * such as during boot if Telemetry was detected as already enabled
+	 * and the list of enabled DEs was enumerated separately.
+	 */
+	tde = scmi_telemetry_tde_lookup(ti, de_id);
+	if (!tde) {
+		/* Create a new one */
+		tde = scmi_telemetry_tde_get(ti, de_id);
+		if (IS_ERR(tde))
+			return PTR_ERR(tde);
+
+		enumerated = true;
+	}
+
+	guard(mutex)(&tde->mtx);
+	if (tde->enumerated) {
+		/* Cannot be a duplicate of a DE already created by enumeration */
+		dev_err(ph->dev,
+			"Discovered INVALID DE with DUPLICATED ID:0x%08X\n",
+			de_id);
+		return -EINVAL;
+	}
+
+	ret = scmi_telemetry_de_descriptor_parse(ti, tde, &p->next, &p->rx_len);
+	if (ret) {
+		dev_err(ph->dev, "Malformed DE Descriptor - ret:%d\n", ret);
+		/* DE not enumerated at this point were created in this call */
+		if (enumerated)
+			scmi_telemetry_free_tde_put(ti, tde);
+		return ret;
+	}
+
+	/* Now fully enumerated */
+	tde->enumerated = true;
+
+	/* Register if it was not already ... */
+	if (enumerated) {
+		ret = scmi_telemetry_tde_register(ti, tde);
+		if (ret) {
+			/* DE not enumerated at this point were created in this call */
+			scmi_telemetry_free_tde_put(ti, tde);
+			return ret;
+		}
+	}
+
+	/* Count timestamped DEs */
+	ti->num_des_tstamp += !!tde->de.tstamp_support;
+
+	return 0;
+}
+
+static void scmi_telemetry_tdes_cleanup(struct telemetry_info *ti)
+{
+	struct scmi_telemetry_de *de;
+	unsigned long idx;
+
+	xa_for_each(&ti->xa_des, idx, de) {
+		struct telemetry_de *tde = to_tde(de);
+
+		guard(mutex)(&tde->mtx);
+
+		scmi_telemetry_free_tde_put(ti, tde);
+	}
+}
+
+static int scmi_telemetry_de_descriptors_get(struct telemetry_info *ti)
+{
+	const struct scmi_protocol_handle *ph = ti->ph;
+
+	struct scmi_iterator_ops ops = {
+		.prepare_message = iter_tlm_prepare_message,
+		.update_state = iter_de_descr_update_state,
+		.process_response = iter_de_descr_process_response,
+	};
+	struct scmi_tlm_de_priv tpriv = {
+		.ti = ti,
+		.next = NULL,
+	};
+	void *iter;
+	int ret;
+
+	if (!ti->info.base.num_des)
+		return 0;
+
+	iter = ph->hops->iter_response_init(ph, &ops, ti->info.base.num_des,
+					    TELEMETRY_DE_DESCRIPTION,
+					    sizeof(u32), &tpriv);
+	if (IS_ERR(iter))
+		return PTR_ERR(iter);
+
+	ret = ph->hops->iter_response_run(iter);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
 static const struct scmi_telemetry_info *
 scmi_telemetry_info_get(const struct scmi_protocol_handle *ph)
 {
@@ -108,10 +633,232 @@ scmi_telemetry_info_get(const struct scmi_protocol_handle *ph)
 	return &ti->info;
 }
 
+static const struct scmi_telemetry_res_info *
+scmi_telemetry_resources_get(const struct scmi_protocol_handle *ph)
+{
+	struct scmi_telemetry_res_info *(*res_get)(struct telemetry_info *ti);
+	struct telemetry_info *ti = ph->get_priv(ph);
+
+	/* Ensure the latest installed res_get helper is visible */
+	res_get = smp_load_acquire(&ti->res_get);
+
+	return res_get(ti);
+}
+
+static const struct scmi_telemetry_de *
+scmi_telemetry_de_lookup(const struct scmi_protocol_handle *ph, u32 id)
+{
+	struct telemetry_info *ti = ph->get_priv(ph);
+
+	/* Trigger a full proper enumeration */
+	scmi_telemetry_resources_get(ph);
+
+	return xa_load(&ti->xa_des, id);
+}
+
 static const struct scmi_telemetry_proto_ops tlm_proto_ops = {
 	.info_get = scmi_telemetry_info_get,
+	.de_lookup = scmi_telemetry_de_lookup,
+	.res_get = scmi_telemetry_resources_get,
 };
 
+/**
+ * scmi_telemetry_resources_alloc  - Resources allocation
+ * @ti: A reference to the telemetry info descriptor for this instance
+ *
+ * This allocates and initializes dedicated resources for the maximum possible
+ * number of needed telemetry resources, based on information gathered from
+ * the initial enumeration: these allocations represent an upper bound on
+ * the number of discoverable telemetry resources and they will be later
+ * populated during late deferred further discovery phases.
+ *
+ * Note that some of the allocations down below are designed to keep some
+ * class of data packed together since it eases retrieval from the ABI layer.
+ *
+ * Return: 0 on Success, errno otherwise
+ */
+static int scmi_telemetry_resources_alloc(struct telemetry_info *ti)
+{
+	/* Array to hold pointers to discovered DEs */
+	struct scmi_telemetry_de **des __free(kfree) =
+		kzalloc_objs(*des, ti->info.base.num_des);
+	if (!des)
+		return -ENOMEM;
+
+	/* The allocated DE descriptors */
+	struct telemetry_de *tdes __free(kfree) =
+		kzalloc_objs(*tdes, ti->info.base.num_des);
+	if (!tdes)
+		return -ENOMEM;
+
+	/* Allocate a set of contiguous DE info descriptors. */
+	struct scmi_telemetry_de_info *dei_store __free(kfree) =
+		kzalloc_objs(*dei_store, ti->info.base.num_des);
+	if (!dei_store)
+		return -ENOMEM;
+
+	struct scmi_telemetry_res_info *rinfo __free(kfree) =
+		kzalloc_obj(*rinfo);
+	if (!rinfo)
+		return -ENOMEM;
+
+	mutex_init(&ti->free_mtx);
+	INIT_LIST_HEAD(&ti->free_des);
+	for (int i = 0; i < ti->info.base.num_des; i++) {
+		mutex_init(&tdes[i].mtx);
+		/* Bind contiguous DE info structures */
+		tdes[i].de.info = &dei_store[i];
+		scmi_telemetry_free_tdes_add(ti, &tdes[i]);
+	}
+
+	INIT_LIST_HEAD(&ti->fcs_des);
+
+	ti->tdes = no_free_ptr(tdes);
+	xa_init(&ti->xa_des);
+
+	rinfo->des = no_free_ptr(des);
+	rinfo->dei_store = no_free_ptr(dei_store);
+
+	/* Ensure all of the above assignments are visible */
+	smp_store_release(&ti->rinfo, no_free_ptr(rinfo));
+
+	return 0;
+}
+
+static struct scmi_telemetry_res_info *
+__scmi_telemetry_resources_get(struct telemetry_info *ti)
+{
+	/* Ensure rinfo descriptor is visible */
+	return smp_load_acquire(&ti->rinfo);
+}
+
+static void scmi_telemetry_resources_free(struct telemetry_info *ti)
+{
+	struct scmi_telemetry_res_info *rinfo;
+
+	/* Get rinfo without triggering a recursive enumeration, cannot be NULL */
+	rinfo = __scmi_telemetry_resources_get(ti);
+	/* Ensure rinfo is no more accessible upfront */
+	smp_store_release(&ti->rinfo, NULL);
+
+	scmi_telemetry_tdes_cleanup(ti);
+	xa_destroy(&ti->xa_des);
+
+	kfree(ti->tdes);
+
+	kfree(rinfo->des);
+	kfree(rinfo->dei_store);
+
+	kfree(rinfo);
+
+	dev_dbg(ti->ph->dev, "SCMI Telemetry resources freed for instance\n");
+}
+
+static void scmi_telemetry_instance_cleanup(void *arg)
+{
+	struct telemetry_info *ti = arg;
+
+	scmi_telemetry_resources_free(ti);
+}
+
+/**
+ * scmi_telemetry_resources_enumerate  - Enumeration helper
+ * @ti: A reference to the telemetry info descriptor for this instance
+ *
+ * This helper is configured to be called once on the first enumeration
+ * attempt, when triggered by invoking ti->res_get() from somewhere else.
+ *
+ * Once run it substitues itself in ti->res_get() with the simple accessor
+ * __scmi_telemetry_resources_get, which returns a descriptor to the resources
+ * that were possibly discovered.
+ *
+ * Note that, while it attempts to fully enumerate Data Events, it does NOT
+ * fail when such enumerations fail, instead it simply gives up with the end
+ * result that only a partially populated, but consistent, resources descriptor
+ * will be returned; in such a case the incomplete descriptor will be marked as
+ * NOT fully_enumerated: this design enables the kernel to deal with badly
+ * implemented out-of-spec firmware support while keep on providing a minimal
+ * sane, albeit possibly incomplete, set of telemetry respources.
+ *
+ * Return: A reference to a fully or partially populated resources descriptor
+ */
+static struct scmi_telemetry_res_info *
+scmi_telemetry_resources_enumerate(struct telemetry_info *ti)
+{
+	struct scmi_telemetry_res_info *rinfo;
+	struct device *dev = ti->ph->dev;
+	int ret;
+
+	/*
+	 * Ensure the following initialization can be called only once
+	 * from one thread of execution.
+	 */
+	if (atomic_cmpxchg(&ti->rinfo_initializing, 0, 1)) {
+		/*
+		 * When initialization is already ongoing in another thread,
+		 * just wait for its completion and return the fully or partially
+		 * populated rinfo.
+		 */
+		if (!completion_done(&ti->rinfo_initdone))
+			wait_for_completion(&ti->rinfo_initdone);
+
+		/* Ensure rinfo descriptor is visible */
+		return smp_load_acquire(&ti->rinfo);
+	}
+
+	/* Note that this code below can be run only once by one thread */
+	ret = scmi_telemetry_de_descriptors_get(ti);
+	if (ret) {
+		dev_err(dev, FW_BUG "Cannot fully enumerate DEs resources. Degraded system.\n");
+		goto done;
+	}
+
+	/* Get rinfo without triggering a recursive enumeration */
+	rinfo = __scmi_telemetry_resources_get(ti);
+	rinfo->fully_enumerated = rinfo->num_des == ti->info.base.num_des;
+done:
+	/* Disable initialization permanently */
+	smp_store_release(&ti->res_get, __scmi_telemetry_resources_get);
+
+	/* Unblock concurrent threads that have been stalled */
+	complete_all(&ti->rinfo_initdone);
+
+	/* Ensure local rinfo is visible before returning it */
+	smp_mb();
+	return ti->rinfo;
+}
+
+/**
+ * scmi_telemetry_instance_init  - Instance initializer
+ * @ti: A reference to the telemetry info descriptor for this instance
+ *
+ * Note that this allocates and initialize all the resources possibly needed
+ * and then setups the @scmi_telemetry_resources_enumerate helper as the
+ * default method for the first call to ti->res_get(): this mechanism enables
+ * the possibility of optionally implementing deferred enumeration policies
+ * which optionally delay the discovery phase and related SCMI message exchanges
+ * to a later point in time.
+ *
+ * Return: 0 on Success, errno otherwise
+ */
+static int scmi_telemetry_instance_init(struct telemetry_info *ti)
+{
+	int ret;
+
+	ret = scmi_telemetry_resources_alloc(ti);
+	if (ret)
+		return ret;
+
+	/* Setup resources lazy initialization */
+	atomic_set(&ti->rinfo_initializing, 0);
+	init_completion(&ti->rinfo_initdone);
+	/* Ensure the new res_get() operation is visible after this point */
+	smp_store_mb(ti->res_get, scmi_telemetry_resources_enumerate);
+
+	return devm_add_action_or_reset(ti->ph->dev,
+					scmi_telemetry_instance_cleanup, ti);
+}
+
 static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
 {
 	struct device *dev = ph->dev;
@@ -133,6 +880,12 @@ static int scmi_telemetry_protocol_init(const struct scmi_protocol_handle *ph)
 		return ret;
 	}
 
+	ret = scmi_telemetry_instance_init(ti);
+	if (ret) {
+		dev_err(dev, "Cannot initialize instance. Abort.\n");
+		return ret;
+	}
+
 	ti->info.base.version = ph->version;
 
 	return ph->set_priv(ph, ti);
diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h
index f076ce5601cf..cc5bd87705ed 100644
--- a/include/linux/scmi_protocol.h
+++ b/include/linux/scmi_protocol.h
@@ -830,6 +830,37 @@ struct scmi_pinctrl_proto_ops {
 	int (*pin_free)(const struct scmi_protocol_handle *ph, u32 pin);
 };
 
+struct scmi_telemetry_de_info {
+	unsigned int id;
+	unsigned int grp_id;
+	unsigned int data_sz;
+	unsigned int type;
+	unsigned int unit;
+	int unit_exp;
+	unsigned int ts_rate;
+	unsigned int instance_id;
+	unsigned int compo_instance_id;
+	unsigned int compo_type;
+	bool persistent;
+	char name[16];
+};
+
+struct scmi_telemetry_de {
+	bool tstamp_support;
+	bool fc_support;
+	bool name_support;
+	bool enabled;
+	bool tstamp_enabled;
+	struct scmi_telemetry_de_info *info;
+};
+
+struct scmi_telemetry_res_info {
+	bool fully_enumerated;
+	unsigned int num_des;
+	struct scmi_telemetry_de **des;
+	struct scmi_telemetry_de_info *dei_store;
+};
+
 struct scmi_telemetry_base_info {
 	unsigned int version;
 	uuid_t primary_revision;
@@ -853,10 +884,16 @@ struct scmi_telemetry_info {
  *	by SCMI Telemetry Protocol
  *
  * @info_get: get the general Telemetry information.
+ * @de_lookup: get a specific DE descriptor from the DE id.
+ * @res_get: get a reference to the Telemetry resources descriptor.
  */
 struct scmi_telemetry_proto_ops {
 	const struct scmi_telemetry_info *(*info_get)
 		(const struct scmi_protocol_handle *ph);
+	const struct scmi_telemetry_de __must_check *(*de_lookup)
+		(const struct scmi_protocol_handle *ph, u32 id);
+	const struct scmi_telemetry_res_info *(*res_get)
+		(const struct scmi_protocol_handle *ph);
 };
 
 /**
-- 
2.54.0


  parent reply	other threads:[~2026-11-24 19:50 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 19:48 [PATCH v17 00/34] Introduce SCMI Telemetry support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 01/34] firmware: arm_scmi: Harden iterators state parsing Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 02/34] [RFC] firmware: arm_scmi: Additional iterators consistency checks Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 03/34] firmware: arm_scmi: Make Clock attributes size deterministic Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 04/34] firmware: arm_scmi: Add centralized detection of truncated replies Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 05/34] firmware: arm_scmi: Introduce a dedicated SCMI Errors header Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 06/34] firmware: arm_scmi: Add new SCMIv4.0 error codes definitions Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 07/34] firmware: arm_scmi: Allow registration of unknown-size events/reports Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 08/34] firmware: arm_scmi: Refactor __scmi_event_handler_get_ops helper Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 09/34] dt-bindings: firmware: arm,scmi: Add support for telemetry protocol Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 10/34] include: trace: Add Telemetry trace events Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 11/34] firmware: arm_scmi: Add Telemetry basic attributes enumeration Cristian Marussi
2026-11-24 19:48 ` Cristian Marussi [this message]
2026-11-24 19:48 ` [PATCH v17 13/34] firmware: arm_scmi: Add Telemetry global update intervals enumeration Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 14/34] firmware: arm_scmi: Add Telemetry SHMTI areas enumeration Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 15/34] firmware: arm_scmi: Add Telemetry TDCF areas parsing support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 16/34] firmware: arm_scmi: Add Telemetry TDCF parsing for BLK_TS lines Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 17/34] firmware: arm_scmi: Add Telemetry TDCF parsing for UUID lines Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 18/34] firmware: arm_scmi: Add Telemetry DEs configuration support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 19/34] firmware: arm_scmi: Add Telemetry global " Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 20/34] firmware: arm_scmi: Add Telemetry Single/Bulk DEs reads Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 21/34] firmware: arm_scmi: Add Telemetry boot-on support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 22/34] firmware: arm_scmi: Invoke proper protocol cleanup on failure Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 23/34] firmware: arm_scmi: Refactor protocol allocation Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 24/34] firmware: arm_scmi: Add common per-protocol debugfs support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 25/34] firmware: arm_scmi: Add Telemetry debugfs SHMTI dump support Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 26/34] firmware: arm_scmi: Add Telemetry debugfs ABI documentation Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 27/34] firmware: arm_scmi: Expose per-instance identifier Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 28/34] uapi: Add ARM SCMI Telemetry definitions Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 29/34] firmware: arm_scmi: Add System Telemetry driver Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 30/34] firmware: arm_scmi: Enable autoloading for " Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 31/34] docs: ioctl-number: Add SCMI Ioctls Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 32/34] [RFC] Documentation: Add SCMI System Telemetry documentation Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 33/34] kselftest/arm64: Add SCMI Telemetry UAPI compliance testcases Cristian Marussi
2026-11-24 19:48 ` [PATCH v17 34/34] [RFC] tools/testing/scmi: Add SCMI Telemetry testing tool Cristian Marussi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124194841.1147072-13-cristian.marussi@arm.com \
    --to=cristian.marussi@arm.com \
    --cc=arm-scmi@vger.kernel.org \
    --cc=d-gole@ti.com \
    --cc=david@kernel.org \
    --cc=elif.topuz@arm.com \
    --cc=etienne.carriere@st.com \
    --cc=f.fainelli@gmail.com \
    --cc=james.quinlan@broadcom.com \
    --cc=jonathan.cameron@oss.qualcomm.com \
    --cc=kas@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=leitao@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=michal.simek@amd.com \
    --cc=peng.fan@oss.nxp.com \
    --cc=philip.radford@arm.com \
    --cc=puranjay@kernel.org \
    --cc=souvik.chakravarty@arm.com \
    --cc=sudeep.holla@kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=vincent.guittot@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®