From nobody Fri Jan 2 13:28:54 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BBC9CD6E5C for ; Wed, 11 Oct 2023 11:15:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346090AbjJKLPp (ORCPT ); Wed, 11 Oct 2023 07:15:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231488AbjJKLPh (ORCPT ); Wed, 11 Oct 2023 07:15:37 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD68594; Wed, 11 Oct 2023 04:15:35 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id ECA831C0007; Wed, 11 Oct 2023 11:15:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1697022934; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+Y+0xrcG+PHrgnJuqiWYG+NeCnOC11MVS2SUmdfnPks=; b=dt+RV3+j8VA5R7V6p6P1Z+IwGvlAp9Qr9PXI1Um1/UhyJCxMYvc21rIYN7POHOWSO51PB1 ZgjXNy6glpcuVcONmFqLF5d2oXHelkLJF0m9KSSkrEYApZhVrvq1l/cJl66EpLWNsG0M51 mVeYQGJ4tnZLMShdG4ttffYgziBSfX3Z3cvaLAjzrObI+q1CPNK6IunGaRBfyTLTX7v0Gq uOUJzTYfcSrPMHJDHaVQ/ObXJbvExZ1ON5r3Pbe/fk9Wl/F6e7hJhjFUevxgi+lCdH+AeT Fc46hNXgDFkQgaPVObuKEYywiQobuPDV8yd7Xv1+zQ+QIgi+8CfwsbPijcbbYQ== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal , Rob Herring Subject: [PATCH v13 1/6] of: device: Export of_device_make_bus_id() Date: Wed, 11 Oct 2023 13:15:24 +0200 Message-Id: <20231011111529.86440-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231011111529.86440-1-miquel.raynal@bootlin.com> References: <20231011111529.86440-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-GND-Sasl: miquel.raynal@bootlin.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This helper is really handy to create unique device names based on their device tree path, we may need it outside of the OF core (in the NVMEM subsystem) so let's export it. As this helper has nothing patform specific, let's move it to of/device.c instead of of/platform.c so we can add its prototype to of_device.h. Signed-off-by: Miquel Raynal Acked-by: Rob Herring --- drivers/of/device.c | 41 +++++++++++++++++++++++++++++++++++++++ drivers/of/platform.c | 40 -------------------------------------- include/linux/of_device.h | 6 ++++++ 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/drivers/of/device.c b/drivers/of/device.c index 1ca42ad9dd15..6e9572c4af83 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -304,3 +304,44 @@ int of_device_uevent_modalias(const struct device *dev= , struct kobj_uevent_env * return 0; } EXPORT_SYMBOL_GPL(of_device_uevent_modalias); + +/** + * of_device_make_bus_id - Use the device node data to assign a unique name + * @dev: pointer to device structure that is linked to a device tree node + * + * This routine will first try using the translated bus address to + * derive a unique name. If it cannot, then it will prepend names from + * parent nodes until a unique name can be derived. + */ +void of_device_make_bus_id(struct device *dev) +{ + struct device_node *node =3D dev->of_node; + const __be32 *reg; + u64 addr; + u32 mask; + + /* Construct the name, using parent nodes if necessary to ensure uniquene= ss */ + while (node->parent) { + /* + * If the address can be translated, then that is as much + * uniqueness as we need. Make it the first component and return + */ + reg =3D of_get_property(node, "reg", NULL); + if (reg && (addr =3D of_translate_address(node, reg)) !=3D OF_BAD_ADDR) { + if (!of_property_read_u32(node, "mask", &mask)) + dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn", + addr, ffs(mask) - 1, node, dev_name(dev)); + + else + dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn", + addr, node, dev_name(dev)); + return; + } + + /* format arguments only used if dev_name() resolves to NULL */ + dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s", + kbasename(node->full_name), dev_name(dev)); + node =3D node->parent; + } +} +EXPORT_SYMBOL_GPL(of_device_make_bus_id); diff --git a/drivers/of/platform.c b/drivers/of/platform.c index f235ab55b91e..be32e28c6f55 100644 --- a/drivers/of/platform.c +++ b/drivers/of/platform.c @@ -97,46 +97,6 @@ static const struct of_device_id of_skipped_node_table[]= =3D { * mechanism for creating devices from device tree nodes. */ =20 -/** - * of_device_make_bus_id - Use the device node data to assign a unique name - * @dev: pointer to device structure that is linked to a device tree node - * - * This routine will first try using the translated bus address to - * derive a unique name. If it cannot, then it will prepend names from - * parent nodes until a unique name can be derived. - */ -static void of_device_make_bus_id(struct device *dev) -{ - struct device_node *node =3D dev->of_node; - const __be32 *reg; - u64 addr; - u32 mask; - - /* Construct the name, using parent nodes if necessary to ensure uniquene= ss */ - while (node->parent) { - /* - * If the address can be translated, then that is as much - * uniqueness as we need. Make it the first component and return - */ - reg =3D of_get_property(node, "reg", NULL); - if (reg && (addr =3D of_translate_address(node, reg)) !=3D OF_BAD_ADDR) { - if (!of_property_read_u32(node, "mask", &mask)) - dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn", - addr, ffs(mask) - 1, node, dev_name(dev)); - - else - dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn", - addr, node, dev_name(dev)); - return; - } - - /* format arguments only used if dev_name() resolves to NULL */ - dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s", - kbasename(node->full_name), dev_name(dev)); - node =3D node->parent; - } -} - /** * of_device_alloc - Allocate and initialize an of_device * @np: device node to assign to device diff --git a/include/linux/of_device.h b/include/linux/of_device.h index 2c7a3d4bc775..a72661e47faa 100644 --- a/include/linux/of_device.h +++ b/include/linux/of_device.h @@ -40,6 +40,9 @@ static inline int of_dma_configure(struct device *dev, { return of_dma_configure_id(dev, np, force_dma, NULL); } + +void of_device_make_bus_id(struct device *dev); + #else /* CONFIG_OF */ =20 static inline int of_driver_match_device(struct device *dev, @@ -82,6 +85,9 @@ static inline int of_dma_configure(struct device *dev, { return 0; } + +static inline void of_device_make_bus_id(struct device *dev) {} + #endif /* CONFIG_OF */ =20 #endif /* _LINUX_OF_DEVICE_H */ --=20 2.34.1 From nobody Fri Jan 2 13:28:54 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF611CD6E5C for ; Wed, 11 Oct 2023 11:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346111AbjJKLPu (ORCPT ); Wed, 11 Oct 2023 07:15:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345945AbjJKLPj (ORCPT ); Wed, 11 Oct 2023 07:15:39 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89F5AA7; Wed, 11 Oct 2023 04:15:37 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id AFF7B1C000D; Wed, 11 Oct 2023 11:15:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1697022936; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EWJphzkvsBfSMHYq9wGyiA3n29gzaJOgdTbtajkF/ws=; b=ETQ80/3NCigpiqIOmCoT2N5EM4fH22WgGKFhuBHealajufHyq8NiTpCSG+3rneaoSou6u/ UsxTMwHQTGzZZRs2RTY9lmw4WBBZHmUU5P+z4DFNRW4E2+RSaLGchATp2wN0WuL/CGJE1P qK9Dev0yi10dT/3cWpnLqPui1r7FRbuj/YS+pyFWdB1slsTiAtb0Egl5X0C7IXQ/Sj1jOl UCfY1kVzHcsDCjakgyPqVhBGzP7VUnU8mBl7ll/uYU099eIGzghKzh6vs2B5ER3z/zueGT Ta/Y2ILDKQaEvRplm9B/IVeMQPoxA9uKmG8LxJ+3Qdb2gpWr2YoqrFd3FrSBjQ== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v13 2/6] nvmem: Move of_nvmem_layout_get_container() in another header Date: Wed, 11 Oct 2023 13:15:25 +0200 Message-Id: <20231011111529.86440-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231011111529.86440-1-miquel.raynal@bootlin.com> References: <20231011111529.86440-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-GND-Sasl: miquel.raynal@bootlin.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" nvmem-consumer.h is included by consumer devices, extracting data from NVMEM devices whereas nvmem-provider.h is included by devices providing NVMEM content. The only users of of_nvmem_layout_get_container() outside of the core are layout drivers, so better move its prototype to nvmem-provider.h. While we do so, we also move the kdoc associated with the function to the header rather than the .c file. Signed-off-by: Miquel Raynal --- drivers/nvmem/core.c | 8 -------- include/linux/nvmem-consumer.h | 7 ------- include/linux/nvmem-provider.h | 14 ++++++++++++++ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index eaf6a3fe8ca6..93b867b3cdf9 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -841,14 +841,6 @@ static int nvmem_add_cells_from_layout(struct nvmem_de= vice *nvmem) } =20 #if IS_ENABLED(CONFIG_OF) -/** - * of_nvmem_layout_get_container() - Get OF node to layout container. - * - * @nvmem: nvmem device. - * - * Return: a node pointer with refcount incremented or NULL if no - * container exists. Use of_node_put() on it when done. - */ struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvm= em) { return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout"); diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h index 4523e4e83319..960728b10a11 100644 --- a/include/linux/nvmem-consumer.h +++ b/include/linux/nvmem-consumer.h @@ -241,7 +241,6 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node= *np, const char *id); struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *name); -struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvm= em); #else static inline struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) @@ -254,12 +253,6 @@ static inline struct nvmem_device *of_nvmem_device_get= (struct device_node *np, { return ERR_PTR(-EOPNOTSUPP); } - -static inline struct device_node * -of_nvmem_layout_get_container(struct nvmem_device *nvmem) -{ - return NULL; -} #endif /* CONFIG_NVMEM && CONFIG_OF */ =20 #endif /* ifndef _LINUX_NVMEM_CONSUMER_H */ diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index dae26295e6be..2905f9e6fc2a 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -205,6 +205,16 @@ void nvmem_layout_unregister(struct nvmem_layout *layo= ut); const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem, struct nvmem_layout *layout); =20 +/** + * of_nvmem_layout_get_container() - Get OF node of layout container + * + * @nvmem: nvmem device + * + * Return: a node pointer with refcount incremented or NULL if no + * container exists. Use of_node_put() on it when done. + */ +struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvm= em); + #else =20 static inline struct nvmem_device *nvmem_register(const struct nvmem_confi= g *c) @@ -242,6 +252,10 @@ nvmem_layout_get_match_data(struct nvmem_device *nvmem, return NULL; } =20 +static inline struct device_node *of_nvmem_layout_get_container(struct nvm= em_device *nvmem) +{ + return NULL; +} #endif /* CONFIG_NVMEM */ =20 #define module_nvmem_layout_driver(__layout_driver) \ --=20 2.34.1 From nobody Fri Jan 2 13:28:54 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5ED7DCD6E5C for ; Wed, 11 Oct 2023 11:15:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346115AbjJKLPy (ORCPT ); Wed, 11 Oct 2023 07:15:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346001AbjJKLPl (ORCPT ); Wed, 11 Oct 2023 07:15:41 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 448D3B0; Wed, 11 Oct 2023 04:15:39 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 504501C0009; Wed, 11 Oct 2023 11:15:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1697022937; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ABL44vf3YRZd8ThfS0o/cypOR6A9WAryG1kFlUlNHwg=; b=kyhGNm1F8nuxS7Zr8Q77a5h9yC/7l65P1c6oc0e/bT5h/8Bm3dURuRda+2jAJtNPmj7HfQ 1PB9R7OmOy+Cgd8ypNLIbUoE5EkQgxgt1nseMAXqyoeFMnbJFFrEyrZrdL10yrLxxvbs1A wfwk7qdU7cq85YATvheGmPkx58nrAY7NUPMTusf/PkmesslmUSdF5DnfHg0er/wN4ljbQV 1FAwoPJAIBtmDraMuGADamr6CqzXkpZOixcWtClN0BrYvJuifYn6wAx1i2Dw5FJJgQiqJF kO/7quAn4Z0EmjJi1ilBTLE8wuebW3qiChRIqkPKu6XAqK7IZUoq6hA2VTVkSw== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v13 3/6] nvmem: Create a header for internal sharing Date: Wed, 11 Oct 2023 13:15:26 +0200 Message-Id: <20231011111529.86440-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231011111529.86440-1-miquel.raynal@bootlin.com> References: <20231011111529.86440-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-GND-Sasl: miquel.raynal@bootlin.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Before adding all the NVMEM layout bus infrastructure to the core, let's move the main nvmem_device structure in an internal header, only available to the core. This way all the additional code can be added in a dedicated file in order to keep the current core file tidy. Signed-off-by: Miquel Raynal --- drivers/nvmem/core.c | 24 +----------------------- drivers/nvmem/internals.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 drivers/nvmem/internals.h diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 93b867b3cdf9..eefb5d0a0c91 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -19,29 +19,7 @@ #include #include =20 -struct nvmem_device { - struct module *owner; - struct device dev; - int stride; - int word_size; - int id; - struct kref refcnt; - size_t size; - bool read_only; - bool root_only; - int flags; - enum nvmem_type type; - struct bin_attribute eeprom; - struct device *base_dev; - struct list_head cells; - const struct nvmem_keepout *keepout; - unsigned int nkeepout; - nvmem_reg_read_t reg_read; - nvmem_reg_write_t reg_write; - struct gpio_desc *wp_gpio; - struct nvmem_layout *layout; - void *priv; -}; +#include "internals.h" =20 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev) =20 diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h new file mode 100644 index 000000000000..ce353831cd65 --- /dev/null +++ b/drivers/nvmem/internals.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_NVMEM_INTERNALS_H +#define _LINUX_NVMEM_INTERNALS_H + +#include +#include +#include + +struct nvmem_device { + struct module *owner; + struct device dev; + struct list_head node; + int stride; + int word_size; + int id; + struct kref refcnt; + size_t size; + bool read_only; + bool root_only; + int flags; + enum nvmem_type type; + struct bin_attribute eeprom; + struct device *base_dev; + struct list_head cells; + const struct nvmem_keepout *keepout; + unsigned int nkeepout; + nvmem_reg_read_t reg_read; + nvmem_reg_write_t reg_write; + struct gpio_desc *wp_gpio; + struct nvmem_layout *layout; + void *priv; +}; + +#endif /* ifndef _LINUX_NVMEM_INTERNALS_H */ --=20 2.34.1 From nobody Fri Jan 2 13:28:54 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DCF1CD6E5C for ; Wed, 11 Oct 2023 11:16:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234759AbjJKLQD (ORCPT ); Wed, 11 Oct 2023 07:16:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346088AbjJKLPo (ORCPT ); Wed, 11 Oct 2023 07:15:44 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFBEEB7; Wed, 11 Oct 2023 04:15:40 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id ED0071C0004; Wed, 11 Oct 2023 11:15:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1697022939; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RSbvxbDMMYFFRCDdTNGY0bgts9xTEH0NDvyF7jV2Grw=; b=XJZlfH6YMWHjbcvFYxif0uHMtPdtMUS0AF4E1mb0Z3N/o7Aa+yaE47FtP66L3STyLn0ehL LqxtzYXTqlGcDgtJWYESbqLzGfA1oyELiaJTEfXIn2xw1t9dHu0Q8i6gYzzx7gBqyT34e7 eneowz1bPRYpM6TPvzKJ7mBwKjk4tkT4M6hARkYFz1yInCeB5SP47mJJDincRrroSmJGFx fpjrIFmqyEXhXsQgyT3rSIPJ1uOfXHsvMsy9HdufVG/5iCMKkEOKVJfPbqaJwpCgYYpUFB NAUiD4hpxp3wDXrv5qH8C5UJL9FluPJju4g3ON9ln29MLOZO8ldUlVq3OInKKw== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v13 4/6] nvmem: core: Rework layouts to become regular devices Date: Wed, 11 Oct 2023 13:15:27 +0200 Message-Id: <20231011111529.86440-5-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231011111529.86440-1-miquel.raynal@bootlin.com> References: <20231011111529.86440-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-GND-Sasl: miquel.raynal@bootlin.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Current layout support was initially written without modules support in mind. When the requirement for module support rose, the existing base was improved to adopt modularization support, but kind of a design flaw was introduced. With the existing implementation, when a storage device registers into NVMEM, the core tries to hook a layout (if any) and populates its cells immediately. This means, if the hardware description expects a layout to be hooked up, but no driver was provided for that, the storage medium will fail to probe and try later from scratch. Even if we consider that the hardware description shall be correct, we could still probe the storage device (especially if it contains the rootfs). One way to overcome this situation is to consider the layouts as devices, and leverage the existing notifier mechanism. When a new NVMEM device is registered, we can: - populate its nvmem-layout child, if any - try to modprobe the relevant driver, if relevant - try to hook the NVMEM device with a layout in the notifier And when a new layout is registered: - try to hook all the existing NVMEM devices which are not yet hooked to a layout with the new layout This way, there is no strong order to enforce, any NVMEM device creation or NVMEM layout driver insertion will be observed as a new event which may lead to the creation of additional cells, without disturbing the probes with costly (and sometimes endless) deferrals. In order to achieve that goal we need: * To keep track of all nvmem devices * To create a new bus for the nvmem-layouts with minimal logic to match nvmem-layout devices with nvmem-layout drivers. All this infrastructure code is created in the layouts.c file. Signed-off-by: Miquel Raynal Tested-by: Rafa=C5=82 Mi=C5=82ecki --- drivers/nvmem/Makefile | 2 +- drivers/nvmem/core.c | 130 ++++-------------- drivers/nvmem/internals.h | 21 +++ drivers/nvmem/layouts.c | 228 +++++++++++++++++++++++++++++++ drivers/nvmem/layouts/onie-tlv.c | 23 +++- drivers/nvmem/layouts/sl28vpd.c | 23 +++- include/linux/nvmem-provider.h | 34 ++--- 7 files changed, 335 insertions(+), 126 deletions(-) create mode 100644 drivers/nvmem/layouts.c diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index 423baf089515..77be96076ea6 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -4,7 +4,7 @@ # =20 obj-$(CONFIG_NVMEM) +=3D nvmem_core.o -nvmem_core-y :=3D core.o +nvmem_core-y :=3D core.o layouts.o obj-y +=3D layouts/ =20 # Devices diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index eefb5d0a0c91..0e364b8e9f99 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -55,9 +55,6 @@ static LIST_HEAD(nvmem_lookup_list); =20 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); =20 -static DEFINE_SPINLOCK(nvmem_layout_lock); -static LIST_HEAD(nvmem_layouts); - static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offse= t, void *val, size_t bytes) { @@ -741,91 +738,22 @@ static int nvmem_add_cells_from_fixed_layout(struct n= vmem_device *nvmem) return err; } =20 -int __nvmem_layout_register(struct nvmem_layout *layout, struct module *ow= ner) +int nvmem_layout_register(struct nvmem_layout *layout) { - layout->owner =3D owner; + if (!layout->add_cells) + return -EINVAL; =20 - spin_lock(&nvmem_layout_lock); - list_add(&layout->node, &nvmem_layouts); - spin_unlock(&nvmem_layout_lock); - - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_ADD, layout); - - return 0; + /* Populate the cells */ + return layout->add_cells(&layout->nvmem->dev, layout->nvmem, layout); } -EXPORT_SYMBOL_GPL(__nvmem_layout_register); +EXPORT_SYMBOL_GPL(nvmem_layout_register); =20 void nvmem_layout_unregister(struct nvmem_layout *layout) { - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_REMOVE, layout= ); - - spin_lock(&nvmem_layout_lock); - list_del(&layout->node); - spin_unlock(&nvmem_layout_lock); + /* Keep the API even with an empty stub in case we need it later */ } EXPORT_SYMBOL_GPL(nvmem_layout_unregister); =20 -static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem) -{ - struct device_node *layout_np; - struct nvmem_layout *l, *layout =3D ERR_PTR(-EPROBE_DEFER); - - layout_np =3D of_nvmem_layout_get_container(nvmem); - if (!layout_np) - return NULL; - - /* - * In case the nvmem device was built-in while the layout was built as a - * module, we shall manually request the layout driver loading otherwise - * we'll never have any match. - */ - of_request_module(layout_np); - - spin_lock(&nvmem_layout_lock); - - list_for_each_entry(l, &nvmem_layouts, node) { - if (of_match_node(l->of_match_table, layout_np)) { - if (try_module_get(l->owner)) - layout =3D l; - - break; - } - } - - spin_unlock(&nvmem_layout_lock); - of_node_put(layout_np); - - return layout; -} - -static void nvmem_layout_put(struct nvmem_layout *layout) -{ - if (layout) - module_put(layout->owner); -} - -static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem) -{ - struct nvmem_layout *layout =3D nvmem->layout; - int ret; - - if (layout && layout->add_cells) { - ret =3D layout->add_cells(&nvmem->dev, nvmem, layout); - if (ret) - return ret; - } - - return 0; -} - -#if IS_ENABLED(CONFIG_OF) -struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvm= em) -{ - return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout"); -} -EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container); -#endif - const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem, struct nvmem_layout *layout) { @@ -833,7 +761,7 @@ const void *nvmem_layout_get_match_data(struct nvmem_de= vice *nvmem, const struct of_device_id *match; =20 layout_np =3D of_nvmem_layout_get_container(nvmem); - match =3D of_match_node(layout->of_match_table, layout_np); + match =3D of_match_node(layout->dev.driver->of_match_table, layout_np); =20 return match ? match->data : NULL; } @@ -944,19 +872,6 @@ struct nvmem_device *nvmem_register(const struct nvmem= _config *config) goto err_put_device; } =20 - /* - * If the driver supplied a layout by config->layout, the module - * pointer will be NULL and nvmem_layout_put() will be a noop. - */ - nvmem->layout =3D config->layout ?: nvmem_layout_get(nvmem); - if (IS_ERR(nvmem->layout)) { - rval =3D PTR_ERR(nvmem->layout); - nvmem->layout =3D NULL; - - if (rval =3D=3D -EPROBE_DEFER) - goto err_teardown_compat; - } - if (config->cells) { rval =3D nvmem_add_cells(nvmem, config->cells, config->ncells); if (rval) @@ -975,7 +890,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_= config *config) if (rval) goto err_remove_cells; =20 - rval =3D nvmem_add_cells_from_layout(nvmem); + rval =3D nvmem_populate_layout(nvmem); if (rval) goto err_remove_cells; =20 @@ -983,16 +898,17 @@ struct nvmem_device *nvmem_register(const struct nvme= m_config *config) =20 rval =3D device_add(&nvmem->dev); if (rval) - goto err_remove_cells; + goto err_destroy_layout; + =20 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); =20 return nvmem; =20 +err_destroy_layout: + nvmem_destroy_layout(nvmem); err_remove_cells: nvmem_device_remove_all_cells(nvmem); - nvmem_layout_put(nvmem->layout); -err_teardown_compat: if (config->compat) nvmem_sysfs_remove_compat(nvmem, config); err_put_device: @@ -1014,7 +930,7 @@ static void nvmem_device_release(struct kref *kref) device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); =20 nvmem_device_remove_all_cells(nvmem); - nvmem_layout_put(nvmem->layout); + nvmem_destroy_layout(nvmem); device_unregister(&nvmem->dev); } =20 @@ -1400,7 +1316,10 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_n= ode *np, const char *id) of_node_put(cell_np); if (!cell_entry) { __nvmem_device_put(nvmem); - return ERR_PTR(-ENOENT); + if (nvmem->layout) + return ERR_PTR(-EAGAIN); + else + return ERR_PTR(-ENOENT); } =20 cell =3D nvmem_create_cell(cell_entry, id, cell_index); @@ -2096,11 +2015,22 @@ EXPORT_SYMBOL_GPL(nvmem_dev_name); =20 static int __init nvmem_init(void) { - return bus_register(&nvmem_bus_type); + int ret; + + ret =3D bus_register(&nvmem_bus_type); + if (ret) + return ret; + + ret =3D nvmem_layout_bus_register(); + if (ret) + bus_unregister(&nvmem_bus_type); + + return ret; } =20 static void __exit nvmem_exit(void) { + nvmem_layout_bus_unregister(); bus_unregister(&nvmem_bus_type); } =20 diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index ce353831cd65..c669c96e9052 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -32,4 +32,25 @@ struct nvmem_device { void *priv; }; =20 +#if IS_ENABLED(CONFIG_OF) +int nvmem_layout_bus_register(void); +void nvmem_layout_bus_unregister(void); +int nvmem_populate_layout(struct nvmem_device *nvmem); +void nvmem_destroy_layout(struct nvmem_device *nvmem); +#else /* CONFIG_OF */ +static inline int nvmem_layout_bus_register(void) +{ + return 0; +} + +static inline void nvmem_layout_bus_unregister(void) {} + +static inline int nvmem_populate_layout(struct nvmem_device *nvmem) +{ + return 0; +} + +static inline void nvmem_destroy_layout(struct nvmem_device *nvmem) { } +#endif /* CONFIG_OF */ + #endif /* ifndef _LINUX_NVMEM_INTERNALS_H */ diff --git a/drivers/nvmem/layouts.c b/drivers/nvmem/layouts.c new file mode 100644 index 000000000000..8c73a8a15dd5 --- /dev/null +++ b/drivers/nvmem/layouts.c @@ -0,0 +1,228 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * NVMEM layout bus handling + * + * Copyright (C) 2023 Bootlin + * Author: Miquel Raynal +#include +#include +#include +#include +#include +#include + +#include "internals.h" + +#if IS_ENABLED(CONFIG_OF) +#define to_nvmem_layout_driver(drv) \ + (container_of((drv), struct nvmem_layout_driver, driver)) +#define to_nvmem_layout_device(_dev) \ + container_of((_dev), struct nvmem_layout, dev) + +static int nvmem_layout_bus_match(struct device *dev, struct device_driver= *drv) +{ + return of_driver_match_device(dev, drv); +} + +static int nvmem_layout_bus_probe(struct device *dev) +{ + struct nvmem_layout_driver *drv =3D to_nvmem_layout_driver(dev->driver); + struct nvmem_layout *layout =3D to_nvmem_layout_device(dev); + + if (!drv->probe || !drv->remove) + return -EINVAL; + + return drv->probe(layout); +} + +static void nvmem_layout_bus_remove(struct device *dev) +{ + struct nvmem_layout_driver *drv =3D to_nvmem_layout_driver(dev->driver); + struct nvmem_layout *layout =3D to_nvmem_layout_device(dev); + + return drv->remove(layout); +} + +static struct bus_type nvmem_layout_bus_type =3D { + .name =3D "nvmem-layout", + .match =3D nvmem_layout_bus_match, + .probe =3D nvmem_layout_bus_probe, + .remove =3D nvmem_layout_bus_remove, +}; + +static struct device nvmem_layout_bus =3D { + .init_name =3D "nvmem-layout", +}; + +int nvmem_layout_driver_register(struct nvmem_layout_driver *drv) +{ + drv->driver.bus =3D &nvmem_layout_bus_type; + + return driver_register(&drv->driver); +} +EXPORT_SYMBOL_GPL(nvmem_layout_driver_register); + +void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv) +{ + driver_unregister(&drv->driver); +} +EXPORT_SYMBOL_GPL(nvmem_layout_driver_unregister); + +static void nvmem_layout_release_device(struct device *dev) +{ + struct nvmem_layout *layout =3D to_nvmem_layout_device(dev); + + of_node_put(layout->dev.of_node); + kfree(layout); +} + +static int nvmem_layout_create_device(struct nvmem_device *nvmem, + struct device_node *np) +{ + struct nvmem_layout *layout; + struct device *dev; + int ret; + + layout =3D kzalloc(sizeof(*dev), GFP_KERNEL); + if (!layout) + return -ENOMEM; + + /* Create a bidirectional link */ + layout->nvmem =3D nvmem; + nvmem->layout =3D layout; + + /* Device model registration */ + dev =3D &layout->dev; + device_initialize(dev); + dev->parent =3D &nvmem_layout_bus; + dev->bus =3D &nvmem_layout_bus_type; + dev->release =3D nvmem_layout_release_device; + dev->coherent_dma_mask =3D DMA_BIT_MASK(32); + dev->dma_mask =3D &dev->coherent_dma_mask; + device_set_node(dev, of_fwnode_handle(of_node_get(np))); + of_device_make_bus_id(dev); + of_msi_configure(dev, dev->of_node); + + ret =3D device_add(dev); + if (ret) { + put_device(dev); + return ret; + } + + return 0; +} + +static const struct of_device_id of_nvmem_layout_skip_table[] =3D { + { .compatible =3D "fixed-layout", }, + {} +}; + +static int nvmem_layout_bus_populate(struct nvmem_device *nvmem, + struct device_node *layout_dn) +{ + int ret; + + /* Make sure it has a compatible property */ + if (!of_get_property(layout_dn, "compatible", NULL)) { + pr_debug("%s() - skipping %pOF, no compatible prop\n", + __func__, layout_dn); + return 0; + } + + /* Fixed layouts are parsed manually somewhere else for now */ + if (of_match_node(of_nvmem_layout_skip_table, layout_dn)) { + pr_debug("%s() - skipping %pOF node\n", __func__, layout_dn); + return 0; + } + + if (of_node_check_flag(layout_dn, OF_POPULATED_BUS)) { + pr_debug("%s() - skipping %pOF, already populated\n", + __func__, layout_dn); + + return 0; + } + + /* NVMEM layout buses expect only a single device representing the layout= */ + ret =3D nvmem_layout_create_device(nvmem, layout_dn); + if (ret) + return ret; + + of_node_set_flag(layout_dn, OF_POPULATED_BUS); + + return 0; +} + +struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvm= em) +{ + return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout"); +} +EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container); + +/* + * Returns the number of devices populated, 0 if the operation was not rel= evant + * for this nvmem device, an error code otherwise. + */ +int nvmem_populate_layout(struct nvmem_device *nvmem) +{ + struct device_node *nvmem_dn, *layout_dn; + int ret; + + layout_dn =3D of_nvmem_layout_get_container(nvmem); + if (!layout_dn) + return 0; + + nvmem_dn =3D of_node_get(nvmem->dev.of_node); + if (!nvmem_dn) { + of_node_put(layout_dn); + return 0; + } + + /* Ensure the layout driver is loaded */ + of_request_module(layout_dn); + + /* Populate the layout device */ + device_links_supplier_sync_state_pause(); + ret =3D nvmem_layout_bus_populate(nvmem, layout_dn); + device_links_supplier_sync_state_resume(); + + of_node_put(nvmem_dn); + of_node_put(layout_dn); + return ret; +} + +void nvmem_destroy_layout(struct nvmem_device *nvmem) +{ + struct device *dev =3D &nvmem->layout->dev; + + of_node_clear_flag(dev->of_node, OF_POPULATED_BUS); + put_device(dev); +} + +int nvmem_layout_bus_register(void) +{ + int ret; + + ret =3D device_register(&nvmem_layout_bus); + if (ret) { + put_device(&nvmem_layout_bus); + return ret; + } + + ret =3D bus_register(&nvmem_layout_bus_type); + if (ret) { + device_unregister(&nvmem_layout_bus); + return ret; + } + + return 0; +} + +void nvmem_layout_bus_unregister(void) +{ + bus_unregister(&nvmem_layout_bus_type); + device_unregister(&nvmem_layout_bus); +} +#endif diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-= tlv.c index 59fc87ccfcff..8d19346b9206 100644 --- a/drivers/nvmem/layouts/onie-tlv.c +++ b/drivers/nvmem/layouts/onie-tlv.c @@ -226,16 +226,31 @@ static int onie_tlv_parse_table(struct device *dev, s= truct nvmem_device *nvmem, return 0; } =20 +static int onie_tlv_probe(struct nvmem_layout *layout) +{ + layout->add_cells =3D onie_tlv_parse_table; + + return nvmem_layout_register(layout); +} + +static void onie_tlv_remove(struct nvmem_layout *layout) +{ + nvmem_layout_unregister(layout); +} + static const struct of_device_id onie_tlv_of_match_table[] =3D { { .compatible =3D "onie,tlv-layout", }, {}, }; MODULE_DEVICE_TABLE(of, onie_tlv_of_match_table); =20 -static struct nvmem_layout onie_tlv_layout =3D { - .name =3D "ONIE tlv layout", - .of_match_table =3D onie_tlv_of_match_table, - .add_cells =3D onie_tlv_parse_table, +static struct nvmem_layout_driver onie_tlv_layout =3D { + .driver =3D { + .name =3D "onie-tlv-layout", + .of_match_table =3D onie_tlv_of_match_table, + }, + .probe =3D onie_tlv_probe, + .remove =3D onie_tlv_remove, }; module_nvmem_layout_driver(onie_tlv_layout); =20 diff --git a/drivers/nvmem/layouts/sl28vpd.c b/drivers/nvmem/layouts/sl28vp= d.c index 05671371f631..ab4ceaf1ea16 100644 --- a/drivers/nvmem/layouts/sl28vpd.c +++ b/drivers/nvmem/layouts/sl28vpd.c @@ -135,16 +135,31 @@ static int sl28vpd_add_cells(struct device *dev, stru= ct nvmem_device *nvmem, return 0; } =20 +static int sl28vpd_probe(struct nvmem_layout *layout) +{ + layout->add_cells =3D sl28vpd_add_cells; + + return nvmem_layout_register(layout); +} + +static void sl28vpd_remove(struct nvmem_layout *layout) +{ + nvmem_layout_unregister(layout); +} + static const struct of_device_id sl28vpd_of_match_table[] =3D { { .compatible =3D "kontron,sl28-vpd" }, {}, }; MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table); =20 -static struct nvmem_layout sl28vpd_layout =3D { - .name =3D "sl28-vpd", - .of_match_table =3D sl28vpd_of_match_table, - .add_cells =3D sl28vpd_add_cells, +static struct nvmem_layout_driver sl28vpd_layout =3D { + .driver =3D { + .name =3D "kontron-sl28vpd-layout", + .of_match_table =3D sl28vpd_of_match_table, + }, + .probe =3D sl28vpd_probe, + .remove =3D sl28vpd_remove, }; module_nvmem_layout_driver(sl28vpd_layout); =20 diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index 2905f9e6fc2a..a0ea8326605a 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -9,6 +9,7 @@ #ifndef _LINUX_NVMEM_PROVIDER_H #define _LINUX_NVMEM_PROVIDER_H =20 +#include #include #include #include @@ -154,15 +155,13 @@ struct nvmem_cell_table { /** * struct nvmem_layout - NVMEM layout definitions * - * @name: Layout name. - * @of_match_table: Open firmware match table. + * @dev: Device-model layout device. + * @nvmem: The underlying NVMEM device * @add_cells: Will be called if a nvmem device is found which * has this layout. The function will add layout * specific cells with nvmem_add_one_cell(). * @fixup_cell_info: Will be called before a cell is added. Can be * used to modify the nvmem_cell_info. - * @owner: Pointer to struct module. - * @node: List node. * * A nvmem device can hold a well defined structure which can just be * evaluated during runtime. For example a TLV list, or a list of "name=3D= val" @@ -170,17 +169,19 @@ struct nvmem_cell_table { * cells. */ struct nvmem_layout { - const char *name; - const struct of_device_id *of_match_table; + struct device dev; + struct nvmem_device *nvmem; int (*add_cells)(struct device *dev, struct nvmem_device *nvmem, struct nvmem_layout *layout); void (*fixup_cell_info)(struct nvmem_device *nvmem, struct nvmem_layout *layout, struct nvmem_cell_info *cell); +}; =20 - /* private */ - struct module *owner; - struct list_head node; +struct nvmem_layout_driver { + struct device_driver driver; + int (*probe)(struct nvmem_layout *layout); + void (*remove)(struct nvmem_layout *layout); }; =20 #if IS_ENABLED(CONFIG_NVMEM) @@ -197,11 +198,15 @@ void nvmem_del_cell_table(struct nvmem_cell_table *ta= ble); int nvmem_add_one_cell(struct nvmem_device *nvmem, const struct nvmem_cell_info *info); =20 -int __nvmem_layout_register(struct nvmem_layout *layout, struct module *ow= ner); -#define nvmem_layout_register(layout) \ - __nvmem_layout_register(layout, THIS_MODULE) +int nvmem_layout_register(struct nvmem_layout *layout); void nvmem_layout_unregister(struct nvmem_layout *layout); =20 +int nvmem_layout_driver_register(struct nvmem_layout_driver *drv); +void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv); +#define module_nvmem_layout_driver(__nvmem_layout_driver) \ + module_driver(__nvmem_layout_driver, nvmem_layout_driver_register, \ + nvmem_layout_driver_unregister) + const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem, struct nvmem_layout *layout); =20 @@ -257,9 +262,4 @@ static inline struct device_node *of_nvmem_layout_get_c= ontainer(struct nvmem_dev return NULL; } #endif /* CONFIG_NVMEM */ - -#define module_nvmem_layout_driver(__layout_driver) \ - module_driver(__layout_driver, nvmem_layout_register, \ - nvmem_layout_unregister) - #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */ --=20 2.34.1 From nobody Fri Jan 2 13:28:54 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3A37CD6E5A for ; Wed, 11 Oct 2023 11:16:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346088AbjJKLQH (ORCPT ); Wed, 11 Oct 2023 07:16:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346103AbjJKLPr (ORCPT ); Wed, 11 Oct 2023 07:15:47 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 665C08F; Wed, 11 Oct 2023 04:15:42 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 8F29D1C000B; Wed, 11 Oct 2023 11:15:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1697022941; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dfOp8t/SWXoAMbVxsmdWr6p/Yay0CylcPPBrARczmbM=; b=eAMEWS0UvEF+PWeZcW0Aftj0Q7o6JvZDzUvY0H0Lche1LfRCfSm6wnPPazx8zuQEvTGNEA zhiFGAkPi9i35Sj4YnqJWsbtglUzWfInLIHRuOTitmw8o/X51WqGu7T+Ai6+2zsTzJVZs7 aeFXvHbVqpnaUeivEs49VJ04dSqoMj8OTF+nq6CQopK+bhlGsWDif+kk+sqxjAJU7jHx2Q Z/6BVuExVsS3LqGZAXKzhgDTf6uceQNdjK1HzgT9pUTZN7tVGr2LtXhI/m2ARO10COYZ7r tEYFUNYfSezzViihEw5nwz11LcY6AtVbhXyWpErLJKWrZZaLJS9E5MCwI8Zdaw== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v13 5/6] ABI: sysfs-nvmem-cells: Expose cells through sysfs Date: Wed, 11 Oct 2023 13:15:28 +0200 Message-Id: <20231011111529.86440-6-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231011111529.86440-1-miquel.raynal@bootlin.com> References: <20231011111529.86440-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-GND-Sasl: miquel.raynal@bootlin.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The binary content of nvmem devices is available to the user so in the easiest cases, finding the content of a cell is rather easy as it is just a matter of looking at a known and fixed offset. However, nvmem layouts have been recently introduced to cope with more advanced situations, where the offset and size of the cells is not known in advance or is dynamic. When using layouts, more advanced parsers are used by the kernel in order to give direct access to the content of each cell regardless of their position/size in the underlying device, but these information were not accessible to the user. By exposing the nvmem cells to the user through a dedicated cell/ folder containing one file per cell, we provide a straightforward access to useful user information without the need for re-writing a userland parser. Content of nvmem cells is usually: product names, manufacturing date, MAC addresses, etc, Signed-off-by: Miquel Raynal Reviewed-by: Greg Kroah-Hartman --- Documentation/ABI/testing/sysfs-nvmem-cells | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-nvmem-cells diff --git a/Documentation/ABI/testing/sysfs-nvmem-cells b/Documentation/AB= I/testing/sysfs-nvmem-cells new file mode 100644 index 000000000000..7af70adf3690 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-nvmem-cells @@ -0,0 +1,21 @@ +What: /sys/bus/nvmem/devices/.../cells/ +Date: May 2023 +KernelVersion: 6.5 +Contact: Miquel Raynal +Description: + The "cells" folder contains one file per cell exposed by the + NVMEM device. The name of the file is: @, with + being the cell name and its location in the NVMEM + device, in hexadecimal (without the '0x' prefix, to mimic device + tree node names). The length of the file is the size of the cell + (when known). The content of the file is the binary content of + the cell (may sometimes be ASCII, likely without trailing + character). + Note: This file is only present if CONFIG_NVMEM_SYSFS + is enabled. + + Example:: + + hexdump -C /sys/bus/nvmem/devices/1-00563/cells/product-name@d + 00000000 54 4e 34 38 4d 2d 50 2d 44 4e |TN48M-P-DN| + 0000000a --=20 2.34.1 From nobody Fri Jan 2 13:28:54 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD3B6CD6E5C for ; Wed, 11 Oct 2023 11:16:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346151AbjJKLQN (ORCPT ); Wed, 11 Oct 2023 07:16:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346135AbjJKLPs (ORCPT ); Wed, 11 Oct 2023 07:15:48 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2802C4; Wed, 11 Oct 2023 04:15:43 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 2AFFC1C0008; Wed, 11 Oct 2023 11:15:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1697022942; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bLpDZ6DqHe9vNyFoMoM9HUw3srf7WmB+iNd7C5Xup7Y=; b=NqMvVlMXueXD9v9LtYSB+NGqHwYkbPwBM4flnWeGBVHswvuuG1PdX14dhy5t3/V9wlM3nr UKKURONGWAwRDmhU9bERMwZ3ioHmhlr2WKZzXwalv7JC8UHpIkZ5rD9kJrxU6jTJxJc3sf 7MmYQDiVgtldHwk8ItFFqJ0y1LJyZqaQxHFRTqW/SZphb1Ea6ulb1KjjOFr7RN2pO/gkz2 2XNux9dei8eICKnj/ko0CWpnWghERDtoJlc020UlNSeCt1DyM8MXn1sCouZX01inbTmez4 Cxw2vFIId4Aiql7uCrUgx2+e5QCCkVnndPpXC1TxOUg9QV82cQKa7spjfjR67w== From: Miquel Raynal To: Srinivas Kandagatla , Greg Kroah-Hartman Cc: Michael Walle , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , Rob Herring , Frank Rowand , devicetree@vger.kernel.org, , Robert Marko , Thomas Petazzoni , Luka Perkov , Randy Dunlap , Chen-Yu Tsai , Daniel Golle , Miquel Raynal Subject: [PATCH v13 6/6] nvmem: core: Expose cells through sysfs Date: Wed, 11 Oct 2023 13:15:29 +0200 Message-Id: <20231011111529.86440-7-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231011111529.86440-1-miquel.raynal@bootlin.com> References: <20231011111529.86440-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-GND-Sasl: miquel.raynal@bootlin.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The binary content of nvmem devices is available to the user so in the easiest cases, finding the content of a cell is rather easy as it is just a matter of looking at a known and fixed offset. However, nvmem layouts have been recently introduced to cope with more advanced situations, where the offset and size of the cells is not known in advance or is dynamic. When using layouts, more advanced parsers are used by the kernel in order to give direct access to the content of each cell, regardless of its position/size in the underlying device. Unfortunately, these information are not accessible by users, unless by fully re-implementing the parser logic in userland. Let's expose the cells and their content through sysfs to avoid these situations. Of course the relevant NVMEM sysfs Kconfig option must be enabled for this support to be available. Not all nvmem devices expose cells. Indeed, the .bin_attrs attribute group member will be filled at runtime only when relevant and will remain empty otherwise. In this case, as the cells attribute group will be empty, it will not lead to any additional folder/file creation. Exposed cells are read-only. There is, in practice, everything in the core to support a write path, but as I don't see any need for that, I prefer to keep the interface simple (and probably safer). The interface is documented as being in the "testing" state which means we can later add a write attribute if though relevant. Signed-off-by: Miquel Raynal Tested-by: Rafa=C5=82 Mi=C5=82ecki Tested-by: Chen-Yu Tsai --- drivers/nvmem/core.c | 134 +++++++++++++++++++++++++++++++++++++- drivers/nvmem/internals.h | 1 + 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 0e364b8e9f99..f0e6d8a16380 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -299,6 +299,43 @@ static umode_t nvmem_bin_attr_is_visible(struct kobjec= t *kobj, return nvmem_bin_attr_get_umode(nvmem); } =20 +static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, + const char *id, int index); + +static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kob= j, + struct bin_attribute *attr, char *buf, + loff_t pos, size_t count) +{ + struct nvmem_cell_entry *entry; + struct nvmem_cell *cell =3D NULL; + size_t cell_sz, read_len; + void *content; + + entry =3D attr->private; + cell =3D nvmem_create_cell(entry, entry->name, 0); + if (IS_ERR(cell)) + return PTR_ERR(cell); + + if (!cell) + return -EINVAL; + + content =3D nvmem_cell_read(cell, &cell_sz); + if (IS_ERR(content)) { + read_len =3D PTR_ERR(content); + goto destroy_cell; + } + + read_len =3D min_t(unsigned int, cell_sz - pos, count); + memcpy(buf, content + pos, read_len); + kfree(content); + +destroy_cell: + kfree_const(cell->id); + kfree(cell); + + return read_len; +} + /* default read/write permissions */ static struct bin_attribute bin_attr_rw_nvmem =3D { .attr =3D { @@ -320,11 +357,21 @@ static const struct attribute_group nvmem_bin_group = =3D { .is_bin_visible =3D nvmem_bin_attr_is_visible, }; =20 +/* Cell attributes will be dynamically allocated */ +static struct attribute_group nvmem_cells_group =3D { + .name =3D "cells", +}; + static const struct attribute_group *nvmem_dev_groups[] =3D { &nvmem_bin_group, NULL, }; =20 +static const struct attribute_group *nvmem_cells_groups[] =3D { + &nvmem_cells_group, + NULL, +}; + static struct bin_attribute bin_attr_nvmem_eeprom_compat =3D { .attr =3D { .name =3D "eeprom", @@ -380,6 +427,68 @@ static void nvmem_sysfs_remove_compat(struct nvmem_dev= ice *nvmem, device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); } =20 +static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem) +{ + struct bin_attribute **cells_attrs, *attrs; + struct nvmem_cell_entry *entry; + unsigned int ncells =3D 0, i =3D 0; + int ret =3D 0; + + mutex_lock(&nvmem_mutex); + + if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated) { + nvmem_cells_group.bin_attrs =3D NULL; + goto unlock_mutex; + } + + /* Allocate an array of attributes with a sentinel */ + ncells =3D list_count_nodes(&nvmem->cells); + cells_attrs =3D devm_kcalloc(&nvmem->dev, ncells + 1, + sizeof(struct bin_attribute *), GFP_KERNEL); + if (!cells_attrs) { + ret =3D -ENOMEM; + goto unlock_mutex; + } + + attrs =3D devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute),= GFP_KERNEL); + if (!attrs) { + ret =3D -ENOMEM; + goto unlock_mutex; + } + + /* Initialize each attribute to take the name and size of the cell */ + list_for_each_entry(entry, &nvmem->cells, node) { + sysfs_bin_attr_init(&attrs[i]); + attrs[i].attr.name =3D devm_kasprintf(&nvmem->dev, GFP_KERNEL, + "%s@%x", entry->name, + entry->offset); + attrs[i].attr.mode =3D 0444; + attrs[i].size =3D entry->bytes; + attrs[i].read =3D &nvmem_cell_attr_read; + attrs[i].private =3D entry; + if (!attrs[i].attr.name) { + ret =3D -ENOMEM; + goto unlock_mutex; + } + + cells_attrs[i] =3D &attrs[i]; + i++; + } + + nvmem_cells_group.bin_attrs =3D cells_attrs; + + ret =3D devm_device_add_groups(&nvmem->dev, nvmem_cells_groups); + if (ret) + goto unlock_mutex; + + nvmem->sysfs_cells_populated =3D true; + +unlock_mutex: + mutex_unlock(&nvmem_mutex); + + return ret; +} + #else /* CONFIG_NVMEM_SYSFS */ =20 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, @@ -740,11 +849,25 @@ static int nvmem_add_cells_from_fixed_layout(struct n= vmem_device *nvmem) =20 int nvmem_layout_register(struct nvmem_layout *layout) { + int ret; + if (!layout->add_cells) return -EINVAL; =20 /* Populate the cells */ - return layout->add_cells(&layout->nvmem->dev, layout->nvmem, layout); + ret =3D layout->add_cells(&layout->nvmem->dev, layout->nvmem, layout); + if (ret) + return ret; + +#ifdef CONFIG_NVMEM_SYSFS + ret =3D nvmem_populate_sysfs_cells(layout->nvmem); + if (ret) { + nvmem_device_remove_all_cells(layout->nvmem); + return ret; + } +#endif + + return 0; } EXPORT_SYMBOL_GPL(nvmem_layout_register); =20 @@ -900,11 +1023,20 @@ struct nvmem_device *nvmem_register(const struct nvm= em_config *config) if (rval) goto err_destroy_layout; =20 +#ifdef CONFIG_NVMEM_SYSFS + rval =3D nvmem_populate_sysfs_cells(nvmem); + if (rval) + goto err_remove_dev; +#endif =20 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); =20 return nvmem; =20 +#ifdef CONFIG_NVMEM_SYSFS +err_remove_dev: + device_del(&nvmem->dev); +#endif err_destroy_layout: nvmem_destroy_layout(nvmem); err_remove_cells: diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index c669c96e9052..88ee2b8aea8e 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -30,6 +30,7 @@ struct nvmem_device { struct gpio_desc *wp_gpio; struct nvmem_layout *layout; void *priv; + bool sysfs_cells_populated; }; =20 #if IS_ENABLED(CONFIG_OF) --=20 2.34.1