From nobody Wed Dec 17 07:10:52 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 189361F540C for ; Wed, 26 Mar 2025 15:37:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743003443; cv=none; b=ZEQl/HUT2fbGcnYS4SNm/bYJ1wjNWWJi1RbrMYWYC5T7RCTua68DF6nHG0atSYx4XPD/7FjhvbJ9SbTOOeC0vZD42MoEw4faQVO505/+BPscsWD6QK+gKiEXcTwixV0K1MrRMsYbnVtRsbguLRtsxurt/NByY6/Sxr8cBrzWF/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743003443; c=relaxed/simple; bh=l70KLgYLIyv4aSEcSxo3W5vkTl81a0q5W5XdNZ8aaR8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eADti2mpXnc7AYSdLNla/hJAEmdGquY2oWXbAsSOfw1k3sWj+RJQ3F7fWXXQ6b9WXZGkhO4H8oNzDjz7S3SgMF6deDLPPuG3ghuY4HIlqxJQvkdRAFQ2ytS56oi3974+A+2+Q3cEzljHsS8syyzzZdK0ILkkB6Flf0QCmTAt5x4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=iEQVy4g8; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="iEQVy4g8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1743003441; x=1774539441; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=l70KLgYLIyv4aSEcSxo3W5vkTl81a0q5W5XdNZ8aaR8=; b=iEQVy4g8fs0cAte1FSXhgedk6ajQ8/3ujH/z6A5fdBZbqAMW9ycS63yb bKDO9rl717uYJ2WVTEqTtnCBZTku6JDhzXeIohPpWmdt/opThEH6I4U9L JJq3Wt659wrn2SHNnSYJBBoy9JqBuG9QIozqlqC9orSW2Ul4GVWBTJBe2 aFWopC+oLBX6gUp9hzzevupDKzuxGkAQlmps00BVbPdhNA5sY5FL0IdFI 3MRQr2pyWJfWgT35B5HsszpNlqEUc7k007dRyh01oMhQ15SFf7ZQbzwrv otB8LqdO29MCEwikNKbB6iVZLjgsJBp0xnEIOB7Yt0dP+9JREqwRuCl2O g==; X-CSE-ConnectionGUID: i6/DOlYYTkeRvAObgSBX8w== X-CSE-MsgGUID: VFgQzsOITIGBJV1212qlHQ== X-IronPort-AV: E=McAfee;i="6700,10204,11385"; a="55665432" X-IronPort-AV: E=Sophos;i="6.14,278,1736841600"; d="scan'208";a="55665432" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Mar 2025 08:37:21 -0700 X-CSE-ConnectionGUID: hNUVRTa+T1ylH3jMudsTHg== X-CSE-MsgGUID: CaLVDnYeTVeJxfgBjeODrw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.14,278,1736841600"; d="scan'208";a="129923408" Received: from sannilnx-dsk.jer.intel.com ([10.12.231.107]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Mar 2025 08:37:15 -0700 From: Alexander Usyskin To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Lucas De Marchi , =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , Rodrigo Vivi , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Jani Nikula , Joonas Lahtinen , Tvrtko Ursulin , Karthik Poosa Cc: Reuven Abliyev , Oren Weil , linux-mtd@lists.infradead.org, dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, Alexander Usyskin Subject: [PATCH v7 01/12] mtd: core: always create master device Date: Wed, 26 Mar 2025 17:26:12 +0200 Message-ID: <20250326152623.3897204-2-alexander.usyskin@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250326152623.3897204-1-alexander.usyskin@intel.com> References: <20250326152623.3897204-1-alexander.usyskin@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Create master device without partition when CONFIG_MTD_PARTITIONED_MASTER flag is unset. This streamlines device tree and allows to anchor runtime power management on master device in all cases. Signed-off-by: Alexander Usyskin --- drivers/mtd/mtdcore.c | 141 +++++++++++++++++++++++++++++------------- drivers/mtd/mtdcore.h | 2 +- drivers/mtd/mtdpart.c | 17 ++--- 3 files changed, 110 insertions(+), 50 deletions(-) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 724f917f91ba..d0e7fb027eb6 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -68,7 +68,13 @@ static struct class mtd_class =3D { .pm =3D MTD_CLS_PM_OPS, }; =20 +static struct class mtd_master_class =3D { + .name =3D "mtd_master", + .pm =3D MTD_CLS_PM_OPS, +}; + static DEFINE_IDR(mtd_idr); +static DEFINE_IDR(mtd_master_idr); =20 /* These are exported solely for the purpose of mtd_blkdevs.c. You should not use them for _anything_ else */ @@ -83,8 +89,9 @@ EXPORT_SYMBOL_GPL(__mtd_next_device); =20 static LIST_HEAD(mtd_notifiers); =20 - +#define MTD_MASTER_DEVS 255 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) +static dev_t mtd_master_devt; =20 /* REVISIT once MTD uses the driver model better, whoever allocates * the mtd_info will probably want to use the release() hook... @@ -104,6 +111,17 @@ static void mtd_release(struct device *dev) device_destroy(&mtd_class, index + 1); } =20 +static void mtd_master_release(struct device *dev) +{ + struct mtd_info *mtd =3D dev_get_drvdata(dev); + + idr_remove(&mtd_master_idr, mtd->index); + of_node_put(mtd_get_of_node(mtd)); + + if (mtd_is_partition(mtd)) + release_mtd_partition(mtd); +} + static void mtd_device_release(struct kref *kref) { struct mtd_info *mtd =3D container_of(kref, struct mtd_info, refcnt); @@ -367,6 +385,11 @@ static const struct device_type mtd_devtype =3D { .release =3D mtd_release, }; =20 +static const struct device_type mtd_master_devtype =3D { + .name =3D "mtd_master", + .release =3D mtd_master_release, +}; + static bool mtd_expert_analysis_mode; =20 #ifdef CONFIG_DEBUG_FS @@ -634,13 +657,13 @@ static void mtd_check_of_node(struct mtd_info *mtd) /** * add_mtd_device - register an MTD device * @mtd: pointer to new MTD device info structure + * @partitioned: create partitioned device * * Add a device to the list of MTD devices present in the system, and * notify each currently active MTD 'user' of its arrival. Returns * zero on success or non-zero on failure. */ - -int add_mtd_device(struct mtd_info *mtd) +int add_mtd_device(struct mtd_info *mtd, bool partitioned) { struct device_node *np =3D mtd_get_of_node(mtd); struct mtd_info *master =3D mtd_get_master(mtd); @@ -687,10 +710,17 @@ int add_mtd_device(struct mtd_info *mtd) ofidx =3D -1; if (np) ofidx =3D of_alias_get_id(np, "mtd"); - if (ofidx >=3D 0) - i =3D idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL); - else - i =3D idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); + if (partitioned) { + if (ofidx >=3D 0) + i =3D idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL); + else + i =3D idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); + } else { + if (ofidx >=3D 0) + i =3D idr_alloc(&mtd_master_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL); + else + i =3D idr_alloc(&mtd_master_idr, mtd, 0, 0, GFP_KERNEL); + } if (i < 0) { error =3D i; goto fail_locked; @@ -738,15 +768,23 @@ int add_mtd_device(struct mtd_info *mtd) /* Caller should have set dev.parent to match the * physical device, if appropriate. */ - mtd->dev.type =3D &mtd_devtype; - mtd->dev.class =3D &mtd_class; - mtd->dev.devt =3D MTD_DEVT(i); - dev_set_name(&mtd->dev, "mtd%d", i); + if (partitioned) { + mtd->dev.type =3D &mtd_devtype; + mtd->dev.class =3D &mtd_class; + mtd->dev.devt =3D MTD_DEVT(i); + dev_set_name(&mtd->dev, "mtd%d", i); + } else { + mtd->dev.type =3D &mtd_master_devtype; + mtd->dev.class =3D &mtd_master_class; + mtd->dev.devt =3D MKDEV(MAJOR(mtd_master_devt), i); + dev_set_name(&mtd->dev, "mtd_master%d", i); + } dev_set_drvdata(&mtd->dev, mtd); mtd_check_of_node(mtd); of_node_get(mtd_get_of_node(mtd)); error =3D device_register(&mtd->dev); if (error) { + pr_err("mtd: %s device_register fail %d\n", mtd->name, error); put_device(&mtd->dev); goto fail_added; } @@ -758,10 +796,13 @@ int add_mtd_device(struct mtd_info *mtd) =20 mtd_debugfs_populate(mtd); =20 - device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, - "mtd%dro", i); + if (partitioned) { + device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, + "mtd%dro", i); + } =20 - pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); + pr_debug("mtd: Giving out %spartitioned device %d to %s\n", + partitioned ? "" : "un-", i, mtd->name); /* No need to get a refcount on the module containing the notifier, since we hold the mtd_table_mutex */ list_for_each_entry(not, &mtd_notifiers, list) @@ -769,13 +810,16 @@ int add_mtd_device(struct mtd_info *mtd) =20 mutex_unlock(&mtd_table_mutex); =20 - if (of_property_read_bool(mtd_get_of_node(mtd), "linux,rootfs")) { - if (IS_BUILTIN(CONFIG_MTD)) { - pr_info("mtd: setting mtd%d (%s) as root device\n", mtd->index, mtd->na= me); - ROOT_DEV =3D MKDEV(MTD_BLOCK_MAJOR, mtd->index); - } else { - pr_warn("mtd: can't set mtd%d (%s) as root device - mtd must be builtin= \n", - mtd->index, mtd->name); + if (partitioned) { + if (of_property_read_bool(mtd_get_of_node(mtd), "linux,rootfs")) { + if (IS_BUILTIN(CONFIG_MTD)) { + pr_info("mtd: setting mtd%d (%s) as root device\n", + mtd->index, mtd->name); + ROOT_DEV =3D MKDEV(MTD_BLOCK_MAJOR, mtd->index); + } else { + pr_warn("mtd: can't set mtd%d (%s) as root device - mtd must be builti= n\n", + mtd->index, mtd->name); + } } } =20 @@ -790,7 +834,10 @@ int add_mtd_device(struct mtd_info *mtd) device_unregister(&mtd->dev); fail_added: of_node_put(mtd_get_of_node(mtd)); - idr_remove(&mtd_idr, i); + if (partitioned) + idr_remove(&mtd_idr, i); + else + idr_remove(&mtd_master_idr, i); fail_locked: mutex_unlock(&mtd_table_mutex); return error; @@ -808,12 +855,14 @@ int add_mtd_device(struct mtd_info *mtd) =20 int del_mtd_device(struct mtd_info *mtd) { - int ret; struct mtd_notifier *not; + struct idr *idr; + int ret; =20 mutex_lock(&mtd_table_mutex); =20 - if (idr_find(&mtd_idr, mtd->index) !=3D mtd) { + idr =3D mtd->dev.class =3D=3D &mtd_class ? &mtd_idr : &mtd_master_idr; + if (idr_find(idr, mtd->index) !=3D mtd) { ret =3D -ENODEV; goto out_error; } @@ -1061,12 +1110,6 @@ int mtd_device_parse_register(struct mtd_info *mtd, = const char * const *types, if (ret) goto out; =20 - if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) { - ret =3D add_mtd_device(mtd); - if (ret) - goto out; - } - /* Prefer parsed partitions over driver-provided fallback */ ret =3D parse_mtd_partitions(mtd, types, parser_data); if (ret =3D=3D -EPROBE_DEFER) @@ -1076,10 +1119,8 @@ int mtd_device_parse_register(struct mtd_info *mtd, = const char * const *types, ret =3D 0; else if (nr_parts) ret =3D add_mtd_partitions(mtd, parts, nr_parts); - else if (!device_is_registered(&mtd->dev)) - ret =3D add_mtd_device(mtd); else - ret =3D 0; + ret =3D add_mtd_device(mtd, true); =20 if (ret) goto out; @@ -1099,13 +1140,14 @@ int mtd_device_parse_register(struct mtd_info *mtd,= const char * const *types, register_reboot_notifier(&mtd->reboot_notifier); } =20 + return 0; out: - if (ret) { - nvmem_unregister(mtd->otp_user_nvmem); - nvmem_unregister(mtd->otp_factory_nvmem); - } + nvmem_unregister(mtd->otp_user_nvmem); + nvmem_unregister(mtd->otp_factory_nvmem); =20 - if (ret && device_is_registered(&mtd->dev)) + del_mtd_partitions(mtd); + + if (device_is_registered(&mtd->dev)) del_mtd_device(mtd); =20 return ret; @@ -1261,8 +1303,7 @@ int __get_mtd_device(struct mtd_info *mtd) mtd =3D mtd->parent; } =20 - if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) - kref_get(&master->refcnt); + kref_get(&master->refcnt); =20 return 0; } @@ -1356,8 +1397,7 @@ void __put_mtd_device(struct mtd_info *mtd) mtd =3D parent; } =20 - if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) - kref_put(&master->refcnt, mtd_device_release); + kref_put(&master->refcnt, mtd_device_release); =20 module_put(master->owner); =20 @@ -2524,6 +2564,16 @@ static int __init init_mtd(void) if (ret) goto err_reg; =20 + ret =3D class_register(&mtd_master_class); + if (ret) + goto err_reg2; + + ret =3D alloc_chrdev_region(&mtd_master_devt, 0, MTD_MASTER_DEVS, "mtd_ma= ster"); + if (ret < 0) { + pr_err("unable to allocate char dev region\n"); + goto err_chrdev; + } + mtd_bdi =3D mtd_bdi_init("mtd"); if (IS_ERR(mtd_bdi)) { ret =3D PTR_ERR(mtd_bdi); @@ -2548,6 +2598,10 @@ static int __init init_mtd(void) bdi_unregister(mtd_bdi); bdi_put(mtd_bdi); err_bdi: + unregister_chrdev_region(mtd_master_devt, MTD_MASTER_DEVS); +err_chrdev: + class_unregister(&mtd_master_class); +err_reg2: class_unregister(&mtd_class); err_reg: pr_err("Error registering mtd class or bdi: %d\n", ret); @@ -2561,9 +2615,12 @@ static void __exit cleanup_mtd(void) if (proc_mtd) remove_proc_entry("mtd", NULL); class_unregister(&mtd_class); + class_unregister(&mtd_master_class); + unregister_chrdev_region(mtd_master_devt, MTD_MASTER_DEVS); bdi_unregister(mtd_bdi); bdi_put(mtd_bdi); idr_destroy(&mtd_idr); + idr_destroy(&mtd_master_idr); } =20 module_init(init_mtd); diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h index b014861a06a6..2258d31c5aa6 100644 --- a/drivers/mtd/mtdcore.h +++ b/drivers/mtd/mtdcore.h @@ -8,7 +8,7 @@ extern struct mutex mtd_table_mutex; extern struct backing_dev_info *mtd_bdi; =20 struct mtd_info *__mtd_next_device(int i); -int __must_check add_mtd_device(struct mtd_info *mtd); +int __must_check add_mtd_device(struct mtd_info *mtd, bool partitioned); int del_mtd_device(struct mtd_info *mtd); int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, in= t); int del_mtd_partitions(struct mtd_info *); diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c index 6811a714349d..97505b132313 100644 --- a/drivers/mtd/mtdpart.c +++ b/drivers/mtd/mtdpart.c @@ -86,8 +86,7 @@ static struct mtd_info *allocate_partition(struct mtd_inf= o *parent, * parent conditional on that option. Note, this is a way to * distinguish between the parent and its partitions in sysfs. */ - child->dev.parent =3D IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is= _partition(parent) ? - &parent->dev : parent->dev.parent; + child->dev.parent =3D &parent->dev; child->dev.of_node =3D part->of_node; child->parent =3D parent; child->part.offset =3D part->offset; @@ -276,7 +275,7 @@ int mtd_add_partition(struct mtd_info *parent, const ch= ar *name, list_add_tail(&child->part.node, &parent->partitions); mutex_unlock(&master->master.partitions_lock); =20 - ret =3D add_mtd_device(child); + ret =3D add_mtd_device(child, true); if (ret) goto err_remove_part; =20 @@ -402,6 +401,12 @@ int add_mtd_partitions(struct mtd_info *parent, printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, parent->name); =20 + if (!mtd_is_partition(parent)) { + ret =3D add_mtd_device(parent, IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)= ); + if (ret) + return ret; + } + for (i =3D 0; i < nbparts; i++) { child =3D allocate_partition(parent, parts + i, i, cur_offset); if (IS_ERR(child)) { @@ -413,7 +418,7 @@ int add_mtd_partitions(struct mtd_info *parent, list_add_tail(&child->part.node, &parent->partitions); mutex_unlock(&master->master.partitions_lock); =20 - ret =3D add_mtd_device(child); + ret =3D add_mtd_device(child, true); if (ret) { mutex_lock(&master->master.partitions_lock); list_del(&child->part.node); @@ -590,9 +595,6 @@ static int mtd_part_of_parse(struct mtd_info *master, int ret, err =3D 0; =20 dev =3D &master->dev; - /* Use parent device (controller) if the top level MTD is not registered = */ - if (!IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) && !mtd_is_partition(maste= r)) - dev =3D master->dev.parent; =20 np =3D mtd_get_of_node(master); if (mtd_is_partition(master)) @@ -712,6 +714,7 @@ int parse_mtd_partitions(struct mtd_info *master, const= char *const *types, if (ret < 0 && !err) err =3D ret; } + return err; } =20 --=20 2.43.0